mihari 5.6.2 → 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 +73 -73
- data/frontend/package.json +5 -5
- 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/actor.rb +14 -4
- data/lib/mihari/analyzers/base.rb +13 -12
- data/lib/mihari/analyzers/binaryedge.rb +4 -1
- data/lib/mihari/analyzers/censys.rb +4 -2
- 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 +6 -1
- data/lib/mihari/analyzers/onyphe.rb +4 -1
- data/lib/mihari/analyzers/otx.rb +4 -1
- data/lib/mihari/analyzers/passivetotal.rb +4 -1
- data/lib/mihari/analyzers/pulsedive.rb +3 -0
- data/lib/mihari/analyzers/securitytrails.rb +4 -1
- data/lib/mihari/analyzers/shodan.rb +4 -1
- data/lib/mihari/analyzers/urlscan.rb +4 -1
- data/lib/mihari/analyzers/virustotal.rb +4 -1
- data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -1
- data/lib/mihari/analyzers/zoomeye.rb +5 -2
- 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 +3 -0
- 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 +3 -0
- data/lib/mihari/clients/publsedive.rb +4 -1
- data/lib/mihari/clients/securitytrails.rb +3 -0
- 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 +10 -1
- 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 +13 -11
- data/lib/mihari/emitters/database.rb +4 -1
- data/lib/mihari/emitters/misp.rb +7 -4
- data/lib/mihari/emitters/slack.rb +3 -3
- data/lib/mihari/emitters/the_hive.rb +3 -3
- data/lib/mihari/emitters/webhook.rb +4 -3
- data/lib/mihari/enrichers/base.rb +15 -9
- 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 +12 -9
- 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 +7 -4
- data/lib/mihari/models/artifact.rb +6 -0
- data/lib/mihari/models/autonomous_system.rb +4 -1
- data/lib/mihari/models/cpe.rb +4 -1
- data/lib/mihari/models/dns.rb +4 -1
- data/lib/mihari/models/geolocation.rb +4 -1
- data/lib/mihari/models/port.rb +4 -1
- data/lib/mihari/models/reverse_dns.rb +4 -1
- data/lib/mihari/models/rule.rb +6 -3
- data/lib/mihari/models/tag.rb +3 -0
- data/lib/mihari/models/tagging.rb +3 -0
- data/lib/mihari/models/whois.rb +4 -3
- data/lib/mihari/rule.rb +17 -12
- data/lib/mihari/schemas/alert.rb +3 -0
- data/lib/mihari/schemas/analyzer.rb +11 -0
- data/lib/mihari/schemas/emitter.rb +3 -0
- data/lib/mihari/schemas/enricher.rb +3 -0
- data/lib/mihari/schemas/macros.rb +4 -0
- data/lib/mihari/schemas/mixins.rb +5 -0
- data/lib/mihari/schemas/rule.rb +3 -0
- data/lib/mihari/service.rb +16 -0
- data/lib/mihari/services/alert_builder.rb +8 -5
- data/lib/mihari/services/alert_proxy.rb +6 -1
- data/lib/mihari/services/alert_runner.rb +8 -12
- data/lib/mihari/services/rule_builder.rb +8 -5
- data/lib/mihari/services/rule_runner.rb +8 -10
- data/lib/mihari/structs/binaryedge.rb +13 -28
- data/lib/mihari/structs/censys.rb +48 -127
- data/lib/mihari/structs/config.rb +19 -30
- 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 +33 -84
- data/lib/mihari/structs/hunterhow.rb +24 -22
- data/lib/mihari/structs/ipinfo.rb +14 -35
- data/lib/mihari/structs/onyphe.rb +31 -76
- data/lib/mihari/structs/shodan.rb +47 -112
- data/lib/mihari/structs/urlscan.rb +24 -63
- data/lib/mihari/structs/virustotal_intelligence.rb +20 -56
- 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 +203 -198
- 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-28d4c79d.js → index-821134e2.js} +30 -30
- 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 +24 -5
- data/mihari.gemspec +8 -1
- data/mkdocs.yml +3 -2
- metadata +39 -7
- data/lib/mihari/templates/rule.yml.erb +0 -5
- data/lib/mihari/web/public/assets/mode-yaml-a21faa53.js +0 -8
@@ -1,225 +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::Models::Rule.search(search_filter_with_pagenation)
|
44
|
-
total = Mihari::Models::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::Models::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
|
-
if result.success?
|
97
|
-
result.value!.analyzer.run
|
98
|
-
status 201
|
99
|
-
return present({ message: "ID:#{id} is ran successfully" }, with: Entities::Message)
|
100
|
-
end
|
76
|
+
return present(result.value!, with: Entities::Rule) if result.success?
|
101
77
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
108
85
|
end
|
109
|
-
end
|
110
86
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
-
post "/" do
|
119
|
-
extend Dry::Monads[:result, :try]
|
120
|
-
|
121
|
-
yaml = params[:yaml]
|
122
|
-
result = Try { Rule.from_yaml(yaml) }.to_result.bind do |rule|
|
123
|
-
Try do
|
124
|
-
found = Mihari::Models::Rule.find_by_id(rule.id)
|
125
|
-
error!({ message: "ID:#{rule.id} is already registered" }, 400) unless found.nil?
|
126
|
-
rule
|
127
|
-
end.to_result
|
128
|
-
end.bind do |rule|
|
129
|
-
Try do
|
130
|
-
rule.model.save
|
131
|
-
rule
|
132
|
-
end.to_result
|
87
|
+
desc "Run a rule", {
|
88
|
+
success: Entities::Message,
|
89
|
+
summary: "Run a rule"
|
90
|
+
}
|
91
|
+
params do
|
92
|
+
requires :id, type: String
|
133
93
|
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
else
|
147
|
-
raise failure
|
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" }
|
148
121
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
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
|
174
153
|
end
|
175
154
|
|
176
|
-
|
177
|
-
|
178
|
-
|
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" }
|
179
162
|
end
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
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
|
191
196
|
end
|
192
|
-
end
|
193
|
-
|
194
|
-
desc "Delete a rule", {
|
195
|
-
success: Entities::Message,
|
196
|
-
failure: [{ code: 404, message: "Not found", model: Entities::Message }],
|
197
|
-
summary: "Delete a rule"
|
198
|
-
}
|
199
|
-
params do
|
200
|
-
requires :id, type: String
|
201
|
-
end
|
202
|
-
delete "/:id" do
|
203
|
-
extend Dry::Monads[:result, :try]
|
204
197
|
|
205
|
-
|
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
|
205
|
+
end
|
206
|
+
delete "/:id" do
|
207
|
+
extend Dry::Monads[:result, :try]
|
206
208
|
|
207
|
-
|
208
|
-
rule = Mihari::Models::Rule.find(id)
|
209
|
-
rule.destroy
|
210
|
-
end.to_result
|
209
|
+
id = params["id"].to_s
|
211
210
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
211
|
+
result = Try do
|
212
|
+
rule = Mihari::Models::Rule.find(id)
|
213
|
+
rule.destroy
|
214
|
+
end.to_result
|
216
215
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
223
228
|
end
|
224
229
|
end
|
225
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
|