-
Notifications
You must be signed in to change notification settings - Fork 243
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
[Enhancement] Update CEL policies to make use of optionals and variables to remove redundant expressions #1058
Comments
So, using these techniques we can simply a policy like this: validate:
cel:
expressions:
- expression: >-
(
(
has(object.spec.securityContext) &&
has(object.spec.securityContext.runAsNonRoot) &&
object.spec.securityContext.runAsNonRoot == true
) && (
(
object.spec.containers +
(has(object.spec.initContainers) ? object.spec.initContainers : []) +
(has(object.spec.ephemeralContainers) ? object.spec.ephemeralContainers : [])
).all(container,
!has(container.securityContext) ||
!has(container.securityContext.runAsNonRoot) ||
container.securityContext.runAsNonRoot == true)
)
) || (
(
object.spec.containers +
(has(object.spec.initContainers) ? object.spec.initContainers : []) +
(has(object.spec.ephemeralContainers) ? object.spec.ephemeralContainers : [])
).all(container,
has(container.securityContext) &&
has(container.securityContext.runAsNonRoot) &&
container.securityContext.runAsNonRoot == true)
)
to: validate:
cel:
variables:
- name: ctnrs
expression: >-
object.spec.containers +
(has(object.spec.initContainers) ? object.spec.initContainers : []) +
(has(object.spec.ephemeralContainers) ? object.spec.ephemeralContainers : [])
expressions:
- expression: >-
(object.spec.?securityContext.?runAsNonRoot.orValue(false) == true
&& variables.ctnrs.all(c, c.?securityContext.?runAsNonRoot.orValue(true) == true))
|| variables.ctnrs.all(c, c.?securityContext.?runAsNonRoot.orValue(false) == true) |
Here are some more examples of simplifying CEL expressions in policies using optionals: Example 1
Can be written as
Example 2
Can be written as:
Example 3
Can be written as:
Example 4
Can be written as:
Example 5
Can be written as:
|
Hi @JimBugwadia , Could you assign this issue to me, it will be helpful if you provide guidance on how to get started? |
@Chandan-DK , @JimBugwadia Can I raise the pr for all . |
Problem Statement
There are redundant expressions in CEL policies that are present in the library. For example, here
container.securityContext
has been repeated multiple times:Here, we can remove redundant expressions by using optionals:
CEL Playground for the above expressions.
Also, we can optimize some CEL policies by using
variables
. ExampleSolution Description
Update the following CEL policies to make use of optionals and variables to remove redundant expressions.
Note:
The below checklist is not exhaustive and more policies will be added here.
pod-security-cel
Other Comments
No response
Slack discussion
No response
Troubleshooting
The text was updated successfully, but these errors were encountered: