MCPcopy Create free account
hub / github.com/cel-expr/cel-go / exercise3

Function exercise3

codelab/solution/codelab.go:127–150  ·  view source on GitHub ↗

exercise3 demonstrates how CEL's commutative logical operators work. Construct an expression which checks whether the `request.auth.claims.group` value is equal to `admin` or the `request.auth.principal` is `user:me@acme.co` and the `request.time` is during work hours (9:00 - 17:00) Evaluate the e

()

Source from the content-addressed store, hash-verified

125// sets the appropriate principal and occurs at 12:00 hours. Then evaluate the
126// request a second time at midnight. Observe the difference in output.
127func exercise3() {
128 fmt.Println("=== Exercise 3: Logical AND/OR ===\n")
129 env, _ := cel.NewEnv(
130 cel.Types(&rpcpb.AttributeContext_Request{}),
131 cel.Variable("request",
132 cel.ObjectType("google.rpc.context.AttributeContext.Request"),
133 ),
134 )
135 ast := compile(env,
136 `request.auth.claims.group == 'admin'
137 || request.auth.principal == 'user:me@acme.co'`,
138 cel.BoolType)
139 program, _ := env.Program(ast)
140
141 // Evaluate once with no claims and the proper user.
142 // Output: true
143 eval(program, request(auth("user:me@acme.co", emptyClaims), time.Now()))
144
145 // Evaluate again with no claims and an unexpected user.
146 // Output: error, no such key
147 eval(program, request(auth("other:me@acme.co", emptyClaims), time.Now()))
148
149 fmt.Println()
150}
151
152// exercise4 demonstrates how to extend CEL with custom functions.
153//

Callers 1

mainFunction · 0.70

Calls 8

ProgramMethod · 0.95
NewEnvFunction · 0.92
TypesFunction · 0.92
VariableFunction · 0.92
compileFunction · 0.70
evalFunction · 0.70
requestFunction · 0.70
authFunction · 0.70

Tested by

no test coverage detected