Sha256: 4ff1114adceadb4dbc4230552695dff5a57120a1f826ed671a76ce0942b98f04

Contents?: true

Size: 1.17 KB

Versions: 35

Compression:

Stored size: 1.17 KB

Contents

require 'aws-sdk-ssm'

class Jets::Dotenv
  class Ssm
    SSM_VARIABLE_REGEXP = /^ssm:(.*)/

    def initialize(variables={})
      @variables = variables
      @missing = []
    end

    def interpolate!
      interpolated_variables = @variables.map do |key, value|
        if value[SSM_VARIABLE_REGEXP]
          value = fetch_ssm_value(key, $1)
        end

        [key, value]
      end

      interpolated_variables.each do |key, value|
        ENV[key] = value
      end

      if @missing.empty?
        interpolated_variables.to_h # success
      else
        message = "Error loading .env variables. No matching SSM parameters found for:\n".color(:red)
        message += @missing.map { |k,v,n| "  #{k}=ssm:#{v} # ssm name: #{n}"}.join("\n")
        abort message
      end
    end

    def fetch_ssm_value(key, value)
      name = value.start_with?("/") ? value :
        "/#{Jets.config.project_name}/#{Jets.env}/#{value}"
      response = ssm.get_parameter(name: name, with_decryption: true)
      response.parameter.value
    rescue Aws::SSM::Errors::ParameterNotFound
      @missing << [key, value, name]
      ''
    end

    def ssm
      @ssm ||= Aws::SSM::Client.new
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
jets-2.2.4 lib/jets/dotenv/ssm.rb
jets-2.2.3 lib/jets/dotenv/ssm.rb
jets-2.2.2 lib/jets/dotenv/ssm.rb
jets-2.2.1 lib/jets/dotenv/ssm.rb
jets-2.2.0 lib/jets/dotenv/ssm.rb
jets-2.1.7 lib/jets/dotenv/ssm.rb
jets-2.1.6 lib/jets/dotenv/ssm.rb
jets-2.1.5 lib/jets/dotenv/ssm.rb
jets-2.1.4 lib/jets/dotenv/ssm.rb
jets-2.1.3 lib/jets/dotenv/ssm.rb
jets-2.1.2 lib/jets/dotenv/ssm.rb
jets_bb_fork-0.0.4 lib/jets/dotenv/ssm.rb
jets-2.1.1 lib/jets/dotenv/ssm.rb
jets-2.1.0 lib/jets/dotenv/ssm.rb
jets-2.0.6 lib/jets/dotenv/ssm.rb
jets-2.0.5 lib/jets/dotenv/ssm.rb
jets-2.0.4 lib/jets/dotenv/ssm.rb
jets-2.0.3 lib/jets/dotenv/ssm.rb
jets-2.0.1 lib/jets/dotenv/ssm.rb
jets-2.0.0 lib/jets/dotenv/ssm.rb