class Chambermaid::ParameterStore

ParameterStore instances fetch all parameters under a namespace/path from AWS SSM

@note AWS authentication requires configuration via ENV (IAM credentials/STS)

Public Class Methods

load!(path:) click to toggle source

Create a ParameterStore and fetch from AWS SSM immediately

@see Chambermaid::ParameterStore#load!

@return [Chambermaid::ParameterStore]

# File lib/chambermaid/parameter_store.rb, line 41
def self.load!(path:)
  store = new(path: path)
  store.load!
  store
end
new(path:) click to toggle source

@param [String] path

# File lib/chambermaid/parameter_store.rb, line 10
def initialize(path:)
  @path = path
end

Public Instance Methods

load!() click to toggle source

Fetch and decrypt all parameters selected by a namespace/path string

@return [Boolean]

# File lib/chambermaid/parameter_store.rb, line 17
def load!
  fetch_ssm_params!
end
loaded?() click to toggle source

Returns true if parameters have been fetched from AWS SSM

@return [Boolean]

# File lib/chambermaid/parameter_store.rb, line 32
def loaded?
  !@params_list.empty?
end
params() click to toggle source

ENV formatted Hash of parameters loaded from AWS SSM

@return [Hash]

# File lib/chambermaid/parameter_store.rb, line 50
def params
  @params ||= @param_list.map { |p|
    [p.name.split("/").last.upcase, p.value]
  }.to_h
end
Also aliased as: to_h
reload!() click to toggle source

Clear cached parameters and re-fetch parameters from AWS SSM

@return [Boolean]

# File lib/chambermaid/parameter_store.rb, line 24
def reload!
  clear_params!
  fetch_ssm_params!
end
to_h()
Alias for: params

Private Instance Methods

clear_params!() click to toggle source
# File lib/chambermaid/parameter_store.rb, line 90
def clear_params!
  @params = nil
  @params_list = []
end
client() click to toggle source
# File lib/chambermaid/parameter_store.rb, line 60
def client
  @client ||= Aws::SSM::Client.new
end
fetch_ssm_param_batch!(next_token = nil) click to toggle source
# File lib/chambermaid/parameter_store.rb, line 81
def fetch_ssm_param_batch!(next_token = nil)
  client.get_parameters_by_path(
    path: @path,
    with_decryption: true,
    recursive: true,
    next_token: next_token
  )
end
fetch_ssm_params!() click to toggle source
# File lib/chambermaid/parameter_store.rb, line 64
def fetch_ssm_params!
  Chambermaid.logger.debug("fetching AWS SSM parameters from `#{@path}`")
  @param_list = []
  response = nil
  loop do
    response = fetch_ssm_param_batch!(response&.next_token)
    @param_list.concat(response.parameters)

    if response.next_token
      Chambermaid.logger.debug("response.next_token found, continuing fetch")
    else
      break
    end
  end
  Chambermaid.logger.debug("fetched #{@param_list.size} parameters from `#{@path}`")
end