Class: Chamber::File
Instance Attribute Summary collapse
-
#decryption_keys ⇒ Object
Returns the value of attribute decryption_keys.
-
#encryption_keys ⇒ Object
Returns the value of attribute encryption_keys.
-
#namespaces ⇒ Object
Returns the value of attribute namespaces.
-
#signature_name ⇒ Object
Returns the value of attribute signature_name.
Instance Method Summary collapse
-
#decrypt ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(path:, namespaces: {}, decryption_keys: {}, encryption_keys: {}, signature_name: nil) ⇒ File
constructor
Internal: Creates a settings file representing a path to a file on the filesystem.
-
#secure ⇒ Object
rubocop:disable Layout/LineLength, Metrics/AbcSize.
-
#sign ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#to_settings ⇒ Object
Internal: Extracts the data from the file on disk.
- #verify ⇒ Object
Methods inherited from Pathname
Constructor Details
#initialize(path:, namespaces: {}, decryption_keys: {}, encryption_keys: {}, signature_name: nil) ⇒ File
Internal: Creates a settings file representing a path to a file on the filesystem.
Optionally, namespaces may be passed in which will be passed to the Settings object for consideration of which data will be parsed (see the Settings object’s documentation for details on how this works).
Examples:
###
# It can be created by passing namespaces
#
settings_file = Chamber::File.new path: '/tmp/settings.yml',
namespaces: {
environment: ENV['RAILS_ENV'] }
# => <Chamber::File>
settings_file.to_settings
# => <Chamber::Settings>
###
# It can also be created without passing any namespaces
#
Chamber::File.new path: '/tmp/settings.yml'
# => <Chamber::File>
46 47 48 49 50 51 52 53 |
# File 'lib/chamber/file.rb', line 46 def initialize(path:, namespaces: {}, decryption_keys: {}, encryption_keys: {}, signature_name: nil) self.namespaces = namespaces self.decryption_keys = decryption_keys self.encryption_keys = encryption_keys self.signature_name = signature_name super(path) end |
Instance Attribute Details
#decryption_keys ⇒ Object
Returns the value of attribute decryption_keys.
14 15 16 |
# File 'lib/chamber/file.rb', line 14 def decryption_keys @decryption_keys end |
#encryption_keys ⇒ Object
Returns the value of attribute encryption_keys.
14 15 16 |
# File 'lib/chamber/file.rb', line 14 def encryption_keys @encryption_keys end |
#namespaces ⇒ Object
Returns the value of attribute namespaces.
14 15 16 |
# File 'lib/chamber/file.rb', line 14 def namespaces @namespaces end |
#signature_name ⇒ Object
Returns the value of attribute signature_name.
14 15 16 |
# File 'lib/chamber/file.rb', line 14 def signature_name @signature_name end |
Instance Method Details
#decrypt ⇒ Object
rubocop:disable Metrics/AbcSize
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chamber/file.rb', line 111 def decrypt decrypted_settings = to_settings.decrypted.to_flattened_name_hash secure_settings = to_settings.encrypted.to_flattened_name_hash file_contents = read decrypted_settings.each_pair do |name_pieces, decrypted_value| encrypted_value = secure_settings[name_pieces] next unless encrypted_value.is_a?(String) escaped_name = Regexp.escape(name_pieces.last) escaped_value = Regexp.escape(encrypted_value) line_pattern = /^(\s*)#{escaped_name}(\s*):(\s*)#{escaped_value}$/ indentation_level = file_contents .match(line_pattern) &.[](1) &.<<(' ') if decrypted_value.include?("\n") decrypted_value = decrypted_value .chomp .gsub("\n", "\n#{indentation_level}") .prepend("|\n#{indentation_level}") end file_contents .sub!( line_pattern, "\\1#{name_pieces.last}\\2:\\3#{decrypted_value}", ) end write(file_contents) end |
#secure ⇒ Object
rubocop:disable Layout/LineLength, Metrics/AbcSize
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/chamber/file.rb', line 82 def secure insecure_settings = to_settings.insecure.to_flattened_name_hash secure_settings = to_settings.insecure.secure.to_flattened_name_hash file_contents = read insecure_settings.each_pair do |name_pieces, value| secure_value = secure_settings[name_pieces] escaped_name = Regexp.escape(name_pieces.last) escaped_value = Regexp.escape(value) file_contents .sub!( /^(\s*)#{secure_prefix_pattern}#{escaped_name}(\s*):(\s*)['"]?#{escaped_value}['"]?$/, "\\1#{secure_prefix}#{name_pieces.last}\\2:\\3#{secure_value}", ) file_contents .sub!( /^(\s*)#{secure_prefix_pattern}#{escaped_name}(\s*):(\s*)\|((?:\n\1\s{2}.*)+)/, "\\1#{secure_prefix}#{name_pieces.last}\\2:\\3#{secure_value}", ) end write(file_contents) end |
#sign ⇒ Object
rubocop:enable Metrics/AbcSize
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/chamber/file.rb', line 147 def sign signature_key_contents = decryption_keys[:signature] fail ArgumentError, 'You asked to sign your settings files but no signature key was found. Run `chamber init --signature` to generate one.' \ unless signature_key_contents signature = Files::Signature.new(to_s, read, signature_key_contents, signature_name) signature.write end |
#to_settings ⇒ Object
Internal: Extracts the data from the file on disk. First passing it through ERB to generate any dynamic properties and then passing the resulting data through YAML.
Therefore if a settings file contains something like:
“‘erb test:
my_dynamic_value: <%= 1 + 1 %>
“‘
then the resulting settings object would have:
“‘ruby settings[:my_dynamic_value] # => 2 “`
74 75 76 77 78 79 |
# File 'lib/chamber/file.rb', line 74 def to_settings @to_settings ||= Settings.new(settings: file_contents_hash, namespaces: namespaces, decryption_keys: decryption_keys, encryption_keys: encryption_keys) end |
#verify ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/chamber/file.rb', line 158 def verify signature_key_contents = encryption_keys[:signature] fail ArgumentError, 'You asked to verify your settings files but no signature key was found. Run `chamber init --signature` to generate one.' \ unless signature_key_contents signature = Files::Signature.new(to_s, read, signature_key_contents, signature_name) signature.verify end |