# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/utils/object_share' require 'contrast/utils/duck_utils' module Contrast module Config module Diagnostics # All config values from all sources, stored in a easy to write representation. class SourceConfigValue NULL = 'null' # @return [String] Name of the config starting form root of yaml config. attr_accessor :canonical_name # @return [String] Name of the config. attr_accessor :key # @return [String, Boolean, array] Value set for the config. Current Effective Value attr_accessor :value # @return [String] The source for the entry in the config. attr_accessor :source # @return [String,nil] The filename for the source of the config, if the source was "yaml". attr_accessor :filename def to_controlled_hash { canonical_name: canonical_name, name: key || Contrast::Utils::ObjectShare::EMPTY_STRING, value: Contrast::Config::Diagnostics::Tools.value_to_s(value), source: source || Contrast::Utils::ObjectShare::EMPTY_STRING, filename: filename || Contrast::Utils::ObjectShare::EMPTY_STRING } end def to_source_hash { canonical_name: canonical_name, name: key || Contrast::Utils::ObjectShare::EMPTY_STRING, value: Contrast::Config::Diagnostics::Tools.value_to_s(value) } end # Assigns file name of the config iv viable, Currently supported formats for config file are *.yaml # and *.yml. For the config loaded from file the filename is kept as source. # # @param source [String] name of the source file yaml | yml # @return [Array, nil] def assign_filename source Contrast::Components::Config::Sources::APP_CONFIGURATION_EXTENSIONS.each do |type| # We use the source to transfer the file's name from the mapping of the extensions. # This is done b/c the user_configuration file has a second field to be filled, # and other sources don't. Transfer the source as filename and set the default value # for it later when we find the sources. instance_variable_set(:@filename, source) if source.include?(".#{ type.downcase }") end end end end end end