Module: Contrast::Config::Diagnostics::EnvironmentVariables

Defined in:
lib/contrast/config/diagnostics/environment_variables.rb

Overview

Reads All ENV variables.

Constant Summary collapse

NON_COMMON_ENV =
%w[
  CONTRAST_CONFIG_PATH CONTRAST_AGENT_TELEMETRY_OPTOUT CONTRAST_AGENT_TELEMETRY_TEST
].cs__freeze
MASKED_ENV =
%w[CONTRAST__API__API_KEY CONTRAST__API__SERVICE_KEY].cs__freeze

Class Method Summary collapse

Class Method Details

.environment_settings(env) ⇒ Array

This method will fill the canonical name for each env var and will check for any uncommon ones.

Parameters:

Returns:

  • (Array)

    array of all the values needed to be written.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/contrast/config/diagnostics/environment_variables.rb', line 23

def environment_settings env # rubocop:disable Metrics/MethodLength
  env_hash = env.select do |e|
    e.to_s.start_with?(Contrast::Configuration::CONTRAST_ENV_MARKER) || NON_COMMON_ENV.include?(e.to_s)
  end
  environment_settings = []
  env_hash.each do |key, value| # rubocop:disable Metrics/BlockLength
    efc_value = Contrast::Config::Diagnostics::EffectiveConfigValue.new.tap do |effective_value|
      next unless value

      effective_value.canonical_name = if NON_COMMON_ENV.include?(key)
                                         key.gsub(Contrast::Utils::ObjectShare::UNDERSCORE,
                                                  Contrast::Utils::ObjectShare::PERIOD).downcase.
                                             gsub(Contrast::Utils::ObjectShare::CONTRAST_DOT,
                                                  Contrast::Utils::ObjectShare::EMPTY_STRING)
                                       else
                                         key.gsub(Contrast::Utils::ObjectShare::DOUBLE_UNDERSCORE,
                                                  Contrast::Utils::ObjectShare::PERIOD).downcase.
                                             gsub(Contrast::Utils::ObjectShare::CONTRAST_DOT,
                                                  Contrast::Utils::ObjectShare::EMPTY_STRING)
                                       end
      effective_value.value = if MASKED_ENV.include?(key)
                                Contrast::Configuration::EFFECTIVE_REDACTED
                              else
                                Contrast::Config::Diagnostics::Tools.value_to_s(value)
                              end
      effective_value.key = key
    end
    next unless efc_value

    environment_settings << efc_value
    Contrast::Config::Diagnostics::Tools.
        update_config(efc_value.key,
                      efc_value.value,
                      Contrast::Components::Config::Sources::ENVIRONMENT_VARIABLE)
  end
  environment_settings
end