123456789_123456789_123456789_123456789_123456789_

Class: Rails::Command::CredentialsCommand

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base, Thor
Instance Chain:
Inherits: Rails::Command::Base
Defined in: railties/lib/rails/commands/credentials/credentials_command.rb

Constant Summary

Diffing - Included

GITATTRIBUTES_ENTRY

Class Attribute Summary

Base - Inherited

.bin, .bin?,
.engine?

Returns true when the app is a Rails engine.

.exit_on_failure?

Class Method Summary

Base - Inherited

.banner,
.base_name

Sets the base_name taking into account the current class namespace.

.command_name

Return command name without namespaces.

.default_command_root

Default file root to place extra files a command might need, placed one folder above the command file.

.desc

Tries to get the description from a USAGE file one folder above the command root.

.executable,
.hide_command!

Convenience method to hide this command from the available ones when running rails command.

.namespace

Convenience method to get the namespace from the class name.

.printing_commands,
.usage_path

Path to lookup a USAGE description in a file.

.create_command

Allow the command method to be called perform.

.namespaced_name, .resolve_path, .class_usage,
.help

Override Thor’s class-level help to also show the USAGE.

.inherited, .perform

Instance Attribute Summary

Instance Method Summary

Instance Method Details

#change_credentials_in_system_editor (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 97

def change_credentials_in_system_editor
  using_system_editor do
    say "Editing #{content_path}..."
    credentials.change { |tmp_path| system_editor(tmp_path) }
    say "File encrypted and saved."
    warn_if_credentials_are_invalid
  end
rescue ActiveSupport::EncryptedFile::MissingKeyError => error
  say error.message
rescue ActiveSupport::MessageEncryptor::InvalidMessage
  say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
end

#config (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 61

def config
  Rails.application.config.credentials
end

#content_path (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 65

def content_path
  @content_path ||= relative_path(config.content_path)
end

#credentials (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 73

def credentials
  @credentials ||= Rails.application.encrypted(content_path, key_path: key_path)
end

#diff(content_path = nil)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 46

def diff(content_path = nil)
  if @content_path = content_path
    self.environment = extract_environment_from_path(content_path)
    load_environment_config!

    say credentials.read.presence || credentials.content_path.read
  else
    disenroll_project_from_credentials_diffing if options[:disenroll]
    enroll_project_in_credentials_diffing if options[:enroll]
  end
rescue ActiveSupport::MessageEncryptor::InvalidMessage
  say credentials.content_path.read
end

#edit

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 18

def edit
  load_environment_config!
  load_generators

  if environment_specified?
    @content_path = "config/credentials/#{environment}.yml.enc" unless config.overridden?(:content_path)
    @key_path = "config/credentials/#{environment}.key" unless config.overridden?(:key_path)
  end

  ensure_encryption_key_has_been_added
  ensure_credentials_have_been_added
  ensure_diffing_driver_is_configured

  change_credentials_in_system_editor
end

#ensure_credentials_have_been_added (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 87

def ensure_credentials_have_been_added
  require "rails/generators/rails/credentials/credentials_generator"

  Rails::Generators::CredentialsGenerator.new(
    [content_path, key_path],
    skip_secret_key_base: environment_specified? && %w[development test].include?(environment),
    quiet: true
  ).invoke_all
end

#ensure_encryption_key_has_been_added (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 77

def ensure_encryption_key_has_been_added
  return if credentials.key?

  require "rails/generators/rails/encryption_key_file/encryption_key_file_generator"

  encryption_key_file_generator = Rails::Generators::EncryptionKeyFileGenerator.new
  encryption_key_file_generator.add_key_file(key_path)
  encryption_key_file_generator.ignore_key_file(key_path)
end

#extract_environment_from_path(path) (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 130

def extract_environment_from_path(path)
  available_environments.find { |env| path.end_with?("#{env}.yml.enc") }
end

#key_path (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 69

def key_path
  @key_path ||= relative_path(config.key_path)
end

#missing_credentials_message (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 118

def missing_credentials_message
  if !credentials.key?
    "Missing '#{key_path}' to decrypt credentials. See `#{executable(:help)}`."
  else
    "File '#{content_path}' does not exist. Use `#{executable(:edit)}` to change that."
  end
end

#relative_path(path) (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 126

def relative_path(path)
  Rails.root.join(path).relative_path_from(Rails.root).to_s
end

#show

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 35

def show
  load_environment_config!

  say credentials.read.presence || missing_credentials_message
end

#warn_if_credentials_are_invalid (private)

[ GitHub ]

  
# File 'railties/lib/rails/commands/credentials/credentials_command.rb', line 110

def warn_if_credentials_are_invalid
  credentials.validate!
rescue ActiveSupport::EncryptedConfiguration::InvalidContentError => error
  say "WARNING: #{error.message}", :red
  say ""
  say "Your application will not be able to load '#{content_path}' until the error has been fixed.", :red
end