mihari 6.3.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +14 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +61 -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 +10 -2
  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 +3 -10
  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 +61 -97
  90. data/lib/mihari/models/autonomous_system.rb +0 -24
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +0 -23
  93. data/lib/mihari/models/dns.rb +0 -20
  94. data/lib/mihari/models/geolocation.rb +0 -24
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +0 -23
  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 +0 -17
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +3 -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/builders.rb +158 -0
  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/services/rule_builder.rb +0 -46
  157. data/lib/mihari/structs/ipinfo.rb +0 -53
  158. data/lib/mihari/web/endpoints/exports.rb +0 -0
  159. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  160. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  161. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -1,21 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "launchy"
4
-
3
+ # Rack
5
4
  require "rack"
6
- require "rackup"
7
5
  require "rack/cors"
6
+ require "rack/session"
7
+ require "rackup"
8
8
 
9
9
  require "rack/handler/puma"
10
10
 
11
+ # Grape
11
12
  require "grape-swagger"
12
13
  require "grape-swagger-entity"
13
14
 
14
- require "mihari/web/middleware/connection_adapter"
15
- require "mihari/web/middleware/error_notification_adapter"
15
+ require "launchy"
16
+
17
+ require "mihari/web/middleware/connection"
18
+ require "mihari/web/middleware/capture_exceptions"
16
19
 
17
20
  require "mihari/web/api"
18
21
 
22
+ # Sidekiq
23
+ require "sidekiq/web"
24
+
25
+ require "mihari/sidekiq/application"
26
+
19
27
  module Mihari
20
28
  module Web
21
29
  #
@@ -41,10 +49,10 @@ module Mihari
41
49
  status, headers, body = API.call(env)
42
50
  return [status, headers, body] unless headers["x-cascade"] == "pass"
43
51
 
52
+ req = Rack::Request.new(env)
44
53
  # Check if the App wants us to pass the response along to others
45
- request_path = env["PATH_INFO"]
46
54
  filenames.each do |path|
47
- static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => request_path + path))
55
+ static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => req.path_info + path))
48
56
  return [static_status, static_headers, static_body] if static_status != 404
49
57
  end
50
58
 
@@ -60,24 +68,29 @@ module Mihari
60
68
  resource "*", headers: :any, methods: %i[get post put delete options]
61
69
  end
62
70
  end
63
- use Middleware::ConnectionAdapter
64
- use Middleware::ErrorNotificationAdapter
71
+ use Middleware::Connection
72
+ use Middleware::CaptureExceptions
73
+ use BetterErrors::Middleware if Mihari.development? && defined?(BetterErrors::Middleware)
74
+
75
+ if Mihari.sidekiq?
76
+ use Rack::Session::Cookie, secret: SecureRandom.hex(32), same_site: true, max_age: 86_400
65
77
 
66
- use Sentry::Rack::CaptureExceptions if Sentry.initialized?
67
- use BetterErrors::Middleware if ENV["RACK_ENV"] == "development" && defined?(BetterErrors::Middleware)
78
+ map "/sidekiq" do
79
+ run Sidekiq::Web
80
+ end
81
+ end
68
82
 
69
83
  run App.new
70
84
  end.to_app
71
85
  end
72
86
 
73
87
  def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60, open: true)
74
- url = "http://#{host}:#{port}"
75
-
76
88
  # set maximum number of threads to use as PARALLEL_PROCESSOR_COUNT (if it is not set)
77
89
  # ref. https://github.com/grosser/parallel#tips
78
90
  # TODO: is this the best way?
79
91
  _min_thread, max_thread = threads.split(":")
80
- ENV["PARALLEL_PROCESSOR_COUNT"] = max_thread if ENV["PARALLEL_PROCESSOR_COUNT"].nil?
92
+ ENV["PARALLEL_PROCESSOR_COUNT"] ||= max_thread
93
+
81
94
  Rackup::Handler::Puma.run(
82
95
  instance,
83
96
  Port: port,
@@ -86,7 +99,7 @@ module Mihari
86
99
  Verbose: verbose,
87
100
  worker_timeout: worker_timeout
88
101
  ) do |_|
89
- Launchy.open(url) if ENV["RACK_ENV"] != "development" && open
102
+ Launchy.open("http://#{host}:#{port}") if !Mihari.development? && open
90
103
  rescue Launchy::CommandNotFoundError
91
104
  # ref. https://github.com/ninoseki/mihari/issues/477
92
105
  # do nothing
@@ -7,84 +7,22 @@ module Mihari
7
7
  # Alert API endpoint
8
8
  #
9
9
  class Alerts < Grape::API
10
- class AlertSearcher < Mihari::Service
11
- class ResultValue
12
- # @return [Array<Mihari::Models::Alert>]
13
- attr_reader :alerts
14
-
15
- # @return [Integer]
16
- attr_reader :total
17
-
18
- # @return [Mihari::Structs::Filters::Alert::SearchFilterWithPagination]
19
- attr_reader :filter
20
-
21
- #
22
- # @param [Array<Mihari::Models::Alert>] alerts
23
- # @param [Integer] total
24
- # @param [Mihari::Structs::Filters::Alert::SearchFilterWithPagination] filter
25
- #
26
- def initialize(alerts:, total:, filter:)
27
- @alerts = alerts
28
- @total = total
29
- @filter = filter
30
- end
31
- end
32
-
33
- #
34
- # @param [Hash] params
35
- #
36
- # @return [ResultValue]
37
- #
38
- def call(params)
39
- filter = params.to_h.to_snake_keys.symbolize_keys
40
- search_filter_with_pagination = Structs::Filters::Alert::SearchFilterWithPagination.new(**filter)
41
- alerts = Mihari::Models::Alert.search(search_filter_with_pagination)
42
- total = Mihari::Models::Alert.count(search_filter_with_pagination.without_pagination)
43
- ResultValue.new(alerts: alerts, total: total, filter: filter)
44
- end
45
- end
46
-
47
- class AlertCreator < Service
48
- #
49
- # @param [Hash] params
50
- #
51
- # @return [Mihari::Models::Alert]
52
- #
53
- def call(params)
54
- proxy = Services::AlertProxy.new(**params.to_snake_keys)
55
- Services::AlertRunner.call proxy
56
- end
57
- end
58
-
59
- class AlertDestroyer < Service
60
- #
61
- # @param [String] id
62
- #
63
- def call(id)
64
- Mihari::Models::Alert.find(id).destroy
65
- end
66
- end
67
-
68
10
  namespace :alerts do
69
- desc "Search alerts", {
11
+ desc "List/search alerts", {
70
12
  is_array: true,
71
13
  success: Entities::AlertsWithPagination,
72
- summary: "Search alerts"
14
+ summary: "List/search alerts"
73
15
  }
74
16
  params do
17
+ optional :q, type: String, default: ""
75
18
  optional :page, type: Integer, default: 1
76
19
  optional :limit, type: Integer, default: 10
77
- optional :artifact, type: String
78
- optional :ruleId, type: String
79
- optional :tag, type: String
80
- optional :fromAt, type: DateTime
81
- optional :toAt, type: DateTime
82
20
  end
83
21
  get "/" do
84
- value = AlertSearcher.call(params.to_h)
22
+ value = Services::AlertSearcher.call(params.to_h)
85
23
  present(
86
24
  {
87
- alerts: value.alerts,
25
+ results: value.results,
88
26
  total: value.total,
89
27
  current_page: value.filter[:page].to_i,
90
28
  page_size: value.filter[:limit].to_i
@@ -93,9 +31,29 @@ module Mihari
93
31
  )
94
32
  end
95
33
 
34
+ desc "Get an alert", {
35
+ success: Entities::Alert,
36
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
37
+ summary: "Get an alert"
38
+ }
39
+ params do
40
+ requires :id, type: Integer
41
+ end
42
+ get "/:id" do
43
+ id = params[:id].to_i
44
+ result = Services::AlertGetter.result(id)
45
+ return present(result.value!, with: Entities::Alert) if result.success?
46
+
47
+ case result.failure
48
+ when ActiveRecord::RecordNotFound
49
+ error!({ message: "ID:#{id} not found" }, 404)
50
+ end
51
+ raise result.failure
52
+ end
53
+
96
54
  desc "Delete an alert", {
97
55
  success: { code: 204, model: Entities::Message },
98
- failure: [{ code: 404, model: Entities::Message }],
56
+ failure: [{ code: 404, model: Entities::ErrorMessage }],
99
57
  summary: "Delete an alert"
100
58
  }
101
59
  params do
@@ -105,34 +63,37 @@ module Mihari
105
63
  status 204
106
64
 
107
65
  id = params["id"].to_i
108
- result = AlertDestroyer.result(id)
66
+ result = Services::AlertDestroyer.result(id)
109
67
  return present({ message: "" }, with: Entities::Message) if result.success?
110
68
 
111
69
  case result.failure
112
70
  when ActiveRecord::RecordNotFound
113
- error!({ message: "ID:#{id} is not found" }, 404)
71
+ error!({ message: "ID:#{id} not found" }, 404)
114
72
  end
115
73
  raise result.failure
116
74
  end
117
75
 
118
76
  desc "Create an alert", {
119
77
  success: { code: 201, model: Entities::Alert },
120
- failure: [{ code: 404, model: Entities::Message }],
78
+ failure: [
79
+ { code: 404, model: Entities::ErrorMessage }
80
+ ],
121
81
  summary: "Create an alert"
122
82
  }
123
83
  params do
124
84
  requires :ruleId, type: String, documentation: { param_type: "body" }
125
85
  requires :artifacts, type: Array, documentation: { type: String, is_array: true, param_type: "body" }
86
+ optional :source, type: String, documentation: { param_type: "body" }
126
87
  end
127
88
  post "/" do
128
89
  status 201
129
90
 
130
- result = AlertCreator.result(params)
91
+ result = Services::AlertCreator.result(params)
131
92
  return present(result.value!, with: Entities::Alert) if result.success?
132
93
 
133
94
  case result.failure
134
95
  when ActiveRecord::RecordNotFound
135
- error!({ message: "Rule:#{params["ruleId"]} is not found" }, 404)
96
+ error!({ message: "Rule:#{params["ruleId"]} not found" }, 404)
136
97
  end
137
98
  raise result.failure
138
99
  end
@@ -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