Class: Securenv::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/securenv/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, stage:) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/securenv/client.rb', line 5

def initialize(app:, stage:)
  @app = app
  @stage = stage
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/securenv/client.rb', line 3

def app
  @app
end

#stageObject (readonly)

Returns the value of attribute stage.



4
5
6
# File 'lib/securenv/client.rb', line 4

def stage
  @stage
end

Instance Method Details

#get(variable:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/securenv/client.rb', line 31

def get(variable:)
  parameter_name = parameter_name_for(variable)
  resp = ssm_client.get_parameter({
    name: parameter_name,
    with_decryption: true
  })
  parameter = Parameter.new(
   name: parameter_name,
   version: resp.parameter.version,
   value: resp.parameter.value
  )

  return parameter
rescue Aws::SSM::Errors::ParameterNotFound
  raise ParameterNotFoundError.new "#{variable} is not set for app: #{app} and stage: #{stage}"
end

#listObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/securenv/client.rb', line 64

def list
  resp = ssm_client.get_parameters_by_path({
    path: parameter_path,
    with_decryption: true
  })
  parameters = resp.parameters.map do |param|
    Parameter.new(
      name: param.name,
      version: param.version,
      value: param.value
    )
  end

  return parameters
end

#parameter_name_for(variable) ⇒ Object



91
92
93
# File 'lib/securenv/client.rb', line 91

def parameter_name_for(variable)
  "#{parameter_path}/#{variable}"
end

#parameter_pathObject



87
88
89
# File 'lib/securenv/client.rb', line 87

def parameter_path
  "/#{app}/#{stage}"
end

#populate_envObject



80
81
82
83
84
85
# File 'lib/securenv/client.rb', line 80

def populate_env
  parameters = list
  parameters.each do |param|
    ENV[param.var_name] = param.value
  end
end

#set(variable:, value:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/securenv/client.rb', line 10

def set(variable:, value:)
  parameter_name = parameter_name_for(variable)

  resp = ssm_client.put_parameter({
    name: parameter_name,
    description: "Set via securenv",
    value: value,
    type: "SecureString",
    #key_id: "ParameterKeyId",
    overwrite: true,
    tier: "Standard" # accepts Standard, Advanced, Intelligent-Tiering
  })
  parameter = Parameter.new(
   name: parameter_name,
   version: resp.version,
   value: value
  )

  return parameter
end

#ssm_clientObject



95
96
97
# File 'lib/securenv/client.rb', line 95

def ssm_client
  @ssm_client = Aws::SSM::Client.new
end

#unset(variable:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/securenv/client.rb', line 48

def unset(variable:)
  parameter_name = parameter_name_for(variable)
  ssm_client.delete_parameter({
    name: parameter_name
  })
  parameter = Parameter.new(
   name: parameter_name,
   version: nil,
   value: nil
  )

  return parameter
rescue Aws::SSM::Errors::ParameterNotFound
  raise ParameterNotFoundError.new "#{variable} is not set for app: #{app} and stage: #{stage}"
end