Record prevented packages on the dependency firewall write path

What does this MR do and why?

This is part of the Dependency Firewall dashboard stats series, work item https://gitlab.com/gitlab-org/gitlab/-/work_items/627538. The former Part 2 (write path) was split into three MRs so each has one concern and one kind of reviewer:

  • MR A, write buffer and flush worker: !257625 (merged) (merged).
  • MR B, rule evaluation changes: !257627 (merged) (merged).
  • This MR, enforcement hook (targets master).

MR A and MR B have both merged, so this MR depends on nothing unmerged.

Part 1 (table and model) has merged: !253941 (merged) and !255353 (merged). Part 3 (ClickHouse replica), !253945 (merged), is independent of all three.

There are no migrations and no UI changes in this MR. There is no changelog entry: this MR is behind a default-off flag.

Security::DependencyFirewall::EnforcementService#record_prevented_entities buffers one ledger entry per matched rule for every blocked or warned result. For vulnerability and risk_severity rules, severity is the highest advisory severity the rule did not except (unlisted values such as none map to unknown). Other rule types record no severity, which a DB CHECK constraint enforces.

  • Best effort, like the existing record_activity: any error is tracked with Gitlab::ErrorTracking and never breaks enforcement.
  • Purls longer than 512 characters are skipped, because the identifier column has a 512-character CHECK and such entries could never be written.
  • Rules whose type the ledger enum does not map yet (for example age, which the policy schema already accepts) are skipped. Recording one would silently store it under license.
  • Skipped entirely when the dependency_firewall_dashboard_v2 flag is off for the project's root namespace.

Removes the Availability.dashboard_v2_enabled? entry from scripts/lint/keela_excluded.yml, since this MR is the first caller of that method.

This MR is behind the dependency_firewall_dashboard_v2 feature flag (type wip, default off), added in Part 1.

The write buffer, worker, batch size and database review moved to MR A, and the evaluator/rule changes moved to MR B, so those parts were reviewed there.

Testing

84 examples in the enforcement service spec, all passing locally. RuboCop clean.

References

Screenshots or screen recordings

Not applicable, no UI changes.

How to set up and validate locally

  1. Check out this branch. MR A and MR B, which it builds on, have merged.
  2. In bin/rails console, enable both flags: Feature.enable(:dependency_firewall_phase1) and Feature.enable(:dependency_firewall_dashboard_v2).
  3. Use an Ultimate license and turn on the dependency firewall setting (instance setting dependency_firewall_enabled on self-managed/GDK).
  4. Configure a dependency firewall policy on a project that blocks a specific package, for example with a license rule.
  5. Trigger enforcement: download the matching package, or in the console set project = Project.find_by_full_path('<path of the project from step 4>') and user = User.first, then call Security::DependencyFirewall::EnforcementService.firewall_check(project: project, pkg_type: 'npm', name: '<name>', version: '<version>', operation: Security::DependencyFirewall::EnforcementService::PACKAGE_DOWNLOAD, current_user: user).
  6. Flush the buffer: Security::DependencyFirewall::FlushPreventedPackagesWriteBufferCronWorker.new.perform.
  7. Check Security::DependencyFirewallPreventedPackage.all: one row per matched rule, with the expected rule_type, severity, first_seen_at and last_blocked_at (or last_warned_at for a warn-mode policy).
  8. Optional: trigger enforcement again, flush again, and confirm the same row is updated (later last_blocked_at, unchanged first_seen_at) rather than a new row being added.

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 Arpit Gogia

Merge request reports

Loading
Loading