Skip to content

Commit

Permalink
Add master key to gitignore on rails new
Browse files Browse the repository at this point in the history
We generate master key on `rails new`.
Therefore, if do not add master key to `.gitginore` on `rails new`as
well, there is a possibility that the master key will be committed
accidentally.
  • Loading branch information
y-yagi committed Nov 15, 2017
1 parent 376094c commit f27319a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -162,7 +162,9 @@ def master_key
return if options[:pretend] || options[:dummy_app]

require "rails/generators/rails/master_key/master_key_generator"
Rails::Generators::MasterKeyGenerator.new([], quiet: options[:quiet]).add_master_key_file_silently
master_key_generator = Rails::Generators::MasterKeyGenerator.new([], quiet: options[:quiet])
master_key_generator.add_master_key_file_silently
master_key_generator.ignore_master_key_file_silently
end

def credentials
Expand Down
Expand Up @@ -44,6 +44,10 @@ def ignore_key_file(key_path, ignore: key_ignore(key_path))
end
end

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

private
def key_ignore(key_path)
[ "", "/#{key_path}", "" ].join("\n")
Expand Down
Expand Up @@ -34,6 +34,10 @@ def ignore_master_key_file
key_file_generator.ignore_key_file(MASTER_KEY_PATH, ignore: key_ignore)
end

def ignore_master_key_file_silently
key_file_generator.ignore_key_file_silently(MASTER_KEY_PATH, ignore: key_ignore)
end

private
def key_file_generator
EncryptionKeyFileGenerator.new([], options)
Expand Down
8 changes: 8 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -838,6 +838,14 @@ def test_after_bundle_callback
assert_equal 5, @sequence_step
end

def test_gitignore
run_generator

assert_file ".gitignore" do |content|
assert_match(/config\/master\.key/, content)
end
end

def test_system_tests_directory_generated
run_generator

Expand Down

0 comments on commit f27319a

Please sign in to comment.