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

fix: Create dynamic CEL object variable #432

Merged
merged 2 commits into from
May 21, 2024

Conversation

maxsmythe
Copy link
Contributor

Defines a variable, anyObject that behaves in the same way request.Object does in Rego, allowing for a 1:1 behavioral analogue for Rego's request.Object command when running a template in VAP.

This also fixes a bug in generated CEL code where user-defined variables are placed before variables.params is defined, making users unable to reference variables.params in the variables they create.

Signed-off-by: Max Smythe <smythe@google.com>
@codecov-commenter
Copy link

codecov-commenter commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.19%. Comparing base (76869f8) to head (523e5ec).
Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #432      +/-   ##
==========================================
- Coverage   54.68%   53.19%   -1.50%     
==========================================
  Files          71      104      +33     
  Lines        5241     6550    +1309     
==========================================
+ Hits         2866     3484     +618     
- Misses       2073     2704     +631     
- Partials      302      362      +60     
Flag Coverage Δ
unittests 53.19% <100.00%> (-1.50%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

func BindObjectV1Beta1() admissionregistrationv1beta1.Variable {
return admissionregistrationv1beta1.Variable{
Name: schema.ObjectName,
Expression: `has(request.operation) && request.operation == "DELETE" && object == null ? oldObject : object`,
Copy link
Member

Choose a reason for hiding this comment

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

This logic for rego is actually in GK not framework: https://github.com/open-policy-agent/gatekeeper/blob/f81f0acacb00802108abc81ba5d862bec6c2d633/pkg/util/request_validation.go#L29, should it be moved to framework to be consistent? not a PR blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Framework should not have any coupling with any particular platform.

We should probably move the logic to the TargetHandler.

In addition, we should update the engine logic to undo the setting of obj == oldObject on DELETE, otherwise CEL code will behave differently in on-k8s VAP vs in-g8r VAP.

Copy link
Member

@ritazh ritazh May 18, 2024

Choose a reason for hiding this comment

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

We should probably move the logic to the TargetHandler.

Move the obj == oldObject on DELETE logic to the TargetHandler instead of the webhook validation handler right? Could we move that logic to the rego driver similar to how it's part of the cel driver here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WRT moving it to target handler -- right.

The problem with moving it to the Rego driver is that the Rego driver is more generic than the VAP driver.

The VAP driver is highly coupled with K8s/KRM -- it is strongly typed, assuming an inbound admission review object. Because of this, and because the VAP driver is explicitly intended to support VAP (as opposed to a more generic application of CEL) I'm okay with adding "undoing" logic to the driver (we're not increasing coupling).

For something like the Rego driver, the only real assumption is that the inbound object is JSON-structured.

@@ -22,6 +22,8 @@ const (
ReservedPrefix = "gatekeeper_internal_"
// ParamsName is the VAP variable constraint parameters will be bound to.
ParamsName = "params"
// ObjectName is the VAP variable that describes either an object or (on DELETE requests) oldObject
ObjectName = "anyObject"
Copy link
Member

Choose a reason for hiding this comment

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

With this change, do we plan to update all cel expressions for gatekeeper-libraries policies to use this before merging those PRs? if so, then we need to cut a patch release for GK and update GK-lib CI and docs to ensure the latest framework/GK is used.

Copy link
Member

@sozercan sozercan May 16, 2024

Choose a reason for hiding this comment

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

yea, we'll need to merge this, vendor it in GK, cherry pick in release-3.16, cut a GK release (3.16.1), and update library

Copy link
Member

@sozercan sozercan left a comment

Choose a reason for hiding this comment

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

LGTM pending @ritazh's comments

@maxsmythe
Copy link
Contributor Author

LGTY @ritazh ?

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

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

LGTM

Looks like there's a race condition failure in the readiness tracker test https://github.com/open-policy-agent/frameworks/actions/runs/9134818421/job/25121118481?pr=432

@maxsmythe
Copy link
Contributor Author

WRT the race condition, I'm hoping @David-Jaeyoon-Lee's PR will address this once we solve the shared-fate issue across goroutines:

open-policy-agent/gatekeeper#3308

@maxsmythe maxsmythe merged commit 2ece026 into open-policy-agent:master May 21, 2024
8 checks passed
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.

5 participants