Sha256: 33c2dd99a33b7445c702ac7db37704d4416ae29b8a980adcdc0fff53e448d82e

Contents?: true

Size: 1.61 KB

Versions: 15

Compression:

Stored size: 1.61 KB

Contents

# Copyright (c) 2020 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

      def initialize key, value
        key = EnvConfigurationItem.resolve_corrected_path(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..-1] 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

15 entries across 15 versions & 1 rubygems

Version Path
contrast-agent-3.14.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.13.2 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.13.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.13.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.12.2 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.12.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.12.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.11.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.10.2 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.10.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.10.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.9.1 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.9.0 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.8.5 lib/contrast/utils/env_configuration_item.rb
contrast-agent-3.8.4 lib/contrast/utils/env_configuration_item.rb