hachi 0.2.2 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efe04e36b93c8278ea1c128e7aa38135d33468c904ad3434dbcc6c7dc7707478
4
- data.tar.gz: caaa81f78a086e6dba10e54f977018fbc19f5485559a59d15047a152d1780d9b
3
+ metadata.gz: 3a64cb219a699afb02768ce856250ee2baa3eb77b51f8fc2bcb76b1ccc36b44a
4
+ data.tar.gz: 8de8ee38a60140d62e6740c874fddd679e91748cba187c6789b9afc3f3cfd09d
5
5
  SHA512:
6
- metadata.gz: 75a96eb5ad3cea0bf65ec999ab14e27bbc081bc5e1315e231a9e1aac27c0db69caf1cc7820d1f782ba8cdcd25cc37606b6be0ea52abe8aa53b357781c1294217
7
- data.tar.gz: 87d6925a83d4faf2d544648d47b6c0caa82641fa25fcf0a138d35c0061a5a421aba6491c7391e9211101ded22f7308b18f49da5d73adf86143068450107bcad7
6
+ metadata.gz: f66d6cf5bcb7c252a2ca4355e0409b28667dc7d9f55c356e482cab043bf6559f6d5eae49e08e83fa58cdf42493ab97fb481eb5c0907d0af5bcfd29b20aeb85bc
7
+ data.tar.gz: d00975cc9e11bfca85828a821f6d9915458f43d6adc22d957fc6dcc13a39b91e06bb315e290b0f5ca12cb87d79915e6611ea7b5542b695e661c13910e7d99f36
@@ -3,5 +3,6 @@ sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
- - 2.6.1
7
- before_install: gem install bundler -v 2.0.1
6
+ - 2.6
7
+ - 2.7
8
+ before_install: gem install bundler -v 2.1
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Hachi
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/hachi.svg)](https://badge.fury.io/rb/hachi)
4
- [![Build Status](https://travis-ci.org/ninoseki/hachi.svg?branch=master)](https://travis-ci.org/ninoseki/hachi)
4
+ [![Build Status](https://travis-ci.com/ninoseki/hachi.svg?branch=master)](https://travis-ci.com/ninoseki/hachi)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/ninoseki/hachi/badge.svg?branch=master)](https://coveralls.io/github/ninoseki/hachi?branch=master)
6
6
  [![CodeFactor](https://www.codefactor.io/repository/github/ninoseki/hachi/badge)](https://www.codefactor.io/repository/github/ninoseki/hachi)
7
7
 
@@ -41,19 +41,19 @@ See `samples` for more.
41
41
  | HTTP Method | URI | Action | API method |
42
42
  |-------------|-----------------------------------|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
43
43
  | GET | /api/alert | List alerts | `#api.alert.list` |
44
- | POST | /api/alert/_search | Find alerts | `#api.alert.search(attributes:, range: "all")` |
44
+ | POST | /api/alert/_search | Find alerts | `#api.alert.search(attributes, range: "all")` |
45
45
  | PATCH | /api/alert/_bulk | Update alerts in bulk | N/A |
46
46
  | POST | /api/alert/_stats | Compute stats on alerts | N/A |
47
47
  | POST | /api/alert | Create an alert | `#api.alert.create(title:, description:, severity: nil, date: nil, tags: nil, tlp: nil, status: nil, type:, source:, source_ref: nil, artifacts: nil, follow: nil)` |
48
48
  | GET | /api/alert/:alertId | Get an alert | `#api.alert.get_by_id(id)` |
49
- | PATCH | /api/alert/:alertId | Update an alert | N/A |
49
+ | PATCH | /api/alert/:alertId | Update an alert | `#api.alert.update(id, title:, description:, severity: nil, tags: nil, tlp: nil, artifacts: nil)` |
50
50
  | DELETE | /api/alert/:alertId | Delete an alert | `#api.alert.delete_by_id(id)` |
51
- | POST | /api/alert/:alertId/markAsRead | Mark an alert as read | N/A |
52
- | POST | /api/alert/:alertId/markAsUnread | Mark an alert as unread | N/A |
53
- | POST | /api/alert/:alertId/createCase | Create a case from an alert | N/A |
51
+ | POST | /api/alert/:alertId/markAsRead | Mark an alert as read | `#api.alert.mark_as_read(id)` |
52
+ | POST | /api/alert/:alertId/markAsUnread | Mark an alert as unread | `#api.alert.mark_as_unread(id)` |
53
+ | POST | /api/alert/:alertId/createCase | Create a case from an alert | `#api.alert.promote_to_case(id)` |
54
54
  | POST | /api/alert/:alertId/follow | Follow an alert | N/A |
55
55
  | POST | /api/alert/:alertId/unfollow | Unfollow an alert | N/A |
56
- | POST | /api/alert/:alertId/merge/:caseId | Merge an alert in a case | N/A |
56
+ | POST | /api/alert/:alertId/merge/:caseId | Merge an alert in a case | `#api.alert.merge_into_case(*ids, case_id)` |
57
57
 
58
58
  ### Artifact(Observable)
59
59
 
@@ -81,7 +81,22 @@ See `samples` for more.
81
81
  | PATCH | /api/case/:caseId | Update a case | N/A |
82
82
  | DELETE | /api/case/:caseId | Remove a case | `#api.case.delete_by_id(id)` |
83
83
  | GET | /api/case/:caseId/links | Get list of cases linked to this case | `#api.case.links(id)` |
84
- | POST | /api/case/:caseId1/_merge/:caseId2 | Merge two cases | N/A |
84
+ | POST | /api/case/:caseId1/_merge/:caseId2 | Merge two cases | `#api.case.merge(id1, id2)` |
85
+
86
+ ### User
87
+
88
+ | HTTP Method | URI | Action | API method |
89
+ |-------------|-----------------------------------|---------------------|------------------------------------------------------|
90
+ | GET | /api/logout | Logout | N/A |
91
+ | POST | /api/login | User login | N/A |
92
+ | GET | /api/user/current | Get current user | `#api.user.current` |
93
+ | POST | /api/user/_search | Find user | N/A |
94
+ | POST | /api/user | Create a user | `#api.user.create(login:, name:, roles:, password:)` |
95
+ | GET | /api/user/:userId | Get a user | `#api.user.get_by_id(id)` |
96
+ | DELETE | /api/user/:userId | Delete a case | `#api.user.delete_by_id(id)` |
97
+ | PATCH | /api/user/:userId | Update user details | N/A |
98
+ | POST | /api/user/:userId/password/set | Set password | N/A |
99
+ | POST | /api/user/:userId/password/change | Change password | N/A |
85
100
 
86
101
  ## License
87
102
 
@@ -24,10 +24,10 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "bundler", "~> 2.1"
28
28
  spec.add_development_dependency "coveralls", "~> 0.8"
29
- spec.add_development_dependency "rake", "~> 12.3"
30
- spec.add_development_dependency "rspec", "~> 3.8"
31
- spec.add_development_dependency "vcr", "~> 5.0"
32
- spec.add_development_dependency "webmock", "~> 3.6"
29
+ spec.add_development_dependency "rake", "~> 13.0"
30
+ spec.add_development_dependency "rspec", "~> 3.9"
31
+ spec.add_development_dependency "vcr", "~> 6.0"
32
+ spec.add_development_dependency "webmock", "~> 3.8"
33
33
  end
@@ -8,11 +8,13 @@ require "hachi/models/base"
8
8
  require "hachi/models/alert"
9
9
  require "hachi/models/artifact"
10
10
  require "hachi/models/case"
11
+ require "hachi/models/user"
11
12
 
12
13
  require "hachi/clients/base"
13
14
  require "hachi/clients/alert"
14
15
  require "hachi/clients/artifact"
15
16
  require "hachi/clients/case"
17
+ require "hachi/clients/user"
16
18
 
17
19
  module Hachi
18
20
  class Error < StandardError; end
@@ -2,17 +2,60 @@
2
2
 
3
3
  module Hachi
4
4
  class API
5
- attr_reader :alert
6
- attr_reader :artifact
7
- attr_reader :case
5
+ # @return [String] TheHive API endpoint
6
+ attr_reader :api_endpoint
8
7
 
8
+ # @return [String] TheHive API key
9
+ attr_reader :api_key
10
+
11
+ #
12
+ # @param [String, nil] api_endpoint TheHive API endpoint
13
+ # @param [String, nil] api_key TheHive API key
14
+ #
15
+ # @raise [ArgumentError] When given or an empty endpoint or key
16
+ #
9
17
  def initialize(api_endpoint: ENV["THEHIVE_API_ENDPOINT"], api_key: ENV["THEHIVE_API_KEY"])
10
- raise(ArgumentError, "api_endpoint argument is required") unless api_endpoint
11
- raise(ArgumentError, "api_key argument is required") unless api_key
18
+ @api_endpoint = api_endpoint
19
+ raise ArgumentError, "api_endpoint argument is required" unless api_endpoint
20
+
21
+ @api_key = api_key
22
+ raise ArgumentError, "api_key argument is required" unless api_key
23
+ end
24
+
25
+ #
26
+ # Alert API endpoint client
27
+ #
28
+ # @return [Clients::Alert]
29
+ #
30
+ def alert
31
+ @alert ||= Clients::Alert.new(api_endpoint: api_endpoint, api_key: api_key)
32
+ end
33
+
34
+ #
35
+ # Artifact API endpoint client
36
+ #
37
+ # @return [Clients::Artifact]
38
+ #
39
+ def artifact
40
+ @artifact ||= Clients::Artifact.new(api_endpoint: api_endpoint, api_key: api_key)
41
+ end
42
+
43
+ #
44
+ # Case API endpoint client
45
+ #
46
+ # @return [Clients::Case]
47
+ #
48
+ def case
49
+ @case ||= Clients::Case.new(api_endpoint: api_endpoint, api_key: api_key)
50
+ end
12
51
 
13
- @alert = Clients::Alert.new(api_endpoint: api_endpoint, api_key: api_key)
14
- @artifact = Clients::Artifact.new(api_endpoint: api_endpoint, api_key: api_key)
15
- @case = Clients::Case.new(api_endpoint: api_endpoint, api_key: api_key)
52
+ #
53
+ # User API endpoint client
54
+ #
55
+ # @return [Clients::User]
56
+ #
57
+ def user
58
+ @user ||= Clients::User.new(api_endpoint: api_endpoint, api_key: api_key)
16
59
  end
17
60
  end
18
61
  end
@@ -6,18 +6,55 @@ require "securerandom"
6
6
  module Hachi
7
7
  module Clients
8
8
  class Alert < Base
9
+ #
10
+ # List alerts
11
+ #
12
+ # @return [Array]
13
+ #
9
14
  def list
10
15
  get("/api/alert") { |json| json }
11
16
  end
12
17
 
18
+ #
19
+ # Get an alert
20
+ #
21
+ # @param [String] id Alert ID
22
+ #
23
+ # @return [Hash]
24
+ #
13
25
  def get_by_id(id)
14
26
  get("/api/alert/#{id}") { |json| json }
15
27
  end
16
28
 
29
+ #
30
+ # Delete an alert
31
+ #
32
+ # @param [String] id Alert ID
33
+ #
34
+ # @return [String]
35
+ #
17
36
  def delete_by_id(id)
18
37
  delete("/api/alert/#{id}") { |json| json }
19
38
  end
20
39
 
40
+ #
41
+ # Create an alert
42
+ #
43
+ # @param [String] title
44
+ # @param [String] description
45
+ # @param [String, nil] severity
46
+ # @param [String, nil] date
47
+ # @param [String, nil] tags
48
+ # @param [String, nil] tlp
49
+ # @param [String, nil] status
50
+ # @param [String, nil] type
51
+ # @param [String, nil] source
52
+ # @param [String, nil] source_ref
53
+ # @param [String, nil] artifacts
54
+ # @param [String, nil] follow
55
+ #
56
+ # @return [Hash]
57
+ #
21
58
  def create(title:, description:, severity: nil, date: nil, tags: nil, tlp: nil, status: nil, type:, source:, source_ref: nil, artifacts: nil, follow: nil)
22
59
  alert = Models::Alert.new(
23
60
  title: title,
@@ -36,8 +73,91 @@ module Hachi
36
73
  post("/api/alert", alert.payload) { |json| json }
37
74
  end
38
75
 
39
- def search(attributes, range: "all")
40
- _search("/api/alert/_search", attributes: attributes, range: range) { |json| json }
76
+ #
77
+ # Find alerts
78
+ #
79
+ # @param [Hash] attributes
80
+ # @param [String] range
81
+ # @param [String, nil] sort
82
+ #
83
+ # @return [Array]
84
+ #
85
+ def search(attributes, range: "all", sort: nil)
86
+ _search("/api/alert/_search", attributes: attributes, range: range, sort: sort) { |json| json }
87
+ end
88
+
89
+ #
90
+ # Mark an alert as read
91
+ #
92
+ # @param [String] id Alert ID
93
+ #
94
+ # @return [Hash]
95
+ #
96
+ def mark_as_read(id)
97
+ post("/api/alert/#{id}/markAsRead") { |json| json }
98
+ end
99
+
100
+ #
101
+ # Mark an alert as unread
102
+ #
103
+ # @param [String] id Alert ID
104
+ #
105
+ # @return [Hash] hash
106
+ #
107
+ def mark_as_unread(id)
108
+ post("/api/alert/#{id}/markAsUnread") { |json| json }
109
+ end
110
+
111
+ #
112
+ # Create a case from an alert
113
+ #
114
+ # @param [String] id Alert ID
115
+ #
116
+ # @return [Hash]
117
+ #
118
+ def promote_to_case(id)
119
+ post("/api/alert/#{id}/createCase") { |json| json }
120
+ end
121
+
122
+ #
123
+ # Merge an alert / alerts in a case
124
+ #
125
+ # @param [String, Array] *ids Alert ID(s)
126
+ # @param [String] case_id Case ID
127
+ #
128
+ # @return [Hash]
129
+ #
130
+ def merge_into_case(*ids, case_id)
131
+ params = {
132
+ alertIds: ids.flatten,
133
+ caseId: case_id
134
+ }
135
+ post("/api/alert/merge/_bulk", params) { |json| json }
136
+ end
137
+
138
+ #
139
+ # Update an alert
140
+ #
141
+ # @param [String, nil] id
142
+ # @param [String, nil] title
143
+ # @param [String, nil] description
144
+ # @param [String, nil] severity
145
+ # @param [String, nil] tags
146
+ # @param [String, nil] tlp
147
+ # @param [String, nil] artifacts
148
+ #
149
+ # @return [Hash]
150
+ #
151
+ def update(id, title: nil, description: nil, severity: nil, tags: nil, tlp: nil, artifacts: nil)
152
+ attributes = {
153
+ title: title,
154
+ description: description,
155
+ severity: severity,
156
+ tags: tags,
157
+ tlp: tlp,
158
+ artifacts: artifacts,
159
+ }.compact
160
+ patch("/api/alert/#{id}", attributes) { |json| json }
41
161
  end
42
162
  end
43
163
  end
@@ -3,6 +3,18 @@
3
3
  module Hachi
4
4
  module Clients
5
5
  class Artifact < Base
6
+ #
7
+ # Create an artifact
8
+ #
9
+ # @param [String] case_id Artifact ID
10
+ # @param [String] data
11
+ # @param [String] data_type
12
+ # @param [String, nil] message
13
+ # @param [Integer, nil] tlp
14
+ # @param [Array<String>, nil] tags
15
+ #
16
+ # @return [Hash]
17
+ #
6
18
  def create(case_id, data:, data_type:, message: nil, tlp: nil, tags: nil)
7
19
  artifact = Models::Artifact.new(
8
20
  data: data,
@@ -15,18 +27,47 @@ module Hachi
15
27
  post("/api/case/#{case_id}/artifact", artifact.payload) { |json| json }
16
28
  end
17
29
 
30
+ #
31
+ # Get an artifact
32
+ #
33
+ # @param [String] id Artifact ID
34
+ #
35
+ # @return [Hash]
36
+ #
18
37
  def get_by_id(id)
19
38
  get("/api/case/artifact/#{id}") { |json| json }
20
39
  end
21
40
 
41
+ #
42
+ # Delete an artifact
43
+ #
44
+ # @param [String] id Artifact ID
45
+ #
46
+ # @return [String]
47
+ #
22
48
  def delete_by_id(id)
23
49
  delete("/api/case/artifact/#{id}") { |json| json }
24
50
  end
25
51
 
26
- def search(attributes, range: "all")
52
+ #
53
+ # Find artifacts
54
+ #
55
+ # @param [Hash] attributes
56
+ # @param [String] range
57
+ #
58
+ # @return [Array]
59
+ #
60
+ def search( range: "all", **attributes)
27
61
  _search("/api/case/artifact/_search", attributes: attributes, range: range) { |json| json }
28
62
  end
29
63
 
64
+ #
65
+ # Get list of similar observables
66
+ #
67
+ # @param [String] id Artifact ID
68
+ #
69
+ # @return [Array]
70
+ #
30
71
  def similar(id)
31
72
  get("/api/case/artifact/#{id}/similar") { |json| json }
32
73
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
  require "net/https"
5
+ require "uri"
5
6
 
6
7
  module Hachi
7
8
  module Clients
@@ -103,6 +104,18 @@ module Hachi
103
104
  request(delete, &block)
104
105
  end
105
106
 
107
+ def patch(path, params = {}, &block)
108
+ url = url_for(path)
109
+
110
+ patch = Net::HTTP::Patch.new(url)
111
+ patch.body = params.is_a?(Hash) ? params.to_json : params.to_s
112
+
113
+ patch.add_field "Content-Type", "application/json"
114
+ patch.add_field "Authorization", "Bearer #{api_key}"
115
+
116
+ request(patch, &block)
117
+ end
118
+
106
119
  def validate_range(range)
107
120
  return true if range == "all"
108
121
  raise ArgumentError, "range should be 'all' or `from-to`" unless range.match?(/(\d+)-(\d+)/)
@@ -113,14 +126,15 @@ module Hachi
113
126
  raise ArgumentError, "from should be smaller than to"
114
127
  end
115
128
 
116
- def _search(path, attributes:, range: "all")
129
+ def _search(path, attributes:, range: "all", sort: nil)
117
130
  validate_range range
118
131
 
132
+ attributes = normalize_attributes(attributes)
119
133
  conditions = attributes.map do |key, value|
120
134
  if key == :data && value.is_a?(Array)
121
135
  { _or: decompose_data(value) }
122
136
  else
123
- { _string: "#{key}:#{value}" }
137
+ { _string: "#{key}:\"#{value}\"" }
124
138
  end
125
139
  end
126
140
 
@@ -135,7 +149,9 @@ module Hachi
135
149
  _and: [conditions, default_conditions].flatten,
136
150
  }
137
151
 
138
- post("#{path}?range=#{range}", query: query) { |json| json }
152
+ query_string = build_query_string(range: range, sort: sort)
153
+
154
+ post("#{path}?#{query_string}", query: query) { |json| json }
139
155
  end
140
156
 
141
157
  def decompose_data(data)
@@ -143,6 +159,23 @@ module Hachi
143
159
  { _field: "data", _value: elem }
144
160
  end
145
161
  end
162
+
163
+ def normalize_attributes(attributes)
164
+ h = {}
165
+ attributes.each do |key, value|
166
+ h[camelize(key).to_sym] = value
167
+ end
168
+ h
169
+ end
170
+
171
+ def camelize(string)
172
+ head, *others = string.to_s.split("_")
173
+ [head, others.map(&:capitalize)].flatten.join
174
+ end
175
+
176
+ def build_query_string(params)
177
+ URI.encode_www_form(params.reject { |_k, v| v.nil? })
178
+ end
146
179
  end
147
180
  end
148
181
  end
@@ -3,18 +3,51 @@
3
3
  module Hachi
4
4
  module Clients
5
5
  class Case < Base
6
+ #
7
+ # List cases
8
+ #
9
+ # @return [Array]
10
+ #
6
11
  def list
7
12
  get("/api/case") { |json| json }
8
13
  end
9
14
 
15
+ #
16
+ # Get a case
17
+ #
18
+ # @param [String] id Case ID
19
+ #
20
+ # @return [Hash]
21
+ #
10
22
  def get_by_id(id)
11
23
  get("/api/case/#{id}") { |json| json }
12
24
  end
13
25
 
26
+ #
27
+ # Delete a case
28
+ #
29
+ # @param [String] id Case ID
30
+ #
31
+ # @return [String]
32
+ #
14
33
  def delete_by_id(id)
15
34
  delete("/api/case/#{id}") { |json| json }
16
35
  end
17
36
 
37
+ #
38
+ # Create a case
39
+ #
40
+ # @param [String, nil] title
41
+ # @param [String, nil] description
42
+ # @param [Integer, nil] severity
43
+ # @param [String, nil] start_date
44
+ # @param [String, nil] owner
45
+ # @param [Boolean, nil] flag
46
+ # @param [Intege, nil] tlp
47
+ # @param [String, nil] tags
48
+ #
49
+ # @return [Hash]
50
+ #
18
51
  def create(title:, description:, severity: nil, start_date: nil, owner: nil, flag: nil, tlp: nil, tags: nil)
19
52
  kase = Models::Case.new(
20
53
  title: title,
@@ -30,13 +63,83 @@ module Hachi
30
63
  post("/api/case", kase.payload) { |json| json }
31
64
  end
32
65
 
33
- def search(attributes, range: "all")
66
+ #
67
+ # Find cases
68
+ #
69
+ # @param [Hash] attributes
70
+ # @param [String] range
71
+ #
72
+ # @return [Hash]
73
+ #
74
+ def search(range: "all", **attributes)
34
75
  _search("/api/case/_search", attributes: attributes, range: range) { |json| json }
35
76
  end
36
77
 
78
+ #
79
+ # Get list of cases linked to this case
80
+ #
81
+ # @param [String] id Case ID
82
+ #
83
+ # @return [Array]
84
+ #
37
85
  def links(id)
38
86
  get("/api/case/#{id}/links") { |json| json }
39
87
  end
88
+
89
+ #
90
+ # Merge two cases
91
+ #
92
+ # @param [String] id1 Case ID
93
+ # @param [String] id2 Case ID
94
+ #
95
+ # @return [Hash]
96
+ #
97
+ def merge(id1, id2)
98
+ post("/api/case/#{id1}/_merge/#{id2}") { |json| json }
99
+ end
100
+
101
+ #
102
+ # Update a case
103
+ #
104
+ # @param [String, nil] id
105
+ # @param [String, nil] title
106
+ # @param [String, nil] description
107
+ # @param [String, nil] severity
108
+ # @param [String, nil] start_date
109
+ # @param [String, nil] owner
110
+ # @param [Boolean, nil] flag
111
+ # @param [Integer, nil] tlp
112
+ # @param [String, nil] tags
113
+ # @param [String, nil] status
114
+ # @param [String, nil] resolution_status
115
+ # @param [String, nil] impact_status
116
+ # @param [String, nil] summary
117
+ # @param [String, nil] end_date
118
+ # @param [String, nil] metrics
119
+ # @param [String, nil] custom_fields
120
+ #
121
+ # @return [Hash]
122
+ #
123
+ def update(id, title: nil, description: nil, severity: nil, start_date: nil, owner: nil, flag: nil, tlp: nil, tags: nil, status: nil, resolution_status: nil, impact_status: nil, summary: nil, end_date: nil, metrics: nil, custom_fields: nil )
124
+ attributes = {
125
+ title: title,
126
+ description: description,
127
+ severity: severity,
128
+ startDate: start_date,
129
+ owner: owner,
130
+ flag: flag,
131
+ tlp: tlp,
132
+ tags: tags,
133
+ status: status,
134
+ resolutionStatus: resolution_status,
135
+ impactStatus: impact_status,
136
+ summary: summary,
137
+ endDate: end_date,
138
+ metrics: metrics,
139
+ customFields: custom_fields
140
+ }.compact
141
+ patch("/api/case/#{id}", attributes) { |json| json }
142
+ end
40
143
  end
41
144
  end
42
145
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hachi
4
+ module Clients
5
+ class User < Base
6
+ #
7
+ # Get current user
8
+ #
9
+ # @return [Hash]
10
+ #
11
+ def current
12
+ get("/api/user/current") { |json| json }
13
+ end
14
+
15
+ #
16
+ # Get a user
17
+ #
18
+ # @param [String] id User ID
19
+ #
20
+ # @return [Hash]
21
+ #
22
+ def get_by_id(id)
23
+ get("/api/user/#{id}") { |json| json }
24
+ end
25
+
26
+ #
27
+ # Delete a user
28
+ #
29
+ # @param [String] id User ID
30
+ #
31
+ # @return [String]
32
+ #
33
+ def delete_by_id(id)
34
+ delete("/api/user/#{id}") { |json| json }
35
+ end
36
+
37
+ #
38
+ # Create a user
39
+ #
40
+ # @param [String] login
41
+ # @param [String] name
42
+ # @param [Array<String>] roles
43
+ # @param [String] password
44
+ #
45
+ # @return [Hash]
46
+ #
47
+ def create(login:, name:, roles:, password:)
48
+ user = Models::User.new(
49
+ login: login,
50
+ name: name,
51
+ roles: roles,
52
+ password: password
53
+ )
54
+
55
+ post("/api/user", user.payload) { |json| json }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -30,7 +30,7 @@ module Hachi
30
30
  @type = type
31
31
  @source = source
32
32
  @source_ref = source_ref || SecureRandom.hex(10)
33
- @artifacts = artifacts.nil? ? nil : artifacts.map { |a| Artifact.new a }
33
+ @artifacts = artifacts.nil? ? nil : artifacts.map { |a| Artifact.new(**a) }
34
34
  @follow = follow
35
35
 
36
36
  validate_date if date
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hachi
4
+ module Models
5
+ class User < Base
6
+ attr_reader :login
7
+ attr_reader :name
8
+ attr_reader :roles
9
+ attr_reader :password
10
+
11
+ ROLES = %w(read write admin).freeze
12
+
13
+ def initialize(login:, name:, roles:, password:)
14
+ @login = login
15
+ @name = name
16
+ @roles = roles
17
+ @password = password
18
+
19
+ validate_roles
20
+ end
21
+
22
+ def payload
23
+ {
24
+ login: login,
25
+ name: name,
26
+ roles: roles,
27
+ password: password
28
+ }.compact
29
+ end
30
+
31
+ private
32
+
33
+ def validate_roles
34
+ raise ArgumentError, "roles should be an array" unless roles.is_a?(Array)
35
+ raise ArgumentError, "role should be one of #{ROLES.join('.')}" unless roles.all? { |role| ROLES.include? role }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hachi
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.2"
5
5
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
+
5
+ require "hachi"
6
+
7
+ def api
8
+ @api ||= Hachi::API.new
9
+ end
10
+
11
+ description = ARGV[0].to_s
12
+ case_id = ARGV[1].to_s
13
+
14
+ alerts = api.alert.search(description: description)
15
+ alert_ids = alerts.map { |alert| alert.dig "id" }
16
+
17
+ api.alert.merge_into_case(alert_ids, case_id)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hachi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manabu Niseki
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-11 00:00:00.000000000 Z
11
+ date: 2020-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: coveralls
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,56 +44,56 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '12.3'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '12.3'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.8'
61
+ version: '3.9'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.8'
68
+ version: '3.9'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: vcr
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '5.0'
75
+ version: '6.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '5.0'
82
+ version: '6.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: webmock
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '3.6'
89
+ version: '3.8'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '3.6'
96
+ version: '3.8'
97
97
  description: A dead simple TheHive API wrapper.
98
98
  email:
99
99
  - manabu.niseki@gmail.com
@@ -117,19 +117,22 @@ files:
117
117
  - lib/hachi/clients/artifact.rb
118
118
  - lib/hachi/clients/base.rb
119
119
  - lib/hachi/clients/case.rb
120
+ - lib/hachi/clients/user.rb
120
121
  - lib/hachi/models/alert.rb
121
122
  - lib/hachi/models/artifact.rb
122
123
  - lib/hachi/models/base.rb
123
124
  - lib/hachi/models/case.rb
125
+ - lib/hachi/models/user.rb
124
126
  - lib/hachi/version.rb
125
127
  - samples/01_create_an_alert.rb
126
128
  - samples/02_search_artifacts.rb
127
129
  - samples/03_list_cases.rb
130
+ - samples/04_merge_alerts.rb
128
131
  homepage: https://github.com/ninoseki/hachi
129
132
  licenses:
130
133
  - MIT
131
134
  metadata: {}
132
- post_install_message:
135
+ post_install_message:
133
136
  rdoc_options: []
134
137
  require_paths:
135
138
  - lib
@@ -144,8 +147,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
147
  - !ruby/object:Gem::Version
145
148
  version: '0'
146
149
  requirements: []
147
- rubygems_version: 3.0.4
148
- signing_key:
150
+ rubygems_version: 3.1.2
151
+ signing_key:
149
152
  specification_version: 4
150
153
  summary: A dead simple TheHive API wrapper.
151
154
  test_files: []