Skip to content

Conversation

fristonio
Copy link
Member

See commit message for more details.

This CCNP now generates the expected policy.

image

image

Fixes: #36393

Fix CIDRGroupRef handling in cilium network policy rule spec.

@fristonio fristonio requested a review from a team as a code owner June 20, 2025 04:20
@fristonio fristonio added kind/bug This is a bug in the Cilium logic. release-note/bug This PR fixes an issue in a previous release of Cilium. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. labels Jun 20, 2025
@maintainer-s-little-helper maintainer-s-little-helper bot added backport/1.16 This PR represents a backport for Cilium 1.16.x of a PR that was merged to main. kind/backports This PR provides functionality previously merged into master. labels Jun 20, 2025
@fristonio fristonio requested review from bimmlerd and squeed June 20, 2025 04:20
Copy link
Member

@bimmlerd bimmlerd left a comment

Choose a reason for hiding this comment

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

Is this a backport of a fix in main? IIRC, @pippolo84 once fixed something similar?

@fristonio fristonio force-pushed the pr/fristonio/v1.16/fix/cidrgroup-ref branch from d7891ea to 28ecc1a Compare June 20, 2025 08:29
@fristonio
Copy link
Member Author

Is this a backport of a fix in main? IIRC, @pippolo84 once fixed something similar?

My bad, I didn't add the full context. I have updated the commit message with more details.
This issue only effects v1.16 so its a point fix for this branch. For v1.17 and above the CIDRGroup handling has been refactored and decoupled from CNP processing. We don't mutate the CNP object directly to resolve these references anymore so don't have the same issue.
I have tested the faulty policy on both v1.17 and main and it works as expected.

@pippolo84
Copy link
Member

Is this a backport of a fix in main? IIRC, @pippolo84 once fixed something similar?

I also remember fixing something similar, but looking at the closed issues/PRs I can't find anything.
Instead, I see two similar issues that might worth mentioning here for more context:

Anyway, I see there are tests for each of the above cases, so I guess we should be able to intercept any regression here. 👍

This commit fixes an issue introduced as part of another bugfix related
to CIDRGroupRef in cilium policy rule spec (mentioned below).
In the current CIDR group reference handling, if *any* rule in the network
policy spec contains a CIDR group reference, we explicitly set
`FromCIDRSet` as an empty slice even if that specific rule doesn't
contain the reference.
https://github.com/cilium/cilium/blob/v1.16.11/pkg/policy/k8s/cilium_cidr_group.go#L228C3-L230C4

This causes a conflict in policy resolution if there is another mutually
exclusive selector present in that rule(`fromEntities`, `fromCIDR`),
resulting in the rule not processing the intended selectors.
https://github.com/cilium/cilium/blob/v1.16/pkg/policy/api/ingress.go#L223

For Example the first rule in the below spec skips processing the
`fromEntities` selector thinking there is another empty selector
`fromCIDRSet`.

```
spec:
  ingress:
  - fromEntities:
    - cluster
  - fromCIDRSet:
    - cidrGroupRef: cidr-group
```

This fix processes CIDR Group reference in the spec rule only if that
rule specifically has a reference.

This issue only effects v1.16. For v1.17 and above the handling of
CiliumCIDRGroup has been refactored and decoupled from CNP processing.
Now we don't transform the CNP object by resolving references against
CIDRGroup object. Instead an identity is created for this object
and CNP directly creates the selector from CIDR Group reference that
selects it.
https://github.com/cilium/cilium/blob/v1.17/pkg/policy/api/cidr.go#L201-L206

Fixes cilium#36393
Fixes: 0a5d2f7 ("policy: wrong cidrGroupRefs select nothing not all")

Signed-off-by: fristonio <deepeshpathak09@gmail.com>
@fristonio fristonio force-pushed the pr/fristonio/v1.16/fix/cidrgroup-ref branch from 28ecc1a to 65d8b9d Compare June 20, 2025 16:11
@fristonio
Copy link
Member Author

Thanks for sharing the context Fabio. I see that the PR you shared attest to the behavior we expect from empty vs nil selectors. I have added the unit test to verify the behavior where translating the policy to resolve CIDR references shouldn't effect selectors in other rule part of the spec. This test fails before the fix and what this change fixes.

@fristonio
Copy link
Member Author

/test

Copy link
Member

@bimmlerd bimmlerd left a comment

Choose a reason for hiding this comment

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

LGTM. Idly wondering whether there is some case where this is a breaking change though - hyrum's law. But I think this behaviour makes more sense 😅

Copy link
Member

@christarazi christarazi left a comment

Choose a reason for hiding this comment

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

The fix LGTM and makes sense. Having the unit test validate that the fix is correct is good, thanks for adding. Did you verify that this implementation is aligned with v1.17 and main behavior? What we don't want is subtly different behavior in different versions for the same policy.

@fristonio
Copy link
Member Author

Hey Chris! Yeah, i have verified the same policy with both v1.17 and main. The way we realize the policy is different between v1.16 and later so inherently there is a behavioral difference. For example with v1.17 the CIDR group policy are directly selected for CIDR group identity:

image

With v1.16 we create a separate identity for each CIDR in the Group object(see PR summary).
That being said the policy realized for other rules in the Spec(eg. with fromEntities selector) is the same. The PR is only fixing the side effects to other components in the spec when processing {from,to}CIDRSet fields.

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Jun 24, 2025
@fristonio fristonio added this pull request to the merge queue Jun 24, 2025
Merged via the queue into cilium:v1.16 with commit 38250a0 Jun 24, 2025
60 of 63 checks passed
@fristonio fristonio deleted the pr/fristonio/v1.16/fix/cidrgroup-ref branch June 24, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/1.16 This PR represents a backport for Cilium 1.16.x of a PR that was merged to main. kind/backports This PR provides functionality previously merged into master. kind/bug This is a bug in the Cilium logic. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/bug This PR fixes an issue in a previous release of Cilium. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants