Sha256: b3f0852b0a5bfb205e228bb6f12be45ad5a3d8f7f7a9e4938c649466d40428d0

Contents?: true

Size: 1.63 KB

Versions: 18

Compression:

Stored size: 1.63 KB

Contents

# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Utils
    # Stores key and value of a environmnt variable and can normalize the
    # path for comparison to common config
    class EnvConfigurationItem
      START_UNDERSCORE = /^(_+)/.cs__freeze
      END_UNDERSCORE   = /(_+)$/.cs__freeze
      REPEATING_UNDERSCORE = /_{3,}/.cs__freeze

      attr_reader :value, :dot_path_array, :key

      def initialize key, value
        key = EnvConfigurationItem.resolve_corrected_path(key)
        @key = key
        @dot_path_array = key.downcase.split(Contrast::Utils::ObjectShare::DOUBLE_UNDERSCORE)
        @value = value

        correct_dot_path
      end

      def correct_dot_path
        return unless @dot_path_array

        # we need to shift by one to access the appropriate level of the config
        @dot_path_array.shift
        # and then we need to fix any rules
        idx = @dot_path_array.find_index('rules')
        return unless idx

        next_idx = idx + 1
        return if next_idx == @dot_path_array.length

        @dot_path_array[next_idx] = @dot_path_array[next_idx].to_s.tr('_', '-')
      end

      def self.resolve_corrected_path path
        tmp_path = path.dup
        start_match = START_UNDERSCORE.match(tmp_path)
        tmp_path = tmp_path[start_match.offset(0).last..] if start_match

        tmp_path.gsub!(REPEATING_UNDERSCORE, '__')

        end_match = END_UNDERSCORE.match(tmp_path)
        tmp_path = tmp_path[0...end_match.offset(0).first] if end_match

        tmp_path
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.6.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.5.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.4.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.4.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.3.2 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.3.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.3.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.2.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.1.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-7.0.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.15.3 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.15.2 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.15.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.15.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.14.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.13.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-6.12.0 lib/contrast/utils/env_configuration_item.rb