class ActiveSupport::EncryptedConfiguration

Overview

An ActiveSupport::EncryptedFile whose contents are YAML, read as a configuration.

credentials = ActiveSupport::EncryptedConfiguration.new(
  config_path: "config/credentials.yml.enc",
  key_path: "config/master.key",
  env_key: "RAILS_MASTER_KEY",
  raise_if_missing_key: true
)

credentials.read # => "secret_key_base: abc\naws:\n  access_key_id: AKIA\n"

credentials["secret_key_base"]           # => "abc", raises KeyError when absent
credentials["secret_key_base"]?          # => "abc", nil when absent
credentials.secret_key_base              # => "abc", nil when absent
credentials.secret_key_base!             # => "abc", raises KeyError when absent
credentials.dig("aws", "access_key_id")  # => "AKIA", raises when absent
credentials.dig?("aws", "access_key_id") # => "AKIA", nil when absent

Values come back as YAML::Any, so #as_s, #as_i and friends are how you reach the underlying type.

Differences from Rails

Rails reaches a value through method_missing at runtime and delegate_missing_to :options, and nests indefinitely: credentials.aws.access_key_id. Crystal resolves calls at compile time, so the accessors here are generated by a method_missing macro, which can only produce the first level. Use #dig or #dig? for anything nested.

#[] and #dig raise on a missing key and #[]? and #dig? return nil, following Crystal rather than Rails, where #[] returns nil. #fetch raises, under either reading.

Ported from https://github.com/rails/rails/blob/v8.1.3/activesupport/lib/active_support/encrypted_configuration.rb

Defined in:

active_support/encrypted_configuration.cr

Constructors

Macro Summary

Instance Method Summary

Instance methods inherited from class ActiveSupport::EncryptedFile

change(&) : Nil change, content_path : Path content_path, edit(editor : String | Nil = nil) : Nil edit, env_key : String env_key, key : String | Nil key, key? : Bool key?, key_path : Path key_path, raise_if_missing_key? : Bool raise_if_missing_key?, read : String read, write(contents : String) : Nil write

Constructor methods inherited from class ActiveSupport::EncryptedFile

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

Class methods inherited from class ActiveSupport::EncryptedFile

expected_key_length : Int32 expected_key_length, generate_key : String generate_key

Constructor Detail

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

Macro Detail

macro method_missing(call) #

Reads a top-level key as if it were a method, the way Rails.application.credentials does.

credentials.secret_key_base is credentials["secret_key_base"]? and credentials.secret_key_base! is credentials.fetch("secret_key_base"). Only a call with no arguments and no block is treated this way; anything else is a compile error, so a mistyped method call is not quietly turned into a lookup that answers nil.


Instance Method Detail

def [](key : String | Symbol) : YAML::Any #

Returns the value of key, raising KeyError when it is absent.


def []?(key : String | Symbol) : YAML::Any | Nil #

Returns the value of key, or nil when it is absent.


def config : YAML::Any #

Returns the decrypted YAML.

An empty file reads as an empty mapping, so #[]? answers nil rather than raising.


def dig(key : String | Symbol) : YAML::Any #

Returns the value of key, raising when it is absent.


def dig(keys : Enumerable) : YAML::Any #

Walks keys down the nested YAML, raising when one of them is absent.

Takes the keys as a collection rather than as a splat, for a path only known at runtime.


def dig(key : String | Symbol, *subkeys) : YAML::Any #

Walks key and subkeys down the nested YAML, raising when one of them is absent.


def dig?(key : String | Symbol) : YAML::Any | Nil #

Returns the value of key, or nil when it is absent.


def dig?(keys : Enumerable) : YAML::Any | Nil #

Walks keys down the nested YAML, returning nil when one of them or keys itself is empty or absent.

Takes the keys as a collection rather than as a splat, for a path only known at runtime.


def dig?(key : String | Symbol, *subkeys) : YAML::Any | Nil #

Walks key and subkeys down the nested YAML, returning nil when one of them is absent.


def fetch(key : String | Symbol) : YAML::Any #

Returns the value of key, raising KeyError when it is absent.

Named after Rails.application.credentials.fetch.


def has_key?(key : String | Symbol) : Bool #

Returns true when key is present.


def read : String #

Returns the decrypted contents, or an empty string when the file does not exist yet.

A missing file is empty, but a missing or malformed key still raises: an application whose secrets silently came back empty would fail much further from the cause.


def reload : Nil #

Forgets the decrypted YAML, so the next read goes back to the file.


def write(contents : String) : Nil #

Encrypts contents and replaces the file with it, then forgets the parsed YAML so the two cannot disagree. Rails keeps the parsed copy, since bin/rails credentials:edit exits right after writing.