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
()
| 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. |
| 127 | func 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 | // |