Sha256: f18e302d8d8c4f0a3272a4531d7f31d6f938763ecad3b477c09e9844d6fe03a1

Contents?: true

Size: 1.6 KB

Versions: 74

Compression:

Stored size: 1.6 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 # :nodoc:
      def add_credentials_file
        unless credentials.content_path.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)
        unless credentials.content_path.exist?
          credentials.write(credentials_template)
        end
      end

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

        def credentials_template
          <<~YAML
            # aws:
            #   access_key_id: 123
            #   secret_access_key: 345

            # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
            secret_key_base: #{SecureRandom.hex(64)}
          YAML
        end
    end
  end
end

Version data entries

74 entries across 70 versions & 6 rubygems

Version Path
railties-7.0.8.6 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.10 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.9 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.8.5 lib/rails/generators/rails/credentials/credentials_generator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/railties-7.0.8.4/lib/rails/generators/rails/credentials/credentials_generator.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/credentials/credentials_generator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/credentials/credentials_generator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.8.4 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.8 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.8.1 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.7 lib/rails/generators/rails/credentials/credentials_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.3.1/lib/rails/generators/rails/credentials/credentials_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.2.3/lib/rails/generators/rails/credentials/credentials_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-6.1.6.1/lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.8 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.7.2 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.6 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-7.0.7.1 lib/rails/generators/rails/credentials/credentials_generator.rb
railties-6.1.7.5 lib/rails/generators/rails/credentials/credentials_generator.rb