Skip to content

Use dedicated signing key for CI_JOB_JWT (take 3)

Krasimir Angelov requested to merge 214607-ci-jwt-signing-key/jwks-take-3 into master

What does this MR do?

In !28063 (merged) we implemented new CI variable (CI_JOB_JWT) that is JWT signed with the instance's OpenID Connect signing key. While this is fine, during security review it was recommended we switch to using dedidcated signing key for these tokens - !28063 (comment 325521818).

This is the third attempt to replace openid_connect_signing_key with the new ci_jwt_signing_key when generating CI_JOB_JWT.

The first MR (!34249 (merged)) had to be reverted due to gitlab-com/gl-infra/production#2284 (closed).

With the second attempt (!38762 (closed)) we took as lightly different approach - instead of trying to generate and save the key on the fly when missing (and fail if there is no write access) we just skip generating CI_JOB_JWT if there is no key available. During review @shinya.maeda suggested we store the new key in application_settings table instead of a file - #222690 (comment 395216567). This idea was approved in general by @dcouture in teh context of application security - #222690 (comment 398368920).

After giving some thought to this proposal I also think it will be better:

  1. All GitLab instances definitely have the secret regardless of the installation methods. With !38762 (closed) we'll also have to update docs for installation from source on how to generate this new key (!38762 (comment 395523313)). I think if we can make it just work for everyone we should do it.
  2. It's guaranteed that all nodes in HA setup will use the same key.
  3. It's easier to rotate the key (update value in DB).

The use of the new key is behind a ci_jwt_signing_key feature flag and disabled by default - !38762 (diffs).

The new key value is populated with migration for existing instances and database fixtures for new installations.

The MR also implements /-/jwks endpoint instead of delegating to the doorkeeper-openid_connect gem. Response will still include openid_connect_signing_key for seamless rollout.

Related to #214607 (closed).

Migrations

Up

$ bundle exec rails db:migrate:up VERSION=20201008011523
== 20201008011523 AddCiJwtSigningKeyToApplicationSettings: migrating ==========
-- add_column(:application_settings, :encrypted_ci_jwt_signing_key, :text)
   -> 0.0065s
-- add_column(:application_settings, :encrypted_ci_jwt_signing_key_iv, :text)
   -> 0.0008s
== 20201008011523 AddCiJwtSigningKeyToApplicationSettings: migrated (0.0075s) =

$ bundle exec rails db:migrate:up VERSION=20201008011937
== 20201008011937 AddTextLimitToApplicationSettingsEncryptedCiJwtSigningKeyIv: migrating 
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0003s
-- execute("ALTER TABLE application_settings\nADD CONSTRAINT check_85a39b68ff\nCHECK ( char_length(encrypted_ci_jwt_signing_key_iv) <= 255 )\nNOT VALID;\n")
   -> 0.0086s
-- current_schema()
   -> 0.0002s
-- execute("SET statement_timeout TO 0")
   -> 0.0003s
-- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT check_85a39b68ff;")
   -> 0.0009s
-- execute("RESET ALL")
   -> 0.0003s
== 20201008011937 AddTextLimitToApplicationSettingsEncryptedCiJwtSigningKeyIv: migrated (0.0231s) 

$ bundle exec rails db:migrate:up VERSION=20201008013434
== 20201008013434 GenerateCiJwtSigningKey: migrating ==========================
== 20201008013434 GenerateCiJwtSigningKey: migrated (0.2750s) =================

Down

$ bundle exec rails db:migrate:down VERSION=20201008013434
== 20201008013434 GenerateCiJwtSigningKey: reverting ==========================
== 20201008013434 GenerateCiJwtSigningKey: reverted (0.1526s) =================

$ bundle exec rails db:migrate:down VERSION=20201008011937
== 20201008011937 AddTextLimitToApplicationSettingsEncryptedCiJwtSigningKeyIv: reverting 
-- execute("ALTER TABLE application_settings\nDROP CONSTRAINT IF EXISTS check_85a39b68ff\n")
   -> 0.0013s
== 20201008011937 AddTextLimitToApplicationSettingsEncryptedCiJwtSigningKeyIv: reverted (0.0046s) 

$ bundle exec rails db:migrate:down VERSION=20201008011523
== 20201008011523 AddCiJwtSigningKeyToApplicationSettings: reverting ==========
-- remove_column(:application_settings, :encrypted_ci_jwt_signing_key_iv, :text)
   -> 0.0015s
-- remove_column(:application_settings, :encrypted_ci_jwt_signing_key, :text)
   -> 0.0004s
== 20201008011523 AddCiJwtSigningKeyToApplicationSettings: reverted (0.0049s) =

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #214607 (closed)

Edited by Krasimir Angelov

Merge request reports