class Uc3Ssm::ConfigResolver

This code is designed to mimic github.com/terrywbrady/yaml/blob/master/config.yml

Public Class Methods

new(**options) click to toggle source

def_value - value to return if no default is configured.

This prevents an exception from being thrown.

region - region to perform the SSM lookup.

Not needed if AWS_REGION is configured.

ssm_root_path - prefix to apply to all key lookups.

This allows the same config to be used in prod and non prod envs.
Can be a list of path strings separated by ':', in which case the
we search for keys under each path sequentially, returning the value
of the first matching key found.  Example:
  ssm_root_path: '/prog/srvc/subsrvc/env:/prod/srvc/subsrvc/default'
# File lib/uc3-ssm.rb, line 28
def initialize(**options)
  # see issue #9 - @regex should not be a user definable option
  dflt_regex = '^(.*)\\{!(ENV|SSM):\\s*([^\\}!]*)(!DEFAULT:\\s([^\\}]*))?\\}(.*)$'
  @regex = options.fetch(:regex, dflt_regex)

  # see issue #10 - @ssm_skip_resolution only settable as ENV var
  @ssm_skip_resolution = ENV.key?('SSM_SKIP_RESOLUTION')
  # dflt_ssm_skip_resolution = ENV['SSM_SKIP_RESOLUTION'] || false
  # @ssm_skip_resolution = options.fetch(:ssm_skip_resolution, dflt_ssm_skip_resolution)

  dflt_region = ENV['AWS_REGION'] || 'us-west-2'
  dflt_ssm_root_path = ENV['SSM_ROOT_PATH'] || ''

  @region = options.fetch(:region, dflt_region)
  @ssm_root_path = sanitize_root_path(options.fetch(:ssm_root_path, dflt_ssm_root_path))
  @def_value = options.fetch(:def_value, '')
  @logger = options.fetch(:logger, Logger.new($stdout))
  @client = Aws::SSM::Client.new(region: @region) unless @ssm_skip_resolution
rescue Aws::Errors::MissingRegionError
  raise ConfigResolverError, 'No AWS region defined. Either set ENV["AWS_REGION"] or pass in `region: [region]`'
end

Public Instance Methods

parameter_for_key(key) click to toggle source

Retrieve a value for a single key

# File lib/uc3-ssm.rb, line 99
def parameter_for_key(key)
  return key if @ssm_skip_resolution

  keylist = sanitize_parameter_key(key)
  keylist.each do |k|
    val = retrieve_ssm_value(k)
    return val unless val.nil?
  end
end
parameters_for_path(**options) click to toggle source

Retrieve all key+values for a path (using the ssm_root_path if none is specified) See docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/SSM/Client.html#get_parameters_by_path-instance_method details on available ‘options` rubocop:disable Metrics/MethodLength

# File lib/uc3-ssm.rb, line 79
def parameters_for_path(**options)
  return [] if @ssm_skip_resolution

  param_list = []
  path_list = options[:path].nil? ? @ssm_root_path : sanitize_parameter_key(options[:path])
  path_list.each do |root_path|
    options[:path] = root_path
    param_list += fetch_param_list(**options)
  rescue Aws::SSM::Errors::ParameterNotFound
    @logger.debug "ParameterNotFound for path '#{root_path}' in parameters_by_path"
    next
  end

  param_list
rescue Aws::Errors::MissingCredentialsError
  raise ConfigResolverError, 'No AWS credentials available. Make sure the server has access to the aws-sdk'
end
resolve_file_values(file:, resolve_key: nil, return_key: nil) click to toggle source

file - config file to process resolve_key - partially process config file using this as a root key - use this to prevent unnecessary lookups return_key - return values for a specific hash key - use this to filter the return object

# File lib/uc3-ssm.rb, line 54
def resolve_file_values(file:, resolve_key: nil, return_key: nil)
  raise ConfigResolverError, "Config file #{file} not found!" unless File.exist?(file)
  raise ConfigResolverError, "Config file #{file} is empty!" unless File.size(file).positive?

  config = YAML.safe_load_file(file, aliases: true)
  resolve_hash_values(hash: config, resolve_key: resolve_key, return_key: return_key)
end
resolve_hash_values(hash:, resolve_key: nil, return_key: nil) click to toggle source

hash - config hash to process resolve_key - partially process config hash using this as a root key - use this to prevent unnecessary lookups return_key - return values for a specific hash key - use this to filter the return object

# File lib/uc3-ssm.rb, line 65
def resolve_hash_values(hash:, resolve_key: nil, return_key: nil)
  if resolve_key && hash.key?(resolve_key)
    rethash = hash.clone
    rethash[resolve_key] = resolve_value(rethash[resolve_key])
  else
    rethash = resolve_value(hash)
  end
  return_hash(rethash, return_key)
end