Class: Aspera::Keychain::HashicorpVault

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/keychain/hashicorp_vault.rb

Overview

Manage secrets in a Hashicorp Vault

Constant Summary

Constants inherited from Base

Base::CONTENT_KEYS

Instance Method Summary collapse

Methods inherited from Base

#validate_set

Constructor Details

#initialize(url:, token:) ⇒ HashicorpVault

Returns a new instance of HashicorpVault.



17
18
19
20
21
22
23
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 17

def initialize(url:, token:)
  super()
  Vault.configure do |config|
    config.address = url
    config.token = token
  end
end

Instance Method Details

#delete(label:) ⇒ Object



62
63
64
65
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 62

def delete(label:)
  path = path(label)
  Vault.logical.delete(path)
end

#get(label:, exception: true) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 53

def get(label:, exception: true)
  secret = Vault.logical.read(path(label))
  if secret.nil?
    raise "Secret '#{label}' not found" if exception
    return
  end
  return secret.data[:data]
end

#infoObject



25
26
27
28
29
30
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 25

def info
  {
    url:      Vault.address,
    password: Vault.auth_token
  }
end

#listObject



32
33
34
35
36
37
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 32

def list
   = STORE_PATH.sub('/data/', '/metadata/')
  return Vault.logical.list().filter_map do |label|
    get(label: label).merge(label: label)
  end
end

#set(options) ⇒ Object

Set a secret

Parameters:

  • options (Hash)

    with keys :label, :username, :password, :url, :description



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aspera/keychain/hashicorp_vault.rb', line 41

def set(options)
  validate_set(options)
  label = options.fetch(:label)
  data = {
    username:    options[:username],
    password:    options[:password],
    url:         options[:url],
    description: options[:description]
  }.compact
  Vault.logical.write(path(label), data: data)
end