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.



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

#configObject



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

Returns:

  • (Boolean)


35
36
37
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 35

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

#keyString

Returns:

  • (String)

Raises:



62
63
64
# File 'lib/diffcrypt/rails/encrypted_configuration.rb', line 62

def key
  read_env_key || read_key_file || handle_missing_key
end

#readString

Allow a config to be started without a file present

Returns:

  • (String)

    Returns decryped content or a blank string



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