Class: Rails::Secrets

Inherits:
Object
  • Object
show all
Defined in:
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 'lib/rails/secrets.rb', line 23

def root=(value)
  @root = value
end

Class Method Details

.decrypt(data) ⇒ Object



43
44
45
# File 'lib/rails/secrets.rb', line 43

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

.encrypt(data) ⇒ Object



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

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

.keyObject



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

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 '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



47
48
49
# File 'lib/rails/secrets.rb', line 47

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

.read_for_editing(&block) ⇒ Object



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

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

.write(contents) ⇒ Object



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

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