Class: ComplexConfig::KeySource

Inherits:
Object
  • Object
show all
Defined in:
lib/complex_config/key_source.rb

Overview

A key source class that provides encryption keys from various sources

The KeySource class is responsible for managing and retrieving encryption keys from different possible sources such as files, environment variables, or direct values. It supports multiple configuration options and ensures that only one source is used at a time.

Instance Method Summary collapse

Constructor Details

#initialize(pathname: nil, env_var: nil, var: nil, master_key_pathname: nil) ⇒ KeySource

The initialize method sets up the key source with one of several possible settings.

Parameters:

  • pathname (String, nil) (defaults to: nil)

    The path to a key file

  • env_var (String, nil) (defaults to: nil)

    The name of an environment variable containing the key

  • var (String, nil) (defaults to: nil)

    A string value representing the key

  • master_key_pathname (String, nil) (defaults to: nil)

    The path to a master key file

Raises:

  • (ArgumentError)

    if more than one setting is provided



16
17
18
19
20
21
22
23
24
25
# File 'lib/complex_config/key_source.rb', line 16

def initialize(pathname: nil, env_var: nil, var: nil, master_key_pathname: nil)
  settings = [ pathname, env_var, var, master_key_pathname ].compact.size
  if settings > 1
    raise ArgumentError, 'only one setting at most possible'
  end
  pathname and pathname = pathname.to_s
  master_key_pathname and master_key_pathname = master_key_pathname.to_s
  @pathname, @env_var, @var, @master_key_pathname =
    pathname, env_var, var, master_key_pathname
end

Instance Method Details

#keyString?

The key method retrieves the encryption key from the configured source.

This method attempts to obtain the encryption key by checking various possible sources in order: a direct value, an environment variable, a master key file, or a key file associated with a pathname.

Returns:

  • (String, nil)

    the encryption key as a string if found, or nil if no key source is configured or accessible



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/complex_config/key_source.rb', line 43

def key
  if @var
    @var.ask_and_send(:chomp)
  elsif @env_var
    ENV[@env_var].ask_and_send(:chomp)
  elsif master_key?
    IO.binread(@master_key_pathname).chomp
  elsif @pathname
    IO.binread(@pathname + '.key').chomp
  end
rescue Errno::ENOENT, Errno::ENOTDIR
end

#key_bytesString

The key_bytes method converts the encryption key to bytes format.

This method takes the encryption key value and packs it into a binary byte representation using hexadecimal encoding.

Returns:

  • (String)

    the encryption key as a binary string of bytes



62
63
64
# File 'lib/complex_config/key_source.rb', line 62

def key_bytes
  [ key ].pack('H*')
end

#master_key?TrueClass, FalseClass

The master_key? method checks whether a master key pathname has been set.

Returns:

  • (TrueClass, FalseClass)

    true if a master key pathname is configured, false otherwise



31
32
33
# File 'lib/complex_config/key_source.rb', line 31

def master_key?
  !!@master_key_pathname
end