From ae032e429c0c6a0f04fa2e1d5a388359d4b4bfd6 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sun, 14 Jan 2024 19:04:52 +0900 Subject: [PATCH 1/3] refactor: use lambda instead of [].tap --- lib/mihari/emitters/slack.rb | 20 +++++--------------- lib/mihari/enrichers/whois.rb | 12 +++++------- lib/mihari/services/proxies.rb | 6 +++--- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/lib/mihari/emitters/slack.rb b/lib/mihari/emitters/slack.rb index 823a62f04..4cec8d741 100644 --- a/lib/mihari/emitters/slack.rb +++ b/lib/mihari/emitters/slack.rb @@ -176,21 +176,11 @@ def target # @return [::Slack::Notifier] # def notifier - @notifier ||= [].tap do |out| - out << if timeout.nil? - ::Slack::Notifier.new( - webhook_url, - channel: channel, username: username - ) - else - ::Slack::Notifier.new( - webhook_url, - channel: channel, - username: username, - http_options: { timeout: timeout } - ) - end - end.first + @notifier ||= lambda do + return ::Slack::Notifier.new(webhook_url, channel: channel, username: username) if timeout.nil? + + ::Slack::Notifier.new(webhook_url, channel: channel, username: username, http_options: { timeout: timeout }) + end.call end # diff --git a/lib/mihari/enrichers/whois.rb b/lib/mihari/enrichers/whois.rb index 2b90be35f..1f9350217 100644 --- a/lib/mihari/enrichers/whois.rb +++ b/lib/mihari/enrichers/whois.rb @@ -53,13 +53,11 @@ def memoized_call(domain) # @return [::Whois::Client] # def whois - @whois ||= [].tap do |out| - out << if timeout.nil? - ::Whois::Client.new - else - ::Whois::Client.new(timeout: timeout) - end - end.last + @whois ||= lambda do + return ::Whois::Client.new if timeout.nil? + + ::Whois::Client.new(timeout: timeout) + end.call end # diff --git a/lib/mihari/services/proxies.rb b/lib/mihari/services/proxies.rb index 6856cb8db..e61c405b0 100644 --- a/lib/mihari/services/proxies.rb +++ b/lib/mihari/services/proxies.rb @@ -79,10 +79,10 @@ def source # @return [Mihari::Rule] # def rule - @rule ||= [].tap do |out| + @rule ||= lambda do data = Mihari::Models::Rule.find(rule_id).data - out << Rule.new(**data) - end.first + Rule.new(**data) + end.call end end end From c7210982aef78672ad420473830b65d4c988e1d1 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sun, 14 Jan 2024 19:05:35 +0900 Subject: [PATCH 2/3] refactor: use #tap & Hash#delete --- lib/mihari/analyzers/base.rb | 25 ++++++--------------- lib/mihari/rule.rb | 42 ++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/lib/mihari/analyzers/base.rb b/lib/mihari/analyzers/base.rb index 577daae6c..4d41a218f 100644 --- a/lib/mihari/analyzers/base.rb +++ b/lib/mihari/analyzers/base.rb @@ -63,12 +63,10 @@ def normalized_artifacts artifacts.compact.sort.map do |artifact| # No need to set data_type manually # It is set automatically in #initialize - artifact = artifact.is_a?(Models::Artifact) ? artifact : Models::Artifact.new(data: artifact) - - artifact.source = self.class.key - artifact.query = query - - artifact + (artifact.is_a?(Models::Artifact) ? artifact : Models::Artifact.new(data: artifact)).tap do |normalized| + normalized.source = self.class.key + normalized.query = query + end end.select(&:valid?).uniq(&:data) end @@ -118,18 +116,9 @@ class << self # # @return [Mihari::Analyzers::Base] # - def from_query(params) - copied = params.deep_dup - - # convert params into arguments for initialization - query = copied[:query] - - # delete analyzer and query - %i[analyzer query].each { |key| copied.delete key } - - copied[:options] = copied[:options] || nil - - new(query, **copied) + def from_params(params) + query = params.delete(:query) + new(query, **params) end def inherited(child) diff --git a/lib/mihari/rule.rb b/lib/mihari/rule.rb index fbf2a3857..cafb5c2c2 100644 --- a/lib/mihari/rule.rb +++ b/lib/mihari/rule.rb @@ -2,6 +2,7 @@ module Mihari class Rule < Service + include Concerns::FalsePositiveNormalizable include Concerns::FalsePositiveValidatable # @return [Hash] @@ -136,8 +137,7 @@ def artifacts analyzer_results.flat_map do |result| artifacts = result.value! artifacts.map do |artifact| - artifact.rule_id = id - artifact + artifact.tap { |tapped| tapped.rule_id = id } end end end @@ -188,9 +188,7 @@ def enriched_artifacts def bulk_emit return [] if enriched_artifacts.empty? - Parallel.map(emitters) do |emitter| - emitter.result(enriched_artifacts).value_or nil - end.compact + Parallel.map(emitters) { |emitter| emitter.result(enriched_artifacts).value_or nil }.compact end # @@ -315,12 +313,12 @@ def get_analyzer_class(key) # @return [Array] # def analyzers - @analyzers ||= queries.map do |params| - analyzer_name = params[:analyzer] - klass = get_analyzer_class(analyzer_name) - analyzer = klass.from_query(params) - analyzer.validate_configuration! - analyzer + @analyzers ||= queries.deep_dup.map do |params| + name = params.delete(:analyzer) + klass = get_analyzer_class(name) + klass.from_params(params).tap do |analyzer| + analyzer.validate_configuration! + end end end @@ -356,16 +354,14 @@ def get_emitter_class(key) # @return [Array] # def emitters - @emitters ||= data[:emitters].map(&:deep_dup).map do |params| - name = params[:emitter] - options = params[:options] - - %i[emitter options].each { |key| params.delete key } + @emitters ||= data[:emitters].deep_dup.map do |params| + name = params.delete(:emitter) + options = params.delete(:options) klass = get_emitter_class(name) - emitter = klass.new(rule: self, options: options, **params) - emitter.validate_configuration! - emitter + klass.new(rule: self, options: options, **params).tap do |emitter| + emitter.validate_configuration! + end end end @@ -386,11 +382,9 @@ def get_enricher_class(key) # @return [Array] enrichers # def enrichers - @enrichers ||= data[:enrichers].map(&:deep_dup).map do |params| - name = params[:enricher] - options = params[:options] - - %i[enricher options].each { |key| params.delete key } + @enrichers ||= data[:enrichers].deep_dup.map do |params| + name = params.delete(:enricher) + options = params.delete(:options) klass = get_enricher_class(name) klass.new(options: options, **params) From 9bfa0f3dbfc974be91845118da6d1b91f254e173 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sun, 14 Jan 2024 19:06:19 +0900 Subject: [PATCH 3/3] refactor: simplify message --- lib/mihari/web/endpoints/artifacts.rb | 2 +- lib/mihari/web/endpoints/rules.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mihari/web/endpoints/artifacts.rb b/lib/mihari/web/endpoints/artifacts.rb index 67369153a..690962442 100644 --- a/lib/mihari/web/endpoints/artifacts.rb +++ b/lib/mihari/web/endpoints/artifacts.rb @@ -74,7 +74,7 @@ class Artifacts < Grape::API end end.to_result - message = queued ? "ID:#{id}'s enrichment has been queued" : "ID:#{id}'s enrichment has been succeeded" + message = queued ? "ID:#{id}'s enrichment is queued" : "ID:#{id}'s enrichment is successful" return present({ message: message, queued: queued }, with: Entities::QueueMessage) if result.success? case result.failure diff --git a/lib/mihari/web/endpoints/rules.rb b/lib/mihari/web/endpoints/rules.rb index 655550f67..fc13f00c3 100644 --- a/lib/mihari/web/endpoints/rules.rb +++ b/lib/mihari/web/endpoints/rules.rb @@ -94,7 +94,7 @@ def call(yaml, overwrite: true) end end.to_result - message = queued ? "ID:#{id}'s search has been queued" : "ID:#{id}'s search has been succeed" + message = queued ? "ID:#{id}'s search is queued" : "ID:#{id}'s search is successful" return present({ message: message, queued: queued }, with: Entities::QueueMessage) if result.success? case result.failure