# 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_value' require 'contrast/config/diagnostics/tools' require 'contrast/utils/object_share' module Contrast module Config module Diagnostics # Reads All ENV variables. module EnvironmentVariables class << self NON_COMMON_ENV = %w[ CONTRAST_CONFIG_PATH CONTRAST_AGENT_TELEMETRY_OPTOUT CONTRAST_AGENT_TELEMETRY_TEST ].cs__freeze # This method will fill the canonical name for each env var and will check for any uncommon ones. # # @param env [Hash] # @return [Array] array of all the values needed to be written. def environment_settings env 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| 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 else key.gsub(Contrast::Utils::ObjectShare::DOUBLE_UNDERSCORE, Contrast::Utils::ObjectShare::PERIOD).downcase end if effective_value.canonical_name effective_value.key = effective_value.canonical_name.gsub(Contrast::Utils::ObjectShare::CONTRAST_DOT, Contrast::Utils::ObjectShare::EMPTY_STRING) end effective_value.value = Contrast::Config::Diagnostics::Tools.value_to_s(value) effective_value.key = key end environment_settings << efc_value if efc_value end environment_settings end end end end end end