Class: Diffcrypt::Rails::EncryptedConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/diffcrypt/rails/encrypted_configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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_keyObject (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_pathObject (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_keyObject (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

#configObject



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

Returns:

  • (Boolean)


40
41
42
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 40

def content_path_diffable?
  content_path.binread.index('---')&.zero?
end

#keyString

Returns:

  • (String)

Raises:



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.

Returns:

  • (Boolean)


75
76
77
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 75

def key?
  !!(read_env_key || read_key_file)
end

#readString

Allow a config to be started without a file present

Returns:

  • (String)

    Returns decryped content or a blank string



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