Class: Rails::Secrets

Inherits:
Object show all
Defined in:
railties/lib/rails/secrets.rb

Overview

Greatly inspired by Ara T. Howard’s magnificent sekrets gem. 😘

Defined Under Namespace

Classes: MissingKeyError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.root=(value) ⇒ Object (writeonly)

Sets the attribute root

Parameters:

  • value

    the value to set the attribute root to.



22
23
24
# File 'railties/lib/rails/secrets.rb', line 22

def root=(value)
  @root = value
end

Class Method Details

.decrypt(data) ⇒ Object



45
46
47
# File 'railties/lib/rails/secrets.rb', line 45

def decrypt(data)
  encryptor.decrypt_and_verify(data)
end

.encrypt(data) ⇒ Object



41
42
43
# File 'railties/lib/rails/secrets.rb', line 41

def encrypt(data)
  encryptor.encrypt_and_sign(data)
end

.keyObject



37
38
39
# File 'railties/lib/rails/secrets.rb', line 37

def key
  ENV["RAILS_MASTER_KEY"] || read_key_file || handle_missing_key
end

.parse(paths, env:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'railties/lib/rails/secrets.rb', line 24

def parse(paths, env:)
  paths.each_with_object(Hash.new) do |path, all_secrets|
    require "erb"

    source = ERB.new(preprocess(path)).result
    secrets = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(source) : YAML.load(source)
    secrets ||= {}

    all_secrets.merge!(secrets["shared"].deep_symbolize_keys) if secrets["shared"]
    all_secrets.merge!(secrets[env].deep_symbolize_keys) if secrets[env]
  end
end

.readObject



49
50
51
# File 'railties/lib/rails/secrets.rb', line 49

def read
  decrypt(IO.binread(path))
end

.read_for_editing(&block) ⇒ Object



58
59
60
# File 'railties/lib/rails/secrets.rb', line 58

def read_for_editing(&block)
  writing(read, &block)
end

.write(contents) ⇒ Object



53
54
55
56
# File 'railties/lib/rails/secrets.rb', line 53

def write(contents)
  IO.binwrite("#{path}.tmp", encrypt(contents))
  FileUtils.mv("#{path}.tmp", path)
end