|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package codelab; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import com.google.protobuf.Struct; |
| 21 | +import com.google.protobuf.Timestamp; |
| 22 | +import com.google.protobuf.Value; |
| 23 | +import com.google.protobuf.util.Timestamps; |
| 24 | +import com.google.rpc.context.AttributeContext; |
| 25 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 26 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | + |
| 30 | +@RunWith(TestParameterInjector.class) |
| 31 | +public final class Exercise6Test { |
| 32 | + private final Exercise6 exercise6 = new Exercise6(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void evaluate_constructAttributeContext() { |
| 36 | + // Given JSON web token and the current time as input variables, |
| 37 | + // Setup an expression to construct an AttributeContext protobuf object. |
| 38 | + // |
| 39 | + // Note: the field names within the proto message types are not quoted as they |
| 40 | + // are well-defined names composed of valid identifier characters. Also, note |
| 41 | + // that when building nested proto objects, the message name needs to prefix |
| 42 | + // the object construction. |
| 43 | + String expression = |
| 44 | + "Request{\n" |
| 45 | + + "auth: Auth{" |
| 46 | + + " principal: jwt.iss + '/' + jwt.sub," |
| 47 | + + " audiences: [jwt.aud]," |
| 48 | + + " presenter: 'azp' in jwt ? jwt.azp : ''," |
| 49 | + + " claims: jwt" |
| 50 | + + "}," |
| 51 | + + "time: now" |
| 52 | + + "}"; |
| 53 | + // Values for `now` and `jwt` variables to be passed into the runtime |
| 54 | + Timestamp now = Timestamps.now(); |
| 55 | + ImmutableMap<String, Object> jwt = |
| 56 | + ImmutableMap.of( |
| 57 | + "sub", "serviceAccount:delegate@acme.co", |
| 58 | + "aud", "my-project", |
| 59 | + "iss", "auth.acme.com:12350", |
| 60 | + "extra_claims", ImmutableMap.of("group", "admin")); |
| 61 | + AttributeContext.Request expectedMessage = |
| 62 | + AttributeContext.Request.newBuilder() |
| 63 | + .setTime(now) |
| 64 | + .setAuth( |
| 65 | + AttributeContext.Auth.newBuilder() |
| 66 | + .setPrincipal("auth.acme.com:12350/serviceAccount:delegate@acme.co") |
| 67 | + .addAudiences("my-project") |
| 68 | + .setClaims( |
| 69 | + Struct.newBuilder() |
| 70 | + .putAllFields( |
| 71 | + ImmutableMap.of( |
| 72 | + "sub", newStringValue("serviceAccount:delegate@acme.co"), |
| 73 | + "aud", newStringValue("my-project"), |
| 74 | + "iss", newStringValue("auth.acme.com:12350"))) |
| 75 | + .putFields( |
| 76 | + "extra_claims", |
| 77 | + Value.newBuilder() |
| 78 | + .setStructValue( |
| 79 | + Struct.newBuilder() |
| 80 | + .putFields("group", newStringValue("admin")) |
| 81 | + .build()) |
| 82 | + .build()))) |
| 83 | + .build(); |
| 84 | + |
| 85 | + // Compile the `Request` message construction expression and validate that |
| 86 | + // the resulting expression type matches the fully qualified message name. |
| 87 | + CelAbstractSyntaxTree ast = exercise6.compile(expression); |
| 88 | + AttributeContext.Request evaluatedResult = |
| 89 | + (AttributeContext.Request) |
| 90 | + exercise6.eval( |
| 91 | + ast, |
| 92 | + ImmutableMap.of( |
| 93 | + "now", now, |
| 94 | + "jwt", jwt)); |
| 95 | + |
| 96 | + assertThat(evaluatedResult).isEqualTo(expectedMessage); |
| 97 | + } |
| 98 | + |
| 99 | + private static Value newStringValue(String value) { |
| 100 | + return Value.newBuilder().setStringValue(value).build(); |
| 101 | + } |
| 102 | +} |
0 commit comments