Sha256: 07db54032f06f319ba41d34c1b2e8f7cc2ece3f98aab5ae7b906708290ae9f05

Contents?: true

Size: 864 Bytes

Versions: 39

Compression:

Stored size: 864 Bytes

Contents

module Shipit
  class EnvironmentVariables
    NotPermitted = Class.new(StandardError)

    class << self
      def with(env)
        EnvironmentVariables.new(env)
      end
    end

    def permit(variable_definitions)
      return {} unless @env
      raise "A whitelist is required to sanitize environment variables" unless variable_definitions
      sanitize_env_vars(variable_definitions)
    end

    private

    def initialize(env)
      @env = env
    end

    def sanitize_env_vars(variable_definitions)
      allowed_variables = variable_definitions.map(&:name)

      allowed, disallowed = @env.partition { |k, _| allowed_variables.include?(k) }.map(&:to_h)

      error_message = "Variables #{disallowed.keys.to_sentence} have not been whitelisted"
      raise NotPermitted.new(error_message) unless disallowed.empty?

      allowed
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
shipit-engine-0.30.0 lib/shipit/environment_variables.rb
shipit-engine-0.29.0 lib/shipit/environment_variables.rb
shipit-engine-0.28.1 lib/shipit/environment_variables.rb
shipit-engine-0.28.0 lib/shipit/environment_variables.rb
shipit-engine-0.27.1 lib/shipit/environment_variables.rb
shipit-engine-0.27.0 lib/shipit/environment_variables.rb
shipit-engine-0.26.0 lib/shipit/environment_variables.rb
shipit-engine-0.25.1 lib/shipit/environment_variables.rb
shipit-engine-0.25.0 lib/shipit/environment_variables.rb
shipit-engine-0.24.0 lib/shipit/environment_variables.rb
shipit-engine-0.23.1 lib/shipit/environment_variables.rb
shipit-engine-0.23.0 lib/shipit/environment_variables.rb
shipit-engine-0.22.0 lib/shipit/environment_variables.rb
shipit-engine-0.21.0 lib/shipit/environment_variables.rb
shipit-engine-0.20.1 lib/shipit/environment_variables.rb
shipit-engine-0.20.0 lib/shipit/environment_variables.rb
shipit-engine-0.19.0 lib/shipit/environment_variables.rb
shipit-engine-0.18.1 lib/shipit/environment_variables.rb
shipit-engine-0.18.0 lib/shipit/environment_variables.rb
shipit-engine-0.17.0 lib/shipit/environment_variables.rb