lib/contrast/config/assess_configuration.rb in contrast-agent-5.1.0 vs lib/contrast/config/assess_configuration.rb in contrast-agent-5.2.0

- old
+ new

@@ -4,21 +4,115 @@ module Contrast module Config # Common Configuration settings. Those in this section pertain to the # assess functionality of the Agent. class AssessConfiguration < BaseConfiguration - KEYS = { - tags: EMPTY_VALUE, - enable: EMPTY_VALUE, - enable_scan_response: true, - enable_dynamic_sources: true, - sampling: Contrast::Config::SamplingConfiguration, - rules: Contrast::Config::AssessRulesConfiguration, - stacktraces: 'ALL' - }.cs__freeze + # @return [String, nil] + attr_reader :tags - def initialize hsh - super(hsh, KEYS) + DEFAULT_STACKTRACES = 'ALL' + + def initialize hsh = {} + @enable = traverse_config(hsh, :enable) + @tags = traverse_config(hsh, :tags) + @enable_scan_response = traverse_config(hsh, :enable_scan_response) + @enable_dynamic_sources = traverse_config(hsh, :enable_dynamic_sources) + @sampling = Contrast::Config::SamplingConfiguration.new(traverse_config(hsh, :sampling)) + @rules = Contrast::Config::AssessRulesConfiguration.new(traverse_config(hsh, :rules)) + @stacktraces = traverse_config(hsh, :stacktraces) + @configuration_map = {} + build_configuration_map + end + + # @return [Boolean, true] + def enable + @enable.nil? ? true : @enable + end + + # @return [Boolean, true] + def enable_scan_response + @enable_scan_response.nil? ? true : @enable_scan_response + end + + # @return [Boolean, true] + def enable_dynamic_sources + @enable_dynamic_sources.nil? ? true : @enable_dynamic_sources + end + + # @return [Contrast::Config::SamplingConfiguration] + def sampling + @sampling ||= Contrast::Config::SamplingConfiguration.new + end + + # @return [Contrast::Config::AssessRulesConfiguration] + def rules + @rules ||= Contrast::Config::AssessRulesConfiguration.new + end + + # @return [String] stacktrace level + def stacktraces + @stacktraces ||= DEFAULT_STACKTRACES + end + + def enable= value + self['enable'] = value + end + + def tags= value + self['tags'] = value + end + + def enable_scan_response= value + self['enable_scan_response'] = value + end + + def enable_dynamic_sources= value + self['enable_dynamic_sources'] = value + end + + def sampling= value + self['sampling'] = value + end + + def rules= value + self['rules'] = value + end + + def stacktraces= value + self['stacktraces'] = value + end + # TODO: RUBY-1493 MOVE TO BASE CONFIG + + def []= key, value + instance_variable_set("@#{ key }".to_sym, value) + @configuration_map[key] = value + end + + def [] key + send(key.to_sym) + end + + # Traverse the given entity to build out the configuration graph. + # + # The values will be either a hash, indicating internal nodes to + # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a + # leaf node. + # + # The spec_key are the Contrast defined keys based on the instance variables of + # a given configuration. + def traverse_config values, spec_key + internal_nodes = values.cs__respond_to?(:has_key?) + val = internal_nodes ? value_from_key_config(spec_key, values) : nil + val == EMPTY_VALUE ? nil : val + end + + def build_configuration_map + instance_variables.each do |key| + str_key = key.to_s.tr('@', '') + next if str_key == 'configuration_map' + + @configuration_map[str_key] = send(str_key.to_sym) + end end end end end