Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbidden sysctls cel #521

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ritazh
Copy link
Member

@ritazh ritazh commented May 10, 2024

What this PR does / why we need it:

Which issue(s) does this PR fix (optional, using fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when the PR gets merged):
Fixes #541

Special notes for your reviewer:

will rebase once #519 is merged

@ritazh ritazh requested a review from a team as a code owner May 10, 2024 06:12
@ritazh ritazh force-pushed the forbidden-sysctls-cel branch 5 times, most recently from 8d2d872 to f9b0657 Compare May 11, 2024 01:15
@ritazh ritazh force-pushed the forbidden-sysctls-cel branch 2 times, most recently from 9887541 to 8bab861 Compare July 6, 2024 00:13
@ritazh ritazh requested a review from maxsmythe July 9, 2024 04:24
@ritazh ritazh requested a review from a team September 4, 2024 20:45
expression: |
!has(variables.params.forbiddenSysctls) ? [] :
variables.params.forbiddenSysctls.filter(sysctl, !sysctl.endsWith("*"))
- name: allAllowed
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the Rego code considers undefined allowedSysctls to mean "all allowed". I think it just denies all sysctls. Worth creating a test case to validate this edge case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the history: https://github.com/open-policy-agent/gatekeeper-library/pull/253/files#diff-b14d9c092cc66bb50ced20769a8508a11132aa8425a27e3a65666fb580b52414R117-R119

comment in the test:

Empty allowedSysctls means none are allowed
unspecified allowedSysctls which does not place any restrictions by itself

The rego code also does not put any restriction on unspecified allowedSysctls.

If we want to change this behavior, it is definitely not backward compatible. IMO we should keep the same logic to reduce confusion. WDYT? @maxsmythe

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely no need to change the behavior. My reading of the Rego code was that empty allowedSysctls leads to a violation (despite what the test comment says).

Maybe my reading is wrong? In either case, adding a test suite for the edge case would validate uniform behavior.

But yeah, to re-iterate, VAP-CEL should behave like the Rego, just want to ensure that is the case.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes I adding 2 new tests (one for empty allowedSysctls and one for unspecified allowedSysctls, which showed existing rego has the same behavior as the test comment due to:

not allowed_sysctl(sysctl) and allowed_sysctl(_) never actually checks for not input.parameters.allowedSysctls

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I think I see why. The message expression below fails if the parameter is unspecified vs. empty:

msg := sprintf("The sysctl %v is not explicitly allowed, pod: %v. Allowed sysctls: %v", [sysctl, input.review.object.metadata.name, input.parameters.allowedSysctls])

which will cause the violation not to be thrown. I imaging replacing the message with a constant would change the behavior of that edge case. Fun times.

Sorry for the extra work. If we add the undefined vs. empty list test, at least we can make sure the edge-case behavior doesn't change in the future. Worth doing?

Copy link
Member Author

Choose a reason for hiding this comment

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

updated for cel.

e.g. output:

The sysctl is not allowed for pod: nginx-forbidden-sysctls-disallowed, forbidden: kernel.*, allowed: unspecified

and

The sysctl is not allowed for pod: nginx-forbidden-sysctls-disallowed, forbidden: kernel.*, allowed: empty

Copy link
Member Author

Choose a reason for hiding this comment

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

re: rego bug, I propose to fix the message and add condition for no violations for unspecified allowedSysctls to ensure backward compat. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the rego not WAI? It will simply not return a message/violation if allowedSysctls is unspecified, which is the expected behavior.

I am definitely down for making the test for allowedSysctls explicit in the Rego... having an implicit logic gate buried in a string formatting statement seems a bit hard to catch for readers.

expression: |
!has(variables.params.forbiddenSysctls) ? [] :
variables.params.forbiddenSysctls.filter(sysctl, !sysctl.endsWith("*"))
- name: allAllowed
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I think I see why. The message expression below fails if the parameter is unspecified vs. empty:

msg := sprintf("The sysctl %v is not explicitly allowed, pod: %v. Allowed sysctls: %v", [sysctl, input.review.object.metadata.name, input.parameters.allowedSysctls])

which will cause the violation not to be thrown. I imaging replacing the message with a constant would change the behavior of that edge case. Fun times.

Sorry for the extra work. If we add the undefined vs. empty list test, at least we can make sure the edge-case behavior doesn't change in the future. Worth doing?

@ritazh ritazh force-pushed the forbidden-sysctls-cel branch 3 times, most recently from fd9ff56 to 76bd55b Compare September 11, 2024 22:17
Copy link
Contributor

@JaydipGabani JaydipGabani left a comment

Choose a reason for hiding this comment

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

LGTM otherwise

apiVersion: v1
kind: Pod
metadata:
name: nginx-forbidden-sysctls-disallowed
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
name: nginx-forbidden-sysctls-disallowed
name: nginx-forbidden-sysctls-allowed

Copy link

stale bot commented Dec 3, 2024

This issue/PR has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 3, 2024
@ritazh ritazh removed the stale label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CEL code for PSP Policies in library
3 participants