Add service to automatically bypass approval policies for bots
What does this MR do and why?
Adds Security::ScanResultPolicies::AutomaticBypassMergeRequestService, which lets a service account or access token listed in a merge request approval policy's bypass_settings bypass that policy on merge requests it authored, without a manual bypass.
This is part of a stack. Nothing calls the service yet: hooking it into merge request creation and pushes, and making the bypass check compare the recorded merge request diff, follow in later MRs. Everything is behind the merge_request_approval_policy_auto_bypass feature flag (wip, default off).
The service bypasses a policy only when all of these hold:
- The feature flag is enabled for the target project and the project has the
security_orchestration_policieslicense. - The merge request is open and its latest diff is saved.
- The current user is the merge request author and is a service account or project bot. AI service accounts (composite identity) are not eligible.
- The latest diff has at most 1,000 commits (
MAX_COMMITS), so the service never loads an unbounded commit list from Gitaly. - Every commit in the diff has a verified signature (
verified,verified_system, orverified_ca) made by that bot. Author and committer emails can be spoofed, so the signature is what ties the commits to the bot. - The bot is allowed by the policy's
bypass_settings(BotBypassChecker: listed service account, or an active listed access token). - The policy is not already bypassed for the merge request.
How the signer is matched depends on the signature status:
verified: the owner of the signing key must be the bot.verified_system(signed by Gitaly, for example commits created through the API) andverified_ca(signed with a certificate from the group's SSH certificate authority): the signing key belongs to no user, soCommit#verified_committerreturnsnilfor these. Instead, the owner of the verified committer email must be the bot. Both verifications already check that email: Gitaly sets the committer to the acting user, and the CA check requires the certificate's user to have the committer email verified.
For each bypassed policy, the service:
- Records an
ApprovalPolicyMergeRequestBypassEventagainst the latest merge request diff, so pushes by anyone else can later be detected and do not inherit the bypass. - Uses a translated default reason, because a bot cannot give one: "Automatically bypassed by service account
<username>in<project>according to security policy<policy>". - Logs a new
security_policy_merge_request_automatic_bypassaudit event, as a service account or access token bypass. It has its own type and message ("has been automatically bypassed by ...") so automatic bypasses can be told apart from manual ones.
It then triggers the mergeRequestApprovalStateUpdated and mergeRequestMergeStatusUpdated subscriptions and, if auto-merge is enabled, enqueues AutoMergeProcessWorker so the merge request merges without waiting for another event. An event recorded concurrently for the same policy is skipped instead of failing the whole run.
Security::Policy#create_merge_request_bypass_event! takes an optional merge_request_diff: for this.
References
- Feature issue: #579429
- Feature flag (merged): !273576 (merged)
merge_request_diff_idcolumn (merged): !273613 (merged)- Rollout issue: #642085
Screenshots or screen recordings
Not applicable. This MR has no UI changes.
How to set up and validate locally
This MR has no user-facing change: nothing calls the service yet. I did not validate it in GDK. To try it in a Rails console on an Ultimate project that has an approval policy with bypass_settings.service_accounts listing a service account:
-
Enable the feature flag:
project = Project.find_by_full_path('your/project') Feature.enable(:merge_request_approval_policy_auto_bypass, project) -
As the service account, open a merge request whose commits are all signed with a verified signature by the service account, for example commits created through the API on an instance that signs web commits.
-
Run the service:
mr = project.merge_requests.last Security::ScanResultPolicies::AutomaticBypassMergeRequestService .new(merge_request: mr, current_user: mr.author).execute -
Check that the response is a success listing the policy, and that a bypass event exists for the latest diff:
mr.approval_policy_merge_request_bypass_events.pluck(:security_policy_id, :merge_request_diff_id, :reason) -
Check that a
security_policy_merge_request_automatic_bypassaudit event was recorded:AuditEvents::ProjectAuditEvent.where(project_id: project.id).last.details[:event_name] -
Run the service again. It returns an error with reason
:no_bypassable_policies.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.