Class: Aspera::Secret::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/secret/reference.rb

Overview

Bootstrap secret of a store in configuration (e.g. password of type file): the value, or @file:<path> to read it from a file when used (e.g. a systemd credential), without final newline. Syntax of extended values of ascli, only @file: is supported: other prefixes may be added. In YAML, quote the value: @ cannot start a plain scalar.

Constant Summary collapse

FILE_PREFIX =
'@file:'

Instance Method Summary collapse

Constructor Details

#initialize(name, text) ⇒ Bootstrap

Returns a new instance of Bootstrap.

Parameters:

  • name (String) —

    Name of parameter, for errors

  • text (String) —

    Value, or @file:<path>

Raises:

  • (Error) —

    Not a String, or empty



48
49
50
51
52
53
54
55
56
57
# File 'lib/aspera/secret/reference.rb', line 48

def initialize(name, text)
  raise Error, "#{name}: expecting the value, or #{FILE_PREFIX}<path>" unless text.is_a?(String) && !text.empty?
  if text.start_with?(FILE_PREFIX)
    @path = text.delete_prefix(FILE_PREFIX)
    raise Error, "#{name}: expecting #{FILE_PREFIX}<path>" if @path.empty?
  else
    @value = text
    Secret::Hider.instance.add_secret_values(text)
  end
end

Instance Method Details

#file? ⇒ Boolean

Returns true if read from a file.

Returns:

  • (Boolean) —

    true if read from a file



60
61
62
# File 'lib/aspera/secret/reference.rb', line 60

def file?
  !@path.nil?
end

#inspect ⇒ Object

No value in logs and errors



79
80
81
# File 'lib/aspera/secret/reference.rb', line 79

def inspect
  "#<#{self.class} #{@path.nil? ? 'value' : "#{FILE_PREFIX}#{@path}"}>"
end

#mtime ⇒ Time?

Returns Modification time of file, nil for a value.

Returns:

  • (Time, nil) —

    Modification time of file, nil for a value

Raises:

  • (Error) —

    Missing file



66
67
68
69
70
# File 'lib/aspera/secret/reference.rb', line 66

def mtime
  File.mtime(@path) unless @path.nil?
rescue SystemCallError => e
  raise Error, "secret file: #{e.message}"
end

#value ⇒ String

Returns Value, read from the file at each call.

Returns:

  • (String) —

    Value, read from the file at each call

Raises:

  • (Error) —

    Missing, unreadable or empty file



74
75
76
# File 'lib/aspera/secret/reference.rb', line 74

def value
  @path.nil? ? @value : Secret.read_file(@path)
end