Class: ActiveSupport::EncryptedFile
- Inherits:
-
Object
- Object
- ActiveSupport::EncryptedFile
- Defined in:
- lib/secure_credentials/active_support/encrypted_file.rb
Direct Known Subclasses
Defined Under Namespace
Classes: MissingContentError, MissingKeyError
Constant Summary collapse
- CIPHER =
"aes-128-gcm"
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.
Class Method Summary collapse
Instance Method Summary collapse
- #change(&block) ⇒ Object
-
#initialize(content_path:, key_path:, env_key:, raise_if_missing_key:) ⇒ EncryptedFile
constructor
A new instance of EncryptedFile.
- #key ⇒ Object
- #read ⇒ Object
- #write(contents) ⇒ Object
Constructor Details
#initialize(content_path:, key_path:, env_key:, raise_if_missing_key:) ⇒ EncryptedFile
Returns a new instance of EncryptedFile.
34 35 36 37 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 34 def initialize(content_path:, key_path:, env_key:, raise_if_missing_key:) @content_path, @key_path = Pathname.new(content_path), Pathname.new(key_path) @env_key, @raise_if_missing_key = env_key, raise_if_missing_key end |
Instance Attribute Details
#content_path ⇒ Object (readonly)
Returns the value of attribute content_path.
32 33 34 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 32 def content_path @content_path end |
#env_key ⇒ Object (readonly)
Returns the value of attribute env_key.
32 33 34 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 32 def env_key @env_key end |
#key_path ⇒ Object (readonly)
Returns the value of attribute key_path.
32 33 34 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 32 def key_path @key_path end |
#raise_if_missing_key ⇒ Object (readonly)
Returns the value of attribute raise_if_missing_key.
32 33 34 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 32 def raise_if_missing_key @raise_if_missing_key end |
Class Method Details
.generate_key ⇒ Object
27 28 29 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 27 def self.generate_key SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(CIPHER)) end |
Instance Method Details
#change(&block) ⇒ Object
56 57 58 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 56 def change(&block) writing read, &block end |
#key ⇒ Object
39 40 41 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 39 def key read_env_key || read_key_file || handle_missing_key end |
#read ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 43 def read if !key.nil? && content_path.exist? decrypt content_path.binread else raise MissingContentError, content_path end end |
#write(contents) ⇒ Object
51 52 53 54 |
# File 'lib/secure_credentials/active_support/encrypted_file.rb', line 51 def write(contents) IO.binwrite "#{content_path}.tmp", encrypt(contents) FileUtils.mv "#{content_path}.tmp", content_path end |