aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/generators/rails/credentials/credentials_generator.rb
blob: 21ca5668186770edd3280833e1fb6164a2f2655c (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
# frozen_string_literal: true

require_relative "../../base"
require_relative "../master_key/master_key_generator"
require "active_support/encrypted_configuration"

module Rails
  module Generators
    class CredentialsGenerator < Base
      CONFIG_PATH = "config/credentials.yml.enc"
      KEY_PATH    = "config/master.key"

      def add_credentials_file
        unless File.exist?(CONFIG_PATH)
          template = credentials_template

          say "Adding #{CONFIG_PATH} to store encrypted credentials."
          say ""
          say "The following content has been encrypted with the Rails master key:"
          say ""
          say template, :on_green
          say ""

          add_credentials_file_silently(template)

          say "You can edit encrypted credentials with `bin/rails credentials:edit`."
          say ""
        end
      end

      def add_credentials_file_silently(template = nil)
        unless File.exist?(CONFIG_PATH)
          setup = { config_path: CONFIG_PATH, key_path: KEY_PATH, env_key: "RAILS_MASTER_KEY" }
          ActiveSupport::EncryptedConfiguration.new(setup).write(credentials_template)
        end
      end

      private
        def credentials_template
          "# aws:\n#  access_key_id: 123\n#  secret_access_key: 345\n\n" +
          "# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.\n" +
          "secret_key_base: #{SecureRandom.hex(64)}"
        end
    end
  end
end