Skip to content

Commit bb30f05

Browse files
committed
Deprecate encrypted secrets in favor of credentials.
Allow edits of existing encrypted secrets generated on Rails 5.1, but refer to credentials when attempting to setup. This also removes the need for any of the setup code, so the generator can be ripped out altogether.
1 parent 99f4d6e commit bb30f05

File tree

6 files changed

+50
-167
lines changed

6 files changed

+50
-167
lines changed

railties/lib/rails/commands/secrets/secrets_command.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def help
1515
end
1616

1717
def setup
18-
generator.start
18+
deprecate_in_favor_of_credentials_and_exit
1919
end
2020

2121
def edit
@@ -42,11 +42,10 @@ def edit
4242
rescue Rails::Secrets::MissingKeyError => error
4343
say error.message
4444
rescue Errno::ENOENT => error
45-
raise unless error.message =~ /secrets\.yml\.enc/
46-
47-
Rails::Secrets.read_template_for_editing do |tmp_path|
48-
system("#{ENV["EDITOR"]} #{tmp_path}")
49-
generator.skip_secrets_file { setup }
45+
if error.message =~ /secrets\.yml\.enc/
46+
deprecate_in_favor_of_credentials_and_exit
47+
else
48+
raise
5049
end
5150
end
5251

@@ -55,11 +54,11 @@ def show
5554
end
5655

5756
private
58-
def generator
59-
require "rails/generators"
60-
require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator"
57+
def deprecate_in_favor_of_credentials_and_exit
58+
say "Encrypted secrets is deprecated in favor of credentials. Run:"
59+
say "bin/rails credentials --help"
6160

62-
Rails::Generators::EncryptedSecretsGenerator
61+
exit 1
6362
end
6463
end
6564
end

railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb

Lines changed: 0 additions & 72 deletions
This file was deleted.

railties/lib/rails/secrets.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,10 @@ def parse(paths, env:)
3232
end
3333
end
3434

35-
def generate_key
36-
SecureRandom.hex(OpenSSL::Cipher.new(@cipher).key_len)
37-
end
38-
3935
def key
4036
ENV["RAILS_MASTER_KEY"] || read_key_file || handle_missing_key
4137
end
4238

43-
def template
44-
<<-end_of_template.strip_heredoc
45-
# See `secrets.yml` for tips on generating suitable keys.
46-
# production:
47-
# external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289
48-
49-
end_of_template
50-
end
51-
5239
def encrypt(data)
5340
encryptor.encrypt_and_sign(data)
5441
end
@@ -70,10 +57,6 @@ def read_for_editing(&block)
7057
writing(read, &block)
7158
end
7259

73-
def read_template_for_editing(&block)
74-
writing(template, &block)
75-
end
76-
7760
private
7861
def handle_missing_key
7962
raise MissingKeyError

railties/test/commands/secrets_test.rb

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,38 @@
88
class Rails::Command::SecretsCommandTest < ActiveSupport::TestCase
99
include ActiveSupport::Testing::Isolation, EnvHelpers
1010

11-
def setup
12-
build_app
11+
setup :build_app
12+
teardown :teardown_app
13+
14+
test "edit without editor gives hint" do
15+
assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "")
1316
end
1417

15-
def teardown
16-
teardown_app
18+
test "encrypted secrets are deprecated when using credentials" do
19+
assert_match "Encrypted secrets is deprecated", run_setup_command
20+
assert_equal 1, $?.exitstatus
21+
assert_not File.exist?("config/secrets.yml.enc")
1722
end
1823

19-
test "edit without editor gives hint" do
20-
assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "")
24+
test "encrypted secrets are deprecated when running edit without setup" do
25+
assert_match "Encrypted secrets is deprecated", run_setup_command
26+
assert_equal 1, $?.exitstatus
27+
assert_not File.exist?("config/secrets.yml.enc")
28+
end
29+
30+
test "encrypted secrets are deprecated for 5.1 config/secrets.yml apps" do
31+
Dir.chdir(app_path) do
32+
FileUtils.rm("config/credentials.yml.enc")
33+
FileUtils.touch("config/secrets.yml")
34+
35+
assert_match "Encrypted secrets is deprecated", run_setup_command
36+
assert_equal 1, $?.exitstatus
37+
assert_not File.exist?("config/secrets.yml.enc")
38+
end
2139
end
2240

2341
test "edit secrets" do
24-
# Runs setup before first edit.
25-
assert_match(/Adding config\/secrets\.yml\.key to store the encryption key/, run_edit_command)
42+
prevent_deprecation
2643

2744
# Run twice to ensure encrypted secrets can be reread after first edit pass.
2845
2.times do
@@ -31,22 +48,30 @@ def teardown
3148
end
3249

3350
test "show secrets" do
34-
run_setup_command
51+
prevent_deprecation
52+
3553
assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289/, run_show_command)
3654
end
3755

3856
private
57+
def prevent_deprecation
58+
Dir.chdir(app_path) do
59+
File.write("config/secrets.yml.key", "f731758c639da2604dfb6bf3d1025de8")
60+
File.write("config/secrets.yml.enc", "sEB0mHxDbeP1/KdnMk00wyzPFACl9K6t0cZWn5/Mfx/YbTHvnI07vrneqHg9kaH3wOS7L6pIQteu1P077OtE4BSx/ZRc/sgQPHyWu/tXsrfHqnPNpayOF/XZqizE91JacSFItNMWpuPsp9ynbzz+7cGhoB1S4aPNIU6u0doMrzdngDbijsaAFJmsHIQh6t/QHoJx--8aMoE0PvUWmw1Iqz--ldFqnM/K0g9k17M8PKoN/Q==")
61+
end
62+
end
63+
3964
def run_edit_command(editor: "cat")
4065
switch_env("EDITOR", editor) do
41-
rails "secrets:edit"
66+
rails "secrets:edit", allow_failure: true
4267
end
4368
end
4469

4570
def run_show_command
46-
rails "secrets:show"
71+
rails "secrets:show", allow_failure: true
4772
end
4873

4974
def run_setup_command
50-
rails "secrets:setup"
75+
rails "secrets:setup", allow_failure: true
5176
end
5277
end

railties/test/generators/encrypted_secrets_generator_test.rb

Lines changed: 0 additions & 44 deletions
This file was deleted.

railties/test/secrets_test.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
# frozen_string_literal: true
22

33
require "isolation/abstract_unit"
4-
require "rails/generators"
5-
require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator"
64
require "rails/secrets"
75

86
class Rails::SecretsTest < ActiveSupport::TestCase
97
include ActiveSupport::Testing::Isolation
108

11-
def setup
12-
build_app
13-
end
14-
15-
def teardown
16-
teardown_app
17-
end
9+
setup :build_app
10+
teardown :teardown_app
1811

1912
test "setting read to false skips parsing" do
2013
run_secrets_generator do
@@ -172,9 +165,8 @@ def teardown
172165
private
173166
def run_secrets_generator
174167
Dir.chdir(app_path) do
175-
capture(:stdout) do
176-
Rails::Generators::EncryptedSecretsGenerator.start
177-
end
168+
File.write("config/secrets.yml.key", "f731758c639da2604dfb6bf3d1025de8")
169+
File.write("config/secrets.yml.enc", "sEB0mHxDbeP1/KdnMk00wyzPFACl9K6t0cZWn5/Mfx/YbTHvnI07vrneqHg9kaH3wOS7L6pIQteu1P077OtE4BSx/ZRc/sgQPHyWu/tXsrfHqnPNpayOF/XZqizE91JacSFItNMWpuPsp9ynbzz+7cGhoB1S4aPNIU6u0doMrzdngDbijsaAFJmsHIQh6t/QHoJx--8aMoE0PvUWmw1Iqz--ldFqnM/K0g9k17M8PKoN/Q==")
178170

179171
add_to_config <<-RUBY
180172
config.read_encrypted_secrets = true

0 commit comments

Comments
 (0)