Class: Aspera::Secret::Store::OnePasswordCli

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

Overview

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

Constant Summary collapse

PARAMETERS =
%i[vault account].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, #list_folder, #read_fields, #read_reference, #validate_set, #write_reference

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)



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

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

Instance Method Details

#all ⇒ Object



38
39
40
41
42
43
# File 'lib/aspera/secret/store/one_password_cli.rb', line 38

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



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

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

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



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

def get(label:, id: nil, exception: true)
  identifier = id || label
  stdout, stderr, status = op_capture('item', 'get', identifier, '--format=json', exception: false)
  if !status.success?
    raise NotFound, "Multiple secrets found with label '#{label}': use id to disambiguate, or option vault to restrict" if stderr.to_s.match?(/more than one item/i)
    # Other failures (e.g. not signed in) must not be reported as a missing secret
    # `op` prefixes its messages with `[ERROR] <date> <time>`
    raise Error, "1Password CLI failed for '#{label}': #{stderr.to_s.sub(%r{\A\[ERROR\]\s+\d{4}/\d\d/\d\d\s+[\d:]+\s+}, '').strip}" unless stderr.to_s.match?(/isn't an item|not found/i)
    raise NotFound, "Secret '#{label}' not found" if exception
    return
  end
  full = JSON.parse(stdout)
  return item_to_secret(full).merge(id: full['id'])
end

#ids ⇒ Object



45
46
47
# File 'lib/aspera/secret/store/one_password_cli.rb', line 45

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

#info ⇒ Object



31
32
33
34
35
36
# File 'lib/aspera/secret/store/one_password_cli.rb', line 31

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

#set(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aspera/secret/store/one_password_cli.rb', line 49

def set(options)
  validate_set(options)
  assert_new_label(options[:label])
  args = ['item', 'create', '--category', ITEM_CATEGORY, "--title=#{options[:label]}"]
  args << "--url=#{options[:url]}" if options[:url]
  # `op` reads an item template from its standard input when it is not a terminal (always the case here):
  # fields are given in the template, which also keeps the secret out of the command line
  fields = []
  fields << {id: FIELD_USERNAME, type: 'STRING', purpose: 'USERNAME', label: FIELD_USERNAME, value: options[:username]} if options[:username]
  fields << {id: FIELD_PASSWORD, type: 'CONCEALED', purpose: 'PASSWORD', label: FIELD_PASSWORD, value: options[:password]} if options[:password]
  fields << {id: FIELD_NOTES_ID, type: 'STRING', purpose: 'NOTES', label: FIELD_NOTES_ID, value: options[:description]} if options[:description]
  op_capture(*args, stdin_data: JSON.generate({fields: fields}), exception: true)
  nil
end