Class: Quails::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.



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

def root=(value)
  @root = value
end

Class Method Details

.decrypt(data) ⇒ Object



56
57
58
# File 'railties/lib/rails/secrets.rb', line 56

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

.encrypt(data) ⇒ Object



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

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

.generate_keyObject



35
36
37
# File 'railties/lib/rails/secrets.rb', line 35

def generate_key
  SecureRandom.hex(OpenSSL::Cipher.new(@cipher).key_len)
end

.keyObject



39
40
41
# File 'railties/lib/rails/secrets.rb', line 39

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

.parse(paths, env:) ⇒ Object



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

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

    secrets = YAML.load(ERB.new(preprocess(path)).result) || {}
    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



60
61
62
# File 'railties/lib/rails/secrets.rb', line 60

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

.read_for_editing(&block) ⇒ Object



69
70
71
# File 'railties/lib/rails/secrets.rb', line 69

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

.read_template_for_editing(&block) ⇒ Object



73
74
75
# File 'railties/lib/rails/secrets.rb', line 73

def read_template_for_editing(&block)
  writing(template, &block)
end

.templateObject



43
44
45
46
47
48
49
50
# File 'railties/lib/rails/secrets.rb', line 43

def template
  "    # See `secrets.yml` for tips on generating suitable keys.\n    # production:\n    #   external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289\n\n  end_of_template\nend\n".strip_heredoc

.write(contents) ⇒ Object



64
65
66
67
# File 'railties/lib/rails/secrets.rb', line 64

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