mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make master key added to gitignore the same value as when creating appplication
For gitignore generated by `rails new`, key with a leading slash is specified.
69f976b859/railties/lib/rails/generators/rails/app/templates/gitignore (L11)
Therefore, when executing `credentials:edit`, also need leading slack.
In order to avoid such a difference, fixed to use same method for
`rails new` and `credentials:edit`.
This commit is contained in:
parent
69f976b859
commit
d5d41c8281
4 changed files with 45 additions and 17 deletions
|
@ -53,7 +53,6 @@ module Rails
|
||||||
|
|
||||||
def ensure_master_key_has_been_added
|
def ensure_master_key_has_been_added
|
||||||
master_key_generator.add_master_key_file
|
master_key_generator.add_master_key_file
|
||||||
master_key_generator.ignore_master_key_file
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_credentials_have_been_added
|
def ensure_credentials_have_been_added
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
# Ignore bundler config.
|
# Ignore bundler config.
|
||||||
/.bundle
|
/.bundle
|
||||||
|
|
||||||
# Ignore master key for decrypting credentials and more.
|
|
||||||
/config/master.key
|
|
||||||
|
|
||||||
<% if sqlite3? -%>
|
<% if sqlite3? -%>
|
||||||
# Ignore the default SQLite database.
|
# Ignore the default SQLite database.
|
||||||
/db/*.sqlite3
|
/db/*.sqlite3
|
||||||
|
|
|
@ -22,6 +22,8 @@ module Rails
|
||||||
say ""
|
say ""
|
||||||
add_master_key_file_silently key
|
add_master_key_file_silently key
|
||||||
say ""
|
say ""
|
||||||
|
|
||||||
|
ignore_master_key_file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -29,24 +31,24 @@ module Rails
|
||||||
create_file MASTER_KEY_PATH, key || ActiveSupport::EncryptedFile.generate_key
|
create_file MASTER_KEY_PATH, key || ActiveSupport::EncryptedFile.generate_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def ignore_master_key_file
|
private
|
||||||
if File.exist?(".gitignore")
|
def ignore_master_key_file
|
||||||
unless File.read(".gitignore").include?(key_ignore)
|
if File.exist?(".gitignore")
|
||||||
say "Ignoring #{MASTER_KEY_PATH} so it won't end up in Git history:"
|
unless File.read(".gitignore").include?(key_ignore)
|
||||||
say ""
|
say "Ignoring #{MASTER_KEY_PATH} so it won't end up in Git history:"
|
||||||
append_to_file ".gitignore", key_ignore
|
say ""
|
||||||
|
append_to_file ".gitignore", key_ignore
|
||||||
|
say ""
|
||||||
|
end
|
||||||
|
else
|
||||||
|
say "IMPORTANT: Don't commit #{MASTER_KEY_PATH}. Add this to your ignore file:"
|
||||||
|
say key_ignore, :on_green
|
||||||
say ""
|
say ""
|
||||||
end
|
end
|
||||||
else
|
|
||||||
say "IMPORTANT: Don't commit #{MASTER_KEY_PATH}. Add this to your ignore file:"
|
|
||||||
say key_ignore, :on_green
|
|
||||||
say ""
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def key_ignore
|
def key_ignore
|
||||||
[ "", "# Ignore master key for decrypting credentials and more.", MASTER_KEY_PATH, "" ].join("\n")
|
[ "", "# Ignore master key for decrypting credentials and more.", "/#{MASTER_KEY_PATH}", "" ].join("\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
30
railties/test/commands/credentials_test.rb
Normal file
30
railties/test/commands/credentials_test.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "isolation/abstract_unit"
|
||||||
|
require "env_helpers"
|
||||||
|
require "rails/command"
|
||||||
|
require "rails/commands/credentials/credentials_command"
|
||||||
|
|
||||||
|
class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
|
||||||
|
include ActiveSupport::Testing::Isolation, EnvHelpers
|
||||||
|
|
||||||
|
setup { build_app }
|
||||||
|
|
||||||
|
teardown { teardown_app }
|
||||||
|
|
||||||
|
test "edit command does not add master key to gitignore when already exist" do
|
||||||
|
run_edit_command
|
||||||
|
|
||||||
|
Dir.chdir(app_path) do
|
||||||
|
gitignore = File.read(".gitignore")
|
||||||
|
assert_equal 1, gitignore.scan(%r|config/master\.key|).length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def run_edit_command(editor: "cat")
|
||||||
|
switch_env("EDITOR", editor) do
|
||||||
|
rails "credentials:edit"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue