2020-06-24 08:09:24 -04:00
|
|
|
# WARNING: Before you make a change to secrets.yml, read the development guide for GitLab secrets
|
|
|
|
# doc/development/application_secrets.md.
|
2019-05-10 14:38:23 -04:00
|
|
|
#
|
2018-05-19 09:03:29 -04:00
|
|
|
# This file needs to be loaded BEFORE any initializers that attempt to
|
|
|
|
# prepend modules that require access to secrets (e.g. EE's 0_as_concern.rb).
|
|
|
|
#
|
2011-10-08 17:36:38 -04:00
|
|
|
# Be sure to restart your server when you modify this file.
|
|
|
|
|
2013-05-22 19:55:48 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
# Transition material in .secret to the secret_key_base key in config/secrets.yml.
|
|
|
|
# Historically, ENV['SECRET_KEY_BASE'] takes precedence over .secret, so we maintain that
|
|
|
|
# behavior.
|
|
|
|
#
|
|
|
|
# It also used to be the case that the key material in ENV['SECRET_KEY_BASE'] or .secret
|
|
|
|
# was used to encrypt OTP (two-factor authentication) data so if present, we copy that key
|
|
|
|
# material into config/secrets.yml under otp_key_base.
|
|
|
|
#
|
|
|
|
# Finally, if we have successfully migrated all secrets to config/secrets.yml, delete the
|
|
|
|
# .secret file to avoid confusion.
|
|
|
|
#
|
2016-07-15 08:19:29 -04:00
|
|
|
def create_tokens
|
|
|
|
secret_file = Rails.root.join('.secret')
|
2016-07-20 10:10:28 -04:00
|
|
|
file_secret_key = File.read(secret_file).chomp if File.exist?(secret_file)
|
|
|
|
env_secret_key = ENV['SECRET_KEY_BASE']
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-07-19 10:12:40 -04:00
|
|
|
# Ensure environment variable always overrides secrets.yml.
|
2016-07-20 10:10:28 -04:00
|
|
|
Rails.application.secrets.secret_key_base = env_secret_key if env_secret_key.present?
|
2016-07-19 10:12:40 -04:00
|
|
|
|
2016-07-17 06:01:38 -04:00
|
|
|
defaults = {
|
2016-07-20 10:10:28 -04:00
|
|
|
secret_key_base: file_secret_key || generate_new_secure_token,
|
|
|
|
otp_key_base: env_secret_key || file_secret_key || generate_new_secure_token,
|
2016-12-09 12:36:50 -05:00
|
|
|
db_key_base: generate_new_secure_token,
|
2020-06-16 14:09:01 -04:00
|
|
|
openid_connect_signing_key: generate_new_rsa_private_key
|
2016-07-17 06:01:38 -04:00
|
|
|
}
|
2016-07-15 08:19:29 -04:00
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
missing_secrets = set_missing_keys(defaults)
|
|
|
|
write_secrets_yml(missing_secrets) unless missing_secrets.empty?
|
|
|
|
|
|
|
|
begin
|
|
|
|
File.delete(secret_file) if file_secret_key
|
|
|
|
rescue => e
|
|
|
|
warn "Error deleting useless .secret file: #{e}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_new_secure_token
|
|
|
|
SecureRandom.hex(64)
|
|
|
|
end
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
def generate_new_rsa_private_key
|
|
|
|
OpenSSL::PKey::RSA.new(2048).to_pem
|
|
|
|
end
|
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
def warn_missing_secret(secret)
|
2020-06-02 08:08:33 -04:00
|
|
|
return if Rails.env.test?
|
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
warn "Missing Rails.application.secrets.#{secret} for #{Rails.env} environment. The secret will be generated and stored in config/secrets.yml."
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_missing_keys(defaults)
|
|
|
|
defaults.stringify_keys.each_with_object({}) do |(key, default), missing|
|
2016-07-17 06:01:38 -04:00
|
|
|
if Rails.application.secrets[key].blank?
|
|
|
|
warn_missing_secret(key)
|
2016-07-15 08:19:29 -04:00
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
missing[key] = Rails.application.secrets[key] = default
|
2016-07-17 06:01:38 -04:00
|
|
|
end
|
2016-07-15 08:19:29 -04:00
|
|
|
end
|
2016-07-20 10:10:28 -04:00
|
|
|
end
|
2016-07-15 08:19:29 -04:00
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
def write_secrets_yml(missing_secrets)
|
|
|
|
secrets_yml = Rails.root.join('config/secrets.yml')
|
|
|
|
rails_env = Rails.env.to_s
|
|
|
|
secrets = YAML.load_file(secrets_yml) if File.exist?(secrets_yml)
|
|
|
|
secrets ||= {}
|
|
|
|
secrets[rails_env] ||= {}
|
|
|
|
|
|
|
|
secrets[rails_env].merge!(missing_secrets) do |key, old, new|
|
|
|
|
# Previously, it was possible this was set to the literal contents of an Erb
|
|
|
|
# expression that evaluated to an empty value. We don't want to support that
|
|
|
|
# specifically, just ensure we don't break things further.
|
|
|
|
#
|
|
|
|
if old.present?
|
|
|
|
warn <<EOM
|
2016-07-19 10:12:40 -04:00
|
|
|
Rails.application.secrets.#{key} was blank, but the literal value in config/secrets.yml was:
|
|
|
|
#{old}
|
|
|
|
|
|
|
|
This probably isn't the expected value for this secret. To keep using a literal Erb string in config/secrets.yml, replace `<%` with `<%%`.
|
|
|
|
EOM
|
|
|
|
|
2016-08-04 05:17:35 -04:00
|
|
|
exit 1 # rubocop:disable Rails/Exit
|
2016-07-19 10:12:40 -04:00
|
|
|
end
|
2016-07-15 08:19:29 -04:00
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
new
|
2016-07-15 08:19:29 -04:00
|
|
|
end
|
2016-07-17 06:01:38 -04:00
|
|
|
|
2016-07-20 10:10:28 -04:00
|
|
|
File.write(secrets_yml, YAML.dump(secrets), mode: 'w', perm: 0600)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
2016-07-15 08:19:29 -04:00
|
|
|
|
|
|
|
create_tokens
|