Skip to content

Commit

Permalink
refactor: remove class_ prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Jan 8, 2024
1 parent be3a315 commit e939ac0
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions lib/mihari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def emitters
#
def emitter_to_class
@emitter_to_class ||= emitters.flat_map do |klass|
klass.class_keys.map { |key| [key, klass] }
klass.keys.map { |key| [key, klass] }
end.to_h
end

Expand All @@ -96,7 +96,7 @@ def analyzers
#
def analyzer_to_class
@analyzer_to_class ||= analyzers.flat_map do |klass|
klass.class_keys.map { |key| [key, klass] }
klass.keys.map { |key| [key, klass] }
end.to_h
end

Expand All @@ -113,7 +113,7 @@ def enrichers
#
def enricher_to_class
@enricher_to_class ||= enrichers.flat_map do |klass|
klass.class_keys.map { |key| [key, klass] }
klass.keys.map { |key| [key, klass] }
end.to_h
end

Expand Down
10 changes: 5 additions & 5 deletions lib/mihari/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def validate_configuration!

joined = self.class.configuration_keys.join(", ")
be = (self.class.configuration_keys.length > 1) ? "are" : "is"
message = "#{self.class.class_key} is not configured correctly. #{joined} #{be} missing."
message = "#{self.class.key} is not configured correctly. #{joined} #{be} missing."
raise ConfigurationError, message
end

Expand All @@ -75,22 +75,22 @@ class << self
#
# @return [String]
#
def class_key
def key
to_s.split("::").last.downcase
end

#
# @return [Array<String>, nil]
#
def class_key_aliases
def key_aliases
nil
end

#
# @return [Array<String>]
#
def class_keys
([class_key] + [class_key_aliases]).flatten.compact.map(&:downcase)
def keys
([key] + [key_aliases]).flatten.compact.map(&:downcase)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mihari/analyzers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def normalized_artifacts
# It is set automatically in #initialize
artifact = artifact.is_a?(Models::Artifact) ? artifact : Models::Artifact.new(data: artifact)

artifact.source = self.class.class_key
artifact.source = self.class.key
artifact.query = query

artifact
Expand Down Expand Up @@ -93,11 +93,11 @@ def result(...)
return result if result.success?

# Wrap failure with AnalyzerError to explicitly name a failed analyzer
error = AnalyzerError.new(result.failure.message, self.class.class_key, cause: result.failure)
error = AnalyzerError.new(result.failure.message, self.class.key, cause: result.failure)
return Failure(error) unless ignore_error?

# Return Success if ignore_error? is true with logging
Mihari.logger.warn("Analyzer:#{self.class.class_key} failed - #{result.failure}")
Mihari.logger.warn("Analyzer:#{self.class.key} failed - #{result.failure}")
Success([])
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/analyzers/passivetotal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def configuration_keys
#
# @return [Array<String>, nil]
#
def class_key_aliases
def key_aliases
["pt"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/analyzers/securitytrails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def configuration_keys
#
# @return [Array<String>, nil]
#
def class_key_aliases
def key_aliases
["st"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/analyzers/virustotal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def configuration_keys
#
# @return [Array<String>, nil]
#
def class_key_aliases
def key_aliases
["vt"]
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mihari/analyzers/virustotal_intelligence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class << self
#
# @return [String]
#
def class_key
def key
"virustotal_intelligence"
end

#
# @return [Array<String>, nil]
#
def class_key_aliases
def key_aliases
["vt_intel"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mihari
DEFAULT_DATA_TYPES = Types::DataTypes.values.freeze

# @return [Array<Hash>]
DEFAULT_EMITTERS = Emitters::Database.class_keys.map { |name| { emitter: name.downcase } }.freeze
DEFAULT_EMITTERS = Emitters::Database.keys.map { |name| { emitter: name.downcase } }.freeze

# @return [Array<Hash>]
DEFAULT_ENRICHERS = Mihari.enricher_to_class.keys.map { |name| { enricher: name.downcase } }.freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/emitters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def result(artifacts)
) { call(artifacts) }
end.to_result

Mihari.logger.warn("Emitter:#{self.class.class_key} failed - #{result.failure}") if result.failure?
Mihari.logger.warn("Emitter:#{self.class.key} failed - #{result.failure}") if result.failure?

result
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/enrichers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def result(value)
) { call value }
end.to_result

Mihari.logger.warn("Enricher:#{self.class.class_key} failed: #{result.failure}") if result.failure?
Mihari.logger.warn("Enricher:#{self.class.key} failed: #{result.failure}") if result.failure?

result
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/enrichers/google_public_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class << self
#
# @return [String]
#
def class_key
def key
"google_public_dns"
end
end
Expand Down
38 changes: 19 additions & 19 deletions lib/mihari/schemas/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module Analyzers

# Analyzer with API key and pagination
[
Mihari::Analyzers::BinaryEdge.class_keys,
Mihari::Analyzers::GreyNoise.class_keys,
Mihari::Analyzers::Onyphe.class_keys,
Mihari::Analyzers::Shodan.class_keys,
Mihari::Analyzers::Urlscan.class_keys,
Mihari::Analyzers::VirusTotalIntelligence.class_keys
Mihari::Analyzers::BinaryEdge.keys,
Mihari::Analyzers::GreyNoise.keys,
Mihari::Analyzers::Onyphe.keys,
Mihari::Analyzers::Shodan.keys,
Mihari::Analyzers::Urlscan.keys,
Mihari::Analyzers::VirusTotalIntelligence.keys
].each do |keys|
key = keys.first
const_set(key.upcase, Dry::Schema.Params do
Expand All @@ -28,10 +28,10 @@ module Analyzers

# Analyzer with API key
[
Mihari::Analyzers::OTX.class_keys,
Mihari::Analyzers::Pulsedive.class_keys,
Mihari::Analyzers::VirusTotal.class_keys,
Mihari::Analyzers::SecurityTrails.class_keys
Mihari::Analyzers::OTX.keys,
Mihari::Analyzers::Pulsedive.keys,
Mihari::Analyzers::VirusTotal.keys,
Mihari::Analyzers::SecurityTrails.keys
].each do |keys|
key = keys.first
const_set(key.upcase, Dry::Schema.Params do
Expand All @@ -43,60 +43,60 @@ module Analyzers
end

DNSTwister = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::DNSTwister.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::DNSTwister.keys))
required(:query).value(:string)
optional(:options).hash(AnalyzerOptions)
end

Censys = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Censys.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Censys.keys))
required(:query).value(:string)
optional(:id).value(:string)
optional(:secret).value(:string)
optional(:options).hash(AnalyzerPaginationOptions)
end

CIRCL = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::CIRCL.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::CIRCL.keys))
required(:query).value(:string)
optional(:username).value(:string)
optional(:password).value(:string)
optional(:options).hash(AnalyzerOptions)
end

Fofa = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Fofa.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Fofa.keys))
required(:query).value(:string)
optional(:api_key).value(:string)
optional(:email).value(:string)
optional(:options).hash(AnalyzerPaginationOptions)
end

PassiveTotal = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::PassiveTotal.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::PassiveTotal.keys))
required(:query).value(:string)
optional(:username).value(:string)
optional(:api_key).value(:string)
optional(:options).hash(AnalyzerOptions)
end

ZoomEye = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::ZoomEye.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::ZoomEye.keys))
required(:query).value(:string)
required(:type).value(Types::String.enum("host", "web"))
optional(:options).hash(AnalyzerPaginationOptions)
end

Crtsh = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Crtsh.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Crtsh.keys))
required(:query).value(:string)
optional(:exclude_expired).value(:bool).default(true)
optional(:match).value(Types::String.enum("=", "ILIKE", "LIKE", "single", "any", "FTS")).default(nil)
optional(:options).hash(AnalyzerOptions)
end

HunterHow = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::HunterHow.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::HunterHow.keys))
required(:query).value(:string)
required(:start_time).value(:date)
required(:end_time).value(:date)
Expand All @@ -105,7 +105,7 @@ module Analyzers
end

Feed = Dry::Schema.Params do
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Feed.class_keys))
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Feed.keys))
required(:query).value(:string)
required(:selector).value(:string)
optional(:method).value(Types::HTTPRequestMethods).default("GET")
Expand Down
10 changes: 5 additions & 5 deletions lib/mihari/schemas/emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ module Emitters
extend Concerns::Orrable

Database = Dry::Schema.Params do
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Database.class_keys))
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Database.keys))
optional(:options).hash(Options)
end

MISP = Dry::Schema.Params do
required(:emitter).value(Types::String.enum(*Mihari::Emitters::MISP.class_keys))
required(:emitter).value(Types::String.enum(*Mihari::Emitters::MISP.keys))
optional(:url).value(:string)
optional(:api_key).value(:string)
optional(:options).hash(Options)
end

TheHive = Dry::Schema.Params do
required(:emitter).value(Types::String.enum(*Mihari::Emitters::TheHive.class_keys))
required(:emitter).value(Types::String.enum(*Mihari::Emitters::TheHive.keys))
optional(:url).value(:string)
optional(:api_key).value(:string)
optional(:options).hash(Options)
end

Slack = Dry::Schema.Params do
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Slack.class_keys))
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Slack.keys))
optional(:webhook_url).value(:string)
optional(:channel).value(:string)
optional(:options).hash(Options)
end

Webhook = Dry::Schema.Params do
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Webhook.class_keys))
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Webhook.keys))
required(:url).value(:string)
optional(:method).value(Types::HTTPRequestMethods).default("POST")
optional(:headers).value(:hash).default({})
Expand Down
8 changes: 4 additions & 4 deletions lib/mihari/schemas/enricher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ module Enrichers
extend Concerns::Orrable

MMDB = Dry::Schema.Params do
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::MMDB.class_keys))
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::MMDB.keys))
optional(:options).hash(Options)
end

Whois = Dry::Schema.Params do
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::Whois.class_keys))
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::Whois.keys))
optional(:options).hash(Options)
end

Shodan = Dry::Schema.Params do
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::Shodan.class_keys))
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::Shodan.keys))
optional(:options).hash(Options)
end

GooglePublicDNS = Dry::Schema.Params do
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::GooglePublicDNS.class_keys))
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::GooglePublicDNS.keys))
optional(:options).hash(Options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/structs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def from_class(klass)
begin
instance = get_dummy(klass)
new(
name: klass.class_key,
name: klass.key,
items: klass.configuration_items,
configured: instance.configured?,
type: type
Expand Down
2 changes: 1 addition & 1 deletion spec/analyzers/analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def artifacts
end

class << self
def class_keys
def keys
# NOTE: returns an empty array to prevent adding this class in Mihari#analyzer_to_class
[]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/schemas/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
title: title,
queries: [{ analyzer: analyzer, query: "foo" }]
)
expect(result.errors.empty?).to be true
expect(result.errors.empty?).to eq true
end
end
end
Expand Down

0 comments on commit e939ac0

Please sign in to comment.