exercise2 shows how to declare and use variables in expressions. Given a `request` of type `google.rpc.context.AttributeContext.Request` determine whether a specific auth claim is set.
()
| 93 | // Given a `request` of type `google.rpc.context.AttributeContext.Request` |
| 94 | // determine whether a specific auth claim is set. |
| 95 | func exercise2() { |
| 96 | fmt.Println("=== Exercise 2: Variables ===\n") |
| 97 | // Construct a standard environment that accepts 'request' as input and uses |
| 98 | // the google.rpc.context.AttributeContext.Request type. |
| 99 | env, err := cel.NewEnv( |
| 100 | cel.Types(&rpcpb.AttributeContext_Request{}), |
| 101 | cel.Variable("request", |
| 102 | cel.ObjectType("google.rpc.context.AttributeContext.Request"), |
| 103 | ), |
| 104 | ) |
| 105 | if err != nil { |
| 106 | glog.Exit(err) |
| 107 | } |
| 108 | ast := compile(env, `request.auth.claims.group == 'admin'`, cel.BoolType) |
| 109 | program, _ := env.Program(ast) |
| 110 | |
| 111 | // Evaluate a request object that sets the proper group claim. |
| 112 | // Output: true |
| 113 | claims := map[string]string{"group": "admin"} |
| 114 | eval(program, request(auth("user:me@acme.co", claims), time.Now())) |
| 115 | fmt.Println() |
| 116 | } |
| 117 | |
| 118 | // exercise3 demonstrates how CEL's commutative logical operators work. |
| 119 | // |