|
| 1 | +// Copyright 2026 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 dev.cel.policy.testing; |
| 16 | + |
| 17 | +import com.google.common.annotations.VisibleForTesting; |
| 18 | +import dev.cel.common.formats.ValueString; |
| 19 | +import dev.cel.common.formats.YamlHelper; |
| 20 | +import dev.cel.common.formats.YamlHelper.YamlNodeType; |
| 21 | +import dev.cel.policy.CelPolicy; |
| 22 | +import dev.cel.policy.CelPolicy.Match; |
| 23 | +import dev.cel.policy.CelPolicyParser.TagVisitor; |
| 24 | +import dev.cel.policy.PolicyParserContext; |
| 25 | +import org.yaml.snakeyaml.nodes.Node; |
| 26 | +import org.yaml.snakeyaml.nodes.SequenceNode; |
| 27 | + |
| 28 | +/** |
| 29 | + * K8sTagHandler is a {@link TagVisitor} implementation to support parsing Kubernetes |
| 30 | + * ValidatingAdmissionPolicy structures in testing and conformance environments. |
| 31 | + */ |
| 32 | +@VisibleForTesting |
| 33 | +public final class K8sTagHandler implements TagVisitor<Node> { |
| 34 | + |
| 35 | + @Override |
| 36 | + public void visitPolicyTag( |
| 37 | + PolicyParserContext<Node> ctx, |
| 38 | + long id, |
| 39 | + String tagName, |
| 40 | + Node node, |
| 41 | + CelPolicy.Builder policyBuilder) { |
| 42 | + switch (tagName) { |
| 43 | + case "kind": |
| 44 | + policyBuilder.putMetadata("kind", ctx.newYamlString(node).value()); |
| 45 | + break; |
| 46 | + case "metadata": |
| 47 | + YamlHelper.assertYamlType(ctx, id, node, YamlNodeType.MAP); |
| 48 | + break; |
| 49 | + case "spec": |
| 50 | + CelPolicy.Rule spec = ctx.parseRule(ctx, policyBuilder, node); |
| 51 | + policyBuilder.setRule(spec); |
| 52 | + break; |
| 53 | + default: |
| 54 | + TagVisitor.super.visitPolicyTag(ctx, id, tagName, node, policyBuilder); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void visitRuleTag( |
| 61 | + PolicyParserContext<Node> ctx, |
| 62 | + long id, |
| 63 | + String tagName, |
| 64 | + Node node, |
| 65 | + CelPolicy.Builder policyBuilder, |
| 66 | + CelPolicy.Rule.Builder ruleBuilder) { |
| 67 | + switch (tagName) { |
| 68 | + case "failurePolicy": |
| 69 | + policyBuilder.putMetadata(tagName, ctx.newYamlString(node).value()); |
| 70 | + break; |
| 71 | + case "matchConstraints": |
| 72 | + YamlHelper.assertYamlType(ctx, id, node, YamlNodeType.MAP); |
| 73 | + break; |
| 74 | + case "validations": |
| 75 | + if (!YamlHelper.assertYamlType(ctx, id, node, YamlNodeType.LIST)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + SequenceNode seqNode = (SequenceNode) node; |
| 79 | + for (Node valNode : seqNode.getValue()) { |
| 80 | + ruleBuilder.addMatches(ctx.parseMatch(ctx, policyBuilder, valNode)); |
| 81 | + } |
| 82 | + break; |
| 83 | + default: |
| 84 | + TagVisitor.super.visitRuleTag(ctx, id, tagName, node, policyBuilder, ruleBuilder); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void visitMatchTag( |
| 91 | + PolicyParserContext<Node> ctx, |
| 92 | + long id, |
| 93 | + String tagName, |
| 94 | + Node node, |
| 95 | + CelPolicy.Builder policyBuilder, |
| 96 | + CelPolicy.Match.Builder matchBuilder) { |
| 97 | + if (!matchBuilder.result().isPresent()) { |
| 98 | + matchBuilder.setResult( |
| 99 | + Match.Result.ofOutput(ValueString.of(ctx.nextId(), "'invalid admission request'"))); |
| 100 | + } |
| 101 | + switch (tagName) { |
| 102 | + case "expression": |
| 103 | + // The K8s expression to validate must return false in order to generate a violation |
| 104 | + // message. |
| 105 | + ValueString condition = ctx.newSourceString(node); |
| 106 | + String invertedCondition = "!(" + condition.value() + ")"; |
| 107 | + matchBuilder.setCondition(ValueString.of(condition.id(), invertedCondition)); |
| 108 | + break; |
| 109 | + case "messageExpression": |
| 110 | + matchBuilder.setResult(Match.Result.ofOutput(ctx.newSourceString(node))); |
| 111 | + break; |
| 112 | + default: |
| 113 | + TagVisitor.super.visitMatchTag(ctx, id, tagName, node, policyBuilder, matchBuilder); |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments