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.16.0 lib/shipit/environment_variables.rb
shipit-engine-0.15.0 lib/shipit/environment_variables.rb
shipit-engine-0.14.0 lib/shipit/environment_variables.rb
shipit-engine-0.13.0 lib/shipit/environment_variables.rb
shipit-engine-0.12.1 lib/shipit/environment_variables.rb
shipit-engine-0.12.0 lib/shipit/environment_variables.rb
shipit-engine-0.11.0 lib/shipit/environment_variables.rb
shipit-engine-0.10.0 lib/shipit/environment_variables.rb
shipit-engine-0.9.0 lib/shipit/environment_variables.rb
shipit-engine-0.8.9 lib/shipit/environment_variables.rb
shipit-engine-0.8.8 lib/shipit/environment_variables.rb
shipit-engine-0.8.7 lib/shipit/environment_variables.rb
shipit-engine-0.8.6 lib/shipit/environment_variables.rb
shipit-engine-0.8.5 lib/shipit/environment_variables.rb
shipit-engine-0.8.4 lib/shipit/environment_variables.rb
shipit-engine-0.8.3 lib/shipit/environment_variables.rb
shipit-engine-0.8.2 lib/shipit/environment_variables.rb
shipit-engine-0.8.1 lib/shipit/environment_variables.rb
shipit-engine-0.8.0 lib/shipit/environment_variables.rb