Skip to content

fix(policies): deduplicate preset entries on re-apply to prevent inva… - #1119

Closed
Junior00619 wants to merge 1 commit into
NVIDIA:mainfrom
Junior00619:fix/policy-preset-dedup
Closed

fix(policies): deduplicate preset entries on re-apply to prevent inva…#1119
Junior00619 wants to merge 1 commit into
NVIDIA:mainfrom
Junior00619:fix/policy-preset-dedup

Conversation

@Junior00619

@Junior00619 Junior00619 commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Prevent policy-add from generating invalid YAML when a preset is applied more than once. The previous merge path appended preset entries into network_policies without 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

  • add extractPolicyKeyNames() to identify top-level policy keys contributed by an incoming preset within network_policies
  • add stripPolicyKeys() to remove overlapping existing entries before merging the updated preset content
  • update mergePresetIntoPolicy() so re-applying a preset replaces prior entries instead of appending duplicate keys
  • preserve the existing lightweight text-based merge approach and avoid introducing new YAML parsing dependencies
  • maintain compatibility with the current preset file structure and indentation conventions used in the repository

Type of Change

  • Code change for a new feature, bug fix, or refactor.
  • Code change with doc updates.
  • Doc only. Prose changes without code sample modifications.
  • Doc only. Includes code sample changes.

Testing

  • npx prek run --all-files passes (or equivalently make check).
  • npm test passes.
  • make docs builds without warnings. (for doc-only changes)

Additional validation:

  • Verified re-applying the same preset no longer produces duplicate YAML mapping keys
  • Confirmed merged output remains syntactically valid YAML
  • Confirmed updated preset entries replace prior values as intended
  • Confirmed existing one-time preset application behavior remains unchanged

Checklist

General

Code Changes

  • Formatters applied — npx prek run --all-files auto-fixes formatting (or make format for targeted runs).
  • Tests added or updated for new or changed behavior.
  • No secrets, API keys, or credentials committed.
  • Doc pages updated for any user-facing behavior changes (new commands, changed defaults, new features, bug fixes that contradict existing docs).

Doc Changes

Summary by CodeRabbit

  • Bug Fixes

    • Reapplying presets in policy files now replaces matching entries under network_policies instead of creating duplicates, preserves unrelated sections, and keeps same-named keys outside network_policies intact.
  • Tests

    • Added tests verifying deduplication when re-merging presets, replacement of stale entries, scoping to network_policies, and preservation of same-named keys in other sections.

@coderabbitai

coderabbitai Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: dd4cad45-c5bf-41c7-bfc5-6879b0fd0ef8

📥 Commits

Reviewing files that changed from the base of the PR and between 6de0b3e and 51d1e46.

📒 Files selected for processing (2)
  • bin/lib/policies.js
  • test/policies.test.js
✅ Files skipped from review due to trivial changes (1)
  • test/policies.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • bin/lib/policies.js

📝 Walkthrough

Walkthrough

Adds text-based deduplication for merging network policy presets: introduces helpers to extract 2-space-indented mapping keys under network_policies: and to strip matching entries from an existing policy before inserting preset entries, so re-applying a preset replaces matching mapping entries instead of duplicating them. (≤50 words)

Changes

Cohort / File(s) Summary
Policy Merge Deduplication
bin/lib/policies.js
Added extractPolicyKeyNames(text) to collect 2-space-indented mapping keys under network_policies: and stripPolicyKeys(policyText, keysToRemove) to remove matching network_policies entries. Updated mergePresetIntoPolicy() to compute incoming preset keys, strip overlapping entries from the current policy, then insert preset entries; exported both new helpers.
Deduplication Test Coverage
test/policies.test.js
Added tests verifying deduplication/update semantics: repeated preset application yields a single telegram_bot: key; stale network_policies.telegram_bot blocks are replaced (old endpoints removed, new endpoints present); deduplication scoped to network_policies and preserves same-named keys in other sections.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 I nibble keys and tidy stacks,
I chase away duplicate pacts,
I strip the old and sew the new,
One tidy YAML stitched for you,
Hooray — no duplicate tracks!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: deduplicating preset entries when re-applying presets to prevent invalid YAML output.
Linked Issues check ✅ Passed The implementation fully addresses issue #1010: deduplicates preset entries by key within network_policies, prevents duplicate YAML mapping keys, and maintains the lightweight text-based approach without introducing new YAML parsing dependencies.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the preset merge deduplication issue identified in #1010; no unrelated modifications are present in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6a7f438 and d9c7648.

📒 Files selected for processing (2)
  • bin/lib/policies.js
  • test/policies.test.js

Comment thread bin/lib/policies.js
@Junior00619
Junior00619 force-pushed the fix/policy-preset-dedup branch from d9c7648 to 1bd3922 Compare March 30, 2026 20:42

@HagegeR HagegeR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@Junior00619
Junior00619 force-pushed the fix/policy-preset-dedup branch from 6de0b3e to 51d1e46 Compare March 30, 2026 21:18
@Junior00619

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion! I considered a structured YAML approach, but the existing codebase intentionally uses text-based manipulation throughout policies.js (no YAML parsing dependencies). Introducing yq would add an external binary dependency that isn't used elsewhere in the project. The current approach stays consistent with the codebase conventions and keeps the fix small. Happy to revisit if the team decides to adopt yq more broadly.

@HagegeR

HagegeR commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Thanks for the suggestion! I considered a structured YAML approach, but the existing codebase intentionally uses text-based manipulation throughout policies.js (no YAML parsing dependencies). Introducing yq would add an external binary dependency that isn't used elsewhere in the project. The current approach stays consistent with the codebase conventions and keeps the fix small. Happy to revisit if the team decides to adopt yq more broadly.

@cv @ericksoa WDYT about a simpler, more maintenable yq approach here? Won't need a special case per policy going forward

@wscurran wscurran added the fix label Mar 31, 2026
@wscurran

Copy link
Copy Markdown
Contributor

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 origin/main before we can review it. Please rebase and resolve any conflicts, and we'll take a look.

@wscurran

Copy link
Copy Markdown
Contributor

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.

@ericksoa

ericksoa commented May 5, 2026

Copy link
Copy Markdown
Contributor

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.

@ericksoa ericksoa closed this May 5, 2026
@wscurran wscurran added bug-fix PR fixes a bug or regression and removed fix labels Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PR fixes a bug or regression

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Policy preset merge creates invalid YAML — text-based manipulation instead of structured YAML parsing

4 participants