Skip to content

Commit

Permalink
Merge pull request #1007 from ninoseki/renew-configuration-error
Browse files Browse the repository at this point in the history
refactor: renew ConfigurationError
  • Loading branch information
ninoseki authored Jan 12, 2024
2 parents afd423f + 671634d commit aa38556
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
16 changes: 12 additions & 4 deletions lib/mihari/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def timeout
def validate_configuration!
return if configured?

joined = self.class.configuration_keys.map(&:upcase).join(", ")
be = (self.class.configuration_keys.length > 1) ? "are" : "is"
message = "#{self.class.key} is not configured correctly. #{joined} #{be} missing."
raise ConfigurationError, message
message = "#{self.class.type.capitalize}:#{self.class.key} is not configured correctly"
detail = self.class.configuration_keys.map { |key| "#{key.upcase} is missing" }

raise ConfigurationError.new(message, detail)
end

def call(*args, **kwargs)
Expand Down Expand Up @@ -92,6 +92,14 @@ def key_aliases
def keys
([key] + [key_aliases]).flatten.compact.map(&:downcase)
end

def type
return "analyzer" if ancestors.include?(Mihari::Analyzers::Base)
return "emitter" if ancestors.include?(Mihari::Emitters::Base)
return "enricher" if ancestors.include?(Mihari::Enrichers::Base)

nil
end
end
end
end
15 changes: 14 additions & 1 deletion lib/mihari/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ class Error < StandardError; end

class ValueError < Error; end

class ConfigurationError < Error; end
class ConfigurationError < Error
# @return [Array<String>, nil]
attr_reader :detail

#
# @param [String] msg
# @param [Array<String>, nil] detail
#
def initialize(msg, detail)
super(msg)

@detail = detail
end
end

class ResponseError < Error; end

Expand Down
23 changes: 3 additions & 20 deletions lib/mihari/structs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ class Config < Dry::Struct
attribute :items, Types.Array(Types::Hash).optional

class << self
#
# Get a type of a class
#
# @param [Class<Mihari::Analyzers::Base>, Class<Mihari::Emitters::Base>, Class<Mihari::Enrichers::Base>] klass
#
# @return [String, nil]
#
def get_type(klass)
return "analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
return "emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
return "enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)

nil
end

#
# Get a dummy instance
#
Expand All @@ -43,8 +28,7 @@ def get_type(klass)
# @return [Mihari::Analyzers::Base, Mihari::Emitter::Base, Mihari::Enricher::Base] dummy
#
def get_dummy(klass)
type = get_type(klass)
case type
case klass.type
when "analyzer"
klass.new("dummy")
when "emitter"
Expand All @@ -62,16 +46,15 @@ def get_dummy(klass)
def from_class(klass)
return nil if klass == Mihari::Rule

type = get_type(klass)
return nil if type.nil?
return nil if klass.type.nil?

begin
instance = get_dummy(klass)
new(
name: klass.key,
items: klass.configuration_items,
configured: instance.configured?,
type: type
type: klass.type
)
rescue ArgumentError
nil
Expand Down
4 changes: 3 additions & 1 deletion spec/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@
end

it do
expect { rule.artifacts }.to raise_error(Mihari::ConfigurationError, /shodan is not configured correctly/)
expect do
rule.artifacts
end.to raise_error(Mihari::ConfigurationError, /Analyzer:shodan is not configured correctly/)
end
end

Expand Down

0 comments on commit aa38556

Please sign in to comment.