railties 5.2.4.5 → 6.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of railties might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +185 -171
- data/MIT-LICENSE +1 -1
- data/RDOC_MAIN.rdoc +35 -28
- data/README.rdoc +1 -1
- data/lib/minitest/rails_plugin.rb +6 -10
- data/lib/rails/all.rb +4 -0
- data/lib/rails/api/generator.rb +2 -1
- data/lib/rails/api/task.rb +16 -0
- data/lib/rails/app_loader.rb +1 -1
- data/lib/rails/app_updater.rb +3 -1
- data/lib/rails/application.rb +21 -45
- data/lib/rails/application/configuration.rb +54 -12
- data/lib/rails/application/default_middleware_stack.rb +2 -0
- data/lib/rails/backtrace_cleaner.rb +5 -17
- data/lib/rails/code_statistics.rb +3 -3
- data/lib/rails/command.rb +11 -10
- data/lib/rails/command/actions.rb +0 -10
- data/lib/rails/command/base.rb +1 -5
- data/lib/rails/command/behavior.rb +4 -46
- data/lib/rails/command/environment_argument.rb +1 -11
- data/lib/rails/command/spellchecker.rb +58 -0
- data/lib/rails/commands/credentials/USAGE +19 -1
- data/lib/rails/commands/credentials/credentials_command.rb +42 -23
- data/lib/rails/commands/db/system/change/change_command.rb +20 -0
- data/lib/rails/commands/dbconsole/dbconsole_command.rb +2 -2
- data/lib/rails/commands/dev/dev_command.rb +17 -0
- data/lib/rails/commands/encrypted/encrypted_command.rb +2 -3
- data/lib/rails/commands/help/help_command.rb +1 -1
- data/lib/rails/commands/initializers/initializers_command.rb +16 -0
- data/lib/rails/commands/new/new_command.rb +2 -2
- data/lib/rails/commands/notes/notes_command.rb +39 -0
- data/lib/rails/commands/plugin/plugin_command.rb +1 -1
- data/lib/rails/commands/routes/routes_command.rb +37 -0
- data/lib/rails/commands/runner/runner_command.rb +6 -6
- data/lib/rails/commands/secrets/USAGE +3 -3
- data/lib/rails/commands/secrets/secrets_command.rb +3 -3
- data/lib/rails/commands/server/server_command.rb +109 -48
- data/lib/rails/configuration.rb +1 -7
- data/lib/rails/engine.rb +3 -9
- data/lib/rails/engine/configuration.rb +3 -1
- data/lib/rails/gem_version.rb +4 -4
- data/lib/rails/generators.rb +11 -12
- data/lib/rails/generators/actions.rb +48 -37
- data/lib/rails/generators/app_base.rb +49 -89
- data/lib/rails/generators/app_name.rb +50 -0
- data/lib/rails/generators/base.rb +0 -4
- data/lib/rails/generators/database.rb +57 -0
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +1 -1
- data/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt +1 -1
- data/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt +1 -1
- data/lib/rails/generators/generated_attribute.rb +17 -17
- data/lib/rails/generators/model_helpers.rb +8 -1
- data/lib/rails/generators/named_base.rb +1 -5
- data/lib/rails/generators/rails/app/app_generator.rb +37 -72
- data/lib/rails/generators/rails/app/templates/Gemfile.tt +3 -6
- data/lib/rails/generators/rails/app/templates/app/assets/config/manifest.js.tt +0 -3
- data/lib/rails/generators/rails/app/templates/app/{assets/javascripts/cable.js.tt → javascript/channels/consumer.js} +2 -9
- data/lib/rails/generators/rails/app/templates/app/javascript/channels/index.js +5 -0
- data/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt +15 -0
- data/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb.tt +5 -0
- data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +4 -4
- data/lib/rails/generators/rails/app/templates/bin/setup.tt +4 -5
- data/lib/rails/generators/rails/app/templates/bin/update.tt +6 -7
- data/lib/rails/generators/rails/app/templates/config/application.rb.tt +2 -0
- data/lib/rails/generators/rails/app/templates/config/cable.yml.tt +1 -1
- data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml.tt +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml.tt +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml.tt +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml.tt +3 -3
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml.tt +3 -3
- data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt +4 -4
- data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml.tt +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +6 -6
- data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml.tt +2 -2
- data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +3 -2
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +5 -12
- data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +6 -2
- data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +1 -1
- data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +4 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt +33 -0
- data/lib/rails/generators/rails/app/templates/config/locales/en.yml +1 -1
- data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +3 -5
- data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +1 -1
- data/lib/rails/generators/rails/app/templates/config/spring.rb.tt +6 -6
- data/lib/rails/generators/rails/app/templates/gitignore.tt +2 -7
- data/lib/rails/generators/rails/app/templates/package.json.tt +7 -1
- data/lib/rails/generators/rails/app/templates/ruby-version.tt +1 -1
- data/lib/rails/generators/rails/app/templates/test/channels/application_cable/connection_test.rb.tt +11 -0
- data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +7 -0
- data/lib/rails/generators/rails/assets/USAGE +1 -4
- data/lib/rails/generators/rails/assets/assets_generator.rb +0 -8
- data/lib/rails/generators/rails/controller/controller_generator.rb +11 -1
- data/lib/rails/generators/rails/credentials/credentials_generator.rb +7 -8
- data/lib/rails/generators/rails/db/system/change/change_generator.rb +55 -0
- data/lib/rails/generators/rails/encrypted_file/encrypted_file_generator.rb +4 -5
- data/lib/rails/generators/rails/helper/helper_generator.rb +5 -0
- data/lib/rails/generators/rails/plugin/plugin_generator.rb +9 -18
- data/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/app/helpers/%namespaced_name%/application_helper.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/app/jobs/%namespaced_name%/application_job.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/app/mailers/%namespaced_name%/application_mailer.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/app/models/%namespaced_name%/application_record.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/gitignore.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/railtie.rb.tt +1 -1
- data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +1 -2
- data/lib/rails/generators/resource_helpers.rb +1 -6
- data/lib/rails/generators/test_unit/integration/integration_generator.rb +6 -0
- data/lib/rails/generators/test_unit/job/job_generator.rb +5 -0
- data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +1 -1
- data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +1 -1
- data/lib/rails/generators/test_unit/system/system_generator.rb +5 -0
- data/lib/rails/generators/testing/behaviour.rb +3 -0
- data/lib/rails/info.rb +2 -2
- data/lib/rails/info_controller.rb +1 -1
- data/lib/rails/mailers_controller.rb +1 -1
- data/lib/rails/paths.rb +19 -9
- data/lib/rails/railtie.rb +1 -1
- data/lib/rails/ruby_version_check.rb +3 -3
- data/lib/rails/secrets.rb +0 -1
- data/lib/rails/source_annotation_extractor.rb +125 -117
- data/lib/rails/tasks/annotations.rake +9 -9
- data/lib/rails/tasks/dev.rake +5 -4
- data/lib/rails/tasks/framework.rake +5 -1
- data/lib/rails/tasks/initializers.rake +5 -4
- data/lib/rails/tasks/log.rake +0 -1
- data/lib/rails/tasks/routes.rake +4 -26
- data/lib/rails/tasks/statistics.rake +1 -0
- data/lib/rails/tasks/yarn.rake +1 -1
- data/lib/rails/templates/rails/welcome/index.html.erb +2 -2
- data/lib/rails/test_help.rb +11 -9
- data/lib/rails/test_unit/reporter.rb +1 -1
- data/lib/rails/test_unit/runner.rb +5 -5
- data/lib/rails/test_unit/testing.rake +1 -1
- metadata +30 -24
- data/lib/rails/commands/encrypted/USAGE +0 -28
- data/lib/rails/generators/js/assets/assets_generator.rb +0 -15
- data/lib/rails/generators/js/assets/templates/javascript.js +0 -2
- data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +0 -22
- data/lib/rails/generators/rails/app/templates/bin/bundle.tt +0 -2
- data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt +0 -38
- data/lib/rails/generators/rails/assets/templates/javascript.js +0 -2
@@ -13,6 +13,8 @@ module Rails
|
|
13
13
|
|
14
14
|
def build_stack
|
15
15
|
ActionDispatch::MiddlewareStack.new do |middleware|
|
16
|
+
middleware.use ::ActionDispatch::HostAuthorization, config.hosts, config.action_dispatch.hosts_response_app
|
17
|
+
|
16
18
|
if config.force_ssl
|
17
19
|
middleware.use ::ActionDispatch::SSL, config.ssl_options
|
18
20
|
end
|
@@ -5,30 +5,18 @@ require "active_support/backtrace_cleaner"
|
|
5
5
|
module Rails
|
6
6
|
class BacktraceCleaner < ActiveSupport::BacktraceCleaner
|
7
7
|
APP_DIRS_PATTERN = /^\/?(app|config|lib|test|\(\w*\))/
|
8
|
-
RENDER_TEMPLATE_PATTERN = /:in
|
9
|
-
EMPTY_STRING = ""
|
10
|
-
SLASH = "/"
|
11
|
-
DOT_SLASH = "./"
|
8
|
+
RENDER_TEMPLATE_PATTERN = /:in `.*_\w+_{2,3}\d+_\d+'/
|
9
|
+
EMPTY_STRING = ""
|
10
|
+
SLASH = "/"
|
11
|
+
DOT_SLASH = "./"
|
12
12
|
|
13
13
|
def initialize
|
14
14
|
super
|
15
|
-
@root = "#{Rails.root}/"
|
15
|
+
@root = "#{Rails.root}/"
|
16
16
|
add_filter { |line| line.sub(@root, EMPTY_STRING) }
|
17
17
|
add_filter { |line| line.sub(RENDER_TEMPLATE_PATTERN, EMPTY_STRING) }
|
18
18
|
add_filter { |line| line.sub(DOT_SLASH, SLASH) } # for tests
|
19
|
-
|
20
|
-
add_gem_filters
|
21
19
|
add_silencer { |line| !APP_DIRS_PATTERN.match?(line) }
|
22
20
|
end
|
23
|
-
|
24
|
-
private
|
25
|
-
def add_gem_filters
|
26
|
-
gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) }
|
27
|
-
return if gems_paths.empty?
|
28
|
-
|
29
|
-
gems_regexp = %r{(#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)}
|
30
|
-
gems_result = '\2 (\3) \4'.freeze
|
31
|
-
add_filter { |line| line.sub(gems_regexp, gems_result) }
|
32
|
-
end
|
33
21
|
end
|
34
22
|
end
|
@@ -46,7 +46,7 @@ class CodeStatistics #:nodoc:
|
|
46
46
|
|
47
47
|
if File.directory?(path) && (/^\./ !~ file_name)
|
48
48
|
stats.add(calculate_directory_statistics(path, pattern))
|
49
|
-
elsif file_name
|
49
|
+
elsif file_name&.match?(pattern)
|
50
50
|
stats.add_by_file_path(path)
|
51
51
|
end
|
52
52
|
end
|
@@ -95,8 +95,8 @@ class CodeStatistics #:nodoc:
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def print_line(name, statistics)
|
98
|
-
m_over_c = (statistics.methods / statistics.classes) rescue
|
99
|
-
loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue
|
98
|
+
m_over_c = (statistics.methods / statistics.classes) rescue 0
|
99
|
+
loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue 0
|
100
100
|
|
101
101
|
print "| #{name.ljust(20)} "
|
102
102
|
HEADERS.each_key do |k|
|
data/lib/rails/command.rb
CHANGED
@@ -4,7 +4,6 @@ require "active_support"
|
|
4
4
|
require "active_support/dependencies/autoload"
|
5
5
|
require "active_support/core_ext/enumerable"
|
6
6
|
require "active_support/core_ext/object/blank"
|
7
|
-
require "active_support/core_ext/hash/transform_values"
|
8
7
|
|
9
8
|
require "thor"
|
10
9
|
|
@@ -12,6 +11,7 @@ module Rails
|
|
12
11
|
module Command
|
13
12
|
extend ActiveSupport::Autoload
|
14
13
|
|
14
|
+
autoload :Spellchecker
|
15
15
|
autoload :Behavior
|
16
16
|
autoload :Base
|
17
17
|
|
@@ -83,20 +83,21 @@ module Rails
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def print_commands # :nodoc:
|
86
|
-
|
86
|
+
commands.each { |command| puts(" #{command}") }
|
87
87
|
end
|
88
88
|
|
89
|
-
|
90
|
-
|
89
|
+
private
|
90
|
+
COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new)
|
91
|
+
private_constant :COMMANDS_IN_USAGE
|
91
92
|
|
92
|
-
|
93
|
-
|
93
|
+
def commands
|
94
|
+
lookup!
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
visible_commands = (subclasses - hidden_commands).flat_map(&:printing_commands)
|
97
|
+
|
98
|
+
(visible_commands - COMMANDS_IN_USAGE).sort
|
99
|
+
end
|
98
100
|
|
99
|
-
private
|
100
101
|
def command_type # :doc:
|
101
102
|
@command_type ||= "command"
|
102
103
|
end
|
@@ -11,20 +11,10 @@ module Rails
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def require_application_and_environment!
|
14
|
-
require_application!
|
15
|
-
require_environment!
|
16
|
-
end
|
17
|
-
|
18
|
-
def require_application!
|
19
14
|
require ENGINE_PATH if defined?(ENGINE_PATH)
|
20
15
|
|
21
16
|
if defined?(APP_PATH)
|
22
17
|
require APP_PATH
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def require_environment!
|
27
|
-
if defined?(APP_PATH)
|
28
18
|
Rails.application.require_environment!
|
29
19
|
end
|
30
20
|
end
|
data/lib/rails/command/base.rb
CHANGED
@@ -17,10 +17,6 @@ module Rails
|
|
17
17
|
include Actions
|
18
18
|
|
19
19
|
class << self
|
20
|
-
def exit_on_failure? # :nodoc:
|
21
|
-
false
|
22
|
-
end
|
23
|
-
|
24
20
|
# Returns true when the app is a Rails engine.
|
25
21
|
def engine?
|
26
22
|
defined?(ENGINE_ROOT)
|
@@ -74,7 +70,7 @@ module Rails
|
|
74
70
|
end
|
75
71
|
|
76
72
|
def executable
|
77
|
-
"
|
73
|
+
"rails #{command_name}"
|
78
74
|
end
|
79
75
|
|
80
76
|
# Use Rails' default banner.
|
@@ -19,46 +19,6 @@ module Rails
|
|
19
19
|
end
|
20
20
|
|
21
21
|
private
|
22
|
-
|
23
|
-
# This code is based directly on the Text gem implementation.
|
24
|
-
# Copyright (c) 2006-2013 Paul Battley, Michael Neumann, Tim Fletcher.
|
25
|
-
#
|
26
|
-
# Returns a value representing the "cost" of transforming str1 into str2.
|
27
|
-
def levenshtein_distance(str1, str2) # :doc:
|
28
|
-
s = str1
|
29
|
-
t = str2
|
30
|
-
n = s.length
|
31
|
-
m = t.length
|
32
|
-
|
33
|
-
return m if (0 == n)
|
34
|
-
return n if (0 == m)
|
35
|
-
|
36
|
-
d = (0..m).to_a
|
37
|
-
x = nil
|
38
|
-
|
39
|
-
# avoid duplicating an enumerable object in the loop
|
40
|
-
str2_codepoint_enumerable = str2.each_codepoint
|
41
|
-
|
42
|
-
str1.each_codepoint.with_index do |char1, i|
|
43
|
-
e = i + 1
|
44
|
-
|
45
|
-
str2_codepoint_enumerable.with_index do |char2, j|
|
46
|
-
cost = (char1 == char2) ? 0 : 1
|
47
|
-
x = [
|
48
|
-
d[j + 1] + 1, # insertion
|
49
|
-
e + 1, # deletion
|
50
|
-
d[j] + cost # substitution
|
51
|
-
].min
|
52
|
-
d[j] = e
|
53
|
-
e = x
|
54
|
-
end
|
55
|
-
|
56
|
-
d[m] = x
|
57
|
-
end
|
58
|
-
|
59
|
-
x
|
60
|
-
end
|
61
|
-
|
62
22
|
# Prints a list of generators.
|
63
23
|
def print_list(base, namespaces)
|
64
24
|
return if namespaces.empty?
|
@@ -96,12 +56,10 @@ module Rails
|
|
96
56
|
def lookup!
|
97
57
|
$LOAD_PATH.each do |base|
|
98
58
|
Dir[File.join(base, *file_lookup_paths)].each do |path|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
# No problem
|
104
|
-
end
|
59
|
+
path = path.sub("#{base}/", "")
|
60
|
+
require path
|
61
|
+
rescue Exception
|
62
|
+
# No problem
|
105
63
|
end
|
106
64
|
end
|
107
65
|
end
|
@@ -8,23 +8,13 @@ module Rails
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
included do
|
11
|
-
argument :environment, optional: true, banner: "environment"
|
12
|
-
|
13
11
|
class_option :environment, aliases: "-e", type: :string,
|
14
12
|
desc: "Specifies the environment to run this console under (test/development/production)."
|
15
13
|
end
|
16
14
|
|
17
15
|
private
|
18
16
|
def extract_environment_option_from_argument
|
19
|
-
if environment
|
20
|
-
self.options = options.merge(environment: acceptable_environment(environment))
|
21
|
-
|
22
|
-
ActiveSupport::Deprecation.warn "Passing the environment's name as a " \
|
23
|
-
"regular argument is deprecated and " \
|
24
|
-
"will be removed in the next Rails " \
|
25
|
-
"version. Please, use the -e option " \
|
26
|
-
"instead."
|
27
|
-
elsif options[:environment]
|
17
|
+
if options[:environment]
|
28
18
|
self.options = options.merge(environment: acceptable_environment(options[:environment]))
|
29
19
|
else
|
30
20
|
self.options = options.merge(environment: Rails::Command.environment)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Command
|
5
|
+
module Spellchecker # :nodoc:
|
6
|
+
class << self
|
7
|
+
def suggest(word, from:)
|
8
|
+
if defined?(DidYouMean::SpellChecker)
|
9
|
+
DidYouMean::SpellChecker.new(dictionary: from.map(&:to_s)).correct(word).first
|
10
|
+
else
|
11
|
+
from.sort_by { |w| levenshtein_distance(word, w) }.first
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# This code is based directly on the Text gem implementation.
|
18
|
+
# Copyright (c) 2006-2013 Paul Battley, Michael Neumann, Tim Fletcher.
|
19
|
+
#
|
20
|
+
# Returns a value representing the "cost" of transforming str1 into str2.
|
21
|
+
def levenshtein_distance(str1, str2) # :doc:
|
22
|
+
s = str1
|
23
|
+
t = str2
|
24
|
+
n = s.length
|
25
|
+
m = t.length
|
26
|
+
|
27
|
+
return m if 0 == n
|
28
|
+
return n if 0 == m
|
29
|
+
|
30
|
+
d = (0..m).to_a
|
31
|
+
x = nil
|
32
|
+
|
33
|
+
# avoid duplicating an enumerable object in the loop
|
34
|
+
str2_codepoint_enumerable = str2.each_codepoint
|
35
|
+
|
36
|
+
str1.each_codepoint.with_index do |char1, i|
|
37
|
+
e = i + 1
|
38
|
+
|
39
|
+
str2_codepoint_enumerable.with_index do |char2, j|
|
40
|
+
cost = (char1 == char2) ? 0 : 1
|
41
|
+
x = [
|
42
|
+
d[j + 1] + 1, # insertion
|
43
|
+
e + 1, # deletion
|
44
|
+
d[j] + cost # substitution
|
45
|
+
].min
|
46
|
+
d[j] = e
|
47
|
+
e = x
|
48
|
+
end
|
49
|
+
|
50
|
+
d[m] = x
|
51
|
+
end
|
52
|
+
|
53
|
+
x
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -14,7 +14,7 @@ that just contains the secret_key_base used by MessageVerifiers/MessageEncryptor
|
|
14
14
|
signing and encrypting cookies.
|
15
15
|
|
16
16
|
For applications created prior to Rails 5.2, we'll automatically generate a new
|
17
|
-
credentials file in `config/credentials.yml.enc` the first time you run `
|
17
|
+
credentials file in `config/credentials.yml.enc` the first time you run `rails credentials:edit`.
|
18
18
|
If you didn't have a master key saved in `config/master.key`, that'll be created too.
|
19
19
|
|
20
20
|
Don't lose this master key! Put it in a password manager your team can access.
|
@@ -38,3 +38,21 @@ the encrypted credentials.
|
|
38
38
|
When the temporary file is next saved the contents are encrypted and written to
|
39
39
|
`config/credentials.yml.enc` while the file itself is destroyed to prevent credentials
|
40
40
|
from leaking.
|
41
|
+
|
42
|
+
=== Environment Specific Credentials
|
43
|
+
|
44
|
+
The `credentials` command supports passing an `--environment` option to create an
|
45
|
+
environment specific override. That override will takes precedence over the
|
46
|
+
global `config/credentials.yml.enc` file when running in that environment. So:
|
47
|
+
|
48
|
+
rails credentials:edit --environment development
|
49
|
+
|
50
|
+
will create `config/credentials/development.yml.enc` with the corresponding
|
51
|
+
encryption key in `config/credentials/development.key` if the credentials file
|
52
|
+
doesn't exist.
|
53
|
+
|
54
|
+
The encryption key can also be put in `ENV["RAILS_MASTER_KEY"]`, which takes
|
55
|
+
precedence over the file encryption key.
|
56
|
+
|
57
|
+
In addition to that, the default credentials lookup paths can be overriden through
|
58
|
+
`config.credentials.content_path` and `config.credentials.key_path`.
|
@@ -8,6 +8,9 @@ module Rails
|
|
8
8
|
class CredentialsCommand < Rails::Command::Base # :nodoc:
|
9
9
|
include Helpers::Editor
|
10
10
|
|
11
|
+
class_option :environment, aliases: "-e", type: :string,
|
12
|
+
desc: "Uses credentials from config/credentials/:environment.yml.enc encrypted by config/credentials/:environment.key key"
|
13
|
+
|
11
14
|
no_commands do
|
12
15
|
def help
|
13
16
|
say "Usage:\n #{self.class.banner}"
|
@@ -17,62 +20,78 @@ module Rails
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def edit
|
20
|
-
|
23
|
+
require_application_and_environment!
|
21
24
|
|
22
25
|
ensure_editor_available(command: "bin/rails credentials:edit") || (return)
|
23
|
-
|
26
|
+
|
27
|
+
ensure_encryption_key_has_been_added if credentials.key.nil?
|
24
28
|
ensure_credentials_have_been_added
|
25
29
|
|
26
30
|
catch_editing_exceptions do
|
27
31
|
change_credentials_in_system_editor
|
28
32
|
end
|
29
33
|
|
30
|
-
say "
|
34
|
+
say "File encrypted and saved."
|
35
|
+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
|
36
|
+
say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
|
31
37
|
end
|
32
38
|
|
33
39
|
def show
|
34
|
-
|
40
|
+
require_application_and_environment!
|
35
41
|
|
36
|
-
say
|
42
|
+
say credentials.read.presence || missing_credentials_message
|
37
43
|
end
|
38
44
|
|
39
45
|
private
|
40
|
-
def
|
41
|
-
|
42
|
-
|
46
|
+
def credentials
|
47
|
+
Rails.application.encrypted(content_path, key_path: key_path)
|
48
|
+
end
|
49
|
+
|
50
|
+
def ensure_encryption_key_has_been_added
|
51
|
+
encryption_key_file_generator.add_key_file(key_path)
|
52
|
+
encryption_key_file_generator.ignore_key_file(key_path)
|
43
53
|
end
|
44
54
|
|
45
55
|
def ensure_credentials_have_been_added
|
46
|
-
|
56
|
+
encrypted_file_generator.add_encrypted_file_silently(content_path, key_path)
|
47
57
|
end
|
48
58
|
|
49
59
|
def change_credentials_in_system_editor
|
50
|
-
|
60
|
+
credentials.change do |tmp_path|
|
51
61
|
system("#{ENV["EDITOR"]} #{tmp_path}")
|
52
62
|
end
|
53
63
|
end
|
54
64
|
|
65
|
+
def missing_credentials_message
|
66
|
+
if credentials.key.nil?
|
67
|
+
"Missing '#{key_path}' to decrypt credentials. See `rails credentials:help`"
|
68
|
+
else
|
69
|
+
"File '#{content_path}' does not exist. Use `rails credentials:edit` to change that."
|
70
|
+
end
|
71
|
+
end
|
55
72
|
|
56
|
-
def master_key_generator
|
57
|
-
require "rails/generators"
|
58
|
-
require "rails/generators/rails/master_key/master_key_generator"
|
59
73
|
|
60
|
-
|
74
|
+
def content_path
|
75
|
+
options[:environment] ? "config/credentials/#{options[:environment]}.yml.enc" : "config/credentials.yml.enc"
|
61
76
|
end
|
62
77
|
|
63
|
-
def
|
78
|
+
def key_path
|
79
|
+
options[:environment] ? "config/credentials/#{options[:environment]}.key" : "config/master.key"
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def encryption_key_file_generator
|
64
84
|
require "rails/generators"
|
65
|
-
require "rails/generators/rails/
|
85
|
+
require "rails/generators/rails/encryption_key_file/encryption_key_file_generator"
|
66
86
|
|
67
|
-
Rails::Generators::
|
87
|
+
Rails::Generators::EncryptionKeyFileGenerator.new
|
68
88
|
end
|
69
89
|
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
90
|
+
def encrypted_file_generator
|
91
|
+
require "rails/generators"
|
92
|
+
require "rails/generators/rails/encrypted_file/encrypted_file_generator"
|
93
|
+
|
94
|
+
Rails::Generators::EncryptedFileGenerator.new
|
76
95
|
end
|
77
96
|
end
|
78
97
|
end
|