Class: Aspera::Secret::Store::MacosSystem

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

Overview

Vault backend implementation wrapping macOS system keychain

Constant Summary collapse

PARAMETERS =
%i[name].freeze

Constants inherited from Base

Base::CONTENT_KEYS, Base::REFERENCE_OPTIONS

Instance Method Summary collapse

Methods inherited from Base

arguments, #assert_new_label, check_parameters, #check_reference, error_reason, #ids, #list_folder, #read_fields, #read_reference, #validate_set, #write_reference

Constructor Details

#initialize(name: nil) ⇒ MacosSystem

Returns a new instance of MacosSystem.

Parameters:

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

    Specific keychain name, or nil for default

Raises:



223
224
225
226
227
228
229
# File 'lib/aspera/secret/store/macos_security.rb', line 223

def initialize(name: nil)
  super()
  raise Error, 'type system: only on macOS' unless Environment.instance.os.eql?(Environment::OS_MACOS)
  @keychain_name = name.nil? ? 'default keychain' : name
  @keychain = name.nil? ? MacosSecurity::Keychain.default : MacosSecurity::Keychain.by_name(name)
  Aspera.assert(!@keychain.nil?, type: Error) { "no such keychain #{name}" }
end

Instance Method Details

#all ⇒ Object

Overview of all secrets (not supported on macOS due to OS restrictions)

Raises:

  • (Error) —

    Always raised



240
241
242
243
# File 'lib/aspera/secret/store/macos_security.rb', line 240

def all
  # the only way to list is `dump-keychain` which triggers security alert
  raise Error, 'overview not implemented, use macos keychain app'
end

#delete(label:) ⇒ Hash?

Delete a secret by label

Parameters:

  • label (String) —

    Secret label identifier

Returns:

  • (Hash, nil) —

    Result



277
278
279
# File 'lib/aspera/secret/store/macos_security.rb', line 277

def delete(label:)
  @keychain.password(:delete, :generic, label: label)
end

#get(label:, exception: true) ⇒ Hash?

Retrieve a secret by label

Parameters:

  • label (String) —

    Secret label identifier

  • exception (Boolean) (defaults to: true) —

    Whether to raise if not found

Returns:

  • (Hash, nil) —

    Secret attributes or nil



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/aspera/secret/store/macos_security.rb', line 260

def get(label:, exception: true)
  info = @keychain.password(:find, :generic, label: label)
  if info.nil?
    raise NotFound, "Secret '#{label}' not found" if exception
    return
  end
  return {
    label:       label,
    username:    (info['acct'] unless info['acct'].eql?(NO_ACCOUNT)),
    password:    info['password'],
    description: info['icmt']
  }.compact
end

#info ⇒ Hash

Returns Vault information metadata.

Returns:

  • (Hash) —

    Vault information metadata



232
233
234
235
236
# File 'lib/aspera/secret/store/macos_security.rb', line 232

def info
  return {
    keychain: @keychain_name
  }
end

#set(options) ⇒ Hash?

Store a secret in the keychain

Parameters:

  • options (Hash) —

    Secret attributes (label, password, username, description)

Returns:

  • (Hash, nil) —

    Result



248
249
250
251
252
253
254
# File 'lib/aspera/secret/store/macos_security.rb', line 248

def set(options)
  validate_set(options)
  @keychain.password(
    :add, :generic, service: options[:label],
    account: options[:username] || NO_ACCOUNT, password: options[:password], comment: options[:description]
  )
end