Module: EasyCreds::Overlay

Defined in:
lib/easy_creds/overlay.rb

Class Method Summary collapse

Class Method Details

.apply!(app_or_creds, root = nil, env = nil) ⇒ void

This method returns an undefined value.

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

Accepts either a Rails application (the form the installer-generated initializer uses) or the explicit (creds, root, env) triple:

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

Parameters:

  • app_or_creds (#credentials, #root, #env)

    a Rails application, or the credentials object when root/env are supplied explicitly

  • root (String, Pathname, nil) (defaults to: nil)

    project root (omit when passing an app)

  • env (String, Symbol, nil) (defaults to: nil)

    environment name (omit when passing an app)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easy_creds/overlay.rb', line 23

def self.apply!(app_or_creds, root = nil, env = nil)
  if root.nil? && env.nil?
    app   = app_or_creds
    creds = app.credentials
    root  = app.root
    env   = app.env.to_s
  else
    creds = app_or_creds
    env   = env.to_s
  end

  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