fix(policies): deduplicate preset entries on re-apply to prevent inva… - #1119
fix(policies): deduplicate preset entries on re-apply to prevent inva…#1119Junior00619 wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds text-based deduplication for merging network policy presets: introduces helpers to extract 2-space-indented mapping keys under Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bin/lib/policies.js`:
- Around line 113-135: stripPolicyKeys currently deletes 2-space-indented keys
globally; restrict it to only remove keys inside the top-level network_policies
block by detecting when we're within that block and only applying the removeSet
there: when you see a top-level "network_policies:" key (e.g.
/^\s*network_policies:/ with no indent) set a flag (e.g. inNetworkPolicies) and
clear it when any other top-level key (/^\S/) appears, then only evaluate
2-space-indented keys against removeSet (keysToRemove) while inNetworkPolicies;
update/remove the existing skipping logic accordingly and keep returning the
joined result.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b47836ad-e3c3-4d32-8e8c-95d000343b2b
📒 Files selected for processing (2)
bin/lib/policies.jstest/policies.test.js
d9c7648 to
1bd3922
Compare
HagegeR
left a comment
There was a problem hiding this comment.
Maybe you can use yq to simplify this?
…lid YAML mergePresetIntoPolicy() blindly appended preset entries into the network_policies block without checking for existing keys. Applying the same preset twice (or re-applying after an update) produced duplicate YAML mapping keys — invalid YAML that could cause unpredictable policy enforcement. Add extractPolicyKeyNames() and stripPolicyKeys() helpers that detect overlapping key names between the incoming preset and the current policy, then strip the old entries before insertion. Re-applying a preset now replaces the previous entry (update semantics), so updated presets propagate correctly without manual cleanup. No new dependencies — the fix uses the same text-line approach as the existing merge logic, scoped to the consistent 2-space-indented key format used by all preset files. Fixes NVIDIA#1010
6de0b3e to
51d1e46
Compare
|
Thanks for the suggestion! I considered a structured YAML approach, but the existing codebase intentionally uses text-based manipulation throughout |
@cv @ericksoa WDYT about a simpler, more maintenable yq approach here? Won't need a special case per policy going forward |
|
Thanks for the preset deduplication fix. The codebase has changed significantly since March 30 — including a full TypeScript migration and rework of the policy preset merge logic — so this will need a rebase on |
|
Just a friendly nudge: we're still waiting on the rebase against the TypeScript migration. If we don't hear back within 7 days, we'll close this to keep the queue clean. Feel free to reopen any time if you're able to update. |
|
Thanks @Junior00619 for the contribution here. This PR correctly targeted the duplicate policy preset merge problem and helped identify the right behavior: re-applying a preset should update existing policy entries instead of producing duplicate YAML keys. We have since landed the broader fix in #1055, which resolved #1010 by moving the merge path to structured YAML parsing and preserving non-network policy sections while overriding matching network policy entries. Since that implementation supersedes this earlier text-based approach, I am closing this PR as superseded rather than rejected on merit. |
Summary
Prevent
policy-addfrom generating invalid YAML when a preset is applied more than once. The previous merge path appended preset entries intonetwork_policieswithout removing existing keys, which could produce duplicate mapping keys; this change gives repeated preset application update semantics by replacing overlapping entries before insertion.Related Issue
Fixes #1010
Changes
extractPolicyKeyNames()to identify top-level policy keys contributed by an incoming preset withinnetwork_policiesstripPolicyKeys()to remove overlapping existing entries before merging the updated preset contentmergePresetIntoPolicy()so re-applying a preset replaces prior entries instead of appending duplicate keysType of Change
Testing
npx prek run --all-filespasses (or equivalentlymake check).npm testpasses.make docsbuilds without warnings. (for doc-only changes)Additional validation:
Checklist
General
Code Changes
npx prek run --all-filesauto-fixes formatting (ormake formatfor targeted runs).Doc Changes
update-docsagent skill to draft changes while complying with the style guide. For example, prompt your agent with "/update-docscatch up the docs for the new changes I made in this PR."Summary by CodeRabbit
Bug Fixes
Tests