|
19 | 19 | import com.google.common.annotations.VisibleForTesting; |
20 | 20 | import com.google.common.base.Ascii; |
21 | 21 | import com.google.common.io.Resources; |
22 | | -import dev.cel.common.formats.ValueString; |
23 | | -import dev.cel.policy.CelPolicy.Match; |
24 | | -import dev.cel.policy.CelPolicy.Match.Result; |
25 | | -import dev.cel.policy.CelPolicy.Rule; |
26 | | -import dev.cel.policy.CelPolicyParser.TagVisitor; |
27 | 22 | import java.io.IOException; |
28 | 23 | import java.net.URL; |
29 | 24 | import java.util.List; |
30 | 25 | import java.util.Map; |
31 | 26 | import org.yaml.snakeyaml.LoaderOptions; |
32 | 27 | import org.yaml.snakeyaml.Yaml; |
33 | 28 | import org.yaml.snakeyaml.constructor.Constructor; |
34 | | -import org.yaml.snakeyaml.nodes.Node; |
35 | | -import org.yaml.snakeyaml.nodes.SequenceNode; |
36 | 29 |
|
37 | 30 | /** Package-private class to assist with policy testing. */ |
38 | 31 | final class PolicyTestHelper { |
@@ -273,106 +266,5 @@ private static String readFile(String path) throws IOException { |
273 | 266 | return Resources.toString(getResource(path), UTF_8); |
274 | 267 | } |
275 | 268 |
|
276 | | - static class K8sTagHandler implements TagVisitor<Node> { |
277 | | - |
278 | | - @Override |
279 | | - public void visitPolicyTag( |
280 | | - PolicyParserContext<Node> ctx, |
281 | | - long id, |
282 | | - String tagName, |
283 | | - Node node, |
284 | | - CelPolicy.Builder policyBuilder) { |
285 | | - switch (tagName) { |
286 | | - case "kind": |
287 | | - policyBuilder.putMetadata("kind", ctx.newYamlString(node)); |
288 | | - break; |
289 | | - case "metadata": |
290 | | - long metadataId = ctx.collectMetadata(node); |
291 | | - if (!node.getTag().getValue().equals("tag:yaml.org,2002:map")) { |
292 | | - ctx.reportError( |
293 | | - metadataId, |
294 | | - String.format( |
295 | | - "invalid 'metadata' type, expected map got: %s", node.getTag().getValue())); |
296 | | - } |
297 | | - break; |
298 | | - case "spec": |
299 | | - Rule rule = ctx.parseRule(ctx, policyBuilder, node); |
300 | | - policyBuilder.setRule(rule); |
301 | | - break; |
302 | | - default: |
303 | | - TagVisitor.super.visitPolicyTag(ctx, id, tagName, node, policyBuilder); |
304 | | - break; |
305 | | - } |
306 | | - } |
307 | | - |
308 | | - @Override |
309 | | - public void visitRuleTag( |
310 | | - PolicyParserContext<Node> ctx, |
311 | | - long id, |
312 | | - String tagName, |
313 | | - Node node, |
314 | | - CelPolicy.Builder policyBuilder, |
315 | | - Rule.Builder ruleBuilder) { |
316 | | - switch (tagName) { |
317 | | - case "failurePolicy": |
318 | | - policyBuilder.putMetadata(tagName, ctx.newYamlString(node)); |
319 | | - break; |
320 | | - case "matchConstraints": |
321 | | - long matchConstraintsId = ctx.collectMetadata(node); |
322 | | - if (!node.getTag().getValue().equals("tag:yaml.org,2002:map")) { |
323 | | - ctx.reportError( |
324 | | - matchConstraintsId, |
325 | | - String.format( |
326 | | - "invalid 'matchConstraints' type, expected map got: %s", |
327 | | - node.getTag().getValue())); |
328 | | - } |
329 | | - break; |
330 | | - case "validations": |
331 | | - long validationId = ctx.collectMetadata(node); |
332 | | - if (!node.getTag().getValue().equals("tag:yaml.org,2002:seq")) { |
333 | | - ctx.reportError( |
334 | | - validationId, |
335 | | - String.format( |
336 | | - "invalid 'validations' type, expected list got: %s", node.getTag().getValue())); |
337 | | - } |
338 | | - |
339 | | - SequenceNode validationNodes = (SequenceNode) node; |
340 | | - for (Node element : validationNodes.getValue()) { |
341 | | - ruleBuilder.addMatches(ctx.parseMatch(ctx, policyBuilder, element)); |
342 | | - } |
343 | | - break; |
344 | | - default: |
345 | | - TagVisitor.super.visitRuleTag(ctx, id, tagName, node, policyBuilder, ruleBuilder); |
346 | | - break; |
347 | | - } |
348 | | - } |
349 | | - |
350 | | - @Override |
351 | | - public void visitMatchTag( |
352 | | - PolicyParserContext<Node> ctx, |
353 | | - long id, |
354 | | - String tagName, |
355 | | - Node node, |
356 | | - CelPolicy.Builder policyBuilder, |
357 | | - Match.Builder matchBuilder) { |
358 | | - switch (tagName) { |
359 | | - case "expression": |
360 | | - // The K8s expression to validate must return false in order to generate a violation |
361 | | - // message. |
362 | | - ValueString conditionValue = ctx.newYamlString(node); |
363 | | - conditionValue = |
364 | | - conditionValue.toBuilder().setValue("!(" + conditionValue.value() + ")").build(); |
365 | | - matchBuilder.setCondition(conditionValue); |
366 | | - break; |
367 | | - case "messageExpression": |
368 | | - matchBuilder.setResult(Result.ofOutput(ctx.newYamlString(node))); |
369 | | - break; |
370 | | - default: |
371 | | - TagVisitor.super.visitMatchTag(ctx, id, tagName, node, policyBuilder, matchBuilder); |
372 | | - break; |
373 | | - } |
374 | | - } |
375 | | - } |
376 | | - |
377 | 269 | private PolicyTestHelper() {} |
378 | 270 | } |
0 commit comments