Skip to content

Commit

Permalink
rename input to object
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Nov 5, 2024
1 parent 4fa6c67 commit 71dc822
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion manifests/policies/demo-policy.example.com.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
spec:
variables:
- name: request_headers
expression: input.attributes.request.http.headers
expression: object.attributes.request.http.headers
- name: force_unauthenticated
expression: variables.?request_headers["x-force-unauthenticated"].orValue("disabled") == "enabled"
- name: force_authorized
Expand Down
19 changes: 12 additions & 7 deletions pkg/policy/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"k8s.io/apiserver/pkg/cel/lazy"
)

const (
VariablesKey = "variables"
ObjectKey = "object"
)

type PolicyFunc func(*authv3.CheckRequest) (*authv3.CheckResponse, error)

type Compiler interface {
Expand All @@ -36,8 +41,8 @@ func (c *compiler) Compile(policy v1alpha1.AuthorizationPolicy) (PolicyFunc, err
}
provider := engine.NewVariablesProvider(base.CELTypeProvider())
env, err := base.Extend(
cel.Variable("input", envoy.CheckRequest),
cel.Variable("variables", engine.VariablesType),
cel.Variable(ObjectKey, envoy.CheckRequest),
cel.Variable(VariablesKey, engine.VariablesType),
cel.CustomTypeProvider(provider),
)
if err != nil {
Expand Down Expand Up @@ -69,11 +74,11 @@ func (c *compiler) Compile(policy v1alpha1.AuthorizationPolicy) (PolicyFunc, err
}
authorizations = append(authorizations, prog)
}
eval := func(req *authv3.CheckRequest) (*authv3.CheckResponse, error) {
eval := func(r *authv3.CheckRequest) (*authv3.CheckResponse, error) {
vars := lazy.NewMapValue(engine.VariablesType)
data := map[string]any{
"input": req,
"variables": vars,
ObjectKey: r,
VariablesKey: vars,
}
for name, variable := range variables {
vars.Append(name, func(*lazy.MapValue) ref.Val {
Expand Down Expand Up @@ -102,8 +107,8 @@ func (c *compiler) Compile(policy v1alpha1.AuthorizationPolicy) (PolicyFunc, err
}
return nil, nil
}
return func(req *authv3.CheckRequest) (*authv3.CheckResponse, error) {
response, err := eval(req)
return func(r *authv3.CheckRequest) (*authv3.CheckResponse, error) {
response, err := eval(r)
if err != nil && policy.Spec.GetFailurePolicy() == admissionregistrationv1.Fail {
return nil, err
}
Expand Down

0 comments on commit 71dc822

Please sign in to comment.