class
ActiveSupport::EncryptedFile
- ActiveSupport::EncryptedFile
- Reference
- Object
Overview
Reads and writes a file encrypted the way Rails encrypts config/credentials.yml.enc.
file = ActiveSupport::EncryptedFile.new(
content_path: "config/credentials.yml.enc",
key_path: "config/master.key",
env_key: "RAILS_MASTER_KEY",
raise_if_missing_key: true
)
file.read # => "secret_key_base: abc\n"
file.write("some_secret: 1\n") # replaces the file in one atomic rename
The layout is Rails', so a file written here reads back through bin/rails credentials:show
and the other way round:
- the cipher is
aes-128-gcm, not theaes-256-gcmofActiveRecord::Encryption - the key is 32 hexadecimal characters, unpacked to the 16 bytes the cipher wants
- the key comes from
ENV[env_key]first, then from key_path - the envelope is
Base64(ciphertext)--Base64(iv)--Base64(auth_tag) - the payload inside the envelope is
Marshal.dump(contents), not the text itself
Ported from https://github.com/rails/rails/blob/v8.1.3/activesupport/lib/active_support/encrypted_file.rb
Direct Known Subclasses
Defined in:
active_support/encrypted_file.crConstant Summary
-
CIPHER =
"aes-128-gcm" -
The cipher Rails encrypts its files with. Note it is not the
aes-256-gcmofActiveRecord::Encryption::Cipher.
Constructors
Class Method Summary
-
.expected_key_length : Int32
Length a key is expected to have, counted in hexadecimal characters rather than in bytes.
-
.generate_key : String
Returns a new random key: 32 hexadecimal characters, the 16 bytes
CIPHERtakes.
Instance Method Summary
-
#change(&) : Nil
Decrypts the file to a temporary path, yields that path, then writes back whatever is there once the block returns.
- #content_path : Path
-
#edit(editor : String | Nil = nil) : Nil
Decrypts the file, opens it in editor, and writes it back.
- #env_key : String
-
#key : String | Nil
Returns the key, from
ENV[env_key]first and from#key_pathsecond. -
#key? : Bool
Returns
truewhen a key is available, without raising when it is not. - #key_path : Path
- #raise_if_missing_key? : Bool
-
#read : String
Returns the decrypted contents of the file.
-
#write(contents : String) : Nil
Encrypts contents and replaces the file with it.
Constructor Detail
Class Method Detail
Length a key is expected to have, counted in hexadecimal characters rather than in bytes.
Returns a new random key: 32 hexadecimal characters, the 16 bytes CIPHER takes.
Instance Method Detail
Decrypts the file to a temporary path, yields that path, then writes back whatever is there once the block returns.
The plaintext is written to the system temporary directory rather than next to
credentials.yml.enc, with mode 0600, and is deleted even when the block raises. Nothing
is written back when the content comes back unchanged.
Raises MissingContentError when the file does not exist yet.
ActiveSupport::EncryptedConfiguration reads a missing file as empty instead, which is what
lets bin/rails credentials:edit start from nothing.
Decrypts the file, opens it in editor, and writes it back.
editor defaults to ENV["EDITOR"]. The command is run through the shell, so
EDITOR="code --wait" works the same way it does for bin/rails credentials:edit.
Nothing is written when the editor exits non-zero or leaves the content unchanged.
Returns the key, from ENV[env_key] first and from #key_path second.
Returns nil when neither holds one, unless #raise_if_missing_key? is set, in which case
MissingKeyError is raised. RAILS_MASTER_KEY therefore wins over every key file, which is
what lets a deployment hand the key over without writing it to disk.
Returns the decrypted contents of the file.
Raises MissingContentError when the file does not exist, MissingKeyError when the key is
missing and #raise_if_missing_key? is set, InvalidKeyLengthError when the key is malformed,
and ActiveSupport::MessageEncryptor::InvalidMessage when the file cannot be decrypted with
the key that was found.
Encrypts contents and replaces the file with it.
The new content is encrypted into a sibling .tmp file and renamed over the target, so a
reader sees either the old file or the new one and never a half-written one. Rails does the
same. Before the rename, the fresh ciphertext is decrypted once and compared against
contents: this file holds every secret an application has, and a write that cannot be read
back would otherwise only be discovered on the next deploy.