Class: Jets::Dotenv::Ssm

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Util::Logging
Defined in:
lib/jets/dotenv/ssm.rb

Instance Method Summary collapse

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(variables) ⇒ Ssm

Returns a new instance of Ssm.



6
7
8
9
# File 'lib/jets/dotenv/ssm.rb', line 6

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

Instance Method Details

#get_ssm_parameters_path(path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jets/dotenv/ssm.rb', line 68

def get_ssm_parameters_path(path)
  if ARGV.include?("dotenv") && ARGV.include?("list")
    warn "# Autoloading SSM parameters within path #{path}"
  end
  parameters = {}
  next_token = nil

  loop do
    resp = ssm.get_parameters_by_path(path: path, with_decryption: true, next_token: next_token)

    resp.parameters.each do |param|
      key = param.name.sub(path, "")
      parameters[key] = param.value
    end

    next_token = resp.next_token
    break unless next_token
  end

  parameters
end

#interpolate!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jets/dotenv/ssm.rb', line 11

def interpolate!
  interpolated_vars = merged_variables.map do |key, value|
    var = Var.new(key, value)
    @missing << var if var.ssm_missing?
    var
  end

  interpolated_vars.each do |var|
    ENV[var.name] = var.value
  end

  if @missing.empty?
    interpolated_vars.map { |var| [var.name, var.value] }.sort.to_h # success
  else
    message = "Error loading .env variables. No matching SSM parameters found for:\n".color(:red)
    message += @missing.map do |var|
      "    #{var.name}=#{var.raw_value} # ssm name: #{var.ssm_name}"
    end.join("\n")
    abort message
  end
end

#merged_variablesObject

Merges the variables from the dotenv files with the conventional ssm variables inferred by path, IE: /demo/dev/ User defined config/jets/env files values win over inferred ssm variables.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jets/dotenv/ssm.rb', line 36

def merged_variables
  if Jets.project.dotenv.ssm.autoload.enable
    ssm_vars = get_ssm_parameters_path(Convention.ssm_path) # IE: /demo/dev/
    if Convention.jets_env_path != Convention.ssm_path
      jets_env_vars = get_ssm_parameters_path(Convention.jets_env_path) # IE: /demo/sbx/
      ssm_vars = ssm_vars.merge(jets_env_vars)
    end

    # skip list
    ssm_vars = ssm_vars.delete_if { |k, v| skip_list?(k) }

    # optimization: no need to get vars with SSM values since they are already in ssm_vars
    vars = @variables.dup.delete_if { |k, v| v == "SSM" && ssm_vars.key?(k) }
    ssm_vars.merge(vars)
  else
    @variables
  end
end

#skip_list?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jets/dotenv/ssm.rb', line 55

def skip_list?(key)
  key = key.to_s
  a = Jets.project.dotenv.ssm.autoload
  skip_list = a.default_skip + a.skip
  skip_list.detect do |i|
    if i.is_a?(Regexp)
      key.match(i)
    else
      key == i
    end
  end
end