Module: EasyCreds::Overlay

Defined in:
lib/easy_creds/overlay.rb

Class Method Summary collapse

Class Method Details

.apply!(creds, root, env) ⇒ void

This method returns an undefined value.

Merge config/credentials/<env>_local.yml.enc on top of the base credentials.

Call it from a Rails initializer with the credentials object, project root, and environment — the values are pulled straight off the Rails globals:

EasyCreds::Overlay.apply!(Rails.application.credentials, Rails.root, Rails.env)

Parameters:

  • creds (#public_send)

    the credentials object to mutate (e.g. Rails.application.credentials)

  • root (String, Pathname)

    project root (Rails.root)

  • env (String, Symbol)

    environment name (Rails.env)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easy_creds/overlay.rb', line 22

def self.apply!(creds, root, env)
  env = env.to_s
  enc = Pathname.new(root).join("config/credentials/#{env}_local.yml.enc")
  key = Pathname.new(root).join("config/credentials/#{env}_local.key")

  return unless enc.exist? && key.exist?

  overlay = ActiveSupport::EncryptedConfiguration.new(
    config_path: enc.to_s,
    key_path: key.to_s,
    env_key: 'RAILS_MASTER_KEY_LOCAL_UNUSED',
    raise_if_missing_key: false
  ).config.deep_symbolize_keys

  overlay.each do |top_key, value|
    merge_key(creds, top_key, value)
  end
rescue StandardError => e
  warn "[easy_creds_overlay] skipped: #{e.class}: #{e.message}"
end