lib/contrast/agent/settings_state.rb in contrast-agent-3.10.2 vs lib/contrast/agent/settings_state.rb in contrast-agent-3.11.0

- old
+ new

@@ -1,47 +1,31 @@ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true cs__scoped_require 'securerandom' cs__scoped_require 'set' +cs__scoped_require 'singleton' -cs__scoped_require 'contrast/utils/assess/sampling_util' -cs__scoped_require 'contrast/utils/assess/tracking_util' -cs__scoped_require 'contrast/utils/environment_util' -cs__scoped_require 'contrast/utils/env_configuration_item' -cs__scoped_require 'contrast/utils/gemfile_reader' -cs__scoped_require 'contrast/utils/object_share' -cs__scoped_require 'contrast/utils/performs_logging' -cs__scoped_require 'contrast/utils/string_utils' -cs__scoped_require 'contrast/agent/reaction_processor' -cs__scoped_require 'contrast/agent/require_state' -cs__scoped_require 'contrast/agent/assess/policy/patcher' -cs__scoped_require 'contrast/agent/patching/policy/patcher' cs__scoped_require 'contrast/components/interface' +cs__scoped_require 'contrast/agent/reaction_processor' + module Contrast module Agent # This class functions as a way to query the Agent for its current # configuration without having to expose other sections of code to the # decision tree needed to make that determination. # It should not be accessed directly, but should instead be inherited from # # @abstract Use the methods in FeatureState to access this data class SettingsState - include Contrast::Utils::PerformsLogging include Contrast::Components::Interface - INVALID_CONFIG = '!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!' - access_component :agent, :analysis, :settings, :config, :contrast_service - MODE_WHITELIST = %i[NO_ACTION BLOCK_AT_PERIMETER MONITOR BLOCK].cs__freeze - # These are components. - attr_accessor :last_update, - :log_file, - :log_level + attr_accessor :last_update # These represent process-level attributes, # not directly related to Contrast function itself. attr_reader :pid, :ppid @@ -55,45 +39,27 @@ # keep track of which process instantiated this instance @pid = Process.pid.to_i @ppid = Process.ppid.to_i - init_log_queueing - check_config - - flush_log_queues end def check_config SETTINGS.reset_state if CONFIG.invalid? AGENT.disable! - self.class.log_error(INVALID_CONFIG) - abort_log_queues - elseif CONFIG.disabled? + logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!') + elsif CONFIG.disabled? AGENT.disable! - self.class.log_warn(LOG_CONFIGURATION_DISABLED) + logger.warn('Contrast disabled by configuration. Continuing without instrumentation.') else AGENT.enable! end end - # workaround to mixed use of class/instance - def self.logger - instance.logger - end - - def logger - logger_manager.current - end - - def logger_manager - (@_logger_manager ||= Contrast::Agent::LoggerManager.new.tap(&:update)) - end - def assess_rule name ASSESS.rule name end def instrument package @@ -103,50 +69,20 @@ return if @instrumented_packages[package] @instrumented_packages[package] = true end - self.class.debug_with_time("instrumenting #{ package }") do + logger.trace_with_time("instrumenting #{ package } ") do cs__scoped_require(package) end rescue LoadError, StandardError => e @instrumented_packages[package] = false - self.class.log_error("[e.class: #{ e.class }] unable to instrument: #{ package }", e) + logger.error("failed instrumenting #{ package } ", e) end + # Test only def protect_rule name PROTECT.rule name - end - - def send_inventory_message - return unless INVENTORY.enabled? - - app_update_msg = Contrast::Api::Dtm::ApplicationUpdate.new - - # TODO: RUBY-770 - Contrast::Utils::EnvironmentUtil.add_library_to_app_update(app_update_msg, protobuf_format(CONFIG.root.inventory.tags)) - - Contrast::Delegators::ApplicationUpdate.new(app_update_msg).instance_eval do - append_view_technology_descriptor_data(Contrast::Agent.framework_manager.find_applicable_view_technologies) if INVENTORY.enabled? - append_route_coverage_data(Contrast::Agent.framework_manager.find_route_discovery_data) if INVENTORY.enabled? - append_platform_version(Contrast::Agent.framework_manager.platform_version) - end - - Contrast::Utils::InventoryUtil.append_db_config(app_update_msg) - - CONTRAST_SERVICE.queue_message app_update_msg - end - - def present? str - Contrast::Utils::EnvironmentUtil.present?(str) - end - - # CONTRAST-35326, move this responsibility toward the protobuf object - def protobuf_format param, truncate: true - param = param&.to_s - param = Contrast::Utils::StringUtils.force_utf8(param) - param = Contrast::Utils::StringUtils.truncate(param) if truncate - param end end end end