Skip to content

Commit

Permalink
refactor: set convention for .configuration_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Jan 14, 2024
1 parent 7e7e98c commit 9e92f2a
Show file tree
Hide file tree
Showing 24 changed files with 26 additions and 126 deletions.
9 changes: 9 additions & 0 deletions lib/mihari/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/binaryedge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
9 changes: 0 additions & 9 deletions lib/mihari/analyzers/censys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ def configured?
configuration_keys? || (id? && secret?)
end

class << self
#
# @return [Array<String>]
#
def configuration_keys
%w[censys_id censys_secret]
end
end

private

#
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/circl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/fofa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/greynoise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ def artifacts
).map(&:artifacts).flatten
end

class << self
def configuration_keys
%w[greynoise_api_key]
end
end

private

def client
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/hunterhow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ def artifacts
end.flatten
end

class << self
def configuration_keys
%w[hunterhow_api_key]
end
end

private

def client
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/onyphe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ def artifacts
).map(&:artifacts).flatten
end

class << self
def configuration_keys
%w[onyphe_api_key]
end
end

private

def client
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/otx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ def artifacts
end
end

class << self
def configuration_keys
%w[otx_api_key]
end
end

private

def client
Expand Down
4 changes: 0 additions & 4 deletions lib/mihari/analyzers/passivetotal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ def configured?
end

class << self
def configuration_keys
%w[passivetotal_username passivetotal_api_key]
end

#
# @return [Array<String>, nil]
#
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/pulsedive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def artifacts
end
end

class << self
def configuration_keys
%w[pulsedive_api_key]
end
end

private

def client
Expand Down
4 changes: 0 additions & 4 deletions lib/mihari/analyzers/securitytrails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ def artifacts
end

class << self
def configuration_keys
%w[securitytrails_api_key]
end

#
# @return [Array<String>, nil]
#
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/shodan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ def artifacts
).map(&:artifacts).flatten.uniq(&:data)
end

class << self
def configuration_keys
%w[shodan_api_key]
end
end

private

#
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/urlscan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions lib/mihari/analyzers/virustotal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def artifacts
end

class << self
def configuration_keys
%w[virustotal_api_key]
end

#
# @return [Array<String>, nil]
#
Expand Down
13 changes: 7 additions & 6 deletions lib/mihari/analyzers/virustotal_intelligence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -44,6 +38,13 @@ def key
def key_aliases
["vt_intel"]
end

#
# @return [Array<String>]
#
def configuration_keys
%w[virustotal_api_key]
end
end

private
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/analyzers/zoomeye.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ def artifacts
end
end

class << self
def configuration_keys
%w[zoomeye_api_key]
end
end

private

#
Expand Down
2 changes: 1 addition & 1 deletion lib/mihari/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def sidekiq_redis_url=(val)
# @return [Array<String>]
#
def keys
to_h.keys.map(&:to_s).map(&:upcase)
@keys ||= to_h.keys.map(&:to_s).map(&:downcase)
end
end
end
6 changes: 0 additions & 6 deletions lib/mihari/emitters/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions lib/mihari/emitters/misp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/mihari/emitters/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions lib/mihari/emitters/the_hive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/cli/config_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 8 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9e92f2a

Please sign in to comment.