# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/config/logger_configuration' module Contrast module Config # Common Configuration settings. Those in this section pertain to the # communication between the Agent & the Service. class ServiceConfiguration < BaseConfiguration # We don't set these b/c we've been asked to handle the default values of # these settings differently, logging when we have to use them. DEFAULT_HOST = '127.0.0.1' DEFAULT_PORT = '30555' attr_writer :enable, :logger, :bypass # @return [String, nil] attr_accessor :socket # @return [String, nil] attr_accessor :port # @return [String, nil] attr_accessor :host def initialize hsh = {} @enable = traverse_config(hsh, :enable) @host = traverse_config(hsh, :host) @port = traverse_config(hsh, :port) @socket = traverse_config(hsh, :socket) @logger = Contrast::Config::LoggerConfiguration.new(traverse_config(hsh, :logger)) @bypass = traverse_config(hsh, :bypass) @configuration_map = {} build_configuration_map end # @return [Boolean, false] def enable !!@enable end # @return [Contrast::Config::LoggerConfiguration] def logger @logger ||= Contrast::Config::LoggerConfiguration.new end # @return [Boolean, false] def bypass !!@bypass 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