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, Secret

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



17
18
19
# File 'lib/muchkeys.rb', line 17

def configuration
  @configuration
end

Class Method Details

.configureObject



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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/muchkeys.rb', line 48

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/muchkeys.rb', line 27

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

.search_order(application_name, key_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/muchkeys.rb', line 67

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