Class: PatientHttp::SecretManager
- Inherits:
-
Object
- Object
- PatientHttp::SecretManager
- Defined in:
- lib/patient_http/secret_manager.rb
Overview
Resolves SecretReference values into their actual secret values when a request is sent by the processor.
A SecretManager is built from the secrets registered on the Configuration.
Defined Under Namespace
Classes: SecretNotFoundError
Instance Method Summary collapse
-
#include?(name) ⇒ Boolean
Check if a secret name is registered in the static registry.
-
#initialize(secrets: {}) ⇒ SecretManager
constructor
Initialize a new SecretManager.
-
#resolve(name) ⇒ String
Resolve a secret by name.
-
#resolve_headers(headers) ⇒ Hash?
Resolve any secret references in a headers hash, returning a new hash.
-
#resolve_params(params) ⇒ Hash?
Resolve any secret references in a params hash, returning a new hash.
-
#resolve_url(url, secret_params) ⇒ String
Append resolved secret params to a URL’s query string.
Constructor Details
#initialize(secrets: {}) ⇒ SecretManager
Initialize a new SecretManager.
19 20 21 |
# File 'lib/patient_http/secret_manager.rb', line 19 def initialize(secrets: {}) @secrets = secrets || {} end |
Instance Method Details
#include?(name) ⇒ Boolean
Check if a secret name is registered in the static registry.
27 28 29 |
# File 'lib/patient_http/secret_manager.rb', line 27 def include?(name) @secrets.include?(name.to_s) end |
#resolve(name) ⇒ String
Resolve a secret by name.
The static registry is checked first; if the registered value responds to #call it is invoked with the name. If the name is not in the registry, an error is raised.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/patient_http/secret_manager.rb', line 39 def resolve(name) name = name.to_s unless @secrets.include?(name) raise SecretNotFoundError.new("No secret registered for #{name.inspect}") end value = @secrets[name] value = value.call(name) if value.respond_to?(:call) value&.to_s end |
#resolve_headers(headers) ⇒ Hash?
Resolve any secret references in a headers hash, returning a new hash.
55 56 57 |
# File 'lib/patient_http/secret_manager.rb', line 55 def resolve_headers(headers) resolve_values(headers) end |
#resolve_params(params) ⇒ Hash?
Resolve any secret references in a params hash, returning a new hash.
63 64 65 |
# File 'lib/patient_http/secret_manager.rb', line 63 def resolve_params(params) resolve_values(params) end |
#resolve_url(url, secret_params) ⇒ String
Append resolved secret params to a URL’s query string.
72 73 74 75 76 77 78 79 |
# File 'lib/patient_http/secret_manager.rb', line 72 def resolve_url(url, secret_params) return url if secret_params.nil? || secret_params.empty? serialized_params = URI.encode_www_form(resolve_params(secret_params)) uri = URI(url) uri.query = [uri.query, serialized_params].compact.reject(&:empty?).join("&") uri.to_s end |