Class: Vaulty::Catacomb

Inherits:
Object
  • Object
show all
Defined in:
lib/vaulty/catacomb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Catacomb

Returns a new instance of Catacomb.

Parameters:

  • the (String)

    path to be used



5
6
7
# File 'lib/vaulty/catacomb.rb', line 5

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/vaulty/catacomb.rb', line 3

def path
  @path
end

Class Method Details

.delete(path) ⇒ Boolean

Wrapper around Vault logical delete

Parameters:

  • path (String)

Returns:

  • (Boolean)


108
109
110
# File 'lib/vaulty/catacomb.rb', line 108

def delete(path)
  Vault.logical.delete(path)
end

.list(path) ⇒ Array

Wrapper around Vault logical list

Parameters:

  • path (String)

Returns:

  • (Array)

    secrets



98
99
100
101
102
# File 'lib/vaulty/catacomb.rb', line 98

def list(path)
  list = Vault.logical.list(path)
  cleaned_list = Array(list).map { |folder| folder.delete('/') }
  cleaned_list.reject(&:empty?).uniq
end

.read(path) ⇒ Hash

Wrapper around Vault logical read, return always a hash

Parameters:

  • path (String)

Returns:

  • (Hash)


79
80
81
82
# File 'lib/vaulty/catacomb.rb', line 79

def read(path)
  secret = Vault.logical.read(path)
  secret ? secret.data : {}
end

.write(path, data) ⇒ Hash

Wrapper around Vault logical write

Parameters:

  • path (String)
  • data (Hash)

Returns:

  • (Hash)

    secret



89
90
91
92
# File 'lib/vaulty/catacomb.rb', line 89

def write(path, data)
  secret = Vault.logical.write(path, data)
  secret.is_a?(Vault::Secret) ? secret.data : {}
end

Instance Method Details

#deleteTrue

Deletes a path (with all values) from Vault

Parameters:

  • confirm (Boolean)
  • msg (String)

Returns:

  • (True)


69
70
71
72
# File 'lib/vaulty/catacomb.rb', line 69

def delete
  self.class.delete(path)
  @read = nil
end

#key?(key) ⇒ Boolean

Checks if the key is found in the data

Parameters:

  • key (String)

Returns:

  • (Boolean)


13
14
15
# File 'lib/vaulty/catacomb.rb', line 13

def key?(key)
  read.key?(key.to_sym)
end

#keys?(keys) ⇒ Boolean

Checks if any of the provided keys are found in the data

Parameters:

  • keys (Array<String>)

Returns:

  • (Boolean)


21
22
23
# File 'lib/vaulty/catacomb.rb', line 21

def keys?(keys)
  matching_keys(keys).any?
end

#matching_keys(keys) ⇒ Array<String>

Returns matching keys

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<String>)

    keys that matched



29
30
31
# File 'lib/vaulty/catacomb.rb', line 29

def matching_keys(keys)
  Array(keys).select { |key| key?(key) }
end

#merge(data) ⇒ Object

Merges the data to Vault with an optional confirm/msg

Parameters:

  • data (Hash)


44
45
46
47
# File 'lib/vaulty/catacomb.rb', line 44

def merge(data)
  current = read
  write(current.merge(data))
end

#readHash

Reads the data from Vault, always returns an Hash

Returns:

  • (Hash)


52
53
54
# File 'lib/vaulty/catacomb.rb', line 52

def read
  @read ||= self.class.read(path)
end

#reloadHash

Clears the cached result from read and returns the result

Returns:

  • (Hash)


59
60
61
62
# File 'lib/vaulty/catacomb.rb', line 59

def reload
  @read = nil
  read
end

#write(data) ⇒ Object

Writes data to Vault with an optional confirm/msg

Parameters:

  • data (Hash)


36
37
38
39
# File 'lib/vaulty/catacomb.rb', line 36

def write(data)
  self.class.write(path, data)
  reload
end