Skip to content

Commit

Permalink
map json to proto in builder
Browse files Browse the repository at this point in the history
Signed-off-by: vithikashukla <[email protected]>
  • Loading branch information
vithikashukla committed Feb 25, 2024
1 parent a944a91 commit 503a0aa
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -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 -> "";
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
});
}

Expand Down

0 comments on commit 503a0aa

Please sign in to comment.