mihari 6.3.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +13 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +62 -0
  7. data/exe/mihari +2 -1
  8. data/lefthook.yml +8 -0
  9. data/lib/mihari/actor.rb +4 -4
  10. data/lib/mihari/analyzers/base.rb +16 -0
  11. data/lib/mihari/analyzers/binaryedge.rb +4 -2
  12. data/lib/mihari/analyzers/censys.rb +7 -5
  13. data/lib/mihari/analyzers/circl.rb +5 -3
  14. data/lib/mihari/analyzers/crtsh.rb +4 -1
  15. data/lib/mihari/analyzers/dnstwister.rb +1 -1
  16. data/lib/mihari/analyzers/feed.rb +12 -20
  17. data/lib/mihari/analyzers/fofa.rb +6 -8
  18. data/lib/mihari/analyzers/greynoise.rb +4 -2
  19. data/lib/mihari/analyzers/hunterhow.rb +4 -2
  20. data/lib/mihari/analyzers/onyphe.rb +4 -2
  21. data/lib/mihari/analyzers/otx.rb +5 -3
  22. data/lib/mihari/analyzers/passivetotal.rb +29 -12
  23. data/lib/mihari/analyzers/pulsedive.rb +5 -3
  24. data/lib/mihari/analyzers/securitytrails.rb +32 -8
  25. data/lib/mihari/analyzers/shodan.rb +4 -2
  26. data/lib/mihari/analyzers/urlscan.rb +4 -2
  27. data/lib/mihari/analyzers/virustotal.rb +5 -5
  28. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
  29. data/lib/mihari/analyzers/zoomeye.rb +4 -2
  30. data/lib/mihari/cli/{main.rb → application.rb} +17 -5
  31. data/lib/mihari/cli/artifact.rb +14 -0
  32. data/lib/mihari/cli/config.rb +14 -0
  33. data/lib/mihari/cli/rule.rb +1 -0
  34. data/lib/mihari/cli/tag.rb +14 -0
  35. data/lib/mihari/clients/base.rb +2 -2
  36. data/lib/mihari/clients/binaryedge.rb +2 -2
  37. data/lib/mihari/clients/crtsh.rb +2 -9
  38. data/lib/mihari/clients/fofa.rb +1 -1
  39. data/lib/mihari/clients/hunterhow.rb +1 -1
  40. data/lib/mihari/clients/mmdb.rb +28 -0
  41. data/lib/mihari/clients/passivetotal.rb +7 -20
  42. data/lib/mihari/clients/securitytrails.rb +19 -43
  43. data/lib/mihari/clients/shodan_internet_db.rb +28 -0
  44. data/lib/mihari/clients/the_hive.rb +7 -5
  45. data/lib/mihari/commands/alert.rb +53 -11
  46. data/lib/mihari/commands/artifact.rb +66 -0
  47. data/lib/mihari/commands/config.rb +23 -0
  48. data/lib/mihari/commands/database.rb +1 -1
  49. data/lib/mihari/commands/rule.rb +40 -27
  50. data/lib/mihari/commands/search.rb +10 -11
  51. data/lib/mihari/commands/sidekiq.rb +31 -0
  52. data/lib/mihari/commands/tag.rb +46 -0
  53. data/lib/mihari/commands/web.rb +6 -7
  54. data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
  55. data/lib/mihari/concerns/configurable.rb +72 -0
  56. data/lib/mihari/concerns/database_connectable.rb +16 -0
  57. data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
  58. data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
  59. data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
  60. data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
  61. data/lib/mihari/config.rb +13 -12
  62. data/lib/mihari/database.rb +30 -42
  63. data/lib/mihari/emitters/database.rb +5 -6
  64. data/lib/mihari/emitters/misp.rb +4 -11
  65. data/lib/mihari/emitters/slack.rb +7 -5
  66. data/lib/mihari/emitters/the_hive.rb +8 -58
  67. data/lib/mihari/emitters/webhook.rb +6 -6
  68. data/lib/mihari/enrichers/google_public_dns.rb +1 -1
  69. data/lib/mihari/enrichers/mmdb.rb +28 -0
  70. data/lib/mihari/enrichers/shodan.rb +3 -5
  71. data/lib/mihari/enrichers/whois.rb +3 -3
  72. data/lib/mihari/entities/alert.rb +3 -10
  73. data/lib/mihari/entities/artifact.rb +6 -14
  74. data/lib/mihari/entities/config.rb +2 -2
  75. data/lib/mihari/entities/cpe.rb +1 -0
  76. data/lib/mihari/entities/dns.rb +1 -0
  77. data/lib/mihari/entities/geolocation.rb +1 -0
  78. data/lib/mihari/entities/ip_address.rb +1 -3
  79. data/lib/mihari/entities/messages.rb +17 -0
  80. data/lib/mihari/entities/pagination.rb +11 -0
  81. data/lib/mihari/entities/port.rb +1 -0
  82. data/lib/mihari/entities/reverse_dns.rb +1 -0
  83. data/lib/mihari/entities/rule.rb +2 -20
  84. data/lib/mihari/entities/tag.rb +2 -2
  85. data/lib/mihari/entities/whois.rb +1 -0
  86. data/lib/mihari/errors.rb +2 -4
  87. data/lib/mihari/http.rb +4 -0
  88. data/lib/mihari/models/alert.rb +21 -53
  89. data/lib/mihari/models/artifact.rb +46 -88
  90. data/lib/mihari/models/autonomous_system.rb +5 -13
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +3 -10
  93. data/lib/mihari/models/dns.rb +2 -6
  94. data/lib/mihari/models/geolocation.rb +7 -12
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +3 -8
  97. data/lib/mihari/models/rule.rb +16 -57
  98. data/lib/mihari/models/tag.rb +17 -1
  99. data/lib/mihari/models/tagging.rb +1 -1
  100. data/lib/mihari/models/whois.rb +1 -4
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +2 -2
  104. data/lib/mihari/schemas/concerns/orrable.rb +24 -0
  105. data/lib/mihari/schemas/emitter.rb +1 -2
  106. data/lib/mihari/schemas/enricher.rb +3 -4
  107. data/lib/mihari/schemas/macros.rb +1 -1
  108. data/lib/mihari/schemas/options.rb +0 -2
  109. data/lib/mihari/schemas/rule.rb +1 -2
  110. data/lib/mihari/services/{rule_builder.rb → builders.rb} +1 -6
  111. data/lib/mihari/services/creators.rb +22 -0
  112. data/lib/mihari/services/destroyers.rb +41 -0
  113. data/lib/mihari/services/enrichers.rb +25 -0
  114. data/lib/mihari/services/feed.rb +107 -0
  115. data/lib/mihari/services/getters.rb +58 -0
  116. data/lib/mihari/services/initializers.rb +22 -0
  117. data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
  118. data/lib/mihari/services/searchers.rb +91 -0
  119. data/lib/mihari/sidekiq/application.rb +13 -0
  120. data/lib/mihari/sidekiq/jobs.rb +36 -0
  121. data/lib/mihari/structs/censys.rb +1 -1
  122. data/lib/mihari/structs/config.rb +10 -10
  123. data/lib/mihari/structs/filters.rb +12 -130
  124. data/lib/mihari/structs/google_public_dns.rb +1 -1
  125. data/lib/mihari/structs/greynoise.rb +1 -1
  126. data/lib/mihari/structs/mmdb.rb +115 -0
  127. data/lib/mihari/structs/onyphe.rb +1 -1
  128. data/lib/mihari/structs/shodan.rb +2 -2
  129. data/lib/mihari/version.rb +1 -1
  130. data/lib/mihari/web/{app.rb → application.rb} +28 -15
  131. data/lib/mihari/web/endpoints/alerts.rb +34 -73
  132. data/lib/mihari/web/endpoints/artifacts.rb +27 -111
  133. data/lib/mihari/web/endpoints/configs.rb +3 -5
  134. data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
  135. data/lib/mihari/web/endpoints/rules.rb +58 -130
  136. data/lib/mihari/web/endpoints/tags.rb +21 -17
  137. data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
  138. data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
  139. data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
  140. data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
  141. data/lib/mihari/web/public/index.html +2 -2
  142. data/lib/mihari/web/public/redoc-static.html +385 -385
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +12 -4
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +164 -34
  148. data/lib/mihari/commands/mixins.rb +0 -11
  149. data/lib/mihari/enrichers/ipinfo.rb +0 -52
  150. data/lib/mihari/entities/message.rb +0 -9
  151. data/lib/mihari/feed/parser.rb +0 -38
  152. data/lib/mihari/feed/reader.rb +0 -111
  153. data/lib/mihari/mixins/configurable.rb +0 -68
  154. data/lib/mihari/schemas/mixins.rb +0 -20
  155. data/lib/mihari/services/alert_runner.rb +0 -20
  156. data/lib/mihari/structs/ipinfo.rb +0 -53
  157. data/lib/mihari/web/endpoints/exports.rb +0 -0
  158. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  159. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  160. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -7,117 +7,22 @@ module Mihari
7
7
  # Artifact API endpoint
8
8
  #
9
9
  class Artifacts < Grape::API
10
- class ArtifactGetter < Service
11
- #
12
- # @param [Integer] id
13
- #
14
- # @return [Mihari::Models::Artifact]
15
- #
16
- def call(id)
17
- artifact = Mihari::Models::Artifact.includes(
18
- :autonomous_system,
19
- :geolocation,
20
- :whois_record,
21
- :dns_records,
22
- :reverse_dns_names
23
- ).find(id)
24
- # TODO: improve queries
25
- alert_ids = Mihari::Models::Artifact.where(data: artifact.data).pluck(:alert_id)
26
- tag_ids = Mihari::Models::Tagging.where(alert_id: alert_ids).pluck(:tag_id)
27
- tags = Mihari::Models::Tag.where(id: tag_ids)
28
-
29
- artifact.tags = tags
30
-
31
- artifact
32
- end
33
- end
34
-
35
- class ArtifactEnricher < Service
36
- #
37
- # @param [String] id
38
- #
39
- def call(id)
40
- artifact = Mihari::Models::Artifact.includes(
41
- :autonomous_system,
42
- :geolocation,
43
- :whois_record,
44
- :dns_records,
45
- :reverse_dns_names,
46
- :cpes,
47
- :ports
48
- ).find(id)
49
-
50
- artifact.enrich_all
51
- artifact.save
52
- end
53
- end
54
-
55
- class ArtifactDestroyer < Service
56
- #
57
- # @param [Integer] id
58
- #
59
- def call(id)
60
- Mihari::Models::Artifact.find(id).destroy
61
- end
62
- end
63
-
64
- class ArtifactSearcher < Mihari::Service
65
- class ResultValue
66
- # @return [Array<Mihari::Models::Artifacts>]
67
- attr_reader :artifacts
68
-
69
- # @return [Integer]
70
- attr_reader :total
71
-
72
- # @return [Mihari::Structs::Filters::Artifact::SearchFilterWithPagination]
73
- attr_reader :filter
74
-
75
- #
76
- # @param [Array<Mihari::Models::Artifact>] artifacts
77
- # @param [Integer] total
78
- # @param [Mihari::Structs::Filters::Artifacts::SearchFilterWithPagination] filter
79
- #
80
- def initialize(artifacts:, total:, filter:)
81
- @artifacts = artifacts
82
- @total = total
83
- @filter = filter
84
- end
85
- end
86
-
87
- #
88
- # @param [Hash] params
89
- #
90
- # @return [ResultValue]
91
- #
92
- def call(params)
93
- filter = params.to_h.to_snake_keys.symbolize_keys
94
- search_filter_with_pagination = Structs::Filters::Artifact::SearchFilterWithPagination.new(**filter)
95
- artifacts = Mihari::Models::Artifact.search(search_filter_with_pagination)
96
- total = Mihari::Models::Artifact.count(search_filter_with_pagination.without_pagination)
97
- ResultValue.new(artifacts: artifacts, total: total, filter: filter)
98
- end
99
- end
100
-
101
10
  namespace :artifacts do
102
- desc "Search artifacts", {
11
+ desc "List/search artifacts", {
103
12
  is_array: true,
104
13
  success: Entities::ArtifactsWithPagination,
105
- summary: "Search artifacts"
14
+ summary: "List/search artifacts"
106
15
  }
107
16
  params do
17
+ optional :q, type: String, default: ""
108
18
  optional :page, type: Integer, default: 1
109
19
  optional :limit, type: Integer, default: 10
110
- optional :dataType, type: String
111
- optional :ruleId, type: String
112
- optional :tag, type: String
113
- optional :fromAt, type: DateTime
114
- optional :toAt, type: DateTime
115
20
  end
116
21
  get "/" do
117
- value = ArtifactSearcher.call(params.to_h)
22
+ value = Services::ArtifactSearcher.call(params.to_h)
118
23
  present(
119
24
  {
120
- artifacts: value.artifacts,
25
+ results: value.results,
121
26
  total: value.total,
122
27
  current_page: value.filter[:page].to_i,
123
28
  page_size: value.filter[:limit].to_i
@@ -128,7 +33,7 @@ module Mihari
128
33
 
129
34
  desc "Get an artifact", {
130
35
  success: Entities::Artifact,
131
- failure: [{ code: 404, model: Entities::Message }],
36
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
132
37
  summary: "Get an artifact"
133
38
  }
134
39
  params do
@@ -136,41 +41,52 @@ module Mihari
136
41
  end
137
42
  get "/:id" do
138
43
  id = params[:id].to_i
139
- result = ArtifactGetter.result(id)
44
+ result = Services::ArtifactGetter.result(id)
140
45
  return present(result.value!, with: Entities::Artifact) if result.success?
141
46
 
142
47
  case result.failure
143
48
  when ActiveRecord::RecordNotFound
144
- error!({ message: "ID:#{id} is not found" }, 404)
49
+ error!({ message: "ID:#{id} not found" }, 404)
145
50
  end
146
51
  raise result.failure
147
52
  end
148
53
 
149
54
  desc "Enrich an artifact", {
150
55
  success: { code: 201, model: Entities::Message },
151
- failure: [{ code: 404, model: Entities::Message }],
56
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
152
57
  summary: "Enrich an artifact"
153
58
  }
154
59
  params do
155
60
  requires :id, type: Integer
156
61
  end
157
- get "/:id/enrich" do
62
+ post "/:id/enrich" do
158
63
  status 201
159
64
 
160
65
  id = params["id"].to_i
161
- result = ArtifactEnricher.result(id)
162
- return present({ message: "#{id} has been enriched" }, with: Entities::Message) if result.success?
66
+
67
+ queued = true
68
+ result = Dry::Monads::Try[StandardError] do
69
+ if Mihari.sidekiq?
70
+ Jobs::ArtifactEnrichJob.perform_async id
71
+ else
72
+ Services::ArtifactEnricher.call id
73
+ queued = false
74
+ end
75
+ end.to_result
76
+
77
+ message = queued ? "ID:#{id}'s enrichment has been queued" : "ID:#{id}'s enrichment has been succeeded"
78
+ return present({ message: message, queued: queued }, with: Entities::QueueMessage) if result.success?
163
79
 
164
80
  case result.failure
165
81
  when ActiveRecord::RecordNotFound
166
- error!({ message: "ID:#{id} is not found" }, 404)
82
+ error!({ message: "ID:#{id} not found" }, 404)
167
83
  end
168
84
  raise result.failure
169
85
  end
170
86
 
171
87
  desc "Delete an artifact", {
172
88
  success: { code: 204, model: Entities::Message },
173
- failure: [{ code: 404, model: Entities::Message }],
89
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
174
90
  summary: "Delete an artifact"
175
91
  }
176
92
  params do
@@ -180,12 +96,12 @@ module Mihari
180
96
  status 204
181
97
 
182
98
  id = params["id"].to_i
183
- result = ArtifactDestroyer.result(id)
99
+ result = Services::ArtifactDestroyer.result(id)
184
100
  return present({ message: "" }, with: Entities::Message) if result.success?
185
101
 
186
102
  case result.failure
187
103
  when ActiveRecord::RecordNotFound
188
- error!({ message: "ID:#{id} is not found" }, 404)
104
+ error!({ message: "ID:#{id} not found" }, 404)
189
105
  end
190
106
  raise result.failure
191
107
  end
@@ -8,15 +8,13 @@ module Mihari
8
8
  #
9
9
  class Configs < Grape::API
10
10
  namespace :configs do
11
- desc "Get configs", {
11
+ desc "list configs", {
12
12
  is_array: true,
13
13
  success: Entities::Config,
14
- summary: "Get configs"
14
+ summary: "List configs"
15
15
  }
16
16
  get "/" do
17
- configs = (Mihari.analyzers + Mihari.emitters + Mihari.enrichers).filter_map do |klass|
18
- Mihari::Structs::Config.from_class(klass)
19
- end
17
+ configs = Services::ConfigSearcher.call
20
18
  present(configs, with: Entities::Config)
21
19
  end
22
20
  end
@@ -7,21 +7,10 @@ module Mihari
7
7
  # IP address API endpoint
8
8
  #
9
9
  class IPAddresses < Grape::API
10
- class IPGetter < Service
11
- #
12
- # @param [String] ip
13
- #
14
- # @return [Mihari::Structs::IPInfo::Response]
15
- #
16
- def call(ip)
17
- Mihari::Enrichers::IPInfo.new.call ip
18
- end
19
- end
20
-
21
10
  namespace :ip_addresses do
22
11
  desc "Get an IP address", {
23
12
  success: Entities::IPAddress,
24
- failure: [{ code: 404, model: Entities::Message }],
13
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
25
14
  summary: "Get an IP address"
26
15
  }
27
16
  params do
@@ -29,13 +18,23 @@ module Mihari
29
18
  end
30
19
  get "/:ip", requirements: { ip: %r{[^/]+} } do
31
20
  ip = params[:ip].to_s
32
- result = IPGetter.result(ip)
33
- return present(result.value!, with: Entities::IPAddress) if result.success?
21
+ result = Services::IPGetter.result(ip)
22
+ if result.success?
23
+ value = result.value!
24
+ return present(
25
+ {
26
+ country_code: value.country_code,
27
+ asn: value.asn,
28
+ loc: value.loc
29
+ },
30
+ with: Entities::IPAddress
31
+ )
32
+ end
34
33
 
35
34
  failure = result.failure
36
35
  case failure
37
36
  when Mihari::StatusCodeError
38
- error!({ message: "ID:#{id} is not found" }, 404) if failure.status_code == 404
37
+ error!({ message: "ID:#{id} not found" }, 404) if failure.status_code == 404
39
38
  end
40
39
  raise failure
41
40
  end
@@ -7,135 +7,38 @@ module Mihari
7
7
  # Rule API endpoint
8
8
  #
9
9
  class Rules < Grape::API
10
- class RuleSearcher < Mihari::Service
11
- class ResultValue
12
- # @return [Array<Mihari::Models::Rule>]
13
- attr_reader :rules
14
-
15
- # @return [Integer]
16
- attr_reader :total
17
-
18
- # @return [Mihari::Structs::Filters::Rule::SearchFilterWithPagination]
19
- attr_reader :filter
20
-
21
- #
22
- # @param [Array<Mihari::Models::Rule>] rules
23
- # @param [Integer] total
24
- # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
25
- #
26
- def initialize(rules:, total:, filter:)
27
- @rules = rules
28
- @total = total
29
- @filter = filter
30
- end
31
- end
32
-
33
- #
34
- # @params [Hash]
10
+ class RuleCreateUpdater < Service
35
11
  #
36
- # @return [ResultValue]
37
- #
38
- def call(params)
39
- filter = params.to_h.to_snake_keys.symbolize_keys
40
- search_filter_with_pagination = Mihari::Structs::Filters::Rule::SearchFilterWithPagination.new(**filter)
41
- rules = Mihari::Models::Rule.search(search_filter_with_pagination)
42
- total = Mihari::Models::Rule.count(search_filter_with_pagination.without_pagination)
43
- ResultValue.new(rules: rules, total: total, filter: filter)
44
- end
45
- end
46
-
47
- class RuleGetter < Service
48
- #
49
- # @params [String] id
50
- #
51
- # @return [Mihari::Models::Rule]
52
- #
53
- def call(id)
54
- Mihari::Models::Rule.find id
55
- end
56
- end
57
-
58
- class RuleRunner < Service
59
- #
60
- # @param [String] id
61
- #
62
- def call(id)
63
- rule = Mihari::Rule.from_model(Mihari::Models::Rule.find(id))
64
- rule.call
65
- end
66
- end
67
-
68
- class RuleCreator < Service
69
- #
70
- # @params [String]
12
+ # @params [String] yaml
13
+ # @params [Boolean] overwrite
71
14
  #
72
15
  # @return [Mihari::Models::Rule]
73
16
  #
74
- def call(yaml)
17
+ def call(yaml, overwrite: true)
75
18
  rule = Rule.from_yaml(yaml)
76
19
 
77
- found = Mihari::Models::Rule.find_by_id(rule.id)
78
- error!({ message: "ID:#{rule.id} is already registered" }, 400) unless found.nil?
20
+ raise IntegrityError, "ID:#{rule.id} is already registered" if rule.exists? && !overwrite
79
21
 
80
- rule.model.save
22
+ rule.update_or_create
81
23
  rule
82
24
  end
83
25
  end
84
26
 
85
- class RuleUpdater < Service
86
- #
87
- # @params [String] id
88
- # @params [String] yaml
89
- #
90
- # @return [Mihari::Models::Rule]
91
- #
92
- def call(id:, yaml:)
93
- Mihari::Models::Rule.find(id)
94
-
95
- rule = Rule.from_yaml(yaml)
96
- rule.model.save
97
- rule
98
- end
99
- end
100
-
101
- class RuleDestroyer < Service
102
- #
103
- # @param [String] id
104
- #
105
- def call(id)
106
- Mihari::Models::Rule.find(id).destroy
107
- end
108
- end
109
-
110
27
  namespace :rules do
111
- desc "Get Rule IDs", {
112
- is_array: true,
113
- success: Entities::RuleIDs,
114
- summary: "Get rule IDs"
115
- }
116
- get "/ids" do
117
- rule_ids = Mihari::Models::Rule.distinct.pluck(:id)
118
- present({ rule_ids: rule_ids }, with: Entities::RuleIDs)
119
- end
120
-
121
- desc "Search rules", {
28
+ desc "List/search rules", {
122
29
  is_array: true,
123
30
  success: Entities::RulesWithPagination,
124
- summary: "Search rules"
31
+ summary: "List/search rules"
125
32
  }
126
33
  params do
34
+ optional :q, type: String, default: ""
127
35
  optional :page, type: Integer, default: 1
128
36
  optional :limit, type: Integer, default: 10
129
- optional :title, type: String
130
- optional :description, type: String
131
- optional :tag, type: String
132
- optional :fromAt, type: DateTime
133
- optional :toAt, type: DateTime
134
37
  end
135
38
  get "/" do
136
- value = RuleSearcher.call(params.to_h)
39
+ value = Services::RuleSearcher.call(params.to_h)
137
40
  present({
138
- rules: value.rules,
41
+ results: value.results,
139
42
  total: value.total,
140
43
  current_page: value.filter[:page].to_i,
141
44
  page_size: value.filter[:limit].to_i
@@ -145,7 +48,7 @@ module Mihari
145
48
 
146
49
  desc "Get a rule", {
147
50
  success: Entities::Rule,
148
- failure: [{ code: 404, model: Entities::Message }],
51
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
149
52
  summary: "Get a rule"
150
53
  }
151
54
  params do
@@ -153,41 +56,57 @@ module Mihari
153
56
  end
154
57
  get "/:id" do
155
58
  id = params[:id].to_s
156
- result = RuleGetter.result(params[:id].to_s)
59
+ result = Services::RuleGetter.result(params[:id].to_s)
157
60
  return present(result.value!, with: Entities::Rule) if result.success?
158
61
 
159
62
  case result.failure
160
63
  when ActiveRecord::RecordNotFound
161
- error!({ message: "ID:#{id} is not found" }, 404)
64
+ error!({ message: "ID:#{id} not found" }, 404)
162
65
  end
163
66
  raise result.failure
164
67
  end
165
68
 
166
- desc "Run a rule", {
167
- success: { code: 201, model: Entities::Message },
168
- failure: [{ code: 404, model: Entities::Message }],
69
+ desc "Search by a rule", {
70
+ success: { code: 201, model: Entities::QueueMessage },
71
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
169
72
  summary: "Run a rule"
170
73
  }
171
74
  params do
172
75
  requires :id, type: String
173
76
  end
174
- get "/:id/run" do
77
+ post "/:id/search" do
175
78
  status 201
176
79
 
177
80
  id = params[:id].to_s
178
- result = RuleRunner.result(id)
179
- return present({ message: "ID:#{id}} has been ran" }, with: Entities::Message) if result.success?
81
+
82
+ queued = true
83
+ result = Dry::Monads::Try[StandardError] do
84
+ rule = Mihari::Rule.from_model(Mihari::Models::Rule.find(id))
85
+
86
+ if Mihari.sidekiq?
87
+ Jobs::SearchJob.perform_async(rule.id)
88
+ else
89
+ rule.call
90
+ queued = false
91
+ end
92
+ end.to_result
93
+
94
+ message = queued ? "ID:#{id}'s search has been queued" : "ID:#{id}'s search has been succeed"
95
+ return present({ message: message, queued: queued }, with: Entities::QueueMessage) if result.success?
180
96
 
181
97
  case result.failure
182
98
  when ActiveRecord::RecordNotFound
183
- error!({ message: "ID:#{id} is not found" }, 404)
99
+ error!({ message: "ID:#{id} not found" }, 404)
184
100
  end
185
101
  raise result.failure
186
102
  end
187
103
 
188
104
  desc "Create a rule", {
189
105
  success: { code: 201, model: Entities::Rule },
190
- failure: [{ code: 404, model: Entities::Message }],
106
+ failure: [
107
+ { code: 400, model: Entities::ErrorMessage },
108
+ { code: 422, model: Entities::ErrorMessage }
109
+ ],
191
110
  summary: "Create a rule"
192
111
  }
193
112
  params do
@@ -196,22 +115,29 @@ module Mihari
196
115
  post "/" do
197
116
  status 201
198
117
 
199
- result = RuleCreator.result(params[:yaml])
118
+ yaml = params[:yaml].to_s
119
+
120
+ result = RuleCreateUpdater.result(yaml, overwrite: false)
200
121
  return present(result.value!.model, with: Entities::Rule) if result.success?
201
122
 
202
123
  failure = result.failure
203
124
  case failure
204
125
  when Psych::SyntaxError
205
- error!({ message: failure.message }, 400)
126
+ error!({ message: failure.message }, 422)
206
127
  when ValidationError
207
- error!({ message: "Rule format is invalid", details: failure.errors.to_h }, 400)
128
+ error!({ message: "Rule format invalid", detail: failure.errors.to_h }, 422)
129
+ when IntegrityError
130
+ error!({ message: failure.message }, 400)
208
131
  end
209
132
  raise failure
210
133
  end
211
134
 
212
135
  desc "Update a rule", {
213
136
  success: { code: 201, model: Entities::Rule },
214
- failure: [{ code: 404, model: Entities::Message }],
137
+ failure: [
138
+ { code: 404, model: Entities::ErrorMessage },
139
+ { code: 422, model: Entities::ErrorMessage }
140
+ ],
215
141
  summary: "Update a rule"
216
142
  }
217
143
  params do
@@ -222,24 +148,26 @@ module Mihari
222
148
  status 201
223
149
 
224
150
  id = params[:id].to_s
225
- result = RuleUpdater.result(id: id, yaml: params[:yaml].to_s)
151
+ yaml = params[:yaml].to_s
152
+
153
+ result = RuleCreateUpdater.result(yaml, overwrite: true)
226
154
  return present(result.value!.model, with: Entities::Rule) if result.success?
227
155
 
228
156
  failure = result.failure
229
157
  case failure
230
158
  when ActiveRecord::RecordNotFound
231
- error!({ message: "ID:#{id} is not found" }, 404)
159
+ error!({ message: "ID:#{id} not found" }, 404)
232
160
  when Psych::SyntaxError
233
- error!({ message: failure.message }, 400)
161
+ error!({ message: failure.message }, 422)
234
162
  when ValidationError
235
- error!({ message: "Rule format is invalid", details: failure.errors.to_h }, 400)
163
+ error!({ message: "Rule format is invalid", detail: failure.errors.to_h }, 422)
236
164
  end
237
165
  raise failure
238
166
  end
239
167
 
240
168
  desc "Delete a rule", {
241
169
  success: { code: 204, model: Entities::Message },
242
- failure: [{ code: 404, model: Entities::Message }],
170
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
243
171
  summary: "Delete a rule"
244
172
  }
245
173
  params do
@@ -249,12 +177,12 @@ module Mihari
249
177
  status 204
250
178
 
251
179
  id = params[:id].to_s
252
- result = RuleDestroyer.result(id)
180
+ result = Services::RuleDestroyer.result(id)
253
181
  return present({ message: "ID:#{id} is deleted" }, with: Entities::Message) if result.success?
254
182
 
255
183
  case result.failure
256
184
  when ActiveRecord::RecordNotFound
257
- error!({ message: "ID:#{id} is not found" }, 404)
185
+ error!({ message: "ID:#{id} not found" }, 404)
258
186
  end
259
187
  raise result.failure
260
188
  end
@@ -7,29 +7,33 @@ module Mihari
7
7
  # Tag API endpoint
8
8
  #
9
9
  class Tags < Grape::API
10
- class TagDestroyer < Service
11
- #
12
- # @param [Integer] id
13
- #
14
- def call(id)
15
- Mihari::Models::Tag.find(id).destroy
16
- end
17
- end
18
-
19
10
  namespace :tags do
20
- desc "Get tags", {
11
+ desc "List tags", {
21
12
  is_array: true,
22
- success: Entities::Tags,
23
- summary: "Get tags"
13
+ success: Entities::TagsWithPagination,
14
+ summary: "List tags"
24
15
  }
16
+ params do
17
+ optional :q, type: String, default: ""
18
+ optional :page, type: Integer, default: 1
19
+ optional :limit, type: Integer, default: 10
20
+ end
25
21
  get "/" do
26
- tags = Mihari::Models::Tag.distinct.pluck(:name)
27
- present({ tags: tags }, with: Entities::Tags)
22
+ value = Services::TagSearcher.call(params.to_h)
23
+ present(
24
+ {
25
+ results: value.results,
26
+ total: value.total,
27
+ current_page: value.filter[:page].to_i,
28
+ page_size: value.filter[:limit].to_i
29
+ },
30
+ with: Entities::TagsWithPagination
31
+ )
28
32
  end
29
33
 
30
34
  desc "Delete a tag", {
31
35
  success: { code: 204, model: Entities::Message },
32
- failure: [{ code: 404, model: Entities::Message }],
36
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
33
37
  summary: "Delete a tag"
34
38
  }
35
39
  params do
@@ -39,12 +43,12 @@ module Mihari
39
43
  status 204
40
44
 
41
45
  id = params[:id].to_i
42
- result = TagDestroyer.result(id)
46
+ result = Services::TagDestroyer.result(id)
43
47
  return present({ message: "" }, with: Entities::Message) if result.success?
44
48
 
45
49
  case result.failure
46
50
  when ActiveRecord::RecordNotFound
47
- error!({ message: "ID:#{id} is not found" }, 404)
51
+ error!({ message: "ID:#{id} not found" }, 404)
48
52
  end
49
53
  raise result.failure
50
54
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Web
5
+ module Middleware
6
+ #
7
+ # Customized Sentry::Rack::CaptureExceptions
8
+ #
9
+ class CaptureExceptions < Sentry::Rack::CaptureExceptions
10
+ include Concerns::ErrorUnwrappable
11
+
12
+ private
13
+
14
+ def capture_exception(exception, env)
15
+ unwrapped = unwrap_error(exception)
16
+ Mihari.logger.error unwrapped
17
+
18
+ Sentry.capture_exception(unwrapped).tap do |event|
19
+ env[ERROR_EVENT_ID_KEY] = event.event_id if event
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -6,7 +6,9 @@ module Mihari
6
6
  #
7
7
  # DB connection adapter for Rack app
8
8
  #
9
- class ConnectionAdapter
9
+ class Connection
10
+ include Concerns::DatabaseConnectable
11
+
10
12
  attr_reader :app
11
13
 
12
14
  def initialize(app)
@@ -14,7 +16,7 @@ module Mihari
14
16
  end
15
17
 
16
18
  def call(env)
17
- Mihari::Database.with_db_connection { app.call env }
19
+ with_db_connection { app.call env }
18
20
  end
19
21
  end
20
22
  end