Skip to content

Populate job token authorizations for projects with disabled scope

Fabio Pitino requested to merge job_token_authentications_log into master

What does this MR do and why?

Related to Track authentications via job token and show lo... (#467292)

Context

When a request is received and successfully authenticated using CI_JOB_TOKEN, we track the user as "coming from a CI_JOB_TOKEN". Then in the ProjectPolicy we check if the specific permissions is allowed based on the job token scope configurations.

The change

In order to log successful "authentications" via CI_JOB_TOKEN we need to look at "authorizations" because the Job Token Scope checks are embedded in the ProjectPolicy via the use of Ci::JobToken::Scope#accessible? method. This is because the CI_JOB_TOKEN could be successfully authenticated, because the job token is still valid, but it may NOT be authorized based on the Job Token Scope (allowlist).

If the "inbound" request via CI_JOB_TOKEN is allowed we capture the authorization attempt. However, the authorization may fail due to any subsequent rules in the ProjectPolicy. Additionally, the request may fail due to more specific permission checks that don't authorize the user. For this reason we use a middleware so we can only persist successful authorizations via CI_JOB_TOKEN for success responses.

Database migrations

$ rails db:migrate
main: == [advisory_lock_connection] object_id: 127700, pg_backend_pid: 90001
main: == 20240813191915 CreateCiJobTokenAuthorizations: migrating ===================
main: -- create_table(:ci_job_token_authorizations, {:if_not_exists=>true})
main:    -> 0.0227s
main: == 20240813191915 CreateCiJobTokenAuthorizations: migrated (0.0422s) ==========

main: == [advisory_lock_connection] object_id: 127700, pg_backend_pid: 90001
ci: == [advisory_lock_connection] object_id: 127960, pg_backend_pid: 90007
ci: == 20240813191915 CreateCiJobTokenAuthorizations: migrating ===================
ci: -- create_table(:ci_job_token_authorizations, {:if_not_exists=>true})
ci:    -> 0.0033s
ci: == 20240813191915 CreateCiJobTokenAuthorizations: migrated (0.0283s) ==========

ci: == [advisory_lock_connection] object_id: 127960, pg_backend_pid: 90007
$ rails db:migrate:redo:ci VERSION=20240813191915
ci: == [advisory_lock_connection] object_id: 127000, pg_backend_pid: 96766
ci: == 20240813191915 CreateCiJobTokenAuthorizations: reverting ===================
ci: -- drop_table(:ci_job_token_authorizations, {:if_not_exists=>true})
ci:    -> 0.0116s
ci: == 20240813191915 CreateCiJobTokenAuthorizations: reverted (0.0406s) ==========

ci: == [advisory_lock_connection] object_id: 127000, pg_backend_pid: 96766
ci: == [advisory_lock_connection] object_id: 127980, pg_backend_pid: 97095
ci: == 20240813191915 CreateCiJobTokenAuthorizations: migrating ===================
ci: -- create_table(:ci_job_token_authorizations, {:if_not_exists=>true})
ci:    -> 0.0265s
ci: == 20240813191915 CreateCiJobTokenAuthorizations: migrated (0.0846s) ==========

ci: == [advisory_lock_connection] object_id: 127980, pg_backend_pid: 97095

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Edited by Fabio Pitino

Merge request reports

Loading