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 withGitlab::ErrorTrackingand never breaks enforcement. - Purls longer than 512 characters are skipped, because the
identifiercolumn 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 underlicense. - Skipped entirely when the
dependency_firewall_dashboard_v2flag 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
- Work item: https://gitlab.com/gitlab-org/gitlab/-/work_items/627538
- MR A (write buffer and flush worker): !257625 (merged)
- MR B (rule evaluation changes): !257627 (merged)
- Part 1 table: !253941 (merged)
- Part 1 model: !255353 (merged)
Screenshots or screen recordings
Not applicable, no UI changes.
How to set up and validate locally
- Check out this branch. MR A and MR B, which it builds on, have merged.
- In
bin/rails console, enable both flags:Feature.enable(:dependency_firewall_phase1)andFeature.enable(:dependency_firewall_dashboard_v2). - Use an Ultimate license and turn on the dependency firewall setting (instance setting
dependency_firewall_enabledon self-managed/GDK). - Configure a dependency firewall policy on a project that blocks a specific package, for example with a license rule.
- Trigger enforcement: download the matching package, or in the console set
project = Project.find_by_full_path('<path of the project from step 4>')anduser = User.first, then callSecurity::DependencyFirewall::EnforcementService.firewall_check(project: project, pkg_type: 'npm', name: '<name>', version: '<version>', operation: Security::DependencyFirewall::EnforcementService::PACKAGE_DOWNLOAD, current_user: user). - Flush the buffer:
Security::DependencyFirewall::FlushPreventedPackagesWriteBufferCronWorker.new.perform. - Check
Security::DependencyFirewallPreventedPackage.all: one row per matched rule, with the expectedrule_type,severity,first_seen_atandlast_blocked_at(orlast_warned_atfor a warn-mode policy). - Optional: trigger enforcement again, flush again, and confirm the same row is updated (later
last_blocked_at, unchangedfirst_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.