Class: Aspera::Secret::Store::SecretService
- Defined in:
- lib/aspera/secret/store/secret_service.rb
Overview
Manage secrets using the Linux Secret Service (GNOME Keyring, KWallet, KeePassXC...), with the secret-tool CLI of libsecret.
https://specifications.freedesktop.org/secret-service/latest/
Items have attributes: service (parameter name), label, and optionally username, url, description.
Constant Summary collapse
- PARAMETERS =
%i[name].freeze
Constants inherited from Base
Base::CONTENT_KEYS, Base::REFERENCE_OPTIONS
Instance Method Summary collapse
- #all ⇒ Object
- #delete(label:) ⇒ Object
-
#get(label:, exception: true) ⇒ Hash?
Secret,
nilif not found andexceptionisfalse. - #ids ⇒ Object
- #info ⇒ Object
-
#initialize(name: 'ascli') ⇒ SecretService
constructor
A new instance of SecretService.
-
#set(options) ⇒ Object
Store a new secret.
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(name: 'ascli') ⇒ SecretService
Returns a new instance of SecretService.
28 29 30 31 32 |
# File 'lib/aspera/secret/store/secret_service.rb', line 28 def initialize(name: 'ascli') super() raise Error, 'type secret_service: only on Linux' unless Environment.instance.os.eql?(Environment::OS_LINUX) @name = name end |
Instance Method Details
#all ⇒ Object
38 39 40 |
# File 'lib/aspera/secret/store/secret_service.rb', line 38 def all labels.map { |label| get(label: label) } end |
#delete(label:) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/aspera/secret/store/secret_service.rb', line 84 def delete(label:) raise NotFound, "Secret '#{label}' not found" if get(label: label, exception: false).nil? _, stderr, status = secret_tool('clear', ATTR_SERVICE, @name, 'label', label) raise Error, stderr.strip unless status.success? nil end |
#get(label:, exception: true) ⇒ Hash?
Returns Secret, nil if not found and exception is false.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aspera/secret/store/secret_service.rb', line 65 def get(label:, exception: true) stdout, stderr, status = secret_tool('lookup', ATTR_SERVICE, @name, 'label', label) if !status.success? # no error message when the item does not exist raise Error, stderr.strip unless stderr.strip.empty? raise NotFound, "Secret '#{label}' not found" if exception return end attributes = search(label).last return { label: label, username: attributes['username'], password: stdout.dup.force_encoding('UTF-8'), url: attributes['url'], description: attributes['description'] }.compact end |
#ids ⇒ Object
42 43 44 |
# File 'lib/aspera/secret/store/secret_service.rb', line 42 def ids labels.map { |label| {label: label} } end |
#info ⇒ Object
34 35 36 |
# File 'lib/aspera/secret/store/secret_service.rb', line 34 def info {service: @name} end |
#set(options) ⇒ Object
Store a new secret
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aspera/secret/store/secret_service.rb', line 48 def set() validate_set() Aspera.assert(.key?(:password), 'password is required') assert_new_label([:label]) attributes = [ATTR_SERVICE, @name] ATTRIBUTES.each do |key| attributes.push(key.to_s, [key]) if .key?(key) end # the password is read from stdin: not visible in process list _, stderr, status = secret_tool('store', "--label=#{[:label]}", *attributes, stdin_data: [:password]) raise Error, stderr.strip unless status.success? nil end |