Class: Diffcrypt::Rails::EncryptedConfiguration
- Inherits:
-
Object
- Object
- Diffcrypt::Rails::EncryptedConfiguration
- Defined in:
- lib/diffcrypt/rails/encrypted_configuration.rb
Instance Attribute Summary collapse
-
#content_path ⇒ Object
readonly
Returns the value of attribute content_path.
-
#env_key ⇒ Object
readonly
Returns the value of attribute env_key.
-
#key_path ⇒ Object
readonly
Returns the value of attribute key_path.
-
#raise_if_missing_key ⇒ Object
readonly
Returns the value of attribute raise_if_missing_key.
Instance Method Summary collapse
- #change(&block) ⇒ Object
- #config ⇒ Object
-
#content_path_diffable? ⇒ Boolean
Determines if file is using the diffable format, or still encrypted using default rails credentials format.
-
#initialize(config_path:, key_path:, env_key:, raise_if_missing_key:) ⇒ EncryptedConfiguration
constructor
A new instance of EncryptedConfiguration.
- #key ⇒ String
-
#read ⇒ String
Allow a config to be started without a file present.
- #write(contents, original_encrypted_contents = nil) ⇒ Object
Constructor Details
#initialize(config_path:, key_path:, env_key:, raise_if_missing_key:) ⇒ EncryptedConfiguration
Returns a new instance of EncryptedConfiguration.
22 23 24 25 26 27 28 29 30 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 22 def initialize(config_path:, key_path:, env_key:, raise_if_missing_key:) @content_path = Pathname.new(File.absolute_path(config_path)).yield_self do |path| path.symlink? ? path.realpath : path end @key_path = Pathname.new(key_path) @env_key = env_key @raise_if_missing_key = raise_if_missing_key @active_support_encryptor = ActiveSupport::MessageEncryptor.new([key].pack('H*'), cipher: Encryptor::CIPHER) end |
Instance Attribute Details
#content_path ⇒ Object (readonly)
Returns the value of attribute content_path.
14 15 16 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 14 def content_path @content_path end |
#env_key ⇒ Object (readonly)
Returns the value of attribute env_key.
16 17 18 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 16 def env_key @env_key end |
#key_path ⇒ Object (readonly)
Returns the value of attribute key_path.
15 16 17 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 15 def key_path @key_path end |
#raise_if_missing_key ⇒ Object (readonly)
Returns the value of attribute raise_if_missing_key.
17 18 19 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 17 def raise_if_missing_key @raise_if_missing_key end |
Instance Method Details
#change(&block) ⇒ Object
66 67 68 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 66 def change(&block) writing read, &block end |
#config ⇒ Object
56 57 58 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 56 def config @config ||= deserialize(read).deep_symbolize_keys end |
#content_path_diffable? ⇒ Boolean
Determines if file is using the diffable format, or still encrypted using default rails credentials format
35 36 37 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 35 def content_path_diffable? content_path.binread.index('---')&.zero? end |
#key ⇒ String
62 63 64 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 62 def key read_env_key || read_key_file || handle_missing_key end |
#read ⇒ String
Allow a config to be started without a file present
41 42 43 44 45 46 47 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 41 def read raise MissingContentError, content_path unless !key.nil? && content_path.exist? decrypt content_path.binread rescue MissingContentError '' end |
#write(contents, original_encrypted_contents = nil) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 49 def write(contents, original_encrypted_contents = nil) deserialize(contents) IO.binwrite "#{content_path}.tmp", encrypt(contents, original_encrypted_contents) FileUtils.mv "#{content_path}.tmp", content_path end |