Reconcile stale security policy project links on resync

What does this MR do and why?

Resolves #630855

A project that inherits group security policies keeps a Security::PolicyProjectLink row per linked policy configuration. The workers that remove these links on transfer only shipped in GitLab 18.1 (projects) and 18.3 (groups), so projects transferred before those versions still carry links to their old group's policy configuration. If one of those stale policies sets block_branch_modification: true, owners can't update or delete protected branches, and there's no supported way to clear it: resyncing with relationship: INHERITED only re-syncs configurations that currently apply, so it never removes a link that no longer applies.

This MR:

  1. Makes ResyncPoliciesService also enqueue Security::Policies::GroupProjectTransferWorker for INHERITED project resyncs. That worker already reconciles a project's links: it links configurations that now apply and unlinks (via LinkProjectService, which also removes approval rules and re-syncs MR rules) configurations that no longer apply.
  2. Makes GroupProjectTransferWorker build its desired configuration set with include_invalid: true, so a group configuration that still applies but currently has invalid policy YAML isn't unlinked. Without this, a transient YAML error would make every INHERITED resync drop that configuration's policies.

Protected branch enforcement needs no change: it reads the project's policy links, so once the stale links are gone the block is lifted.

Reviewable LOC: 3 (plus 22 in tests)

References

Context for LLM agents

Rejected alternatives

  • A post-deploy or batched background migration to delete the stale rows directly. A correct cleanup needs application logic (removing approval rules, scan_result_policy_reads, violations and pipeline schedules, and re-syncing MRs via LinkProjectService) that a migration can't reach. It also does nothing for self-managed instances.
  • Hardening the protected branch checks to ignore links whose configuration no longer applies. It only hides the symptom: stale approval rules still enforce on MRs, and it adds a configuration lookup to every protected branch check.
  • Re-running SyncProjectPoliciesWorker for the stale configurations. The policy scope check doesn't look at the group hierarchy, so it would re-link them.

Invariants a follow-up must keep

  • GroupProjectTransferWorker must stay idempotent — re-running it on an already-reconciled project must be a no-op.
  • It must never unlink a policy of a configuration that still applies, including one with invalid YAML.

Owed elsewhere

  • GitLab.com cleanup of existing stale links, plus a self-managed troubleshooting docs entry pointing admins/owners at the resync mutation: #630859
  • Fix for the broken "security policies" popover link on the protected branch settings page: #630860 (closed)

Screenshots or screen recordings

Not applicable — backend-only change, no UI changes.

How to set up and validate locally

Run the specs:

bundle exec rspec \
  ee/spec/services/security/security_orchestration_policies/resync_policies_service_spec.rb \
  ee/spec/workers/security/policies/group_project_transfer_worker_spec.rb

Manual check in the GDK:

  1. In a group with a policy project whose approval policy sets block_branch_modification: true, transfer a project out of the group.
  2. In the Rails console, insert a stale link with plain ActiveRecord (no FactoryBot):
    Security::PolicyProjectLink.create!(project: project, security_policy: policy)
  3. Confirm the bug: Settings > Repository won't let you unprotect the branch.
  4. Run the resync mutation with relationship: INHERITED:
    mutation {
      resyncSecurityPolicies(input: { fullPath: "<project full path>", relationship: INHERITED }) {
        errors
      }
    }
  5. Confirm the fix: Settings > Repository now lets you unprotect the branch, and project.security_policy_project_links.count has dropped.

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 Alan (Maciej) Paruszewski

Merge request reports

Loading
Loading