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.crrails/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
-
.credential_string?(*keys : String) : String | Nil
Returns the credentials value at keys as a
String, ornilwhen it is absent or empty. -
.credentials : ActiveSupport::EncryptedConfiguration
Returns the application's encrypted credentials.
- .credentials=(value : ActiveSupport::EncryptedConfiguration) : ActiveSupport::EncryptedConfiguration
-
.credentials_content_path : Path
Returns the path of the credentials file that is in use.
-
.credentials_key_path : Path
Returns the path of the key file that is in use.
-
.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::EncryptedConfigurationfor any encrypted file under.root, the wayRails.application.encrypteddoes. -
.env : String
Name of the environment, from
RAILS_ENV, thenRACK_ENV, then"development"— the order Rails uses. - .env=(value : String) : String
-
.generate_local_secret : String
Returns the generated secret in
tmp/local_secret.txt, creating it on first use. -
.load_active_record_encryption! : Bool
Configures
ActiveRecord::Encryptionfrom theactive_record_encryptionentry of the credentials, and rebuilds its ciphers. -
.load_secrets!(active_record_encryption : Bool = true) : Nil
Configures every secret the bridge holds from the Rails application's credentials.
-
.local? : Bool
Returns
trueindevelopmentandtest, which is where Rails allows a generated.secret_key_base. -
.reset! : Nil
Forgets the memoized environment and credentials, so both are looked up again on next use.
-
.root : Path
Path to the Rails application root, the directory holding
config/. - .root=(value : Path | String) : Path
-
.secret_key_base : String
Returns the secret every
MessageVerifierandMessageEncryptorin the bridge derives its key from.
Class Method Detail
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.
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
Returns the path of the key file that is in use. ENV["RAILS_MASTER_KEY"] still takes
precedence over whatever is there.
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"
Name of the environment, from RAILS_ENV, then RACK_ENV, then "development" — the order
Rails uses.
Returns the generated secret in tmp/local_secret.txt, creating it on first use.
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.
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.instanceis derived fromRails.secret_key_base, with the same 1000 iterations Rails uses, so everyMessageVerifierin the bridge agrees with Rails on the signatures it produces and acceptsSignedGlobalID.verifieris thesigned_global_idskey, signed with SHA-1, the way the GlobalID railtie sets it upActiveRecord::Encryptiontakesprimary_key,deterministic_keyandkey_derivation_saltfrom theactive_record_encryptionentry 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.
Returns true in development and test, which is where Rails allows a generated
.secret_key_base.
Forgets the memoized environment and credentials, so both are looked up again on next use.
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