module Rails

Overview

The bridge's stand-in for Rails.application.

A Marten app has no Rails application object, but it does need the few things Rails reads off one: where the Rails app lives, which environment it runs as, its encrypted credentials, and the .secret_key_base every signature in the bridge derives from.

Rails.root = "../my_rails_app"
Rails.env = "production"

Rails.secret_key_base                              # => "3f9a..."
Rails.credentials.read                             # => "secret_key_base: 3f9a...\n..."
Rails.credentials.dig("aws", "access_key_id").as_s # => "AKIA..."
Rails.credential_string?("aws", "access_key_id")   # => "AKIA...", nil when absent

Nothing here reaches into a Rails process. It reads the same files Rails reads, with the same rules, so no secret has to be copied into Marten's settings.

Defined in:

rails.cr
rails/secrets.cr

Constant Summary

KEY_GENERATOR_ITERATIONS = 1000

Number of PBKDF2 iterations Rails uses for its application key generator.

https://github.com/rails/rails/blob/v8.1.3/railties/lib/rails/application.rb

SIGNED_GLOBAL_IDS_SALT = "signed_global_ids"

Salt GlobalID derives its verifier key with.

https://github.com/rails/globalid/blob/main/lib/global_id/railtie.rb

Class Method Summary

Class Method Detail

def self.credential_string?(*keys : String) : String | Nil #

Returns the credentials value at keys as a String, or nil when it is absent or empty.

Raises InvalidCredentialError when the value is there but is not a string, instead of quietly treating it as missing.


def self.credentials : ActiveSupport::EncryptedConfiguration #

Returns the application's encrypted credentials.

The file is config/credentials/<env>.yml.enc when that exists and config/credentials.yml.enc otherwise. The key is config/credentials/<env>.key when that exists and config/master.key otherwise — and ENV["RAILS_MASTER_KEY"] wins over both, since EncryptedFile looks there first. The two fall back independently, exactly as in Rails.

https://github.com/rails/rails/blob/v8.1.3/railties/lib/rails/application/configuration.rb



def self.credentials_content_path : Path #

Returns the path of the credentials file that is in use.


def self.credentials_key_path : Path #

Returns the path of the key file that is in use. ENV["RAILS_MASTER_KEY"] still takes precedence over whatever is there.


def self.encrypted(path : Path | String, key_path : Path | String = "config/master.key", env_key : String = "RAILS_MASTER_KEY", raise_if_missing_key : Bool = false) : ActiveSupport::EncryptedConfiguration #

Returns an ActiveSupport::EncryptedConfiguration for any encrypted file under .root, the way Rails.application.encrypted does.

Rails.encrypted("config/my_config.enc").read # => "foo:\n  bar: 123\n"

def self.env : String #

Name of the environment, from RAILS_ENV, then RACK_ENV, then "development" — the order Rails uses.


def self.env=(value : String) : String #

def self.generate_local_secret : String #

Returns the generated secret in tmp/local_secret.txt, creating it on first use.


def self.load_active_record_encryption! : Bool #

Configures ActiveRecord::Encryption from the active_record_encryption entry of the credentials, and rebuilds its ciphers.

Does nothing when the entry is absent. Returns true when the keys were applied.


def self.load_secrets!(active_record_encryption : Bool = true) : Nil #

Configures every secret the bridge holds from the Rails application's credentials.

# config/initializers/rails.cr
require "marten_rails_bridge/rails/secrets"

Rails.root = "../my_rails_app"
Rails.load_secrets!

One call, and nothing copied into Marten's settings:

  • ActiveSupport::KeyGenerator.instance is derived from Rails.secret_key_base, with the same 1000 iterations Rails uses, so every MessageVerifier in the bridge agrees with Rails on the signatures it produces and accepts
  • SignedGlobalID.verifier is the signed_global_ids key, signed with SHA-1, the way the GlobalID railtie sets it up
  • ActiveRecord::Encryption takes primary_key, deterministic_key and key_derivation_salt from the active_record_encryption entry of the credentials, the same three keys the Active Record railtie reads, and its ciphers are rebuilt from them

The Active Record keys are left alone when the credentials carry no active_record_encryption entry, so an application that configured them by hand keeps what it set. Pass active_record_encryption: false to skip them even when they are there.

Raises Rails::MissingSecretKeyBaseError when there is no .secret_key_base to be found outside development and test. Signing with an empty secret would hand out signatures a Rails app rejects, and the first symptom would be a 404 from an endpoint that looks fine.


def self.local? : Bool #

Returns true in development and test, which is where Rails allows a generated .secret_key_base.


def self.reset! : Nil #

Forgets the memoized environment and credentials, so both are looked up again on next use.


def self.root : Path #

Path to the Rails application root, the directory holding config/.


def self.root=(value : Path | String) : Path #

def self.secret_key_base : String #

Returns the secret every MessageVerifier and MessageEncryptor in the bridge derives its key from.

Looked up in the order Rails uses: ENV["SECRET_KEY_BASE"], then credentials.secret_key_base, then — in development and test only — a random one kept in tmp/local_secret.txt. ENV["SECRET_KEY_BASE_DUMMY"] forces that last one, which is how Rails precompiles assets without handing the build the production secrets.

Raises MissingSecretKeyBaseError anywhere else, rather than signing with an empty secret.

https://github.com/rails/rails/blob/v8.1.3/railties/lib/rails/application/configuration.rb