mihari 6.3.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +13 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +62 -0
  7. data/exe/mihari +2 -1
  8. data/lefthook.yml +8 -0
  9. data/lib/mihari/actor.rb +4 -4
  10. data/lib/mihari/analyzers/base.rb +16 -0
  11. data/lib/mihari/analyzers/binaryedge.rb +4 -2
  12. data/lib/mihari/analyzers/censys.rb +7 -5
  13. data/lib/mihari/analyzers/circl.rb +5 -3
  14. data/lib/mihari/analyzers/crtsh.rb +4 -1
  15. data/lib/mihari/analyzers/dnstwister.rb +1 -1
  16. data/lib/mihari/analyzers/feed.rb +12 -20
  17. data/lib/mihari/analyzers/fofa.rb +6 -8
  18. data/lib/mihari/analyzers/greynoise.rb +4 -2
  19. data/lib/mihari/analyzers/hunterhow.rb +4 -2
  20. data/lib/mihari/analyzers/onyphe.rb +4 -2
  21. data/lib/mihari/analyzers/otx.rb +5 -3
  22. data/lib/mihari/analyzers/passivetotal.rb +29 -12
  23. data/lib/mihari/analyzers/pulsedive.rb +5 -3
  24. data/lib/mihari/analyzers/securitytrails.rb +32 -8
  25. data/lib/mihari/analyzers/shodan.rb +4 -2
  26. data/lib/mihari/analyzers/urlscan.rb +4 -2
  27. data/lib/mihari/analyzers/virustotal.rb +5 -5
  28. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
  29. data/lib/mihari/analyzers/zoomeye.rb +4 -2
  30. data/lib/mihari/cli/{main.rb → application.rb} +17 -5
  31. data/lib/mihari/cli/artifact.rb +14 -0
  32. data/lib/mihari/cli/config.rb +14 -0
  33. data/lib/mihari/cli/rule.rb +1 -0
  34. data/lib/mihari/cli/tag.rb +14 -0
  35. data/lib/mihari/clients/base.rb +2 -2
  36. data/lib/mihari/clients/binaryedge.rb +2 -2
  37. data/lib/mihari/clients/crtsh.rb +2 -9
  38. data/lib/mihari/clients/fofa.rb +1 -1
  39. data/lib/mihari/clients/hunterhow.rb +1 -1
  40. data/lib/mihari/clients/mmdb.rb +28 -0
  41. data/lib/mihari/clients/passivetotal.rb +7 -20
  42. data/lib/mihari/clients/securitytrails.rb +19 -43
  43. data/lib/mihari/clients/shodan_internet_db.rb +28 -0
  44. data/lib/mihari/clients/the_hive.rb +7 -5
  45. data/lib/mihari/commands/alert.rb +53 -11
  46. data/lib/mihari/commands/artifact.rb +66 -0
  47. data/lib/mihari/commands/config.rb +23 -0
  48. data/lib/mihari/commands/database.rb +1 -1
  49. data/lib/mihari/commands/rule.rb +40 -27
  50. data/lib/mihari/commands/search.rb +10 -11
  51. data/lib/mihari/commands/sidekiq.rb +31 -0
  52. data/lib/mihari/commands/tag.rb +46 -0
  53. data/lib/mihari/commands/web.rb +6 -7
  54. data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
  55. data/lib/mihari/concerns/configurable.rb +72 -0
  56. data/lib/mihari/concerns/database_connectable.rb +16 -0
  57. data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
  58. data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
  59. data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
  60. data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
  61. data/lib/mihari/config.rb +13 -12
  62. data/lib/mihari/database.rb +30 -42
  63. data/lib/mihari/emitters/database.rb +5 -6
  64. data/lib/mihari/emitters/misp.rb +4 -11
  65. data/lib/mihari/emitters/slack.rb +7 -5
  66. data/lib/mihari/emitters/the_hive.rb +8 -58
  67. data/lib/mihari/emitters/webhook.rb +6 -6
  68. data/lib/mihari/enrichers/google_public_dns.rb +1 -1
  69. data/lib/mihari/enrichers/mmdb.rb +28 -0
  70. data/lib/mihari/enrichers/shodan.rb +3 -5
  71. data/lib/mihari/enrichers/whois.rb +3 -3
  72. data/lib/mihari/entities/alert.rb +3 -10
  73. data/lib/mihari/entities/artifact.rb +6 -14
  74. data/lib/mihari/entities/config.rb +2 -2
  75. data/lib/mihari/entities/cpe.rb +1 -0
  76. data/lib/mihari/entities/dns.rb +1 -0
  77. data/lib/mihari/entities/geolocation.rb +1 -0
  78. data/lib/mihari/entities/ip_address.rb +1 -3
  79. data/lib/mihari/entities/messages.rb +17 -0
  80. data/lib/mihari/entities/pagination.rb +11 -0
  81. data/lib/mihari/entities/port.rb +1 -0
  82. data/lib/mihari/entities/reverse_dns.rb +1 -0
  83. data/lib/mihari/entities/rule.rb +2 -20
  84. data/lib/mihari/entities/tag.rb +2 -2
  85. data/lib/mihari/entities/whois.rb +1 -0
  86. data/lib/mihari/errors.rb +2 -4
  87. data/lib/mihari/http.rb +4 -0
  88. data/lib/mihari/models/alert.rb +21 -53
  89. data/lib/mihari/models/artifact.rb +46 -88
  90. data/lib/mihari/models/autonomous_system.rb +5 -13
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +3 -10
  93. data/lib/mihari/models/dns.rb +2 -6
  94. data/lib/mihari/models/geolocation.rb +7 -12
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +3 -8
  97. data/lib/mihari/models/rule.rb +16 -57
  98. data/lib/mihari/models/tag.rb +17 -1
  99. data/lib/mihari/models/tagging.rb +1 -1
  100. data/lib/mihari/models/whois.rb +1 -4
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +2 -2
  104. data/lib/mihari/schemas/concerns/orrable.rb +24 -0
  105. data/lib/mihari/schemas/emitter.rb +1 -2
  106. data/lib/mihari/schemas/enricher.rb +3 -4
  107. data/lib/mihari/schemas/macros.rb +1 -1
  108. data/lib/mihari/schemas/options.rb +0 -2
  109. data/lib/mihari/schemas/rule.rb +1 -2
  110. data/lib/mihari/services/{rule_builder.rb → builders.rb} +1 -6
  111. data/lib/mihari/services/creators.rb +22 -0
  112. data/lib/mihari/services/destroyers.rb +41 -0
  113. data/lib/mihari/services/enrichers.rb +25 -0
  114. data/lib/mihari/services/feed.rb +107 -0
  115. data/lib/mihari/services/getters.rb +58 -0
  116. data/lib/mihari/services/initializers.rb +22 -0
  117. data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
  118. data/lib/mihari/services/searchers.rb +91 -0
  119. data/lib/mihari/sidekiq/application.rb +13 -0
  120. data/lib/mihari/sidekiq/jobs.rb +36 -0
  121. data/lib/mihari/structs/censys.rb +1 -1
  122. data/lib/mihari/structs/config.rb +10 -10
  123. data/lib/mihari/structs/filters.rb +12 -130
  124. data/lib/mihari/structs/google_public_dns.rb +1 -1
  125. data/lib/mihari/structs/greynoise.rb +1 -1
  126. data/lib/mihari/structs/mmdb.rb +115 -0
  127. data/lib/mihari/structs/onyphe.rb +1 -1
  128. data/lib/mihari/structs/shodan.rb +2 -2
  129. data/lib/mihari/version.rb +1 -1
  130. data/lib/mihari/web/{app.rb → application.rb} +28 -15
  131. data/lib/mihari/web/endpoints/alerts.rb +34 -73
  132. data/lib/mihari/web/endpoints/artifacts.rb +27 -111
  133. data/lib/mihari/web/endpoints/configs.rb +3 -5
  134. data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
  135. data/lib/mihari/web/endpoints/rules.rb +58 -130
  136. data/lib/mihari/web/endpoints/tags.rb +21 -17
  137. data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
  138. data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
  139. data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
  140. data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
  141. data/lib/mihari/web/public/index.html +2 -2
  142. data/lib/mihari/web/public/redoc-static.html +385 -385
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +12 -4
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +164 -34
  148. data/lib/mihari/commands/mixins.rb +0 -11
  149. data/lib/mihari/enrichers/ipinfo.rb +0 -52
  150. data/lib/mihari/entities/message.rb +0 -9
  151. data/lib/mihari/feed/parser.rb +0 -38
  152. data/lib/mihari/feed/reader.rb +0 -111
  153. data/lib/mihari/mixins/configurable.rb +0 -68
  154. data/lib/mihari/schemas/mixins.rb +0 -20
  155. data/lib/mihari/services/alert_runner.rb +0 -20
  156. data/lib/mihari/structs/ipinfo.rb +0 -53
  157. data/lib/mihari/web/endpoints/exports.rb +0 -0
  158. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  159. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  160. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -13,11 +13,11 @@ module Mihari
13
13
 
14
14
  # @!attribute [r] is_configured
15
15
  # @return [Boolean]
16
- attribute :is_configured, Types::Bool
16
+ attribute :configured, Types::Bool
17
17
 
18
18
  # @!attribute [r] values
19
19
  # @return [Array<Hash>, nil]
20
- attribute :values, Types.Array(Types::Hash).optional
20
+ attribute :items, Types.Array(Types::Hash).optional
21
21
 
22
22
  class << self
23
23
  #
@@ -28,9 +28,9 @@ module Mihari
28
28
  # @return [String, nil]
29
29
  #
30
30
  def get_type(klass)
31
- return "Analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
- return "Emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
- return "Enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
31
+ return "analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
+ return "emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
+ return "enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
34
34
 
35
35
  nil
36
36
  end
@@ -45,9 +45,9 @@ module Mihari
45
45
  def get_dummy(klass)
46
46
  type = get_type(klass)
47
47
  case type
48
- when "Analyzer"
48
+ when "analyzer"
49
49
  klass.new("dummy")
50
- when "Emitter"
50
+ when "emitter"
51
51
  klass.new(rule: nil)
52
52
  else
53
53
  klass.new
@@ -68,9 +68,9 @@ module Mihari
68
68
  begin
69
69
  instance = get_dummy(klass)
70
70
  new(
71
- name: klass.to_s.split("::").last.to_s,
72
- values: instance.configuration_values,
73
- is_configured: instance.configured?,
71
+ name: klass.class_key,
72
+ items: klass.configuration_items,
73
+ configured: instance.configured?,
74
74
  type: type
75
75
  )
76
76
  rescue ArgumentError
@@ -3,136 +3,18 @@
3
3
  module Mihari
4
4
  module Structs
5
5
  module Filters
6
- module Artifact
7
- class SearchFilter < Dry::Struct
8
- # @!attribute [r] data_type
9
- # @return [String, nil]
10
- attribute? :data_type, Types::String.optional
11
-
12
- # @!attribute [r] rule_id
13
- # @return [String, nil]
14
- attribute? :rule_id, Types::String.optional
15
-
16
- # @!attribute [r] tag
17
- # @return [String, nil]
18
- attribute? :tag, Types::String.optional
19
-
20
- # @!attribute [r] from_at
21
- # @return [DateTime, nil]
22
- attribute? :from_at, Types::DateTime.optional
23
-
24
- # @!attribute [r] to_at
25
- # @return [DateTime, nil]
26
- attribute? :to_at, Types::DateTime.optional
27
- end
28
-
29
- class SearchFilterWithPagination < SearchFilter
30
- # @!attribute [r] page
31
- # @return [Integer, nil]
32
- attribute? :page, Types::Int.default(1)
33
-
34
- # @!attribute [r] limit
35
- # @return [Integer, nil]
36
- attribute? :limit, Types::Int.default(10)
37
-
38
- def without_pagination
39
- SearchFilter.new(
40
- data_type: data_type,
41
- from_at: from_at,
42
- rule_id: rule_id,
43
- tag: tag,
44
- to_at: to_at
45
- )
46
- end
47
- end
48
- end
49
-
50
- module Alert
51
- class SearchFilter < Dry::Struct
52
- # @!attribute [r] artifact
53
- # @return [String, nil]
54
- attribute? :artifact, Types::String.optional
55
-
56
- # @!attribute [r] rule_id
57
- # @return [String, nil]
58
- attribute? :rule_id, Types::String.optional
59
-
60
- # @!attribute [r] tag
61
- # @return [String, nil]
62
- attribute? :tag, Types::String.optional
63
-
64
- # @!attribute [r] from_at
65
- # @return [DateTime, nil]
66
- attribute? :from_at, Types::DateTime.optional
67
-
68
- # @!attribute [r] to_at
69
- # @return [DateTime, nil]
70
- attribute? :to_at, Types::DateTime.optional
71
- end
72
-
73
- class SearchFilterWithPagination < SearchFilter
74
- # @!attribute [r] page
75
- # @return [Integer, nil]
76
- attribute? :page, Types::Int.default(1)
77
-
78
- # @!attribute [r] limit
79
- # @return [Integer, nil]
80
- attribute? :limit, Types::Int.default(10)
81
-
82
- def without_pagination
83
- SearchFilter.new(
84
- artifact: artifact,
85
- from_at: from_at,
86
- rule_id: rule_id,
87
- tag: tag,
88
- to_at: to_at
89
- )
90
- end
91
- end
92
- end
93
-
94
- module Rule
95
- class SearchFilter < Dry::Struct
96
- # @!attribute [r] description
97
- # @return [String, nil]
98
- attribute? :description, Types::String.optional
99
-
100
- # @!attribute [r] tag
101
- # @return [String, nil]
102
- attribute? :tag, Types::String.optional
103
-
104
- # @!attribute [r] title
105
- # @return [String, nil]
106
- attribute? :title, Types::String.optional
107
-
108
- # @!attribute [r] from_at
109
- # @return [DateTime, nil]
110
- attribute? :from_at, Types::DateTime.optional
111
-
112
- # @!attribute [r] to_at
113
- # @return [DateTime, nil]
114
- attribute? :to_at, Types::DateTime.optional
115
- end
116
-
117
- class SearchFilterWithPagination < SearchFilter
118
- # @!attribute [r] page
119
- # @return [Integer, nil]
120
- attribute? :page, Types::Int.default(1)
121
-
122
- # @!attribute [r] limit
123
- # @return [Integer, nil]
124
- attribute? :limit, Types::Int.default(10)
125
-
126
- def without_pagination
127
- SearchFilter.new(
128
- description: description,
129
- from_at: from_at,
130
- tag: tag,
131
- title: title,
132
- to_at: to_at
133
- )
134
- end
135
- end
6
+ class Search < Dry::Struct
7
+ # @!attribute [r] q
8
+ # @return [String]
9
+ attribute :q, Types::String
10
+
11
+ # @!attribute [r] page
12
+ # @return [Integer, nil]
13
+ attribute? :page, Types::Int.default(1)
14
+
15
+ # @!attribute [r] limit
16
+ # @return [Integer, nil]
17
+ attribute? :limit, Types::Int.default(10)
136
18
  end
137
19
  end
138
20
  end
@@ -5,7 +5,7 @@ module Mihari
5
5
  module GooglePublicDNS
6
6
  INT_TYPE_TO_TYPE =
7
7
  { 1 => :A, 38 => :A6, 28 => :AAAA, 18 => :AFSDB, 255 => :ANY, 42 => :APL, 34 => :ATMA, 252 => :AXFR, 37 => :CERT,
8
- 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }
8
+ 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }.freeze
9
9
 
10
10
  class Answer < Dry::Struct
11
11
  # @!attribute [r] name
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module GreyNoise
6
6
  class Metadata < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] country
10
10
  # @return [String]
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Structs
5
+ module MMDB
6
+ class Country < Dry::Struct
7
+ # @!attribute [r] iso_code
8
+ # @return [String]
9
+ attribute :iso_code, Types::String
10
+
11
+ # @!attribute [r] autonomous_system_number
12
+ # @return [String]
13
+ attribute :autonomous_system_number, Types::String.optional
14
+
15
+ class << self
16
+ def from_dynamic!(d)
17
+ d = Types::Hash[d]
18
+ new(
19
+ iso_code: d.fetch("iso_code"),
20
+ autonomous_system_number: d["AutonomousSystemNumber"]
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ class CountryInfo < Dry::Struct
27
+ # @!attribute [r] latitude
28
+ # @return [String]
29
+ attribute :latitude, Types::String
30
+
31
+ # @!attribute [r] longitude
32
+ # @return [String]
33
+ attribute :longitude, Types::String
34
+
35
+ def self.from_dynamic!(d)
36
+ d = Types::Hash[d]
37
+ new(
38
+ latitude: d.fetch("Latitude (average)"),
39
+ longitude: d.fetch("Longitude (average)")
40
+ )
41
+ end
42
+
43
+ class << self
44
+ def from_json!(json)
45
+ from_dynamic!(JSON.parse(json))
46
+ end
47
+ end
48
+ end
49
+
50
+ class Item < Dry::Struct
51
+ # @!attribute [r] country
52
+ # @return [Country]
53
+ attribute :country, Country
54
+
55
+ # @!attribute [r] country_info
56
+ # @return [CountryInfo]
57
+ attribute :country_info, CountryInfo
58
+
59
+ class << self
60
+ def from_dynamic!(d)
61
+ d = Types::Hash[d]
62
+ new(
63
+ country: Country.from_dynamic!(d.fetch("country")),
64
+ country_info: CountryInfo.from_dynamic!(d.fetch("country_info"))
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ class Response < Dry::Struct
71
+ # @!attribute [r] items
72
+ # @return [Array<Item>]
73
+ attribute :items, Types.Array(Item)
74
+
75
+ #
76
+ # @return [Item, nil]
77
+ #
78
+ def item
79
+ items.find { |item| item.country.autonomous_system_number }
80
+ end
81
+
82
+ #
83
+ # @return [Integer, nil]
84
+ #
85
+ def asn
86
+ item&.country&.autonomous_system_number&.to_i
87
+ end
88
+
89
+ #
90
+ # @return [String, nil]
91
+ #
92
+ def country_code
93
+ item&.country&.iso_code
94
+ end
95
+
96
+ #
97
+ # @return [String, nil]
98
+ #
99
+ def loc
100
+ return nil if item.nil?
101
+
102
+ [item.country_info.latitude, item.country_info.longitude].join(",")
103
+ end
104
+
105
+ class << self
106
+ def from_dynamic!(d)
107
+ new(
108
+ items: d.map { |x| Item.from_dynamic!(x) }
109
+ )
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module Onyphe
6
6
  class Result < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] asn
10
10
  # @return [String]
@@ -16,7 +16,7 @@ module Mihari
16
16
  # @return [Mihari::Geolocation, nil]
17
17
  #
18
18
  def geolocation
19
- return nil if country_name.nil? && country_code.nil?
19
+ return nil if country_name.nil? || country_code.nil?
20
20
 
21
21
  Mihari::Models::Geolocation.new(
22
22
  country: country_name,
@@ -41,7 +41,7 @@ module Mihari
41
41
  end
42
42
 
43
43
  class Match < Dry::Struct
44
- include Mixins::AutonomousSystem
44
+ include Concerns::AutonomousSystemNormalizable
45
45
 
46
46
  # @!attribute [r] asn
47
47
  # @return [String, nil]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "6.3.0"
4
+ VERSION = "7.0.0"
5
5
  end
@@ -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