Class: ComplexConfig::KeySource
- 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
-
#initialize(pathname: nil, env_var: nil, var: nil, master_key_pathname: nil) ⇒ KeySource
constructor
The initialize method sets up the key source with one of several possible settings.
-
#key ⇒ String?
The key method retrieves the encryption key from the configured source.
-
#key_bytes ⇒ String
The key_bytes method converts the encryption key to bytes format.
-
#master_key? ⇒ TrueClass, FalseClass
The master_key? method checks whether a master key pathname has been set.
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.
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
#key ⇒ String?
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.
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_bytes ⇒ String
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.
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.
31 32 33 |
# File 'lib/complex_config/key_source.rb', line 31 def master_key? !!@master_key_pathname end |