Module: MuchKeys

Defined in:
lib/muchkeys.rb,
lib/muchkeys/cli.rb,
lib/muchkeys/errors.rb,
lib/muchkeys/version.rb,
lib/muchkeys/configuration.rb,
lib/muchkeys/key_validator.rb

Defined Under Namespace

Modules: BlankKey Classes: CLI, Configuration, KeyValidator, Rails, Secret

Constant Summary collapse

InvalidKey =
Class.new(StandardError)
CLIOptionsError =
Class.new(StandardError)
NoKeysSet =
Class.new(StandardError)
UnknownApplication =
Class.new(StandardError)
VERSION =
"0.5.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



21
22
23
# File 'lib/muchkeys.rb', line 21

def configuration
  @configuration
end

Class Method Details

.configureObject



23
24
25
26
27
28
# File 'lib/muchkeys.rb', line 23

def configure
  self.configuration ||= MuchKeys::Configuration.new
  if block_given?
    yield configuration
  end
end

.fetch_key(key_name, public_key: nil, private_key: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/muchkeys.rb', line 59

def fetch_key(key_name, public_key:nil, private_key:nil)
  return ENV[key_name] unless ENV[key_name].nil?

  if secret_adapter.is_secret?(key_name)
    raise InvalidKey unless validate_key_name(key_name)

    # configure automatic certificates
    public_key  = find_certfile_for_key(key_name) if public_key.nil?
    private_key = find_certfile_for_key(key_name) if private_key.nil?

    response = fetch_secret_key(key_name, public_key, private_key)
  else
    response = fetch_plain_key(key_name)
  end

  # otherwise, we got consul data so return that
  response
end

.find_first(key_name) ⇒ Object

this is the entry point to the ENV-like object that devs will use



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/muchkeys.rb', line 38

def find_first(key_name)
  unless MuchKeys.configuration.search_paths
    raise MuchKeys::UnknownApplication, "Can't detect app name and application_name isn't set." if !application_name
  end

  consul_paths_to_search = search_order(application_name, key_name)

  # search consul in a specific order until we find something
  response = nil
  consul_paths_to_search.detect do |consul_path|
    response = fetch_key(consul_path)
    response if response != MuchKeys::BlankKey
  end

  if response == BlankKey
    raise MuchKeys::NoKeysSet, "Bailing.  Consul isn't set with any keys for #{key_name}."
  end

  response
end

.populate_environment!(*keys) ⇒ Object



30
31
32
33
34
35
# File 'lib/muchkeys.rb', line 30

def populate_environment!(*keys)
  keys.each do |key|
    ENV[key] = MuchKeys.find_first(key)
  end
  nil
end

.search_order(application_name, key_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/muchkeys.rb', line 78

def search_order(application_name, key_name)
  if MuchKeys.configuration.search_paths
    search_paths = MuchKeys.configuration.search_paths.collect do |path|
      "#{path}/#{key_name}"
    end
  else
    search_paths = [
      "git/#{application_name}/secrets/#{key_name}",
      "git/#{application_name}/config/#{key_name}",
      "git/shared/secrets/#{key_name}",
      "git/shared/config/#{key_name}"
    ]
  end

  search_paths
end