Class: Aspera::Keychain::OnePasswordCli
- 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
Instance Method Summary collapse
- #all ⇒ Object
- #delete(label:, id: nil) ⇒ Object
- #get(label:, id: nil, exception: true) ⇒ Object
- #ids ⇒ Object
- #info ⇒ Object
-
#initialize(vault: nil, account: nil) ⇒ OnePasswordCli
constructor
A new instance of OnePasswordCli.
- #set(options) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(vault: nil, account: nil) ⇒ OnePasswordCli
Returns a new instance of OnePasswordCli.
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 = 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() validate_set() args = ['item', 'create', '--category', ITEM_CATEGORY, "--title=#{[:label]}"] args << "#{FIELD_USERNAME}=#{[:username]}" if [:username] args << "#{FIELD_PASSWORD}[password]=#{[:password]}" if [:password] args << "#{FIELD_URL}[url]=#{[:url]}" if [:url] args << "#{FIELD_NOTES_ID}=#{[:description]}" if [:description] op_run(*args) nil end |