blob: 23dbed20ac3f8f37252670cf247682d3043344c4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# frozen_string_literal: true
require "rails/generators/base"
require "pathname"
require "active_support/encrypted_file"
module Rails
module Generators
class MasterKeyGenerator < Base
MASTER_KEY_PATH = Pathname.new("config/master.key")
def add_master_key_file
unless MASTER_KEY_PATH.exist?
key = ActiveSupport::EncryptedFile.generate_key
log "Adding #{MASTER_KEY_PATH} to store the master encryption key: #{key}"
log ""
log "Save this in a password manager your team can access."
log ""
log "If you lose the key, no one, including you, can access anything encrypted with it."
log ""
create_file MASTER_KEY_PATH, key
log ""
ignore_master_key_file
end
end
private
def ignore_master_key_file
if File.exist?(".gitignore")
unless File.read(".gitignore").include?(key_ignore)
log "Ignoring #{MASTER_KEY_PATH} so it won't end up in Git history:"
log ""
append_to_file ".gitignore", key_ignore
log ""
end
else
log "IMPORTANT: Don't commit #{MASTER_KEY_PATH}. Add this to your ignore file:"
log key_ignore, :on_green
log ""
end
end
def key_ignore
[ "", "# Ignore master key for decrypting credentials and more.", "/#{MASTER_KEY_PATH}", "" ].join("\n")
end
end
end
end
|