Sha256: f8a26fc25d8f3c52d0bdcc79e7e404a201f8e5f97cf92aad2853d2a53fda2821

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "rails/generators/base"
require "rails/generators/rails/master_key/master_key_generator"
require "active_support/encrypted_configuration"

module Rails
  module Generators
    class CredentialsGenerator < Base
      def add_credentials_file
        unless credentials.exist?
          template = credentials_template

          say "Adding #{credentials.content_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)
        credentials.write(credentials_template)
      end

      private
        def credentials
          ActiveSupport::EncryptedConfiguration.new(
            config_path: "config/credentials.yml.enc",
            key_path: "config/master.key",
            env_key: "RAILS_MASTER_KEY"
          )
        end

        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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
railties-5.2.0.beta2 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-5.2.0.beta1 lib/rails/generators/rails/credentials/credentials_generator.rb