Class: Rails::Generators::EncryptionKeyFileGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/rails/commands/encrypted_command.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_key_file(key_path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rails/commands/encrypted_command.rb', line 151

def add_key_file(key_path)
  key_path = Pathname.new(key_path)

  unless key_path.exist?
    key = ActiveSupport::EncryptedFile.generate_key

    log "Adding #{key_path} to store the 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 ""
    add_key_file_silently(key_path, key)
    log ""
  end
end

#add_key_file_silently(key_path, key = nil) ⇒ Object



169
170
171
172
# File 'lib/rails/commands/encrypted_command.rb', line 169

def add_key_file_silently(key_path, key = nil)
  create_file key_path, key || ActiveSupport::EncryptedFile.generate_key
  key_path.chmod 0600
end

#ignore_key_file(key_path, ignore: key_ignore(key_path)) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rails/commands/encrypted_command.rb', line 174

def ignore_key_file(key_path, ignore: key_ignore(key_path))
  if File.exist?(".gitignore")
    unless File.read(".gitignore").include?(ignore)
      log "Ignoring #{key_path} so it won't end up in Git history:"
      log ""
      append_to_file ".gitignore", ignore
      log ""
    end
  else
    log "IMPORTANT: Don't commit #{key_path}. Add this to your ignore file:"
    log ignore, :on_green
    log ""
  end
end

#ignore_key_file_silently(key_path, ignore: key_ignore(key_path)) ⇒ Object



189
190
191
# File 'lib/rails/commands/encrypted_command.rb', line 189

def ignore_key_file_silently(key_path, ignore: key_ignore(key_path))
  append_to_file ".gitignore", ignore if File.exist?(".gitignore")
end