class ActiveSupport::EncryptedFile

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:

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.cr

Constant Summary

CIPHER = "aes-128-gcm"

The cipher Rails encrypts its files with. Note it is not the aes-256-gcm of ActiveRecord::Encryption::Cipher.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(content_path : Path | String, key_path : Path | String, env_key : String = "RAILS_MASTER_KEY", raise_if_missing_key : Bool = false) #

Class Method Detail

def self.expected_key_length : Int32 #

Length a key is expected to have, counted in hexadecimal characters rather than in bytes.


def self.generate_key : String #

Returns a new random key: 32 hexadecimal characters, the 16 bytes CIPHER takes.


Instance Method Detail

def change(&) : Nil #

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.


def content_path : Path #

def edit(editor : String | Nil = nil) : Nil #

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.


def env_key : String #

def key : String | Nil #

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.


def key? : Bool #

Returns true when a key is available, without raising when it is not.


def key_path : Path #

def raise_if_missing_key? : Bool #

def read : String #

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.


def write(contents : String) : Nil #

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.