instance method encrypted

Ruby on Rails 5.2.8.1

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

encrypted(path, key_path: "config/master.key", env_key: "RAILS_MASTER_KEY")

Shorthand to decrypt any encrypted configurations or files.

For any file added with bin/rails encrypted:edit call read to decrypt the file with the master key. The master key is either stored in config/master.key or ENV["RAILS_MASTER_KEY"].

Rails.application.encrypted("config/mystery_man.txt.enc").read
# => "We've met before, haven't we?"

It’s also possible to interpret encrypted YAML files with config.

Rails.application.encrypted("config/credentials.yml.enc").config
# => { next_guys_line: "I don't think so. Where was it you think we met?" }

Any top-level configs are also accessible directly on the return value:

Rails.application.encrypted("config/credentials.yml.enc").next_guys_line
# => "I don't think so. Where was it you think we met?"

The files or configs can also be encrypted with a custom key. To decrypt with a key in the ENV, use:

Rails.application.encrypted("config/special_tokens.yml.enc", env_key: "SPECIAL_TOKENS")

Or to decrypt with a file, that should be version control ignored, relative to Rails.root:

Rails.application.encrypted("config/special_tokens.yml.enc", key_path: "config/special_tokens.key")

Parameters

path req
key_path key = "config/master.key"
env_key key = "RAILS_MASTER_KEY"
Source
# File railties/lib/rails/application.rb, line 472
    def encrypted(path, key_path: "config/master.key", env_key: "RAILS_MASTER_KEY")
      ActiveSupport::EncryptedConfiguration.new(
        config_path: Rails.root.join(path),
        key_path: Rails.root.join(key_path),
        env_key: env_key,
        raise_if_missing_key: config.require_master_key
      )
    end

Defined in railties/lib/rails/application.rb line 472 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Rails::Application

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close