doc/development/application_secrets.md
GitLab must be able to access various secrets such as access tokens and other credentials to function. These secrets are encrypted and stored at rest and may be found in different data stores depending on use. Use this guide to understand how different kinds of secrets are stored and managed.
Broadly speaking, there are two classes of secrets:
<!-- vale gitlab_base.SubstitutionWarning = NO -->Application secrets. The GitLab application uses these to implement a particular feature or function. An example would be access tokens or private keys to create cryptographic signatures. We store these secrets in the database in encrypted columns. See Secure Coding Guidelines: At rest.
Operational secrets. Used to read and store other secrets or bootstrap the application. For this reason,
they cannot be stored in the database.
These secrets are stored as Rails credentials
in the config/secrets.yml file:
Application secrets should be stored in PostgreSQL using ActiveRecord::Encryption:
class MyModel < ApplicationRecord
encrypts :my_secret
end
Until recently, we used attr_encrypted instead of ActiveRecord::Encryption. We are in the process of
migrating all columns to use the new Rails-native encryption framework (see epic 15420).
For guidance on migrating existing attr_encrypted attributes, see Migrating from attr_encrypted to ActiveRecord::Encryption.
Despite there being precedent, application secrets should not be stored as an ApplicationSetting.
This can lead to the entire application malfunctioning if this secret fails to decode. To reduce
coupling to other features, isolate secrets into dedicated tables.
In some cases, it can be undesirable to store secrets in the database. For example, if the secret is needed to bootstrap the Rails application, it may have to access the database in an initializer, which can lead to initialization races as the database connection itself may not yet be ready. In this case, store the secret as an operational secret instead.
attr_encrypted to ActiveRecord::EncryptionWe are migrating all encrypted attributes from the attr_encrypted gem to Rails' native ActiveRecord::Encryption framework. This migration ensures better security, performance, and maintainability while maintaining backward compatibility during the transition.
migrate_to_encrypts methodThe migrate_to_encrypts method provides a seamless migration path from attr_encrypted to ActiveRecord::Encryption. It temporarily stores data in both encryption formats during the transition period.
Usage:
class MyModel < ApplicationRecord
include Gitlab::EncryptedAttribute
# Replace attr_encrypted with migrate_to_encrypts
# Keep the same encryption options (mode, key, algorithm etc.) during migration
migrate_to_encrypts :my_secret_attribute,
mode: :per_attribute_iv,
key: :db_key_base_truncated,
algorithm: 'aes-256-cbc',
insecure_mode: true
end
How it works:
attr_encrypted) and new (ActiveRecord::Encryption) formatstmp_<attribute> column), then falls back to the old format if neededGenerated methods:
The migrate_to_encrypts method creates several helper methods:
attr_encrypted_<attribute>: Access to the original attr_encrypted valuetmp_<attribute>: Access to the new ActiveRecord::Encryption value<attribute>: Primary accessor that reads from new format first, falls back to old formatThe migration follows a four-milestone process to ensure zero-downtime deployment:
Milestone M (Initial Migration):
Add temporary column: Create a tmp_<attribute> column with :jsonb type:
class AddTmpSecretKeyToMyModel < Gitlab::Database::Migration[2.3]
milestone '18.4'
def change
add_column :my_models, :tmp_secret_key, :jsonb
end
end
Update model: Replace attr_encrypted with migrate_to_encrypts:
class MyModel < ApplicationRecord
include Gitlab::EncryptedAttribute
# Before:
# attr_encrypted :secret_key, mode: :per_attribute_iv, key: :db_key_base_truncated, algorithm: 'aes-256-cbc', insecure_mode: true
# After:
# Keep the same encryption options (mode, key, algorithm etc.) during migration
migrate_to_encrypts :secret_key,
mode: :per_attribute_iv,
key: :db_key_base_truncated,
algorithm: 'aes-256-cbc',
insecure_mode: true
end
Create data migration: Create a post-deployment migration to populate the new column:
class MigrateSecretKeyToNewEncryptionFramework < Gitlab::Database::Migration[2.3]
milestone '18.1'
restrict_gitlab_migration gitlab_schema: :gitlab_main
class MigrationMyModel < MigrationRecord
include Gitlab::EncryptedAttribute
self.table_name = 'my_models'
migrate_to_encrypts :secret_key,
mode: :per_attribute_iv,
key: :db_key_base_truncated,
algorithm: 'aes-256-cbc',
insecure_mode: true
end
def up
MigrationMyModel.find_each do |record|
next if record.secret_key.blank?
record.secret_key = record.attr_encrypted_secret_key
record.save!
end
end
def down
execute "UPDATE my_models SET tmp_secret_key = NULL"
end
end
Large tables may require batched background migrations instead of regular post-deployment migrations.
Milestone M+1 (Column Rename):
tmp_<attribute> column to <attribute>
migrate_to_encrypts method call with the native encrypts Rails methodignore_columns for the encrypted_<attribute>, encrypted_<attribute>_iv, and encrypted_<attribute>_salt columnsMilestone M+2 (Cleanup):
encrypted_<attribute>, encrypted_<attribute>_iv, and encrypted_<attribute>_salt columnsignore_column for tmp_<attribute>Milestone M+3 (Final Cleanup):
ignore_columns for the encrypted_<attribute>, encrypted_<attribute>_iv, and encrypted_<attribute>_salt columnsUse the provided shared example to test that attributes are properly encrypted with both frameworks:
RSpec.describe MyModel, feature_category: :my_feature do
let(:record) { build(:my_model) }
it_behaves_like 'encrypted attribute being migrated to the new encryption framework',
:secret_key do
let(:record) { build(:my_model) }
end
end
This shared example verifies that:
attr_encrypted_<attribute> and tmp_<attribute> accessors work correctly:jsonb type for new encrypted columns, not :textSee the complete implementation example in:
migrate_to_encrypts methodApplicationSetting.asset_proxy_secret_keyWe maintain a number of operational secrets in config/secrets.yml, primarily to manage other secrets. Historically, GitLab
used this approach for all secrets, including application secrets, but has meanwhile moved most of these into postgres.
The only exception is openid_connect_signing_key since it needs to be accessed from a Rails initializer before
the database may be ready.
| Entry | Description |
|---|---|
secret_key_base | The base key to be used for generating a various secrets |
otp_key_base | The base key for One Time Passwords, described in User management |
db_key_base | The base key to encrypt the data for attr_encrypted columns |
openid_connect_signing_key | The signing key for OpenID Connect |
encrypted_settings_key_base | The base key to encrypt settings files with |
active_record_encryption_primary_key | The base key to non-deterministically-encrypt data for ActiveRecord::Encryption encrypted columns |
active_record_encryption_deterministic_key | The base key to deterministically-encrypt data for ActiveRecord::Encryption encrypted columns |
active_record_encryption_key_derivation_salt | The derivation salt to encrypt data for ActiveRecord::Encryption encrypted columns |
| Installation type | Location |
|---|---|
| Linux package | /etc/gitlab/gitlab-secrets.json |
| Cloud Native GitLab Charts | Kubernetes Secrets |
| Self-compiled | <path-to-gitlab-rails>/config/secrets.yml (Automatically generated by config/initializers/01_secret_token.rb) |
Before adding a new secret to
config/initializers/01_secret_token.rb,
ensure you also update the GitLab Linux package and the Cloud Native GitLab charts, or the update will fail.
Both installation methods are responsible for writing the config/secrets.yml file.
If if they don't know about a secret, Rails attempts to write to the file, and fails because it doesn't
have write access.
Examples
Additionally, in case you need the secret to have the same value on all nodes (which is usually the case), you need to make sure it's configured for all live environments (GitLab.com, staging, pre) prior to changing this file.
Add the new secrets to this documentation file.
Mention the new secrets in the next release upgrade notes.
For instance, for the 17.8 release, the notes would go in data/release_posts/17_8/17-8-upgrade.yml and contain something like the following:
---
upgrades:
- reporter: <your username> # item author username
description: |
In Gitlab 17.8, three new secrets have been added to support the upcoming encryption framework:
- `active_record_encryption_primary_key`
- `active_record_encryption_deterministic_key`
- `active_record_encryption_key_derivation_salt`
**If you have a multi-node configuration, you should ensure these secrets are the same on all nodes.** Otherwise, the application will automatically generate the missing secrets.
If you use the [GitLab helm chart](https://docs.gitlab.com/charts/) and disabled the [shared-secrets chart](https://docs.gitlab.com/charts/charts/shared-secrets/), you will need to [manually create these secrets](https://docs.gitlab.com/charts/installation/secrets/#gitlab-rails-secret).
Mention the new secrets in the next Cloud Native GitLab charts upgrade notes. For instance, for 8.8, you should document the new secrets in https://docs.gitlab.com/charts/releases/8_0/.
We may either deprecate or remove this automatic secret generation performed by config/initializers/01_secret_token.rb in the future.
See issue #222690 for more information.