Class: Aspera::Secret::Store::Keeper

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

Overview

Manage secrets in Keeper Secrets Manager (Keeper Vault), using the Ruby SDK https://docs.keeper.io/keeperpam/secrets-manager/overview

A secret is a record of the Keeper vault shared with the application: its label is its title (or its UID). Reference: secret://<store>/<title or uid>#<field>, field password by default. Fields are those of the record: standard fields by type (login, password, url...), custom fields by label, and notes. username is an alias of login.

Constant Summary collapse

PARAMETERS =

config: configuration of the client device (base64 or JSON), the value, or @file:<path> (Bootstrap) folder_uid: shared folder where set creates records (not needed to read)

%i[config folder_uid].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, #list_folder, #validate_set, #write_reference

Constructor Details

#initialize(config:, folder_uid: nil) ⇒ Keeper

Returns a new instance of Keeper.

Parameters:

  • config (String, Bootstrap) —

    Configuration of the client device, from the one-time access token (ksm profile export)

  • folder_uid (String, nil) (defaults to: nil) —

    UID of the shared folder where set creates records



39
40
41
42
43
44
45
46
# File 'lib/aspera/secret/store/keeper.rb', line 39

def initialize(config:, folder_uid: nil)
  super()
  Aspera.assert_type(folder_uid, String, type: ParameterError) { 'keeper folder_uid' } unless folder_uid.nil?
  config = Bootstrap.new('config', config) unless config.is_a?(Bootstrap)
  @folder_uid = folder_uid
  # The SDK logs on STDOUT by default: do not mix with results
  @client = ::KeeperSecretsManager.from_config(config.value, logger: ::Logger.new($stderr))
end

Class Method Details

.arguments(params) ⇒ Object



32
33
34
# File 'lib/aspera/secret/store/keeper.rb', line 32

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

Instance Method Details

#all ⇒ Object



55
56
57
# File 'lib/aspera/secret/store/keeper.rb', line 55

def all
  @client.get_secrets.map { |record| secret(record) }
end

#delete(label:, id: nil) ⇒ Object



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

def delete(label:, id: nil)
  @client.delete_secret(find(id || label).uid)
  nil
end

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



78
79
80
81
82
# File 'lib/aspera/secret/store/keeper.rb', line 78

def get(label:, id: nil, exception: true)
  record = find(id || label, exception: exception)
  return if record.nil?
  secret(record)
end

#ids ⇒ Object



59
60
61
# File 'lib/aspera/secret/store/keeper.rb', line 59

def ids
  @client.get_secrets.map { |record| {id: record.uid, label: record.title} }
end

#info ⇒ Object



48
49
50
51
52
53
# File 'lib/aspera/secret/store/keeper.rb', line 48

def info
  {
    hostname:   @client.hostname,
    folder_uid: @folder_uid
  }.compact
end

#read_fields(ref) ⇒ Object



95
96
97
98
# File 'lib/aspera/secret/store/keeper.rb', line 95

def read_fields(ref)
  record = find(ref.path)
  {fields: field_names(record_values(record).except('title')), version: record.revision, updated_time: nil}
end

#read_reference(ref) ⇒ Object

The record is found by title or UID, the field is password by default



90
91
92
93
# File 'lib/aspera/secret/store/keeper.rb', line 90

def read_reference(ref)
  record = find(ref.path)
  {value: field_value(record_values(record), ref, default: DEFAULT_FIELD), version: record.revision, updated_time: nil}
end

#set(options) ⇒ Object

Create a record of type login in the shared folder folder_uid

Parameters:

  • options (Hash) —

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aspera/secret/store/keeper.rb', line 65

def set(options)
  validate_set(options)
  Aspera.assert(!@folder_uid.nil?, type: ParameterError) { 'keeper: parameter folder_uid is required to create a secret' }
  assert_new_label(options[:label])
  fields = {login: :username, password: :password, url: :url}.filter_map do |type, key|
    {type: type.to_s, value: [options[key]]} if options[key]
  end
  record = {type: 'login', title: options[:label], fields: fields}
  record[:notes] = options[:description] if options[:description]
  @client.create_secret(record, ::KeeperSecretsManager::Dto::CreateOptions.new(folder_uid: @folder_uid))
  nil
end