mihari 5.7.0 → 5.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/lib/mihari/actor.rb +10 -4
  4. data/lib/mihari/cli/main.rb +2 -0
  5. data/lib/mihari/clients/base.rb +23 -1
  6. data/lib/mihari/clients/binaryedge.rb +1 -3
  7. data/lib/mihari/clients/censys.rb +1 -2
  8. data/lib/mihari/clients/crtsh.rb +2 -3
  9. data/lib/mihari/clients/dnstwister.rb +1 -2
  10. data/lib/mihari/clients/fofa.rb +1 -3
  11. data/lib/mihari/clients/greynoise.rb +1 -2
  12. data/lib/mihari/clients/hunterhow.rb +1 -2
  13. data/lib/mihari/clients/misp.rb +1 -2
  14. data/lib/mihari/clients/onyphe.rb +1 -2
  15. data/lib/mihari/clients/otx.rb +2 -14
  16. data/lib/mihari/clients/passivetotal.rb +3 -16
  17. data/lib/mihari/clients/publsedive.rb +2 -17
  18. data/lib/mihari/clients/securitytrails.rb +3 -25
  19. data/lib/mihari/clients/shodan.rb +1 -2
  20. data/lib/mihari/clients/the_hive.rb +1 -2
  21. data/lib/mihari/clients/urlscan.rb +1 -2
  22. data/lib/mihari/clients/virustotal.rb +3 -17
  23. data/lib/mihari/clients/zoomeye.rb +9 -19
  24. data/lib/mihari/commands/alert.rb +11 -9
  25. data/lib/mihari/commands/database.rb +4 -1
  26. data/lib/mihari/commands/mixins.rb +11 -0
  27. data/lib/mihari/commands/search.rb +13 -32
  28. data/lib/mihari/constants.rb +1 -1
  29. data/lib/mihari/database.rb +1 -1
  30. data/lib/mihari/enrichers/ipinfo.rb +1 -1
  31. data/lib/mihari/entities/tag.rb +1 -0
  32. data/lib/mihari/http.rb +13 -11
  33. data/lib/mihari/rule.rb +14 -0
  34. data/lib/mihari/service.rb +12 -2
  35. data/lib/mihari/services/alert_builder.rb +81 -8
  36. data/lib/mihari/services/alert_runner.rb +3 -10
  37. data/lib/mihari/services/rule_builder.rb +8 -10
  38. data/lib/mihari/services/rule_runner.rb +2 -25
  39. data/lib/mihari/structs/binaryedge.rb +9 -0
  40. data/lib/mihari/structs/censys.rb +0 -14
  41. data/lib/mihari/structs/fofa.rb +3 -0
  42. data/lib/mihari/structs/google_public_dns.rb +0 -4
  43. data/lib/mihari/structs/greynoise.rb +0 -6
  44. data/lib/mihari/structs/hunterhow.rb +0 -6
  45. data/lib/mihari/structs/ipinfo.rb +0 -2
  46. data/lib/mihari/structs/onyphe.rb +0 -4
  47. data/lib/mihari/structs/shodan.rb +0 -2
  48. data/lib/mihari/structs/urlscan.rb +0 -6
  49. data/lib/mihari/structs/virustotal_intelligence.rb +0 -8
  50. data/lib/mihari/version.rb +1 -1
  51. data/lib/mihari/web/app.rb +20 -17
  52. data/lib/mihari/web/endpoints/alerts.rb +75 -38
  53. data/lib/mihari/web/endpoints/artifacts.rb +60 -53
  54. data/lib/mihari/web/endpoints/ip_addresses.rb +19 -4
  55. data/lib/mihari/web/endpoints/rules.rb +132 -88
  56. data/lib/mihari/web/endpoints/tags.rb +15 -13
  57. data/lib/mihari/web/middleware/error_notification_adapter.rb +8 -3
  58. data/lib/mihari/web/public/assets/{index-821134e2.js → index-ec641cb0.js} +45 -44
  59. data/lib/mihari/web/public/index.html +1 -1
  60. data/lib/mihari/web/public/redoc-static.html +400 -400
  61. data/lib/mihari.rb +0 -2
  62. data/mihari.gemspec +5 -5
  63. data/mkdocs.yml +14 -7
  64. metadata +13 -140
  65. data/docs/alternatives.md +0 -5
  66. data/docs/analyzers/binaryedge.md +0 -26
  67. data/docs/analyzers/censys.md +0 -31
  68. data/docs/analyzers/circl.md +0 -37
  69. data/docs/analyzers/crtsh.md +0 -26
  70. data/docs/analyzers/dnstwister.md +0 -25
  71. data/docs/analyzers/feed.md +0 -73
  72. data/docs/analyzers/fofa.md +0 -31
  73. data/docs/analyzers/greynoise.md +0 -26
  74. data/docs/analyzers/hunterhow.md +0 -33
  75. data/docs/analyzers/index.md +0 -104
  76. data/docs/analyzers/onyphe.md +0 -26
  77. data/docs/analyzers/otx.md +0 -28
  78. data/docs/analyzers/passivetotal.md +0 -52
  79. data/docs/analyzers/pulsedive.md +0 -28
  80. data/docs/analyzers/securitytrails.md +0 -41
  81. data/docs/analyzers/shodan.md +0 -26
  82. data/docs/analyzers/urlscan.md +0 -28
  83. data/docs/analyzers/virustotal.md +0 -43
  84. data/docs/analyzers/virustotal_intelligence.md +0 -33
  85. data/docs/analyzers/zoomeye.md +0 -38
  86. data/docs/configuration.md +0 -35
  87. data/docs/emitters/database.md +0 -22
  88. data/docs/emitters/hive.md +0 -26
  89. data/docs/emitters/index.md +0 -36
  90. data/docs/emitters/misp.md +0 -21
  91. data/docs/emitters/slack.md +0 -21
  92. data/docs/emitters/webhook.md +0 -63
  93. data/docs/enrichers/google_public_dns.md +0 -19
  94. data/docs/enrichers/index.md +0 -35
  95. data/docs/enrichers/ipinfo.md +0 -26
  96. data/docs/enrichers/shodan.md +0 -22
  97. data/docs/enrichers/whois.md +0 -17
  98. data/docs/github_actions.md +0 -43
  99. data/docs/index.md +0 -11
  100. data/docs/installation.md +0 -31
  101. data/docs/requirements.md +0 -13
  102. data/docs/rule.md +0 -168
  103. data/docs/tags.md +0 -3
  104. data/docs/usage.md +0 -103
  105. data/frontend/.eslintrc.cjs +0 -22
  106. data/frontend/.gitignore +0 -31
  107. data/frontend/.prettierrc.json +0 -8
  108. data/frontend/README.md +0 -3
  109. data/frontend/env.d.ts +0 -5
  110. data/frontend/index.html +0 -21
  111. data/frontend/package-lock.json +0 -7219
  112. data/frontend/package.json +0 -67
  113. data/frontend/public/favicon.ico +0 -0
  114. data/frontend/scripts/swagger_doc_to_yaml.rb +0 -23
  115. data/frontend/src/App.vue +0 -27
  116. data/frontend/src/ace-config.ts +0 -6
  117. data/frontend/src/api-helper.ts +0 -111
  118. data/frontend/src/api.ts +0 -105
  119. data/frontend/src/components/ErrorMessage.vue +0 -31
  120. data/frontend/src/components/Loading.vue +0 -15
  121. data/frontend/src/components/Navbar.vue +0 -42
  122. data/frontend/src/components/Pagination.vue +0 -119
  123. data/frontend/src/components/alert/Alert.vue +0 -87
  124. data/frontend/src/components/alert/Alerts.vue +0 -63
  125. data/frontend/src/components/alert/AlertsWithPagination.vue +0 -90
  126. data/frontend/src/components/alert/AlertsWrapper.vue +0 -128
  127. data/frontend/src/components/alert/Form.vue +0 -169
  128. data/frontend/src/components/artifact/AS.vue +0 -23
  129. data/frontend/src/components/artifact/Artifact.vue +0 -287
  130. data/frontend/src/components/artifact/ArtifactTag.vue +0 -64
  131. data/frontend/src/components/artifact/ArtifactTags.vue +0 -29
  132. data/frontend/src/components/artifact/ArtifactWrapper.vue +0 -57
  133. data/frontend/src/components/artifact/CPEs.vue +0 -23
  134. data/frontend/src/components/artifact/DnsRecords.vue +0 -32
  135. data/frontend/src/components/artifact/Ports.vue +0 -23
  136. data/frontend/src/components/artifact/ReverseDnsNames.vue +0 -23
  137. data/frontend/src/components/artifact/Tags.vue +0 -29
  138. data/frontend/src/components/artifact/WhoisRecord.vue +0 -44
  139. data/frontend/src/components/config/Configs.vue +0 -65
  140. data/frontend/src/components/config/ConfigsWrapper.vue +0 -32
  141. data/frontend/src/components/link/Link.vue +0 -32
  142. data/frontend/src/components/link/Links.vue +0 -42
  143. data/frontend/src/components/rule/EditRule.vue +0 -72
  144. data/frontend/src/components/rule/EditRuleWrapper.vue +0 -48
  145. data/frontend/src/components/rule/Form.vue +0 -158
  146. data/frontend/src/components/rule/InputForm.vue +0 -45
  147. data/frontend/src/components/rule/NewRule.vue +0 -57
  148. data/frontend/src/components/rule/Rule.vue +0 -100
  149. data/frontend/src/components/rule/RuleWrapper.vue +0 -53
  150. data/frontend/src/components/rule/Rules.vue +0 -84
  151. data/frontend/src/components/rule/RulesWrapper.vue +0 -121
  152. data/frontend/src/components/rule/YAML.vue +0 -37
  153. data/frontend/src/components/tag/Tag.vue +0 -65
  154. data/frontend/src/components/tag/Tags.vue +0 -37
  155. data/frontend/src/countries.ts +0 -350
  156. data/frontend/src/index.ts +0 -20
  157. data/frontend/src/links/anyrun.ts +0 -19
  158. data/frontend/src/links/base.ts +0 -14
  159. data/frontend/src/links/censys.ts +0 -20
  160. data/frontend/src/links/crtsh.ts +0 -20
  161. data/frontend/src/links/dnslytics.ts +0 -38
  162. data/frontend/src/links/greynoise.ts +0 -20
  163. data/frontend/src/links/index.ts +0 -40
  164. data/frontend/src/links/intezer.ts +0 -20
  165. data/frontend/src/links/otx.ts +0 -33
  166. data/frontend/src/links/securitytrails.ts +0 -38
  167. data/frontend/src/links/shodan.ts +0 -20
  168. data/frontend/src/links/urlscan.ts +0 -50
  169. data/frontend/src/links/virustotal.ts +0 -72
  170. data/frontend/src/main.ts +0 -41
  171. data/frontend/src/router/index.ts +0 -57
  172. data/frontend/src/rule.ts +0 -14
  173. data/frontend/src/shims-vue.d.ts +0 -6
  174. data/frontend/src/swagger.yaml +0 -771
  175. data/frontend/src/types.ts +0 -188
  176. data/frontend/src/utils.ts +0 -54
  177. data/frontend/src/views/Alerts.vue +0 -20
  178. data/frontend/src/views/Artifact.vue +0 -39
  179. data/frontend/src/views/Configs.vue +0 -20
  180. data/frontend/src/views/EditRule.vue +0 -39
  181. data/frontend/src/views/NewRule.vue +0 -26
  182. data/frontend/src/views/Rule.vue +0 -39
  183. data/frontend/src/views/Rules.vue +0 -20
  184. data/frontend/tests/utils.spec.ts +0 -9
  185. data/frontend/tsconfig.app.json +0 -21
  186. data/frontend/tsconfig.json +0 -14
  187. data/frontend/tsconfig.node.json +0 -13
  188. data/frontend/tsconfig.vitest.json +0 -12
  189. data/frontend/vite.config.ts +0 -24
  190. data/frontend/vitest.config.ts +0 -21
  191. data/lib/mihari/mixins/error_notification.rb +0 -21
  192. data/lib/mihari/services/alert_proxy.rb +0 -97
@@ -7,6 +7,72 @@ 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
40
+
41
+ # normalize keys
42
+ filter["artifact_data"] = filter["artifact"]
43
+ filter["tag_name"] = filter["tag"]
44
+ # symbolize hash keys
45
+ filter = filter.to_h.symbolize_keys
46
+
47
+ search_filter_with_pagination = Structs::Filters::Alert::SearchFilterWithPagination.new(**filter)
48
+ alerts = Mihari::Models::Alert.search(search_filter_with_pagination)
49
+ total = Mihari::Models::Alert.count(search_filter_with_pagination.without_pagination)
50
+
51
+ ResultValue.new(alerts: alerts, total: total, filter: filter)
52
+ end
53
+ end
54
+
55
+ class AlertCreator < Service
56
+ #
57
+ # @param [Hash] params
58
+ #
59
+ # @return [Mihari::Models::Alert]
60
+ #
61
+ def call(params)
62
+ proxy = Services::AlertProxy.new(**params.to_snake_keys)
63
+ Services::AlertRunner.call proxy
64
+ end
65
+ end
66
+
67
+ class AlertDestroyer < Service
68
+ #
69
+ # @param [String] id
70
+ #
71
+ def call(id)
72
+ Mihari::Models::Alert.find(id).destroy
73
+ end
74
+ end
75
+
10
76
  namespace :alerts do
11
77
  desc "Search alerts", {
12
78
  is_array: true,
@@ -17,33 +83,20 @@ module Mihari
17
83
  params do
18
84
  optional :page, type: Integer, default: 1
19
85
  optional :limit, type: Integer, default: 10
20
-
21
86
  optional :artifact, type: String
22
87
  optional :rule_id, type: String
23
88
  optional :tag, type: String
24
-
25
89
  optional :fromAt, type: DateTime
26
90
  optional :toAt, type: DateTime
27
91
  end
28
92
  get "/" do
29
- filter = params.to_h.to_snake_keys
30
-
31
- # normalize keys
32
- filter["artifact_data"] = filter["artifact"]
33
- filter["tag_name"] = filter["tag"]
34
- # symbolize hash keys
35
- filter = filter.to_h.symbolize_keys
36
-
37
- search_filter_with_pagination = Structs::Filters::Alert::SearchFilterWithPagination.new(**filter)
38
- alerts = Mihari::Models::Alert.search(search_filter_with_pagination)
39
- total = Mihari::Models::Alert.count(search_filter_with_pagination.without_pagination)
40
-
93
+ value = AlertSearcher.call(params.to_h)
41
94
  present(
42
95
  {
43
- alerts: alerts,
44
- total: total,
45
- current_page: filter[:page].to_i,
46
- page_size: filter[:limit].to_i
96
+ alerts: value.alerts,
97
+ total: value.total,
98
+ current_page: value.filter[:page].to_i,
99
+ page_size: value.filter[:limit].to_i
47
100
  },
48
101
  with: Entities::AlertsWithPagination
49
102
  )
@@ -58,15 +111,8 @@ module Mihari
58
111
  requires :id, type: Integer
59
112
  end
60
113
  delete "/:id" do
61
- extend Dry::Monads[:result, :try]
62
-
63
114
  id = params["id"].to_i
64
-
65
- result = Try do
66
- alert = Mihari::Models::Alert.find(id)
67
- alert.destroy
68
- end.to_result
69
-
115
+ result = AlertDestroyer.result(id)
70
116
  if result.success?
71
117
  status 204
72
118
  return present({ message: "" }, with: Entities::Message)
@@ -76,9 +122,8 @@ module Mihari
76
122
  case failure
77
123
  when ActiveRecord::RecordNotFound
78
124
  error!({ message: "ID:#{id} is not found" }, 404)
79
- else
80
- raise failure
81
125
  end
126
+ raise failure
82
127
  end
83
128
 
84
129
  desc "Create an alert", {
@@ -90,14 +135,7 @@ module Mihari
90
135
  requires :artifacts, type: Array, documentation: { type: String, is_array: true, param_type: "body" }
91
136
  end
92
137
  post "/" do
93
- extend Dry::Monads[:result, :try]
94
-
95
- result = Try do
96
- proxy = Services::AlertProxy.new(**params.to_snake_keys)
97
- runner = Services::AlertRunner.new(proxy)
98
- runner.call
99
- end.to_result
100
-
138
+ result = AlertCreator.result(params)
101
139
  if result.success?
102
140
  status 201
103
141
  return present(result.value!, with: Entities::Alert)
@@ -107,9 +145,8 @@ module Mihari
107
145
  case failure
108
146
  when ActiveRecord::RecordNotFound
109
147
  error!({ message: "Rule:#{params["ruleId"]} is not found" }, 404)
110
- else
111
- raise failure
112
148
  end
149
+ raise failure
113
150
  end
114
151
  end
115
152
  end
@@ -7,6 +7,60 @@ 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
+
10
64
  namespace :artifacts do
11
65
  desc "Get an artifact", {
12
66
  success: Entities::Artifact,
@@ -17,37 +71,16 @@ module Mihari
17
71
  requires :id, type: Integer
18
72
  end
19
73
  get "/:id" do
20
- extend Dry::Monads[:result, :try]
21
-
22
74
  id = params[:id].to_i
23
-
24
- result = Try do
25
- artifact = Mihari::Models::Artifact.includes(
26
- :autonomous_system,
27
- :geolocation,
28
- :whois_record,
29
- :dns_records,
30
- :reverse_dns_names
31
- ).find(id)
32
- # TODO: improve queries
33
- alert_ids = Mihari::Models::Artifact.where(data: artifact.data).pluck(:alert_id)
34
- tag_ids = Mihari::Models::Tagging.where(alert_id: alert_ids).pluck(:tag_id)
35
- tag_names = Mihari::Models::Tag.where(id: tag_ids).distinct.pluck(:name)
36
-
37
- artifact.tags = tag_names
38
-
39
- artifact
40
- end.to_result
41
-
75
+ result = ArtifactGetter.result(id)
42
76
  return present(result.value!, with: Entities::Artifact) if result.success?
43
77
 
44
78
  failure = result.failure
45
79
  case failure
46
80
  when ActiveRecord::RecordNotFound
47
81
  error!({ message: "ID:#{id} is not found" }, 404)
48
- else
49
- raise failure
50
82
  end
83
+ raise failure
51
84
  end
52
85
 
53
86
  desc "Enrich an artifact", {
@@ -59,25 +92,8 @@ module Mihari
59
92
  requires :id, type: Integer
60
93
  end
61
94
  get "/:id/enrich" do
62
- extend Dry::Monads[:result, :try]
63
-
64
95
  id = params["id"].to_i
65
-
66
- result = Try do
67
- artifact = Mihari::Models::Artifact.includes(
68
- :autonomous_system,
69
- :geolocation,
70
- :whois_record,
71
- :dns_records,
72
- :reverse_dns_names,
73
- :cpes,
74
- :ports
75
- ).find(id)
76
-
77
- artifact.enrich_all
78
- artifact.save
79
- end.to_result
80
-
96
+ result = ArtifactEnricher.result(id)
81
97
  if result.success?
82
98
  status 201
83
99
  return present({ message: "" }, with: Entities::Message)
@@ -87,9 +103,8 @@ module Mihari
87
103
  case failure
88
104
  when ActiveRecord::RecordNotFound
89
105
  error!({ message: "ID:#{id} is not found" }, 404)
90
- else
91
- raise failure
92
106
  end
107
+ raise failure
93
108
  end
94
109
 
95
110
  desc "Delete an artifact", {
@@ -101,15 +116,8 @@ module Mihari
101
116
  requires :id, type: Integer
102
117
  end
103
118
  delete "/:id" do
104
- extend Dry::Monads[:result, :try]
105
-
106
119
  id = params["id"].to_i
107
-
108
- result = Try do
109
- alert = Mihari::Models::Artifact.find(id)
110
- alert.destroy
111
- end.to_result
112
-
120
+ result = ArtifactDestroyer.result(id)
113
121
  if result.success?
114
122
  status 204
115
123
  return present({ message: "" }, with: Entities::Message)
@@ -119,9 +127,8 @@ module Mihari
119
127
  case failure
120
128
  when ActiveRecord::RecordNotFound
121
129
  error!({ message: "ID:#{id} is not found" }, 404)
122
- else
123
- raise failure
124
130
  end
131
+ raise failure
125
132
  end
126
133
  end
127
134
  end
@@ -7,6 +7,17 @@ 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
+
10
21
  namespace :ip_addresses do
11
22
  desc "Get an IP address", {
12
23
  success: Entities::IPAddress,
@@ -18,11 +29,15 @@ module Mihari
18
29
  end
19
30
  get "/:ip", requirements: { ip: %r{[^/]+} } do
20
31
  ip = params[:ip].to_s
32
+ result = IPGetter.result(ip)
33
+ return present(result.value!, with: Entities::IPAddress) if result.success?
21
34
 
22
- data = Enrichers::IPInfo.new.call(ip)
23
- error!({ message: "IP:#{ip} is not found" }, 404) if data.nil?
24
-
25
- present data, with: Entities::IPAddress
35
+ failure = result.failure
36
+ case failure
37
+ when Mihari::StatusCodeError
38
+ error!({ message: "ID:#{id} is not found" }, 404) if failure.status_code == 404
39
+ end
40
+ raise failure
26
41
  end
27
42
  end
28
43
  end
@@ -7,6 +7,113 @@ 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]
35
+ #
36
+ # @return [ResultValue]
37
+ #
38
+ def call(params)
39
+ filter = params.to_h.to_snake_keys
40
+
41
+ # normalize keys
42
+ filter["tag_name"] = filter["tag"]
43
+ # symbolize hash keys
44
+ filter = filter.to_h.symbolize_keys
45
+
46
+ search_filter_with_pagination = Mihari::Structs::Filters::Rule::SearchFilterWithPagination.new(**filter)
47
+ rules = Mihari::Models::Rule.search(search_filter_with_pagination)
48
+ total = Mihari::Models::Rule.count(search_filter_with_pagination.without_pagination)
49
+
50
+ ResultValue.new(rules: rules, total: total, filter: filter)
51
+ end
52
+ end
53
+
54
+ class RuleGetter < Service
55
+ #
56
+ # @params [String] id
57
+ #
58
+ # @return [Mihari::Models::Rule]
59
+ #
60
+ def call(id)
61
+ Mihari::Models::Rule.find id
62
+ end
63
+ end
64
+
65
+ class RuleRunner < Service
66
+ #
67
+ # @param [String] id
68
+ #
69
+ def call(id)
70
+ rule = Mihari::Rule.from_model(Mihari::Models::Rule.find(id))
71
+ rule.call
72
+ end
73
+ end
74
+
75
+ class RuleCreator < Service
76
+ #
77
+ # @params [String]
78
+ #
79
+ # @return [Mihari::Models::Rule]
80
+ #
81
+ def call(yaml)
82
+ rule = Rule.from_yaml(yaml)
83
+
84
+ found = Mihari::Models::Rule.find_by_id(rule.id)
85
+ error!({ message: "ID:#{rule.id} is already registered" }, 400) unless found.nil?
86
+
87
+ rule.model.save
88
+ rule
89
+ end
90
+ end
91
+
92
+ class RuleUpdater < Service
93
+ #
94
+ # @params [String] id
95
+ # @params [String] yaml
96
+ #
97
+ # @return [Mihari::Models::Rule]
98
+ #
99
+ def call(id:, yaml:)
100
+ Mihari::Models::Rule.find(id)
101
+
102
+ rule = Rule.from_yaml(yaml)
103
+ rule.model.save
104
+ rule
105
+ end
106
+ end
107
+
108
+ class RuleDestroyer < Service
109
+ #
110
+ # @param [String] id
111
+ #
112
+ def call(id)
113
+ Mihari::Models::Rule.find(id).destroy
114
+ end
115
+ end
116
+
10
117
  namespace :rules do
11
118
  desc "Get Rule IDs", {
12
119
  is_array: true,
@@ -27,33 +134,21 @@ module Mihari
27
134
  params do
28
135
  optional :page, type: Integer, default: 1
29
136
  optional :limit, type: Integer, default: 10
30
-
31
137
  optional :title, type: String
32
138
  optional :description, type: String
33
139
  optional :tag, type: String
34
-
35
140
  optional :fromAt, type: DateTime
36
141
  optional :toAt, type: DateTime
37
142
  end
38
143
  get "/" do
39
- filter = params.to_h.to_snake_keys
40
-
41
- # normalize keys
42
- filter["tag_name"] = filter["tag"]
43
- # symbolize hash keys
44
- filter = filter.to_h.symbolize_keys
45
-
46
- search_filter_with_pagenation = Structs::Filters::Rule::SearchFilterWithPagination.new(**filter)
47
- rules = Mihari::Models::Rule.search(search_filter_with_pagenation)
48
- total = Mihari::Models::Rule.count(search_filter_with_pagenation.without_pagination)
49
-
50
- present(
51
- { rules: rules,
52
- total: total,
53
- current_page: filter[:page].to_i,
54
- page_size: filter[:limit].to_i },
55
- with: Entities::RulesWithPagination
56
- )
144
+ value = RuleSearcher.call(params.to_h)
145
+ present({
146
+ rules: value.rules,
147
+ total: value.total,
148
+ current_page: value.filter[:page].to_i,
149
+ page_size: value.filter[:limit].to_i
150
+ },
151
+ with: Entities::RulesWithPagination)
57
152
  end
58
153
 
59
154
  desc "Get a rule", {
@@ -65,23 +160,16 @@ module Mihari
65
160
  requires :id, type: String
66
161
  end
67
162
  get "/:id" do
68
- extend Dry::Monads[:result, :try]
69
-
70
- id = params["id"].to_s
71
-
72
- result = Try do
73
- Mihari::Models::Rule.find(id)
74
- end.to_result
75
-
163
+ id = params[:id].to_s
164
+ result = RuleGetter.result(params[:id].to_s)
76
165
  return present(result.value!, with: Entities::Rule) if result.success?
77
166
 
78
167
  failure = result.failure
79
168
  case failure
80
169
  when ActiveRecord::RecordNotFound
81
170
  error!({ message: "ID:#{id} is not found" }, 404)
82
- else
83
- raise failure
84
171
  end
172
+ raise failure
85
173
  end
86
174
 
87
175
  desc "Run a rule", {
@@ -92,24 +180,19 @@ module Mihari
92
180
  requires :id, type: String
93
181
  end
94
182
  get "/:id/run" do
95
- extend Dry::Monads[:result, :try]
96
-
97
- id = params["id"].to_s
98
-
99
- result = Try { Rule.from_model(Mihari::Models::Rule.find(id)) }.to_result
183
+ id = params[:id].to_s
184
+ result = RuleRunner.result(id)
100
185
  if result.success?
101
- result.value!.analyzer.run
102
186
  status 201
103
- return present({ message: "ID:#{id} is ran successfully" }, with: Entities::Message)
187
+ return present({ message: "ID:#{id}} ran successfully" }, with: Entities::Message)
104
188
  end
105
189
 
106
190
  failure = result.failure
107
191
  case failure
108
192
  when ActiveRecord::RecordNotFound
109
193
  error!({ message: "ID:#{id} is not found" }, 404)
110
- else
111
- raise failure
112
194
  end
195
+ raise failure
113
196
  end
114
197
 
115
198
  desc "Create a rule", {
@@ -120,22 +203,7 @@ module Mihari
120
203
  requires :yaml, type: String, documentation: { param_type: "body" }
121
204
  end
122
205
  post "/" do
123
- extend Dry::Monads[:result, :try]
124
-
125
- yaml = params[:yaml]
126
- result = Try { Rule.from_yaml(yaml) }.to_result.bind do |rule|
127
- Try do
128
- found = Mihari::Models::Rule.find_by_id(rule.id)
129
- error!({ message: "ID:#{rule.id} is already registered" }, 400) unless found.nil?
130
- rule
131
- end.to_result
132
- end.bind do |rule|
133
- Try do
134
- rule.model.save
135
- rule
136
- end.to_result
137
- end
138
-
206
+ result = RuleCreator.result(params[:yaml])
139
207
  if result.success?
140
208
  status 201
141
209
  return present(result.value!.model, with: Entities::Rule)
@@ -146,10 +214,9 @@ module Mihari
146
214
  when Psych::SyntaxError
147
215
  error!({ message: failure.message }, 400)
148
216
  when ValidationError
149
- error!({ message: "Data format is invalid", details: failure.errors.to_h }, 400)
150
- else
151
- raise failure
217
+ error!({ message: "Rule format is invalid", details: failure.errors.to_h }, 400)
152
218
  end
219
+ raise failure
153
220
  end
154
221
 
155
222
  desc "Update a rule", {
@@ -161,22 +228,8 @@ module Mihari
161
228
  requires :yaml, type: String, documentation: { param_type: "body" }
162
229
  end
163
230
  put "/" do
164
- extend Dry::Monads[:result, :try]
165
-
166
- id = params[:id]
167
- yaml = params[:yaml]
168
-
169
- result = Try do
170
- Mihari::Models::Rule.find(id)
171
- end.to_result.bind do |_|
172
- Try { Rule.from_yaml(yaml) }.to_result
173
- end.bind do |rule|
174
- Try do
175
- rule.model.save
176
- rule
177
- end.to_result
178
- end
179
-
231
+ id = params[:id].to_s
232
+ result = RuleUpdater.result(id: id, yaml: params[:yaml].to_s)
180
233
  if result.success?
181
234
  status 201
182
235
  return present(result.value!.model, with: Entities::Rule)
@@ -189,10 +242,9 @@ module Mihari
189
242
  when Psych::SyntaxError
190
243
  error!({ message: failure.message }, 400)
191
244
  when ValidationError
192
- error!({ message: "Data format is invalid", details: failure.errors.to_h }, 400)
193
- else
194
- raise failure
245
+ error!({ message: "Rule format is invalid", details: failure.errors.to_h }, 400)
195
246
  end
247
+ raise failure
196
248
  end
197
249
 
198
250
  desc "Delete a rule", {
@@ -204,15 +256,8 @@ module Mihari
204
256
  requires :id, type: String
205
257
  end
206
258
  delete "/:id" do
207
- extend Dry::Monads[:result, :try]
208
-
209
- id = params["id"].to_s
210
-
211
- result = Try do
212
- rule = Mihari::Models::Rule.find(id)
213
- rule.destroy
214
- end.to_result
215
-
259
+ id = params[:id].to_s
260
+ result = RuleDestroyer.result(id)
216
261
  if result.success?
217
262
  status 204
218
263
  return present({ message: "ID:#{id} is deleted" }, with: Entities::Message)
@@ -222,9 +267,8 @@ module Mihari
222
267
  case failure
223
268
  when ActiveRecord::RecordNotFound
224
269
  error!({ message: "ID:#{id} is not found" }, 404)
225
- else
226
- raise failure
227
270
  end
271
+ raise failure
228
272
  end
229
273
  end
230
274
  end