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
-
#key? ⇒ Boolean
It’s required since the commit github.com/rails/rails/commit/1740b1f2cb8104435b6041ec6bfaabe58a6d74e6 Returns truthy if #key is truthy.
-
#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.
27 28 29 30 31 32 33 34 35 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 27 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 @diffcrypt_file = Diffcrypt::File.new(@content_path) @key_path = Pathname.new(key_path) @env_key = env_key @raise_if_missing_key = raise_if_missing_key end |
Instance Attribute Details
#content_path ⇒ Object (readonly)
Returns the value of attribute content_path.
19 20 21 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 19 def content_path @content_path end |
#env_key ⇒ Object (readonly)
Returns the value of attribute env_key.
21 22 23 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 21 def env_key @env_key end |
#key_path ⇒ Object (readonly)
Returns the value of attribute key_path.
20 21 22 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 20 def key_path @key_path end |
#raise_if_missing_key ⇒ Object (readonly)
Returns the value of attribute raise_if_missing_key.
22 23 24 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 22 def raise_if_missing_key @raise_if_missing_key end |
Instance Method Details
#change(&block) ⇒ Object
79 80 81 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 79 def change(&block) writing read, &block end |
#config ⇒ Object
61 62 63 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 61 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
40 41 42 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 40 def content_path_diffable? content_path.binread.index('---')&.zero? end |
#key ⇒ String
67 68 69 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 67 def key read_env_key || read_key_file || handle_missing_key end |
#key? ⇒ Boolean
It’s required since the commit github.com/rails/rails/commit/1740b1f2cb8104435b6041ec6bfaabe58a6d74e6 Returns truthy if #key is truthy. Returns falsy otherwise. Unlike #key, does not raise MissingKeyError when raise_if_missing_key
is true.
75 76 77 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 75 def key? !!(read_env_key || read_key_file) end |
#read ⇒ String
Allow a config to be started without a file present
46 47 48 49 50 51 52 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 46 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
54 55 56 57 58 59 |
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 54 def write(contents, original_encrypted_contents = nil) deserialize(contents) ::File.binwrite "#{content_path}.tmp", encrypt(contents, original_encrypted_contents) ::FileUtils.mv "#{content_path}.tmp", content_path end |