# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/config/diagnostics/effective_config' require 'contrast/config/diagnostics/user_configuration_file' require 'contrast/config/diagnostics/environment_variables' require 'contrast/config/diagnostics/command_line' require 'contrast/config/diagnostics/contrast_ui' module Contrast module Config module Diagnostics # This class is responsible for logging to file the effective Agent configurations after startup. class Config # @return [Contrast::Config::Diagnostics::EffectiveConfig] attr_reader :effective_config # @return [String] Status message of the connection with TS. attr_accessor :config_status # @return [Contrast::Config::Diagnostics::UserConfigurationFile] attr_reader :user_configuration_file MESSAGE_FAIL = 'Unable to connect to Contrast, configuration details from the Contrast UI will not be included.' MESSAGE_SUCCESSFUL = 'Success' CONN_STATUS_MSG_FAILURE = 'Unable to connect to Contrast, insufficient connection properties provided.' def initialize @effective_config = Contrast::Config::Diagnostics::EffectiveConfig.new @config_status = Contrast::Utils::ObjectShare::EMPTY_STRING @user_configuration_file = Contrast::Config::Diagnostics::UserConfigurationFile.new end # This method will set the status from the request/response cycles # # @param response_code [string] def determine_config_status response_code @config_status = case response_code when '200', '304' MESSAGE_SUCCESSFUL else MESSAGE_FAIL end end # This method will set the status message from the config validation def populate_fail_connection @config_status = CONN_STATUS_MSG_FAILURE end def to_controlled_hash { status: @config_status, effective_config: effective_config.to_controlled_hash, user_configuration_file: user_configuration_file.to_controlled_hash, environment_variable: Contrast::Config::Diagnostics::EnvironmentVariables.environment_settings(ENV). map(&:to_controlled_hash), command_line: Contrast::Config::Diagnostics::CommandLine.command_line_settings.map(&:to_controlled_hash), contrast_ui: Contrast::Config::Diagnostics::ContrastUI.contrast_ui_settings.map(&:to_controlled_hash) }.compact end end end end end