Class: Muchkeys::ApplicationClient
- Inherits:
-
Object
- Object
- Muchkeys::ApplicationClient
- Defined in:
- lib/muchkeys/application_client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#config ⇒ Object
Returns the value of attribute config.
-
#key_validator ⇒ Object
Returns the value of attribute key_validator.
-
#secret_adapter ⇒ Object
Returns the value of attribute secret_adapter.
Instance Method Summary collapse
- #all(key_name) ⇒ Object
- #allow_unsafe_operation ⇒ Object
- #delete_key(key) ⇒ Object
- #each_path ⇒ Object
- #fetch_key(key_name, public_key: nil, private_key: nil) ⇒ Object
- #first(key_name) ⇒ Object
-
#initialize(config = Muchkeys.config) ⇒ ApplicationClient
constructor
A new instance of ApplicationClient.
- #known_keys ⇒ Object
- #search_paths(key_name = nil) ⇒ Object
- #set_app_key(key, value, type: nil, **options) ⇒ Object
- #set_key(key, value, scope: nil, type: nil, **options) ⇒ Object
- #set_shared_key(key, value, type: nil, **options) ⇒ Object
- #verify_keys(*required_keys) ⇒ Object
Constructor Details
#initialize(config = Muchkeys.config) ⇒ ApplicationClient
Returns a new instance of ApplicationClient.
14 15 16 17 18 19 |
# File 'lib/muchkeys/application_client.rb', line 14 def initialize(config = Muchkeys.config) @config = config @secret_adapter = Muchkeys::Secret.new(self) @key_validator = Muchkeys::KeyValidator.new(self) @client = Muchkeys::ConsulClient.new(self) end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
8 9 10 |
# File 'lib/muchkeys/application_client.rb', line 8 def client @client end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/muchkeys/application_client.rb', line 8 def config @config end |
#key_validator ⇒ Object
Returns the value of attribute key_validator.
8 9 10 |
# File 'lib/muchkeys/application_client.rb', line 8 def key_validator @key_validator end |
#secret_adapter ⇒ Object
Returns the value of attribute secret_adapter.
8 9 10 |
# File 'lib/muchkeys/application_client.rb', line 8 def secret_adapter @secret_adapter end |
Instance Method Details
#all(key_name) ⇒ Object
60 61 62 |
# File 'lib/muchkeys/application_client.rb', line 60 def all(key_name) search_paths(key_name).map { |path| fetch_key(path) }.compact end |
#allow_unsafe_operation ⇒ Object
21 22 23 24 25 26 |
# File 'lib/muchkeys/application_client.rb', line 21 def allow_unsafe_operation client.unsafe = true yield ensure client.unsafe = false end |
#delete_key(key) ⇒ Object
48 49 50 |
# File 'lib/muchkeys/application_client.rb', line 48 def delete_key(key) client.delete(key) end |
#each_path ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/muchkeys/application_client.rb', line 85 def each_path known_keys.each do |key| search_paths(key).each do |path| yield path if fetch_key(path) end end end |
#fetch_key(key_name, public_key: nil, private_key: nil) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/muchkeys/application_client.rb', line 68 def fetch_key(key_name, public_key: nil, private_key: nil) if is_secret?(key_name) fetch_secret_key(key_name, public_key, private_key) else fetch_plain_key(key_name) end end |
#first(key_name) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/muchkeys/application_client.rb', line 52 def first(key_name) # http://stackoverflow.com/questions/17853912/ruby-enumerables-is-there-a-detect-for-results-of-block-evaluation # weirdly, this seems to be the most straightforward method of doing this # without a monkey patch, as there is neither a core method that returns # the first non-nil result of evaluating the block, or a lazy compact search_paths(key_name).detect { |path| v = fetch_key(path) and break v } end |
#known_keys ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/muchkeys/application_client.rb', line 76 def known_keys @known_keys ||= application_search_paths .map { |path| client.get(path, recursive: true) } .compact .each_with_object([]) { |response, keys| keys << parse_recurse_response(response) } .flatten .uniq end |
#search_paths(key_name = nil) ⇒ Object
64 65 66 |
# File 'lib/muchkeys/application_client.rb', line 64 def search_paths(key_name = nil) application_search_paths.map { |p| [p, key_name].join('/') } end |
#set_app_key(key, value, type: nil, **options) ⇒ Object
28 29 30 |
# File 'lib/muchkeys/application_client.rb', line 28 def set_app_key(key, value, type: nil, **) set_key(key, value, scope: :application, type: type, **) end |
#set_key(key, value, scope: nil, type: nil, **options) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/muchkeys/application_client.rb', line 36 def set_key(key, value, scope: nil, type: nil, **) if scope && type key = construct_key_path(key, scope, type) || key end if type == :secret value = secret_adapter.encrypt_string(value.chomp, config.public_key).to_s end client.put(value, key, **) end |
#set_shared_key(key, value, type: nil, **options) ⇒ Object
32 33 34 |
# File 'lib/muchkeys/application_client.rb', line 32 def set_shared_key(key, value, type: nil, **) set_key(key, value, scope: :shared, type: type, **) end |
#verify_keys(*required_keys) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/muchkeys/application_client.rb', line 93 def verify_keys(*required_keys) if (required_keys - known_keys).any? # if there are any required keys (in the .env file) that are not known # about by the app's consul space, raise an error raise Muchkeys::KeyNotSet, "Consul isn't set with any keys for #{required_keys - known_keys}." end end |