Module: Aspera::Secret

Defined in:
lib/aspera/secret/reference.rb,
lib/aspera/secret/hider.rb,
lib/aspera/secret/resolver.rb,
lib/aspera/secret/strength.rb,
lib/aspera/secret/store/base.rb,
lib/aspera/secret/store/keeper.rb,
lib/aspera/secret/store/factory.rb,
lib/aspera/secret/store/encrypted_hash.rb,
lib/aspera/secret/store/macos_security.rb,
lib/aspera/secret/store/secret_service.rb,
lib/aspera/secret/store/hashicorp_vault.rb,
lib/aspera/secret/store/one_password_api.rb,
lib/aspera/secret/store/one_password_cli.rb,
lib/aspera/secret/store/one_password_base.rb

Overview

Reference to a secret held in a store: secret://<store>/<path>[?<options>][#<field>]

The reference is stored instead of the value, the value is read when used (see Secret::Resolver). A reference is a whole value, it is not searched in a text. Path and field are percent-decoded (%20 or a space), options are a URL query (version=2&mount=a/b). In ascli: @vault://<store>/<path>[?<options>][#<field>], stores of option vault.

Defined Under Namespace

Modules: Store, Strength Classes: AuthenticationError, Bootstrap, Error, Hider, NotFound, Reference, Resolver

Constant Summary collapse

SCHEME =
'secret://'

Class Method Summary collapse

Class Method Details

.parse_reference(text) ⇒ Reference

Returns Parsed reference.

Parameters:

Returns:

Raises:

  • (Error) —

    Invalid syntax



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/aspera/secret/reference.rb', line 115

def parse_reference(text)
  match = SYNTAX.match(text) if text.is_a?(String)
  raise Error, "secret reference #{text}: expected #{SCHEME}<store>/<path>[?<options>][#<field>]" if match.nil?
  Reference.new(
    text:    text,
    store:   match[1],
    path:    decode(text, match[2]),
    options: match[3].nil? ? {} : URI.decode_www_form(match[3]).to_h.freeze,
    field:   match[4].nil? ? nil : decode(text, match[4])
  )
rescue ArgumentError => e
  raise Error, "secret reference #{text}: #{e.message}"
end

.read_file(path) ⇒ String

Read a bootstrap secret from a file (@file: of Bootstrap, e.g. a systemd credential). The value is declared to Secret::Hider.

Parameters:

  • path (String) —

    Path of file

Returns:

  • (String) —

    Content of file without trailing newline

Raises:

  • (Error) —

    Missing, unreadable or empty file



134
135
136
137
138
139
140
141
# File 'lib/aspera/secret/reference.rb', line 134

def read_file(path)
  value = File.read(path).chomp
  raise Error, "empty secret file: #{path}" if value.empty?
  Secret::Hider.instance.add_secret_values(value)
  value
rescue SystemCallError => e
  raise Error, "secret file: #{e.message}"
end

.reference?(value) ⇒ Boolean

Returns true if value is a String starting with secret:// (then parse_reference checks it).

Parameters:

  • value (Object) —

    Any value

Returns:

  • (Boolean) —

    true if value is a String starting with secret:// (then parse_reference checks it)



92
93
94
# File 'lib/aspera/secret/reference.rb', line 92

def reference?(value)
  value.is_a?(String) && value.start_with?(SCHEME)
end

.reference_text(store:, path:, options: {}, field: nil) ⇒ String

Build a reference string from its components (inverse of parse_reference). Path segments are percent-encoded individually so / separators are preserved. Field and option values are fully percent-encoded.

Parameters:

  • store (String) —

    Name of store

  • path (String) —

    Path of secret in store

  • options (Hash<String,String>) (defaults to: {}) —

    Options (e.g. {'mount' => 'kv/team'})

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

    Field of secret, nil for none

Returns:



104
105
106
107
108
109
110
# File 'lib/aspera/secret/reference.rb', line 104

def reference_text(store:, path:, options: {}, field: nil)
  encoded_path = path.split('/', -1).map { |seg| encode(seg) }.join('/')
  text = "#{SCHEME}#{store}/#{encoded_path}"
  text += "?#{URI.encode_www_form(options)}" unless options.empty?
  text += "##{encode(field)}" unless field.nil?
  text
end