-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from DependencyTrack/use-proto-instead-of-jso…
…n-string Replace json string with proto in Version Distance Cel Policy Closes DependencyTrack/hyades#931
- Loading branch information
Showing
7 changed files
with
127 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
src/main/java/org/dependencytrack/policy/cel/compat/VersionDistanceCelScriptBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,55 @@ | ||
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(policyCondition.getOperator(), CelPolicyScriptSourceBuilder.escapeQuotes(policyCondition.getValue())); | ||
component.version_distance("%s", %s) | ||
""".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) { | ||
return switch (operator) { | ||
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 -> "<"; | ||
default -> ""; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ndencytrack/util/VersionDistanceTest.java → ...dencytrack/model/VersionDistanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
package org.dependencytrack.util; | ||
package org.dependencytrack.model; | ||
|
||
import org.junit.Test; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters