Class: Aspera::Secret::Store::HashicorpVault
- Defined in:
- lib/aspera/secret/store/hashicorp_vault.rb
Overview
Manage secrets in a Hashicorp Vault
Constant Summary collapse
- PARAMETERS =
Reference:
secret://<store>/<mount>/<path>#<field>in a KV v2 secrets engine, options:version=<n>,mount=<mount>(when the mount contains/). Authentication bytokenorapprole: secrets are the value, or@file:<path>(Bootstrap). %i[url token approle namespace ca_file allowed_prefixes].freeze
- REFERENCE_OPTIONS =
%w[version mount].freeze
- LIST_OPTIONS =
Options of a listing of a folder (
list_folder) %w[mount].freeze
Constants inherited from Base
Class Method Summary collapse
-
.error_reason(error) ⇒ Object
Errors of gem
vault: code and errors of response, or address of server.
Instance Method Summary collapse
- #all ⇒ Object
- #check_reference(ref) ⇒ Object
- #delete(label:) ⇒ Object
- #get(label:, exception: true) ⇒ Object
- #info ⇒ Object
-
#initialize(url:, token: nil, approle: nil, namespace: nil, ca_file: nil, allowed_prefixes: nil) ⇒ HashicorpVault
constructor
One of
tokenorapproleauthenticates the client. -
#list_folder(path, options = {}) ⇒ Array<Hash>
Secrets and folders of a folder of a KV v2 secrets engine that references can read (
allowed_prefixes): list of its metadata, with capabilityliston<mount>/metadata/<path>. -
#read(path:, mount: 'secret', version: nil) ⇒ Hash?
Read a secret of a KV v2 secrets engine.
-
#read_fields(ref) ⇒ Object
Field names of a secret, version and time of creation: the secret is read, its values are not returned.
-
#read_reference(ref) ⇒ Object
The field is required.
-
#set(options) ⇒ Object
Set a secret.
-
#write_reference(ref, fields) ⇒ Hash
Write a new secret (KV v2, check-and-set: refuses if the path already exists).
Methods inherited from Base
arguments, #assert_new_label, check_parameters, #ids, #validate_set
Constructor Details
#initialize(url:, token: nil, approle: nil, namespace: nil, ca_file: nil, allowed_prefixes: nil) ⇒ HashicorpVault
One of token or approle authenticates the client
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 91 def initialize(url:, token: nil, approle: nil, namespace: nil, ca_file: nil, allowed_prefixes: nil) super() Aspera.assert_type(url, String, type: ParameterError) { 'vault url' } Aspera.assert([token, approle].compact.size.eql?(1), type: ParameterError) { 'vault: one of token or approle is required' } Aspera.assert(allowed_prefixes.nil? || (allowed_prefixes.is_a?(Array) && allowed_prefixes.all?(String)), type: ParameterError) { 'vault allowed_prefixes: list of strings' } @allowed_prefixes = allowed_prefixes @token = Bootstrap.new('token', token) unless token.nil? # Modification time of file of token when read, `nil` before @token_mtime = nil @approle = approle.nil? ? nil : approle_parameters(approle) # Monotonic time of renewal of the AppRole token, `nil` before login @renew_at = nil @auth_lock = Mutex.new = {address: url} [:token] = @token.value unless @token.nil? || @token.file? [:namespace] = namespace unless namespace.nil? [:ssl_ca_cert] = ca_file unless ca_file.nil? @client = Client.new() end |
Class Method Details
.error_reason(error) ⇒ Object
Errors of gem vault: code and errors of response, or address of server
40 41 42 43 44 45 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 40 def error_reason(error) case error when ::Vault::HTTPError then "Vault: #{error.code}#{" #{error.errors.join(', ')}" unless error.errors.empty?}" when ::Vault::HTTPConnectionError then "Vault: cannot connect to #{error.address}: #{error.original_exception.}" end end |
Instance Method Details
#all ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 119 def all = STORE_PATH.sub('/data/', '/metadata/') labels = call { |client| client.logical.list() } return labels.filter_map do |label| # KV v2: deleted secrets are still listed in metadata get(label: label, exception: false)&.merge(label: label) end end |
#check_reference(ref) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 174 def check_reference(ref) super mount, path = mount_and_path(ref) full_path = "#{mount}/#{path}" raise Error, "invalid path: #{full_path}" if full_path.split('/', -1).any? { |s| INVALID_SEGMENTS.include?(s) } raise Error, "path not allowed: #{full_path}" unless allowed?(full_path) reference_version(ref) nil end |
#delete(label:) ⇒ Object
153 154 155 156 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 153 def delete(label:) path = path(label) call { |client| client.logical.delete(path) } end |
#get(label:, exception: true) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 143 def get(label:, exception: true) # KV v2: a deleted secret may have metadata but no data data = call { |client| client.logical.read(path(label)) }&.data&.[](:data) if data.nil? raise NotFound, "Secret '#{label}' not found" if exception return end return data end |
#info ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 111 def info { url: @client.address, namespace: @client.namespace, auth: auth_method }.compact end |
#list_folder(path, options = {}) ⇒ Array<Hash>
Secrets and folders of a folder of a KV v2 secrets engine that references can read (allowed_prefixes): list of its metadata,
with capability list on <mount>/metadata/<path>
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 198 def list_folder(path, = {}) unsupported = .keys - LIST_OPTIONS raise Error, "unsupported options: #{unsupported.join(', ')}" unless unsupported.empty? folder = path.delete_suffix('/') raise Error, 'path shall be <mount>[/<path>]' if folder.empty? raise Error, "invalid path: #{path}" if folder.split('/', -1).any? { |s| INVALID_SEGMENTS.include?(s) } mount = ['mount'] || folder.split('/').first raise Error, "path shall start with mount #{mount}/" unless folder.eql?(mount) || folder.start_with?("#{mount}/") raise Error, "path not allowed: #{folder}/" unless reachable?("#{folder}/") sub = folder.delete_prefix(mount).delete_prefix('/') keys = call { |client| client.kv(mount).list(sub.empty? ? '' : "#{sub}/") } entries = keys.filter_map do |key| full = "#{folder}/#{key}" is_folder = key.end_with?('/') {name: key.delete_suffix('/'), folder: is_folder} if is_folder ? reachable?(full) : allowed?(full) end entries.sort_by { |entry| [entry[:name], entry[:folder] ? 0 : 1] } end |
#read(path:, mount: 'secret', version: nil) ⇒ Hash?
Read a secret of a KV v2 secrets engine
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 163 def read(path:, mount: 'secret', version: nil) secret = call { |client| client.kv(mount).read(path, version) } return if secret.nil? || secret.data.nil? = secret. || {} return { data: secret.data, version: [:version], created_time: [:created_time].nil? ? nil : Time.iso8601([:created_time]) } end |
#read_fields(ref) ⇒ Object
Field names of a secret, version and time of creation: the secret is read, its values are not returned
218 219 220 221 222 223 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 218 def read_fields(ref) mount, path = mount_and_path(ref) secret = read(path: path, mount: mount, version: reference_version(ref)) raise NotFound, 'not found' if secret.nil? {fields: field_names(secret[:data]), version: secret[:version], updated_time: secret[:created_time]} end |
#read_reference(ref) ⇒ Object
The field is required
185 186 187 188 189 190 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 185 def read_reference(ref) mount, path = mount_and_path(ref) secret = read(path: path, mount: mount, version: reference_version(ref)) raise NotFound, 'not found' if secret.nil? {value: field_value(secret[:data], ref), version: secret[:version], updated_time: secret[:created_time]} end |
#set(options) ⇒ Object
Set a secret
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 130 def set() validate_set() label = .fetch(:label) assert_new_label(label) data = { username: [:username], password: [:password], url: [:url], description: [:description] }.compact call { |client| client.logical.write(path(label), data: data) } end |
#write_reference(ref, fields) ⇒ Hash
Write a new secret (KV v2, check-and-set: refuses if the path already exists)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/aspera/secret/store/hashicorp_vault.rb', line 230 def write_reference(ref, fields) mount, path = mount_and_path(ref) symbol_fields = fields.transform_keys(&:to_sym) response = call do |client| client.logical.write( "#{mount}/data/#{path}", options: {cas: 0}, data: symbol_fields ) end = response.data[:metadata] || response.data version = [:version] created_time = [:created_time] {version: version, updated_time: created_time.nil? ? nil : Time.iso8601(created_time)} rescue ::Vault::HTTPClientError => e raise Error, "secret exists: #{mount}/#{path}" if e.code == 400 raise end |