From 503a0aaabd356de9f6557edd49d7b672820b7837 Mon Sep 17 00:00:00 2001 From: vithikashukla Date: Sun, 25 Feb 2024 16:46:56 +0000 Subject: [PATCH] map json to proto in builder Signed-off-by: vithikashukla --- .../VersionDistanceCelScriptBuilder.java | 39 ++++- ...VersionDistanceCelPolicyEvaluatorTest.java | 164 ++++++------------ 2 files changed, 85 insertions(+), 118 deletions(-) diff --git a/src/main/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelScriptBuilder.java b/src/main/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelScriptBuilder.java index 2fbd9b428..17528c769 100644 --- a/src/main/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelScriptBuilder.java +++ b/src/main/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelScriptBuilder.java @@ -1,23 +1,54 @@ package org.dependencytrack.policy.cel.compat; +import alpine.common.logging.Logger; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.util.JsonFormat; +import org.apache.commons.lang3.StringUtils; import org.dependencytrack.model.PolicyCondition; +import org.dependencytrack.proto.policy.v1.VersionDistance; public class VersionDistanceCelScriptBuilder implements CelPolicyScriptSourceBuilder { + + private static final Logger LOGGER = Logger.getLogger(VersionDistanceCelScriptBuilder.class); + @Override public String apply(PolicyCondition policyCondition) { return """ component.version_distance("%s", %s) - """.formatted(comparator(policyCondition.getOperator()), policyCondition.getValue()); + """.formatted(comparator(policyCondition.getOperator()), toProtoString(policyCondition.getValue())); + } + + + private String toProtoString(String conditionValue) { + try { + VersionDistance.Builder structBuilder = VersionDistance.newBuilder(); + JsonFormat.parser().ignoringUnknownFields().merge(conditionValue, structBuilder); + return convertToString(structBuilder.build()); + } catch (InvalidProtocolBufferException e) { + LOGGER.error("Invalid version distance proto " + e); + return convertToString(VersionDistance.newBuilder().build()); + } + } + + private String convertToString(VersionDistance versionDistance) { + StringBuilder sbf = new StringBuilder(); + if (!StringUtils.isEmpty(versionDistance.getEpoch())) { + sbf.append("epoch:").append("\"").append(versionDistance.getEpoch()).append("\"").append(","); + } + sbf.append("major:").append("\"").append(versionDistance.getMajor()).append("\"").append(","); + sbf.append("minor:").append("\"").append(versionDistance.getMinor()).append("\"").append(","); + sbf.append("patch:").append("\"").append(versionDistance.getPatch()).append("\""); + return "v1.VersionDistance{" + sbf + "}"; } - private String comparator (PolicyCondition.Operator operator) { + private String comparator(PolicyCondition.Operator operator) { return switch (operator) { - case NUMERIC_GREATER_THAN -> ">"; + case NUMERIC_GREATER_THAN -> ">"; case NUMERIC_GREATER_THAN_OR_EQUAL -> ">="; case NUMERIC_EQUAL -> "=="; case NUMERIC_NOT_EQUAL -> "!="; case NUMERIC_LESSER_THAN_OR_EQUAL -> "<="; - case NUMERIC_LESS_THAN -> "<"; + case NUMERIC_LESS_THAN -> "<"; default -> ""; }; } diff --git a/src/test/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelPolicyEvaluatorTest.java b/src/test/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelPolicyEvaluatorTest.java index 1459ea568..0018d8ce2 100644 --- a/src/test/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelPolicyEvaluatorTest.java +++ b/src/test/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelPolicyEvaluatorTest.java @@ -44,126 +44,62 @@ public class VersionDistanceCelPolicyEvaluatorTest extends AbstractPostgresEnabl @Parameterized.Parameters(name = "[{index}] version={0} latestVersion={1} operator={2} distance={3} shouldViolate={4}") public static Collection testParameters() { return Arrays.asList(new Object[][]{ - {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{epoch: "1", major: "1", minor: "?",patch: "?"} - """, false}, - {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{epoch: "1", major: "1", minor: "?",patch: "?"} - """, false}, + {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, // Latest version is 1 minor newer than current version - {"1.2.3", "1.3.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "0", minor: "1",patch: "?"} - """, true}, - {"1.2.3", "1.3.1", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{major: "0", minor: "1",patch: "?"} - """, false}, - {"1.2.3", "1.3.1", Operator.NUMERIC_EQUAL, """ - v1.VersionDistance{major: "0", minor: "1", patch: "?"} - """, true}, - {"1.2.3", "1.3.1", Operator.NUMERIC_NOT_EQUAL, """ - v1.VersionDistance{major: "0", minor: "1", patch: "?" - }""", false}, - {"1.2.3", "1.3.1", Operator.NUMERIC_LESS_THAN, """ - v1.VersionDistance{major: "0", minor: "1", patch: "?" - }""", false}, - {"1.2.3", "1.3.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "0", minor: "1", patch: "?" - }""", true}, - // Latest version is 1 minor newer than current version - {"1.2.3", "2.1.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "1", minor: "1",patch: "?"} - """, true}, - {"1.2.3", "2.1.1", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{major: "1", minor: "?",patch: "?"} - """, false}, - {"1.2.3", "2.1.1", Operator.NUMERIC_EQUAL, """ - v1.VersionDistance{major: "1", minor: "1", patch: "?"} - """, true}, - {"1.2.3", "2.1.1", Operator.NUMERIC_NOT_EQUAL, """ - v1.VersionDistance{major: "1", minor: "?", patch: "?" - }""", false}, - {"1.2.3", "2.1.1", Operator.NUMERIC_LESS_THAN, """ - v1.VersionDistance{major: "1", minor: "1", patch: "?" - }""", false}, - {"1.2.3", "2.1.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "1", minor: "1", patch: "?" - }""", true}, + {"1.2.3", "1.3.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", true}, + {"1.2.3", "1.3.1", Operator.NUMERIC_GREATER_THAN, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", false}, + {"1.2.3", "1.3.1", Operator.NUMERIC_EQUAL, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", true}, + {"1.2.3", "1.3.1", Operator.NUMERIC_NOT_EQUAL, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", false}, + {"1.2.3", "1.3.1", Operator.NUMERIC_LESS_THAN, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", false}, + {"1.2.3", "1.3.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{ \"major\": \"0\", \"minor\": \"1\", \"patch\": \"?\" }", true}, + // Latest version is 1 major newer than current version + {"1.2.3", "2.1.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.2.3", "2.1.1", Operator.NUMERIC_GREATER_THAN, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "2.1.1", Operator.NUMERIC_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.2.3", "2.1.1", Operator.NUMERIC_NOT_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "2.1.1", Operator.NUMERIC_LESS_THAN, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "2.1.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, // Latest version is 2 major newer than current version - {"1.2.3", "3.0.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "2", minor: "?",patch: "?"} - """, true}, - {"1.2.3", "3.0.1", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{major: "2", minor: "?",patch: "?"} - """, false}, - {"1.2.3", "3.0.1", Operator.NUMERIC_EQUAL, """ - v1.VersionDistance{major: "2", minor: "?", patch: "?"} - """, true}, - {"1.2.3", "3.0.1", Operator.NUMERIC_NOT_EQUAL, """ - v1.VersionDistance{major: "2", minor: "?", patch: "?"} - """, false}, - {"1.2.3", "3.0.1", Operator.NUMERIC_LESS_THAN, """ - v1.VersionDistance{major: "2", minor: "2", patch: "?"} - """, false}, - {"1.2.3", "3.0.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "2", minor: "2", patch: "?"} - """, true}, - // Component is latest version - {"1.2.3", "1.2.3", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "0", minor: "0",patch: "0"} - """, true}, - {"1.2.3", "1.2.3", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{major: "0", minor: "0",patch: "0"} - """, false}, - {"1.2.3", "1.2.3", Operator.NUMERIC_EQUAL, """ - v1.VersionDistance{major: "0", minor: "0", patch: "0"} - """, true}, - {"1.2.3", "1.2.3", Operator.NUMERIC_NOT_EQUAL, """ - v1.VersionDistance{major: "0", minor: "0", patch: "0"} - """, false}, - {"1.2.3", "1.2.3", Operator.NUMERIC_LESS_THAN, """ - v1.VersionDistance{major: "0", minor: "0", patch: "0"} - """, false}, - {"1.2.3", "1.2.3", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "0", minor: "0", patch: "0"} - """, true}, - // Negative distance - {"2.3.4", "1.2.3", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "1", minor: "?",patch: "?"} - """, true}, - {"2.3.4", "1.2.3", Operator.NUMERIC_GREATER_THAN, """ - v1.VersionDistance{major: "1", minor: "?",patch: "?"} - """, false}, - {"2.3.4", "1.2.3", Operator.NUMERIC_EQUAL, """ - v1.VersionDistance{major: "1", minor: "?", patch: "?"} - """, true}, - {"2.3.4", "1.2.3", Operator.NUMERIC_NOT_EQUAL, """ - v1.VersionDistance{major: "1", minor: "?", patch: "?"} - """, false}, - {"2.3.4", "1.2.3", Operator.NUMERIC_LESS_THAN, """ - v1.VersionDistance{major: "1", minor: "?", patch: "?"} - """, false}, - {"2.3.4", "1.2.3", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, """ - v1.VersionDistance{major: "1", minor: "?", patch: "?"} - """, true}, + {"1.2.3", "3.0.1", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.2.3", "3.0.1", Operator.NUMERIC_GREATER_THAN, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "3.0.1", Operator.NUMERIC_EQUAL, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.2.3", "3.0.1", Operator.NUMERIC_NOT_EQUAL, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "3.0.1", Operator.NUMERIC_LESS_THAN, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.2.3", "3.0.1", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{ \"major\": \"2\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + // Component is latest version. + {"1.2.3", "1.2.3", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", true}, + {"1.2.3", "1.2.3", Operator.NUMERIC_GREATER_THAN, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", false}, + {"1.2.3", "1.2.3", Operator.NUMERIC_EQUAL, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", true}, + {"1.2.3", "1.2.3", Operator.NUMERIC_NOT_EQUAL, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", false}, + {"1.2.3", "1.2.3", Operator.NUMERIC_LESS_THAN, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", false}, + {"1.2.3", "1.2.3", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", true}, + // Negative distanse. + {"2.3.4", "1.2.3", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"2.3.4", "1.2.3", Operator.NUMERIC_GREATER_THAN, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"2.3.4", "1.2.3", Operator.NUMERIC_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"2.3.4", "1.2.3", Operator.NUMERIC_NOT_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"2.3.4", "1.2.3", Operator.NUMERIC_LESS_THAN, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"2.3.4", "1.2.3", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, // Combined policies. - {"2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", true}, - {"1:1.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", true}, - {"1:2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", true}, - {"1.0.0", "1.0.0", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", true}, - {"1.0.0", "1.0.0", Operator.NUMERIC_LESS_THAN, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", true}, - {"1.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", false}, - {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", false}, - {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", false}, - {"2:2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"1\", major: \"1\", minor: \"?\", patch: \"?\" }", false}, - {"3.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"0\", major: \"1\", minor: \"1\", patch: \"1\" }", false}, - {"1.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"0\", major: \"0\", minor: \"1\", patch: \"1\" }", false}, - {"0.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "v1.VersionDistance{epoch: \"0\", major: \"0\", minor: \"1\", patch: \"1\" }", false}, + {"2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1:1.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1:2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.0.0", "1.0.0", Operator.NUMERIC_LESSER_THAN_OR_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.0.0", "1.0.0", Operator.NUMERIC_LESS_THAN, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", true}, + {"1.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN_OR_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"1.0.0", "1.0.0", Operator.NUMERIC_GREATER_THAN, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"2:2.0.0", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"1\", \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, + {"3.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"0\", \"major\": \"1\", \"minor\": \"1\", \"patch\": \"1\" }", false}, + {"1.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"0\", \"major\": \"0\", \"minor\": \"1\", \"patch\": \"1\" }", false}, + {"0.2.2", "1.0.0", Operator.NUMERIC_EQUAL, "{\"epoch\": \"0\", \"major\": \"0\", \"minor\": \"1\", \"patch\": \"1\" }", false}, // Unsupported operator. - {"1.2.3", "2.1.1", Operator.MATCHES, "v1.VersionDistance{ major: \"1\", minor: \"?\", patch: \"?\" }", false}, + {"1.2.3", "2.1.1", Operator.MATCHES, "{ \"major\": \"1\", \"minor\": \"?\", \"patch\": \"?\" }", false}, // Invalid distanse format. - {"1.2.3", "2.1.1", Operator.NUMERIC_EQUAL, "v1.VersionDistance{ major: \"1a\" }", false}, + {"1.2.3", "2.1.1", Operator.NUMERIC_EQUAL, "{ \"major\": \"1a\" }", false}, // No known latestVersion. - {"1.2.3", null, Operator.NUMERIC_EQUAL, "v1.VersionDistance{ major: \"0\", minor: \"0\", patch: \"0\" }", false}, + {"1.2.3", null, Operator.NUMERIC_EQUAL, "{ \"major\": \"0\", \"minor\": \"0\", \"patch\": \"0\" }", false}, }); }