Add service to revert automatic approval policy bypasses

What does this MR do and why?

Adds Security::ScanResultPolicies::RevertAutomaticBypassMergeRequestService, which reverts the automatic approval policy bypasses of a merge request. It is the counterpart of AutomaticBypassMergeRequestService from !273914 (merged): an automatic bypass is recorded against a merge request diff, so when the merge request changes the bypass must stop applying.

Nothing calls the service yet. Hooking it into pushes follows in a later MR. It only runs when the merge_request_approval_policy_auto_bypass feature flag (wip, default off) is enabled and the project has the security_orchestration_policies license. Otherwise it returns an error with reason :not_available.

The service:

  • Deletes every automatic bypass event of the merge request, one per bypassed policy, in a single DELETE ... RETURNING statement (delete_all_returning). Automatic events are the ones with a merge_request_diff_id. Manual bypasses (merge_request_diff_id is NULL) are kept.
  • Builds the audit events and the response from the returned rows, so a concurrent revert cannot audit the same bypass twice.
  • Logs a new security_policy_merge_request_automatic_bypass_reverted audit event per reverted policy, authored by the current user. On a push this is the pusher.
  • Triggers the mergeRequestApprovalStateUpdated and mergeRequestMergeStatusUpdated subscriptions.
  • Returns an error with reason :no_automatic_bypasses when there is nothing to revert. The caller decides how to handle that.

It also adds an automatic scope to Security::ApprovalPolicyMergeRequestBypassEvent, so callers do not need to know that a NULL diff marks a manual bypass.

References

  • Feature issue: #579429
  • Sibling MR adding the automatic bypass: !273914 (merged)
  • Depends on the merge_request_diff_id column: !273613 (merged). Until it merges, this MR's diff also contains that MR's commits. Review only the last commits, starting at "Add service to revert automatic approval policy bypasses".

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 validated it in GDK on a merge request authored by a service account, with one approval policy whose bypass_settings lists that service account, and an automatic bypass event recorded against the latest merge request diff. Run these in a Rails console:

mr = Project.find_by_full_path('your/project').merge_requests.find_by(iid: 1)
bot = User.find_by_username('your-service-account')
revert = -> { Security::ScanResultPolicies::RevertAutomaticBypassMergeRequestService.new(merge_request: mr, current_user: bot).execute }
  1. With the feature flag disabled, revert.call.reason returns :not_available and the bypass event remains.
  2. Enable the feature flag with Feature.enable(:merge_request_approval_policy_auto_bypass, mr.project). revert.call returns a success listing the policy, and the bypass event is deleted.
  3. Reload the merge request page. The policy requires approval again.
  4. The security policy project's audit events page shows "Automatic bypass of security policy <policy> in merge request (<project>!<iid>) has been reverted", with the merge_request_diff_id of the reverted bypass in its details.
  5. Run revert.call again. It returns an error with reason :no_automatic_bypasses.
  6. Record a manual bypass for the policy (merge_request_diff_id: nil) and run revert.call. It returns :no_automatic_bypasses and the manual bypass remains.

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.

Edited by Jans Rautenbach

Merge request reports

Loading
Loading