module Mihari::CLI::Mixins::Utils

Public Instance Methods

load_configuration() click to toggle source

Load configuration and establish DB connection

@return [Hash]

# File lib/mihari/cli/mixins/utils.rb, line 35
def load_configuration
  config = options["config"]
  return unless config

  Mihari.load_config_from_yaml config
  Database.connect
end
normalize_options(options) click to toggle source

Normalize options (reject keys not for analyzers)

@param [Hash] options

@return [Hash]

# File lib/mihari/cli/mixins/utils.rb, line 78
def normalize_options(options)
  # Delete :config because it is not intended to use for running an analyzer
  [:config, :ignore_old_artifacts, :ignore_threshold].each do |ignore_key|
    options.delete(ignore_key)
  end
  options
end
required_alert_keys?(json) click to toggle source

Check required keys in JSON

@param [Hash] json

@return [Boolean]

# File lib/mihari/cli/mixins/utils.rb, line 26
def required_alert_keys?(json)
  %w[title description artifacts].all? { |key| json.key? key }
end
run_analyzer(analyzer_class, query:, options:) click to toggle source

Run analyzer

@param [Class<Mihari::Analyzers::Base>] analyzer_class @param [String] query @param [Hash] options

@return [nil]

# File lib/mihari/cli/mixins/utils.rb, line 52
def run_analyzer(analyzer_class, query:, options:)
  load_configuration

  # options = Thor::CoreExt::HashWithIndifferentAccess
  # ref. https://www.rubydoc.info/github/wycats/thor/Thor/CoreExt/HashWithIndifferentAccess
  # so need to covert it to a plain hash
  hash_options = options.to_hash

  hash_options = symbolize_hash(hash_options)
  hash_options = normalize_options(hash_options)

  analyzer = analyzer_class.new(query, **hash_options)

  analyzer.ignore_old_artifacts = options[:ignore_old_artifacts] || false
  analyzer.ignore_threshold = options[:ignore_threshold] || 0

  analyzer.run
end
with_error_handling() { || ... } click to toggle source

Send an exception notification if there is any error in a block

@return [Nil]

# File lib/mihari/cli/mixins/utils.rb, line 12
def with_error_handling
  yield
rescue StandardError => e
  notifier = Notifiers::ExceptionNotifier.new
  notifier.notify e
end