Class: Aspera::Keychain::OnePasswordCli

Inherits:
Base
  • Object
show all
Includes:
OnePasswordBase
Defined in:
lib/aspera/keychain/one_password_cli.rb

Overview

Manage secrets using the 1Password CLI (op) https://developer.1password.com/docs/cli/

Constant Summary

Constants inherited from Base

Base::CONTENT_KEYS

Instance Method Summary collapse

Methods inherited from Base

#validate_set

Constructor Details

#initialize(vault: nil, account: nil) ⇒ OnePasswordCli

Returns a new instance of OnePasswordCli.

Parameters:

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

    Name or ID of the 1Password vault (optional, uses default vault if omitted)

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

    Account shorthand for op (optional, uses default account if omitted)



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

def initialize(vault: nil, account: nil)
  super()
  @vault   = vault
  @account = 
end

Instance Method Details

#all ⇒ Object



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

def all
  items = op_json('item', 'list', '--categories', ITEM_CATEGORY)
  return items.map do |item|
    item_to_secret(op_json('item', 'get', item['id'])).merge(id: item['id'])
  end
end

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



65
66
67
68
# File 'lib/aspera/keychain/one_password_cli.rb', line 65

def delete(label:, id: nil)
  op_run('item', 'delete', id || label)
  nil
end

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



54
55
56
57
58
59
60
61
62
63
# File 'lib/aspera/keychain/one_password_cli.rb', line 54

def get(label:, id: nil, exception: true)
  identifier = id || label
  stdout, _stderr, status = op_capture('item', 'get', identifier, '--format=json')
  if !status.success?
    raise "Secret '#{label}' not found" if exception
    return
  end
  full = JSON.parse(stdout)
  return item_to_secret(full).merge(id: full['id'])
end

#ids ⇒ Object



39
40
41
# File 'lib/aspera/keychain/one_password_cli.rb', line 39

def ids
  op_json('item', 'list', '--categories', ITEM_CATEGORY).map { |item| {id: item['id'], label: item['title']} }
end

#info ⇒ Object



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

def info
  h = {}
  h[:vault]   = @vault   unless @vault.nil?
  h[:account] = @account unless @account.nil?
  h
end

#set(options) ⇒ Object



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

def set(options)
  validate_set(options)
  args = ['item', 'create', '--category', ITEM_CATEGORY, "--title=#{options[:label]}"]
  args << "#{FIELD_USERNAME}=#{options[:username]}"           if options[:username]
  args << "#{FIELD_PASSWORD}[password]=#{options[:password]}" if options[:password]
  args << "#{FIELD_URL}[url]=#{options[:url]}"                if options[:url]
  args << "#{FIELD_NOTES_ID}=#{options[:description]}"        if options[:description]
  op_run(*args)
  nil
end