Class: Contrast::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Contrast::Components::Scope::InstanceMethods, Forwardable
Includes:
Contrast::Components::Scope::InstanceMethods, Contrast::Config::BaseConfiguration
Defined in:
lib/contrast/configuration.rb

Overview

This is how we read in the local settings for the Agent, both ENV/ CMD line and from properties files, in order to determine which settings, if any, the user has overridden.

Constant Summary collapse

CONTRAST_ENV_MARKER =
'CONTRAST__'
DEFAULT_YAML_PATH =
'contrast_security.yaml'
MILLISECOND_MARKER =
'_ms'
CONVERSION =
{}.cs__freeze
CONFIG_BASE_PATHS =

Precedence of paths, shift if config file values needs to go up the chain.

%w[./ config/ /etc/contrast/ruby/ /etc/contrast/ /etc/].cs__freeze
KEYS_TO_REDACT =
%i[api_key url service_key user_name].cs__freeze
REDACTED =
'**REDACTED**'
EFFECTIVE_REDACTED =
'****'
DEPRECATED_PROPERTIES =
%w[
  CONTRAST__AGENT__SERVICE__ENABLE CONTRAST__AGENT__SERVICE__LOGGER__LEVEL
  CONTRAST__AGENT__SERVICE__LOGGER__PATH CONTRAST__AGENT__SERVICE__LOGGER__STDOUT
].cs__freeze

Constants included from Contrast::Config::BaseConfiguration

Contrast::Config::BaseConfiguration::AT_UNDERSCORE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contrast::Components::Scope::InstanceMethods

contrast_enter_method_scopes!, contrast_exit_method_scopes!, with_app_scope, with_contrast_scope, with_deserialization_scope, with_split_scope

Methods included from Contrast::Config::BaseConfiguration

#to_contrast_hash

Constructor Details

#initialize(cli_options = nil, default_name = DEFAULT_YAML_PATH) ⇒ Configuration

Returns a new instance of Configuration.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/contrast/configuration.rb', line 73

def initialize cli_options = nil, default_name = DEFAULT_YAML_PATH
  @default_name = default_name

  # Load config_kv from file
  config_kv = Contrast::Utils::HashUtils.deep_symbolize_all_keys(load_config)

  # Load cli options from env
  cli_options ||= cli_to_hash
  config_kv = Contrast::Utils::HashUtils.precedence_merge(config_kv, cli_options)
  update_sources_from_cli(cli_options)

  # Some in-flight rewrites to maintain backwards compatibility
  config_kv = update_prop_keys(config_kv)
  @sources = Contrast::Components::Config::Sources.new(source_file_extensions)
  @loaded_config = config_kv

  # requires loaded_config:
  create_config_components
end

Instance Attribute Details

#agentContrast::Components::Agent::Interface



111
112
113
# File 'lib/contrast/configuration.rb', line 111

def agent
  @agent ||= Contrast::Components::Agent::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#apiContrast::Components::Api::Interface



106
107
108
# File 'lib/contrast/configuration.rb', line 106

def api
  @api ||= Contrast::Components::Api::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#applicationContrast::Components::AppContext::Interface



116
117
118
# File 'lib/contrast/configuration.rb', line 116

def application
  @application ||= Contrast::Components::AppContext::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#assessContrast::Components::Assess::Interface



126
127
128
# File 'lib/contrast/configuration.rb', line 126

def assess
  @assess ||= Contrast::Components::Assess::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#config_fileString? (readonly)

Returns:



56
57
58
# File 'lib/contrast/configuration.rb', line 56

def config_file
  @config_file
end

#default_nameObject (readonly)

Returns the value of attribute default_name.



33
34
35
# File 'lib/contrast/configuration.rb', line 33

def default_name
  @default_name
end

#enableBoolean?

Returns:

  • (Boolean, nil)


50
51
52
# File 'lib/contrast/configuration.rb', line 50

def enable
  @enable
end

#inventoryContrast::Components::Inventory::Interface



131
132
133
# File 'lib/contrast/configuration.rb', line 131

def inventory
  @inventory ||= Contrast::Components::Inventory::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#loaded_configHash

Returns:



52
53
54
# File 'lib/contrast/configuration.rb', line 52

def loaded_config
  @loaded_config
end

#protectContrast::Components::Protect::Interface



136
137
138
# File 'lib/contrast/configuration.rb', line 136

def protect
  @protect ||= Contrast::Components::Protect::Interface.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#serverContrast::Config::ServerConfiguration



121
122
123
# File 'lib/contrast/configuration.rb', line 121

def server
  @server ||= Contrast::Config::ServerConfiguration.new # rubocop:disable Naming/MemoizedInstanceVariableName
end

#sourcesContrast::Config::Sources

Returns:

  • (Contrast::Config::Sources)


54
55
56
# File 'lib/contrast/configuration.rb', line 54

def sources
  @sources
end

Instance Method Details

#configuration_pathsObject

Base paths to check for the contrast configuration file, sorted by reverse order of precedence (first is most important).



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/contrast/configuration.rb', line 142

def configuration_paths
  @_configuration_paths ||= begin
    basename = default_name.split('.')[0]
    # Order of extensions comes from here:
    extensions = Contrast::Components::Config::Sources::APP_CONFIGURATION_EXTENSIONS

    paths = []
    # Environment paths takes precedence here. Look first through them.
    config_path = ENV.fetch('CONTRAST_CONFIG_PATH', nil)
    security_path = ENV.fetch('CONTRAST_SECURITY_CONFIG', nil)
    paths << config_path if config_path
    paths << security_path if security_path

    extensions.each do |ext|
      places = CONFIG_BASE_PATHS.product(["#{ basename }.#{ ext }"])
      paths += places.map!(&:join)
    end
    paths
  end
end

#loggableString

Get a loggable YAML format of this configuration

Returns:

  • (String)

    the current active configuration of the Agent, represented as a YAML string



96
97
98
# File 'lib/contrast/configuration.rb', line 96

def loggable
  convert_to_hash.to_yaml
end

#originContrast::Config::ConfigurationFiles

List of all read configuration files.

Returns:



166
167
168
# File 'lib/contrast/configuration.rb', line 166

def origin
  @_origin ||= Contrast::Config::ConfigurationFiles.new
end

#source_file_extensionsHash

Returns map of all extensions for each config key.

Returns:

  • (Hash)

    map of all extensions for each config key.



101
102
103
# File 'lib/contrast/configuration.rb', line 101

def source_file_extensions
  @_source_file_extensions ||= {}
end