Class: Aspera::Secret::Store::EncryptedHash

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/secret/store/encrypted_hash.rb

Overview

Manage secrets in a simple Hash

Defined Under Namespace

Classes: OsslCipher

Constant Summary collapse

PARAMETERS =
%i[file password].freeze

Constants inherited from Base

Base::CONTENT_KEYS, Base::REFERENCE_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assert_new_label, check_parameters, #check_reference, error_reason, #ids, #list_folder, #validate_set

Constructor Details

#initialize(file:, password:) ⇒ EncryptedHash

The vault file is read when used, and again when it, or the file of the password, is modified. It is created by the first set.

Parameters:

  • file (String) —

    Vault file

  • password (String, Bootstrap) —

    Password of vault file: the value, or a Bootstrap



44
45
46
47
48
49
50
51
52
53
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 44

def initialize(file:, password:)
  super()
  Aspera.assert_type(file, String) { 'path to vault file' }
  @path = file
  @password = password.is_a?(Bootstrap) ? password : Bootstrap.new('password', password)
  # Modification times of vault file (`nil` if absent) and file of password, when read, `nil` before
  @mtimes = nil
  @lock = Mutex.new
  Log.dump(:vault_file, File.expand_path(@path))
end

Class Method Details

.arguments(params) ⇒ Object

password is the value, or @file:<path>



35
36
37
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 35

def arguments(params)
  params.merge(password: Bootstrap.new('password', params[:password]))
end

Instance Method Details

#all ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 61

def all
  result = []
  secrets.each do |label, values|
    normal = values.symbolize_keys
    normal[:label] = label
    CONTENT_KEYS.each { |k| normal[k] = '' unless normal.key?(k) }
    result.push(normal)
  end
  return result
end

#change_password(password) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 99

def change_password(password)
  secrets
  # Generate new KDF params so a password change also re-salts the vault
  # (and migrates a legacy vault to PBKDF2)
  @legacy_key = false
  @kdf_params = nil
  @cipher = cipher(password)
  @password = Bootstrap.new('password', password)
  save
end

#delete(label:) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 91

def delete(label:)
  with_lock do
    Aspera.assert(secrets_unlocked.key?(label), type: NotFound) { "Label not found: #{label}" }
    secrets_unlocked.delete(label)
    save
  end
end

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



84
85
86
87
88
89
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 84

def get(label:, exception: true)
  Aspera.assert(secrets.key?(label), type: NotFound) { "Label not found: #{label}" } if exception
  result = secrets[label].clone
  result[:label] = label if result.is_a?(Hash)
  return result
end

#info ⇒ Object



55
56
57
58
59
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 55

def info
  return {
    file: @path
  }
end

#read_fields(ref) ⇒ Object

Modification time of vault file

Raises:



117
118
119
120
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 117

def read_fields(ref)
  raise Error, "no vault file: #{@path}" unless File.exist?(@path)
  super.merge(updated_time: @mtimes&.first)
end

#read_reference(ref) ⇒ Object

Modification time of vault file

Raises:



111
112
113
114
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 111

def read_reference(ref)
  raise Error, "no vault file: #{@path}" unless File.exist?(@path)
  super.merge(updated_time: @mtimes&.first)
end

#set(options) ⇒ Object

set a secret

Parameters:

  • options (Hash) —

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



74
75
76
77
78
79
80
81
82
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 74

def set(options)
  validate_set(options)
  label = options.delete(:label)
  with_lock do
    assert_new_label(label)
    secrets_unlocked[label] = options.symbolize_keys
    save
  end
end

#write_reference(ref, fields) ⇒ Hash

Write a new secret by reference: label = path of reference, fields within CONTENT_KEYS

Parameters:

Returns:

  • (Hash) —

    version: nil, updated_time: nil

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 126

def write_reference(ref, fields)
  symbol_fields = fields.transform_keys(&:to_sym)
  unsupported = symbol_fields.keys - CONTENT_KEYS
  raise Error, "unsupported fields: #{unsupported.join(', ')}, use #{CONTENT_KEYS.join(', ')}" unless unsupported.empty?
  with_lock do
    assert_new_label(ref.path)
    secrets_unlocked[ref.path] = symbol_fields
    save
  end
  {version: nil, updated_time: nil}
end