From 9e92f2a9e7b9ea5a4c6e9a70ce4e806f7b7902be Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sun, 14 Jan 2024 20:53:35 +0900 Subject: [PATCH] refactor: set convention for .configuration_keys --- lib/mihari/actor.rb | 9 +++++++++ lib/mihari/analyzers/binaryedge.rb | 6 ------ lib/mihari/analyzers/censys.rb | 9 --------- lib/mihari/analyzers/circl.rb | 6 ------ lib/mihari/analyzers/fofa.rb | 6 ------ lib/mihari/analyzers/greynoise.rb | 6 ------ lib/mihari/analyzers/hunterhow.rb | 6 ------ lib/mihari/analyzers/onyphe.rb | 6 ------ lib/mihari/analyzers/otx.rb | 6 ------ lib/mihari/analyzers/passivetotal.rb | 4 ---- lib/mihari/analyzers/pulsedive.rb | 6 ------ lib/mihari/analyzers/securitytrails.rb | 4 ---- lib/mihari/analyzers/shodan.rb | 6 ------ lib/mihari/analyzers/urlscan.rb | 6 ------ lib/mihari/analyzers/virustotal.rb | 4 ---- lib/mihari/analyzers/virustotal_intelligence.rb | 13 +++++++------ lib/mihari/analyzers/zoomeye.rb | 6 ------ lib/mihari/config.rb | 2 +- lib/mihari/emitters/database.rb | 6 ------ lib/mihari/emitters/misp.rb | 6 ------ lib/mihari/emitters/slack.rb | 6 ------ lib/mihari/emitters/the_hive.rb | 6 ------ spec/cli/config_spec.rb | 2 +- spec/spec_helper.rb | 15 ++++++++------- 24 files changed, 26 insertions(+), 126 deletions(-) diff --git a/lib/mihari/actor.rb b/lib/mihari/actor.rb index 85c68fefd..833bafb90 100644 --- a/lib/mihari/actor.rb +++ b/lib/mihari/actor.rb @@ -93,6 +93,15 @@ def keys ([key] + [key_aliases]).flatten.compact.map(&:downcase) end + def configuration_keys + # Automatically generate configuration keys based on key + # For example, + # - Shodan analyzer's key is "shodan" + # - Mihari.config has "shodan_api_key" + # - Select "shodan_api_key" by using "#{key}_" prefix + Mihari.config.keys.select { |config_key| config_key.start_with?("#{key}_") } + end + def type return "analyzer" if ancestors.include?(Mihari::Analyzers::Base) return "emitter" if ancestors.include?(Mihari::Emitters::Base) diff --git a/lib/mihari/analyzers/binaryedge.rb b/lib/mihari/analyzers/binaryedge.rb index 24c309961..c6bb4be59 100644 --- a/lib/mihari/analyzers/binaryedge.rb +++ b/lib/mihari/analyzers/binaryedge.rb @@ -24,12 +24,6 @@ def artifacts client.search_with_pagination(query, pagination_limit: pagination_limit).map(&:artifacts).flatten end - class << self - def configuration_keys - %w[binaryedge_api_key] - end - end - private # diff --git a/lib/mihari/analyzers/censys.rb b/lib/mihari/analyzers/censys.rb index bac4ac78b..5b7296a45 100644 --- a/lib/mihari/analyzers/censys.rb +++ b/lib/mihari/analyzers/censys.rb @@ -41,15 +41,6 @@ def configured? configuration_keys? || (id? && secret?) end - class << self - # - # @return [Array] - # - def configuration_keys - %w[censys_id censys_secret] - end - end - private # diff --git a/lib/mihari/analyzers/circl.rb b/lib/mihari/analyzers/circl.rb index 6a80e37ab..613708473 100644 --- a/lib/mihari/analyzers/circl.rb +++ b/lib/mihari/analyzers/circl.rb @@ -47,12 +47,6 @@ def configured? configuration_keys? || (username? && password?) end - class << self - def configuration_keys - %w[circl_passive_password circl_passive_username] - end - end - private def client diff --git a/lib/mihari/analyzers/fofa.rb b/lib/mihari/analyzers/fofa.rb index 6b5f7f7d2..1b59898e0 100644 --- a/lib/mihari/analyzers/fofa.rb +++ b/lib/mihari/analyzers/fofa.rb @@ -35,12 +35,6 @@ def configured? api_key? && email? end - class << self - def configuration_keys - %w[fofa_api_key fofa_email] - end - end - private def email? diff --git a/lib/mihari/analyzers/greynoise.rb b/lib/mihari/analyzers/greynoise.rb index 460dabb90..ad72ae385 100644 --- a/lib/mihari/analyzers/greynoise.rb +++ b/lib/mihari/analyzers/greynoise.rb @@ -27,12 +27,6 @@ def artifacts ).map(&:artifacts).flatten end - class << self - def configuration_keys - %w[greynoise_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/hunterhow.rb b/lib/mihari/analyzers/hunterhow.rb index 9bbff5199..870c6c95b 100644 --- a/lib/mihari/analyzers/hunterhow.rb +++ b/lib/mihari/analyzers/hunterhow.rb @@ -44,12 +44,6 @@ def artifacts end.flatten end - class << self - def configuration_keys - %w[hunterhow_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/onyphe.rb b/lib/mihari/analyzers/onyphe.rb index b6ecddbeb..293971814 100644 --- a/lib/mihari/analyzers/onyphe.rb +++ b/lib/mihari/analyzers/onyphe.rb @@ -29,12 +29,6 @@ def artifacts ).map(&:artifacts).flatten end - class << self - def configuration_keys - %w[onyphe_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/otx.rb b/lib/mihari/analyzers/otx.rb index 67c84ac1c..c38d2ee87 100644 --- a/lib/mihari/analyzers/otx.rb +++ b/lib/mihari/analyzers/otx.rb @@ -38,12 +38,6 @@ def artifacts end end - class << self - def configuration_keys - %w[otx_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/passivetotal.rb b/lib/mihari/analyzers/passivetotal.rb index bc848b6ea..6ab806859 100644 --- a/lib/mihari/analyzers/passivetotal.rb +++ b/lib/mihari/analyzers/passivetotal.rb @@ -50,10 +50,6 @@ def configured? end class << self - def configuration_keys - %w[passivetotal_username passivetotal_api_key] - end - # # @return [Array, nil] # diff --git a/lib/mihari/analyzers/pulsedive.rb b/lib/mihari/analyzers/pulsedive.rb index 9d54940c8..543217009 100644 --- a/lib/mihari/analyzers/pulsedive.rb +++ b/lib/mihari/analyzers/pulsedive.rb @@ -43,12 +43,6 @@ def artifacts end end - class << self - def configuration_keys - %w[pulsedive_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/securitytrails.rb b/lib/mihari/analyzers/securitytrails.rb index c32cd72e4..ede843c8d 100644 --- a/lib/mihari/analyzers/securitytrails.rb +++ b/lib/mihari/analyzers/securitytrails.rb @@ -44,10 +44,6 @@ def artifacts end class << self - def configuration_keys - %w[securitytrails_api_key] - end - # # @return [Array, nil] # diff --git a/lib/mihari/analyzers/shodan.rb b/lib/mihari/analyzers/shodan.rb index 4d478f840..b1870dad5 100644 --- a/lib/mihari/analyzers/shodan.rb +++ b/lib/mihari/analyzers/shodan.rb @@ -27,12 +27,6 @@ def artifacts ).map(&:artifacts).flatten.uniq(&:data) end - class << self - def configuration_keys - %w[shodan_api_key] - end - end - private # diff --git a/lib/mihari/analyzers/urlscan.rb b/lib/mihari/analyzers/urlscan.rb index aabc11b3b..27d06f00c 100644 --- a/lib/mihari/analyzers/urlscan.rb +++ b/lib/mihari/analyzers/urlscan.rb @@ -37,12 +37,6 @@ def artifacts artifacts.select { |artifact| allowed_data_types.include? artifact.data_type } end - class << self - def configuration_keys - %w[urlscan_api_key] - end - end - private def client diff --git a/lib/mihari/analyzers/virustotal.rb b/lib/mihari/analyzers/virustotal.rb index 1c95df768..c020f4c2f 100644 --- a/lib/mihari/analyzers/virustotal.rb +++ b/lib/mihari/analyzers/virustotal.rb @@ -39,10 +39,6 @@ def artifacts end class << self - def configuration_keys - %w[virustotal_api_key] - end - # # @return [Array, nil] # diff --git a/lib/mihari/analyzers/virustotal_intelligence.rb b/lib/mihari/analyzers/virustotal_intelligence.rb index 65fc6890b..e47b65353 100644 --- a/lib/mihari/analyzers/virustotal_intelligence.rb +++ b/lib/mihari/analyzers/virustotal_intelligence.rb @@ -24,12 +24,6 @@ def artifacts client.intel_search_with_pagination(query, pagination_limit: pagination_limit).map(&:artifacts).flatten end - class << self - def configuration_keys - %w[virustotal_api_key] - end - end - class << self # # @return [String] @@ -44,6 +38,13 @@ def key def key_aliases ["vt_intel"] end + + # + # @return [Array] + # + def configuration_keys + %w[virustotal_api_key] + end end private diff --git a/lib/mihari/analyzers/zoomeye.rb b/lib/mihari/analyzers/zoomeye.rb index ddcbb3716..545d5a261 100644 --- a/lib/mihari/analyzers/zoomeye.rb +++ b/lib/mihari/analyzers/zoomeye.rb @@ -40,12 +40,6 @@ def artifacts end end - class << self - def configuration_keys - %w[zoomeye_api_key] - end - end - private # diff --git a/lib/mihari/config.rb b/lib/mihari/config.rb index c3c2e6f64..1aa879c5c 100644 --- a/lib/mihari/config.rb +++ b/lib/mihari/config.rb @@ -170,7 +170,7 @@ def sidekiq_redis_url=(val) # @return [Array] # def keys - to_h.keys.map(&:to_s).map(&:upcase) + @keys ||= to_h.keys.map(&:to_s).map(&:downcase) end end end diff --git a/lib/mihari/emitters/database.rb b/lib/mihari/emitters/database.rb index 763fbc702..7404af324 100644 --- a/lib/mihari/emitters/database.rb +++ b/lib/mihari/emitters/database.rb @@ -24,12 +24,6 @@ def call(artifacts) def target Mihari.config.database_url.host || Mihari.config.database_url.to_s end - - class << self - def configuration_keys - %w[database_url] - end - end end end end diff --git a/lib/mihari/emitters/misp.rb b/lib/mihari/emitters/misp.rb index b46e83d4e..6967c040c 100644 --- a/lib/mihari/emitters/misp.rb +++ b/lib/mihari/emitters/misp.rb @@ -63,12 +63,6 @@ def target URI(url).host || "N/A" end - class << self - def configuration_keys - %w[misp_url misp_api_key] - end - end - private def client diff --git a/lib/mihari/emitters/slack.rb b/lib/mihari/emitters/slack.rb index 4cec8d741..0b46d5c59 100644 --- a/lib/mihari/emitters/slack.rb +++ b/lib/mihari/emitters/slack.rb @@ -217,12 +217,6 @@ def call(artifacts) notifier.post(text: text, attachments: attachments, mrkdwn: true) end - - class << self - def configuration_keys - %w[slack_webhook_url slack_channel] - end - end end end end diff --git a/lib/mihari/emitters/the_hive.rb b/lib/mihari/emitters/the_hive.rb index 493a45e9f..82f506ba0 100644 --- a/lib/mihari/emitters/the_hive.rb +++ b/lib/mihari/emitters/the_hive.rb @@ -53,12 +53,6 @@ def call(artifacts) client.alert payload end - class << self - def configuration_keys - %w[thehive_url thehive_api_key] - end - end - private def client diff --git a/spec/cli/config_spec.rb b/spec/cli/config_spec.rb index d7bf5f9f6..f27683d2b 100644 --- a/spec/cli/config_spec.rb +++ b/spec/cli/config_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Mihari::CLI::Config do - let!(:key) { Mihari.config.keys.first } + let!(:key) { Mihari.config.keys.first.upcase } describe "#list" do it do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0e2b9ac59..95fe0b02a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -84,13 +84,14 @@ def authorization_field(username, password) config.hook_into :webmock config.ignore_localhost = true - api_keys = Mihari.config.keys.select { |key| key.end_with?("_API_KEY") } - passwords = Mihari.config.keys.select { |key| key.end_with?("_PASSWORD") } - secrets = Mihari.config.keys.select { |key| key.end_with?("_SECRET") } - usernames = Mihari.config.keys.select { |key| key.end_with?("_USERNAME") } - emails = Mihari.config.keys.select { |key| key.end_with?("_EMAIL") } - ids = Mihari.config.keys.select { |key| key.end_with?("_ID") } - api_urls = Mihari.config.keys.select do |key| + keys = Mihari.config.keys.map(&:upcase) + api_keys = keys.select { |key| key.end_with?("_API_KEY") } + passwords = keys.select { |key| key.end_with?("_PASSWORD") } + secrets = keys.select { |key| key.end_with?("_SECRET") } + usernames = keys.select { |key| key.end_with?("_USERNAME") } + emails = keys.select { |key| key.end_with?("_EMAIL") } + ids = keys.select { |key| key.end_with?("_ID") } + api_urls = keys.select do |key| key != "DATABASE_URL" && key != "SIDEKIQ_REDIS_URL" && key.end_with?("_URL") end