Class: SymmetricEncryption::Config
- Inherits:
-
Object
- Object
- SymmetricEncryption::Config
- Defined in:
- lib/symmetric_encryption/config.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
Class Method Summary collapse
-
.load!(file_name: nil, env: nil) ⇒ Object
Load the Encryption Configuration from a YAML file.
-
.read_file(file_name) ⇒ Object
Reads the entire configuration for all environments from the supplied file name.
-
.write_file(file_name, config) ⇒ Object
Write the entire configuration for all environments to the supplied file name.
Instance Method Summary collapse
-
#ciphers ⇒ Object
Returns [Array(SymmetricEncrytion::Cipher)] ciphers specified in the configuration file.
-
#config ⇒ Object
Returns [Hash] the configuration for the supplied environment.
-
#initialize(file_name: nil, env: nil) ⇒ Config
constructor
Load the Encryption Configuration from a YAML file.
Constructor Details
#initialize(file_name: nil, env: nil) ⇒ Config
Load the Encryption Configuration from a YAML file.
See: ‘.load!` for parameters.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/symmetric_encryption/config.rb', line 54 def initialize(file_name: nil, env: nil) env ||= defined?(Rails) ? Rails.env : ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development' unless file_name root = defined?(Rails) ? Rails.root : '.' file_name = if (env_var = ENV['SYMMETRIC_ENCRYPTION_CONFIG']) File.(env_var) else File.join(root, 'config', 'symmetric-encryption.yml') end raise(ConfigError, "Cannot find config file: #{file_name}") unless File.exist?(file_name) end @env = env @file_name = file_name end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/symmetric_encryption/config.rb', line 5 def env @env end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
5 6 7 |
# File 'lib/symmetric_encryption/config.rb', line 5 def file_name @file_name end |
Class Method Details
.load!(file_name: nil, env: nil) ⇒ Object
Load the Encryption Configuration from a YAML file.
file_name:
Name of configuration file.
Default: "#{Rails.root}/config/symmetric-encryption.yml"
Note:
The Symmetric Encryption config file name can also be set using the `SYMMETRIC_ENCRYPTION_CONFIG`
environment variable.
env:
Which environments config to load. Usually: production, development, etc.
Non-Rails apps can set env vars: RAILS_ENV, or RACK_ENV
Default: Rails.env || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
20 21 22 23 24 25 26 |
# File 'lib/symmetric_encryption/config.rb', line 20 def self.load!(file_name: nil, env: nil) config = new(file_name: file_name, env: env) ciphers = config.ciphers SymmetricEncryption.cipher = ciphers.shift SymmetricEncryption.secondary_ciphers = ciphers true end |
.read_file(file_name) ⇒ Object
Reads the entire configuration for all environments from the supplied file name.
29 30 31 32 33 34 |
# File 'lib/symmetric_encryption/config.rb', line 29 def self.read_file(file_name) config = YAML.load(ERB.new(File.new(file_name).read).result) config = deep_symbolize_keys(config) config.each_pair { |_env, cfg| SymmetricEncryption::Config.send(:migrate_old_formats!, cfg) } config end |
.write_file(file_name, config) ⇒ Object
Write the entire configuration for all environments to the supplied file name.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/symmetric_encryption/config.rb', line 37 def self.write_file(file_name, config) config = deep_stringify_keys(config) FileUtils.mkdir_p(File.dirname(file_name)) File.open(file_name, 'w') do |f| f.puts '# This file was auto generated by symmetric-encryption.' f.puts '# Recommend using symmetric-encryption to make changes.' f.puts '# For more info, run:' f.puts '# symmetric-encryption --help' f.puts '#' f.write(config.to_yaml) end end |
Instance Method Details
#ciphers ⇒ Object
Returns [Array(SymmetricEncrytion::Cipher)] ciphers specified in the configuration file.
86 87 88 |
# File 'lib/symmetric_encryption/config.rb', line 86 def ciphers @ciphers ||= config[:ciphers].collect { |cipher_config| Cipher.from_config(cipher_config) } end |
#config ⇒ Object
Returns [Hash] the configuration for the supplied environment.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/symmetric_encryption/config.rb', line 73 def config @config ||= begin raise(ConfigError, "Cannot find config file: #{file_name}") unless File.exist?(file_name) env_config = YAML.load(ERB.new(File.new(file_name).read).result)[env] raise(ConfigError, "Cannot find environment: #{env} in config file: #{file_name}") unless env_config env_config = self.class.send(:deep_symbolize_keys, env_config) self.class.send(:migrate_old_formats!, env_config) end end |