mihari 5.6.1 → 5.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -1
- data/README.md +1 -0
- data/config.ru +1 -1
- data/docs/analyzers/fofa.md +31 -0
- data/docs/analyzers/index.md +1 -0
- data/frontend/package-lock.json +183 -186
- data/frontend/package.json +10 -10
- data/frontend/src/components/alert/Form.vue +1 -14
- data/frontend/src/components/artifact/AS.vue +2 -8
- data/frontend/src/components/artifact/DnsRecords.vue +2 -8
- data/frontend/src/components/artifact/ReverseDnsNames.vue +2 -10
- data/frontend/src/components/artifact/WhoisRecord.vue +1 -1
- data/lib/mihari/{base.rb → actor.rb} +27 -3
- data/lib/mihari/analyzers/base.rb +16 -20
- data/lib/mihari/analyzers/binaryedge.rb +4 -1
- data/lib/mihari/analyzers/censys.rb +5 -3
- data/lib/mihari/analyzers/circl.rb +4 -1
- data/lib/mihari/analyzers/crtsh.rb +4 -1
- data/lib/mihari/analyzers/dnstwister.rb +4 -1
- data/lib/mihari/analyzers/feed.rb +3 -0
- data/lib/mihari/analyzers/fofa.rb +65 -0
- data/lib/mihari/analyzers/greynoise.rb +4 -1
- data/lib/mihari/analyzers/hunterhow.rb +7 -2
- data/lib/mihari/analyzers/onyphe.rb +4 -1
- data/lib/mihari/analyzers/otx.rb +4 -1
- data/lib/mihari/analyzers/passivetotal.rb +5 -2
- data/lib/mihari/analyzers/pulsedive.rb +4 -1
- data/lib/mihari/analyzers/securitytrails.rb +5 -2
- data/lib/mihari/analyzers/shodan.rb +4 -1
- data/lib/mihari/analyzers/urlscan.rb +5 -2
- data/lib/mihari/analyzers/virustotal.rb +9 -6
- data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -1
- data/lib/mihari/analyzers/zoomeye.rb +8 -5
- data/lib/mihari/cli/alert.rb +3 -0
- data/lib/mihari/cli/base.rb +3 -0
- data/lib/mihari/cli/database.rb +3 -0
- data/lib/mihari/cli/main.rb +3 -0
- data/lib/mihari/cli/rule.rb +3 -0
- data/lib/mihari/clients/base.rb +3 -0
- data/lib/mihari/clients/binaryedge.rb +5 -2
- data/lib/mihari/clients/censys.rb +7 -4
- data/lib/mihari/clients/circl.rb +3 -0
- data/lib/mihari/clients/crtsh.rb +5 -2
- data/lib/mihari/clients/dnstwister.rb +3 -0
- data/lib/mihari/clients/fofa.rb +83 -0
- data/lib/mihari/clients/greynoise.rb +5 -2
- data/lib/mihari/clients/hunterhow.rb +5 -2
- data/lib/mihari/clients/misp.rb +3 -0
- data/lib/mihari/clients/onyphe.rb +5 -2
- data/lib/mihari/clients/otx.rb +3 -0
- data/lib/mihari/clients/passivetotal.rb +7 -4
- data/lib/mihari/clients/publsedive.rb +4 -1
- data/lib/mihari/clients/securitytrails.rb +6 -3
- data/lib/mihari/clients/shodan.rb +5 -2
- data/lib/mihari/clients/the_hive.rb +3 -0
- data/lib/mihari/clients/urlscan.rb +7 -4
- data/lib/mihari/clients/virustotal.rb +5 -2
- data/lib/mihari/clients/zoomeye.rb +3 -0
- data/lib/mihari/commands/alert.rb +5 -14
- data/lib/mihari/commands/database.rb +3 -0
- data/lib/mihari/commands/rule.rb +11 -11
- data/lib/mihari/commands/search.rb +9 -6
- data/lib/mihari/commands/version.rb +3 -0
- data/lib/mihari/commands/web.rb +4 -1
- data/lib/mihari/config.rb +139 -150
- data/lib/mihari/constants.rb +1 -1
- data/lib/mihari/database.rb +6 -0
- data/lib/mihari/emitters/base.rb +16 -25
- data/lib/mihari/emitters/database.rb +10 -9
- data/lib/mihari/emitters/misp.rb +20 -41
- data/lib/mihari/emitters/slack.rb +16 -13
- data/lib/mihari/emitters/the_hive.rb +18 -46
- data/lib/mihari/emitters/webhook.rb +34 -23
- data/lib/mihari/enrichers/base.rb +16 -15
- data/lib/mihari/enrichers/google_public_dns.rb +6 -5
- data/lib/mihari/enrichers/ipinfo.rb +10 -8
- data/lib/mihari/enrichers/shodan.rb +4 -6
- data/lib/mihari/enrichers/whois.rb +13 -10
- data/lib/mihari/errors.rb +6 -0
- data/lib/mihari/feed/parser.rb +3 -0
- data/lib/mihari/feed/reader.rb +3 -0
- data/lib/mihari/http.rb +6 -0
- data/lib/mihari/mixins/autonomous_system.rb +3 -0
- data/lib/mihari/mixins/configurable.rb +3 -0
- data/lib/mihari/mixins/error_notification.rb +3 -0
- data/lib/mihari/mixins/falsepositive.rb +3 -0
- data/lib/mihari/mixins/refang.rb +3 -0
- data/lib/mihari/mixins/retriable.rb +6 -2
- data/lib/mihari/models/alert.rb +78 -73
- data/lib/mihari/models/artifact.rb +186 -178
- data/lib/mihari/models/autonomous_system.rb +25 -20
- data/lib/mihari/models/cpe.rb +24 -19
- data/lib/mihari/models/dns.rb +27 -22
- data/lib/mihari/models/geolocation.rb +25 -20
- data/lib/mihari/models/port.rb +24 -19
- data/lib/mihari/models/reverse_dns.rb +24 -19
- data/lib/mihari/models/rule.rb +71 -66
- data/lib/mihari/models/tag.rb +8 -3
- data/lib/mihari/models/tagging.rb +8 -3
- data/lib/mihari/models/whois.rb +20 -17
- data/lib/mihari/rule.rb +357 -0
- data/lib/mihari/schemas/alert.rb +3 -0
- data/lib/mihari/schemas/analyzer.rb +105 -87
- data/lib/mihari/schemas/emitter.rb +12 -5
- data/lib/mihari/schemas/enricher.rb +11 -4
- data/lib/mihari/schemas/macros.rb +4 -0
- data/lib/mihari/schemas/mixins.rb +20 -0
- data/lib/mihari/schemas/rule.rb +6 -10
- data/lib/mihari/service.rb +16 -0
- data/lib/mihari/services/alert_builder.rb +8 -5
- data/lib/mihari/services/alert_proxy.rb +16 -7
- data/lib/mihari/services/alert_runner.rb +10 -14
- data/lib/mihari/services/rule_builder.rb +10 -7
- data/lib/mihari/services/rule_runner.rb +11 -13
- data/lib/mihari/structs/binaryedge.rb +14 -29
- data/lib/mihari/structs/censys.rb +54 -133
- data/lib/mihari/structs/config.rb +20 -31
- data/lib/mihari/structs/filters.rb +38 -0
- data/lib/mihari/structs/fofa.rb +44 -0
- data/lib/mihari/structs/google_public_dns.rb +10 -28
- data/lib/mihari/structs/greynoise.rb +38 -89
- data/lib/mihari/structs/hunterhow.rb +27 -25
- data/lib/mihari/structs/ipinfo.rb +14 -35
- data/lib/mihari/structs/onyphe.rb +36 -81
- data/lib/mihari/structs/shodan.rb +53 -118
- data/lib/mihari/structs/urlscan.rb +27 -66
- data/lib/mihari/structs/virustotal_intelligence.rb +23 -59
- data/lib/mihari/type_checker.rb +4 -0
- data/lib/mihari/types.rb +3 -0
- data/lib/mihari/version.rb +1 -1
- data/lib/mihari/web/api.rb +15 -10
- data/lib/mihari/web/app.rb +59 -54
- data/lib/mihari/web/endpoints/alerts.rb +94 -89
- data/lib/mihari/web/endpoints/artifacts.rb +115 -110
- data/lib/mihari/web/endpoints/configs.rb +18 -13
- data/lib/mihari/web/endpoints/ip_addresses.rb +21 -16
- data/lib/mihari/web/endpoints/rules.rb +202 -204
- data/lib/mihari/web/endpoints/tags.rb +41 -36
- data/lib/mihari/web/middleware/connection_adapter.rb +16 -9
- data/lib/mihari/web/middleware/error_notification_adapter.rb +17 -10
- data/lib/mihari/web/public/assets/{index-9cc489e6.js → index-821134e2.js} +54 -54
- data/lib/mihari/web/public/assets/mode-yaml-24faa242.js +8 -0
- data/lib/mihari/web/public/index.html +1 -1
- data/lib/mihari.rb +30 -13
- data/mihari.gemspec +9 -3
- data/mkdocs.yml +3 -2
- data/requirements.txt +1 -1
- metadata +44 -26
- data/lib/mihari/analyzers/rule.rb +0 -232
- data/lib/mihari/services/rule_proxy.rb +0 -182
- data/lib/mihari/templates/rule.yml.erb +0 -5
- data/lib/mihari/web/public/assets/mode-yaml-a21faa53.js +0 -8
@@ -1,24 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mihari
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
module Web
|
5
|
+
module Endpoints
|
6
|
+
#
|
7
|
+
# IP address API endpoint
|
8
|
+
#
|
9
|
+
class IPAddresses < Grape::API
|
10
|
+
namespace :ip_addresses do
|
11
|
+
desc "Get an IP address", {
|
12
|
+
success: Entities::IPAddress,
|
13
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
14
|
+
summary: "Get an IP address"
|
15
|
+
}
|
16
|
+
params do
|
17
|
+
requires :ip, type: String, regexp: /\A[0-9.]+\z/
|
18
|
+
end
|
19
|
+
get "/:ip", requirements: { ip: %r{[^/]+} } do
|
20
|
+
ip = params[:ip].to_s
|
17
21
|
|
18
|
-
|
19
|
-
|
22
|
+
data = Enrichers::IPInfo.new.call(ip)
|
23
|
+
error!({ message: "IP:#{ip} is not found" }, 404) if data.nil?
|
20
24
|
|
21
|
-
|
25
|
+
present data, with: Entities::IPAddress
|
26
|
+
end
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|
@@ -1,232 +1,230 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mihari
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
21
|
-
summary: "Search rules"
|
22
|
-
}
|
23
|
-
params do
|
24
|
-
optional :page, type: Integer, default: 1
|
25
|
-
optional :limit, type: Integer, default: 10
|
26
|
-
|
27
|
-
optional :title, type: String
|
28
|
-
optional :description, type: String
|
29
|
-
optional :tag, type: String
|
30
|
-
|
31
|
-
optional :fromAt, type: DateTime
|
32
|
-
optional :toAt, type: DateTime
|
33
|
-
end
|
34
|
-
get "/" do
|
35
|
-
filter = params.to_h.to_snake_keys
|
36
|
-
|
37
|
-
# normalize keys
|
38
|
-
filter["tag_name"] = filter["tag"]
|
39
|
-
# symbolize hash keys
|
40
|
-
filter = filter.to_h.symbolize_keys
|
41
|
-
|
42
|
-
search_filter_with_pagenation = Structs::Filters::Rule::SearchFilterWithPagination.new(**filter)
|
43
|
-
rules = Mihari::Rule.search(search_filter_with_pagenation)
|
44
|
-
total = Mihari::Rule.count(search_filter_with_pagenation.without_pagination)
|
45
|
-
|
46
|
-
present(
|
47
|
-
{ rules: rules,
|
48
|
-
total: total,
|
49
|
-
current_page: filter[:page].to_i,
|
50
|
-
page_size: filter[:limit].to_i },
|
51
|
-
with: Entities::RulesWithPagination
|
52
|
-
)
|
53
|
-
end
|
54
|
-
|
55
|
-
desc "Get a rule", {
|
56
|
-
success: Entities::Rule,
|
57
|
-
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
58
|
-
summary: "Get a rule"
|
59
|
-
}
|
60
|
-
params do
|
61
|
-
requires :id, type: String
|
62
|
-
end
|
63
|
-
get "/:id" do
|
64
|
-
extend Dry::Monads[:result, :try]
|
65
|
-
|
66
|
-
id = params["id"].to_s
|
67
|
-
|
68
|
-
result = Try do
|
69
|
-
Mihari::Rule.find(id)
|
70
|
-
end.to_result
|
4
|
+
module Web
|
5
|
+
module Endpoints
|
6
|
+
#
|
7
|
+
# Rule API endpoint
|
8
|
+
#
|
9
|
+
class Rules < Grape::API
|
10
|
+
namespace :rules do
|
11
|
+
desc "Get Rule IDs", {
|
12
|
+
is_array: true,
|
13
|
+
success: Entities::RuleIDs,
|
14
|
+
summary: "Get rule IDs"
|
15
|
+
}
|
16
|
+
get "/ids" do
|
17
|
+
rule_ids = Mihari::Models::Rule.distinct.pluck(:id)
|
18
|
+
present({ rule_ids: rule_ids }, with: Entities::RuleIDs)
|
19
|
+
end
|
71
20
|
|
72
|
-
|
21
|
+
desc "Search rules", {
|
22
|
+
is_array: true,
|
23
|
+
success: Entities::RulesWithPagination,
|
24
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
25
|
+
summary: "Search rules"
|
26
|
+
}
|
27
|
+
params do
|
28
|
+
optional :page, type: Integer, default: 1
|
29
|
+
optional :limit, type: Integer, default: 10
|
30
|
+
|
31
|
+
optional :title, type: String
|
32
|
+
optional :description, type: String
|
33
|
+
optional :tag, type: String
|
34
|
+
|
35
|
+
optional :fromAt, type: DateTime
|
36
|
+
optional :toAt, type: DateTime
|
37
|
+
end
|
38
|
+
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
|
+
)
|
57
|
+
end
|
73
58
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
59
|
+
desc "Get a rule", {
|
60
|
+
success: Entities::Rule,
|
61
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
62
|
+
summary: "Get a rule"
|
63
|
+
}
|
64
|
+
params do
|
65
|
+
requires :id, type: String
|
80
66
|
end
|
81
|
-
|
67
|
+
get "/:id" do
|
68
|
+
extend Dry::Monads[:result, :try]
|
82
69
|
|
83
|
-
|
84
|
-
success: Entities::Message,
|
85
|
-
summary: "Run a rule"
|
86
|
-
}
|
87
|
-
params do
|
88
|
-
requires :id, type: String
|
89
|
-
end
|
90
|
-
get "/:id/run" do
|
91
|
-
extend Dry::Monads[:result, :try]
|
70
|
+
id = params["id"].to_s
|
92
71
|
|
93
|
-
|
72
|
+
result = Try do
|
73
|
+
Mihari::Models::Rule.find(id)
|
74
|
+
end.to_result
|
94
75
|
|
95
|
-
|
96
|
-
Mihari::Services::RuleProxy.from_model(Mihari::Rule.find(id))
|
97
|
-
end.to_result
|
76
|
+
return present(result.value!, with: Entities::Rule) if result.success?
|
98
77
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
78
|
+
failure = result.failure
|
79
|
+
case failure
|
80
|
+
when ActiveRecord::RecordNotFound
|
81
|
+
error!({ message: "ID:#{id} is not found" }, 404)
|
82
|
+
else
|
83
|
+
raise failure
|
84
|
+
end
|
103
85
|
end
|
104
86
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
87
|
+
desc "Run a rule", {
|
88
|
+
success: Entities::Message,
|
89
|
+
summary: "Run a rule"
|
90
|
+
}
|
91
|
+
params do
|
92
|
+
requires :id, type: String
|
111
93
|
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
rule
|
132
|
-
end.to_result
|
133
|
-
end.bind do |rule|
|
134
|
-
Try do
|
135
|
-
rule.model.save
|
136
|
-
rule
|
137
|
-
end.to_result
|
94
|
+
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
|
100
|
+
if result.success?
|
101
|
+
result.value!.analyzer.run
|
102
|
+
status 201
|
103
|
+
return present({ message: "ID:#{id} is ran successfully" }, with: Entities::Message)
|
104
|
+
end
|
105
|
+
|
106
|
+
failure = result.failure
|
107
|
+
case failure
|
108
|
+
when ActiveRecord::RecordNotFound
|
109
|
+
error!({ message: "ID:#{id} is not found" }, 404)
|
110
|
+
else
|
111
|
+
raise failure
|
112
|
+
end
|
138
113
|
end
|
139
114
|
|
140
|
-
|
141
|
-
|
142
|
-
|
115
|
+
desc "Create a rule", {
|
116
|
+
success: Entities::Rule,
|
117
|
+
summary: "Create a rule"
|
118
|
+
}
|
119
|
+
params do
|
120
|
+
requires :yaml, type: String, documentation: { param_type: "body" }
|
143
121
|
end
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
122
|
+
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
|
+
|
139
|
+
if result.success?
|
140
|
+
status 201
|
141
|
+
return present(result.value!.model, with: Entities::Rule)
|
142
|
+
end
|
143
|
+
|
144
|
+
failure = result.failure
|
145
|
+
case failure
|
146
|
+
when Psych::SyntaxError
|
147
|
+
error!({ message: failure.message }, 400)
|
148
|
+
when ValidationError
|
149
|
+
error!({ message: "Data format is invalid", details: failure.errors.to_h }, 400)
|
150
|
+
else
|
151
|
+
raise failure
|
152
|
+
end
|
153
153
|
end
|
154
|
-
end
|
155
154
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
end
|
164
|
-
put "/" do
|
165
|
-
extend Dry::Monads[:result, :try]
|
166
|
-
|
167
|
-
id = params[:id]
|
168
|
-
yaml = params[:yaml]
|
169
|
-
|
170
|
-
result = Try do
|
171
|
-
Mihari::Rule.find(id)
|
172
|
-
end.to_result.bind do |_|
|
173
|
-
Try do
|
174
|
-
Services::RuleProxy.from_yaml(yaml)
|
175
|
-
end.to_result
|
176
|
-
end.bind do |rule|
|
177
|
-
Try do
|
178
|
-
rule.model.save
|
179
|
-
rule
|
180
|
-
end.to_result
|
155
|
+
desc "Update a rule", {
|
156
|
+
success: Entities::Rule,
|
157
|
+
summary: "Update a rule"
|
158
|
+
}
|
159
|
+
params do
|
160
|
+
requires :id, type: String, documentation: { param_type: "body" }
|
161
|
+
requires :yaml, type: String, documentation: { param_type: "body" }
|
181
162
|
end
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
163
|
+
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
|
+
|
180
|
+
if result.success?
|
181
|
+
status 201
|
182
|
+
return present(result.value!.model, with: Entities::Rule)
|
183
|
+
end
|
184
|
+
|
185
|
+
failure = result.failure
|
186
|
+
case failure
|
187
|
+
when ActiveRecord::RecordNotFound
|
188
|
+
error!({ message: "ID:#{id} is not found" }, 404)
|
189
|
+
when Psych::SyntaxError
|
190
|
+
error!({ message: failure.message }, 400)
|
191
|
+
when ValidationError
|
192
|
+
error!({ message: "Data format is invalid", details: failure.errors.to_h }, 400)
|
193
|
+
else
|
194
|
+
raise failure
|
195
|
+
end
|
186
196
|
end
|
187
197
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
error!({ message: "Data format is invalid", details: failure.errors.to_h }, 400)
|
196
|
-
else
|
197
|
-
raise failure
|
198
|
+
desc "Delete a rule", {
|
199
|
+
success: Entities::Message,
|
200
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
201
|
+
summary: "Delete a rule"
|
202
|
+
}
|
203
|
+
params do
|
204
|
+
requires :id, type: String
|
198
205
|
end
|
199
|
-
|
200
|
-
|
201
|
-
desc "Delete a rule", {
|
202
|
-
success: Entities::Message,
|
203
|
-
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
204
|
-
summary: "Delete a rule"
|
205
|
-
}
|
206
|
-
params do
|
207
|
-
requires :id, type: String
|
208
|
-
end
|
209
|
-
delete "/:id" do
|
210
|
-
extend Dry::Monads[:result, :try]
|
211
|
-
|
212
|
-
id = params["id"].to_s
|
206
|
+
delete "/:id" do
|
207
|
+
extend Dry::Monads[:result, :try]
|
213
208
|
|
214
|
-
|
215
|
-
rule = Mihari::Rule.find(id)
|
216
|
-
rule.destroy
|
217
|
-
end.to_result
|
209
|
+
id = params["id"].to_s
|
218
210
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
211
|
+
result = Try do
|
212
|
+
rule = Mihari::Models::Rule.find(id)
|
213
|
+
rule.destroy
|
214
|
+
end.to_result
|
223
215
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
216
|
+
if result.success?
|
217
|
+
status 204
|
218
|
+
return present({ message: "ID:#{id} is deleted" }, with: Entities::Message)
|
219
|
+
end
|
220
|
+
|
221
|
+
failure = result.failure
|
222
|
+
case failure
|
223
|
+
when ActiveRecord::RecordNotFound
|
224
|
+
error!({ message: "ID:#{id} is not found" }, 404)
|
225
|
+
else
|
226
|
+
raise failure
|
227
|
+
end
|
230
228
|
end
|
231
229
|
end
|
232
230
|
end
|
@@ -1,47 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mihari
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
module Web
|
5
|
+
module Endpoints
|
6
|
+
#
|
7
|
+
# Tag API endpoint
|
8
|
+
#
|
9
|
+
class Tags < Grape::API
|
10
|
+
namespace :tags do
|
11
|
+
desc "Get tags", {
|
12
|
+
is_array: true,
|
13
|
+
success: Entities::Tags,
|
14
|
+
summary: "Get tags"
|
15
|
+
}
|
16
|
+
get "/" do
|
17
|
+
tags = Mihari::Models::Tag.distinct.pluck(:name)
|
18
|
+
present({ tags: tags }, with: Entities::Tags)
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
desc "Delete a tag", {
|
22
|
+
success: Entities::Message,
|
23
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
24
|
+
summary: "Delete a tag"
|
25
|
+
}
|
26
|
+
params do
|
27
|
+
requires :name, type: String
|
28
|
+
end
|
29
|
+
delete "/:name" do
|
30
|
+
extend Dry::Monads[:result, :try]
|
27
31
|
|
28
|
-
|
32
|
+
name = params[:name].to_s
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
result = Try do
|
35
|
+
Mihari::Models::Tag.where(name: name).destroy_all
|
36
|
+
end.to_result
|
33
37
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
if result.success?
|
39
|
+
status 204
|
40
|
+
return present({ message: "" }, with: Entities::Message)
|
41
|
+
end
|
38
42
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
failure = result.failure
|
44
|
+
case failure
|
45
|
+
when ActiveRecord::RecordNotFound
|
46
|
+
error!({ message: "Name:#{name} is not found" }, 404)
|
47
|
+
else
|
48
|
+
raise failure
|
49
|
+
end
|
45
50
|
end
|
46
51
|
end
|
47
52
|
end
|
@@ -1,15 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Mihari
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
module Web
|
5
|
+
module Middleware
|
6
|
+
#
|
7
|
+
# DB connection adapter for Rack app
|
8
|
+
#
|
9
|
+
class ConnectionAdapter
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
end
|
7
13
|
|
8
|
-
|
9
|
-
|
10
|
-
|
14
|
+
def call(env)
|
15
|
+
Mihari::Database.with_db_connection do
|
16
|
+
status, headers, body = @app.call(env)
|
11
17
|
|
12
|
-
|
18
|
+
[status, headers, body]
|
19
|
+
end
|
13
20
|
end
|
14
21
|
end
|
15
22
|
end
|
@@ -1,17 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Mihari
|
2
|
-
module
|
3
|
-
|
4
|
-
|
4
|
+
module Web
|
5
|
+
module Middleware
|
6
|
+
#
|
7
|
+
# Error notification adapter for Rack app
|
8
|
+
#
|
9
|
+
class ErrorNotificationAdapter
|
10
|
+
include Mixins::ErrorNotification
|
5
11
|
|
6
|
-
|
7
|
-
|
8
|
-
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
end
|
9
15
|
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
def call(env)
|
17
|
+
with_error_notification do
|
18
|
+
status, headers, body = @app.call(env)
|
13
19
|
|
14
|
-
|
20
|
+
[status, headers, body]
|
21
|
+
end
|
15
22
|
end
|
16
23
|
end
|
17
24
|
end
|