Sha256: 2b441cec8433d1415f63184ae201a31e2a5f41855612c84de082ce5b6a53502f

Contents?: true

Size: 1.26 KB

Versions: 20

Compression:

Stored size: 1.26 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.sort_by { |k,_| k }.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)
      return "fake-ssm-value" if ENV['JETS_BUILD_NO_INTERNET']

      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

20 entries across 20 versions & 1 rubygems

Version Path
jets-2.3.19 lib/jets/dotenv/ssm.rb
jets-2.3.18 lib/jets/dotenv/ssm.rb
jets-2.3.17 lib/jets/dotenv/ssm.rb
jets-2.3.16 lib/jets/dotenv/ssm.rb
jets-2.3.15 lib/jets/dotenv/ssm.rb
jets-2.3.14 lib/jets/dotenv/ssm.rb
jets-2.3.13 lib/jets/dotenv/ssm.rb
jets-2.3.12 lib/jets/dotenv/ssm.rb
jets-2.3.11 lib/jets/dotenv/ssm.rb
jets-2.3.10 lib/jets/dotenv/ssm.rb
jets-2.3.9 lib/jets/dotenv/ssm.rb
jets-2.3.8 lib/jets/dotenv/ssm.rb
jets-2.3.7 lib/jets/dotenv/ssm.rb
jets-2.3.6 lib/jets/dotenv/ssm.rb
jets-2.3.5 lib/jets/dotenv/ssm.rb
jets-2.3.4 lib/jets/dotenv/ssm.rb
jets-2.3.3 lib/jets/dotenv/ssm.rb
jets-2.3.2 lib/jets/dotenv/ssm.rb
jets-2.3.1 lib/jets/dotenv/ssm.rb
jets-2.3.0 lib/jets/dotenv/ssm.rb