doc/development/permissions/granular_access/job_tokens.md
Job token permissions allow fine-grained access control for CI/CD job tokens that access GitLab API endpoints. When enabled, the job token can only perform actions allowed for the project.
Historically, job tokens have provided broad access to resources by default. With the introduction of fine-grained permissions for job tokens, we can enable granular access controls while adhering to the principle of least privilege.
This topic provide guidance on the requirements and contribution guidelines for new job token permissions.
Before being accepted, all new job token permissions must:
@gitlab-com/gl-security/product-security/appsec for reviewThese requirements ensure that new permissions allow users to maintain explicit control over their security configuration, prevent unintended privilege escalation, and adhere to the principle of least privilege.
Job token permissions are defined in several locations. When adding new permissions, ensure the following files are updated:
lib/ci/job_token/policies.rb - Lists the available permissions.app/validators/json_schemas/ci_job_token_policies.json - Defines the validation schema for the job_token_policies attribute of the Ci::JobToken::GroupScopeLink and Ci::JobToken::ProjectScopeLink models.app/assets/javascripts/token_access/constants.js - Lists the permission definitions for the UITo add job token policy support to an API endpoint, you need to configure two route settings:
route_setting :authenticationThis setting controls which authentication methods are allowed for the endpoint.
Parameters:
job_token_allowed: true - Enables CI/CD job tokens to authenticate against this endpointroute_setting :authorizationThis setting defines the permission level and access controls for job token access.
Parameters:
job_token_policies: The required permission level. Available policies are listed in lib/ci/job_token/policies.rb.allow_public_access_for_enabled_project_features: Optional. Allows access based on the visibility settings of the project feature. See public access configuration.This example shows how to add support for tags API endpoints to the job token policy's repository resource:
# In lib/api/tags.rb
resource :projects do
# Enable job token authentication for this endpoint
route_setting :authentication, job_token_allowed: true
# Require the `read_repository` policy for reading tags
route_setting :authorization, job_token_policies: :read_repository,
allow_public_access_for_enabled_project_features: :repository
get ':id/repository/tags' do
# ... existing endpoint implementation
end
# Enable job token authentication for this endpoint
route_setting :authentication, job_token_allowed: true
# Require the `admin_repository` policy for creating tags
route_setting :authorization, job_token_policies: :admin_repository
post ':id/repository/tags' do
# ... existing endpoint implementation
end
end
Choose the appropriate permission level based on the operation:
:read_* permissions:admin_* permissionsThe allow_public_access_for_enabled_project_features parameter allows job tokens to access endpoints when:
This provides backward compatibility while enabling fine-grained control when the project feature is not publicly accessible.
When implementing job token permissions for API endpoints, use the shared RSpec example 'enforcing job token policies' to test the authorization behavior. This shared example provides comprehensive coverage for all job token policy scenarios.
Add the shared example to your API endpoint tests by including it with the required parameters:
describe 'GET /projects/:id/repository/tags' do
let(:route) { "/projects/#{project.id}/repository/tags" }
it_behaves_like 'enforcing job token policies', :read_repository,
allow_public_access_for_enabled_project_features: :repository do
let(:user) { developer }
let(:request) do
get api(route), params: { job_token: target_job.token }
end
end
# Your other endpoint-specific tests...
end
The shared example takes the following parameters:
:read_repository)allow_public_access_for_enabled_project_features - (Optional) The project feature that the endpoint controls (e.g., :repository)expected_success_status - (Optional) The expected success status of the request (by default: :success)The 'enforcing job token policies' shared example automatically tests:
allow_public_access_for_enabled_project_features behavior when permissions aren't configured.After you add job token support for a new API endpoint, you must update the fine-grained permissions for CI/CD job tokens documentation. Run the following command to regenerate this topic:
bundle exec rake ci:job_tokens:compile_docs