-
-
Notifications
You must be signed in to change notification settings - Fork 598
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 #930 from nscuro/version-policy-condition
Add support for version policy conditions
- Loading branch information
Showing
6 changed files
with
548 additions
and
3 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
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
73 changes: 73 additions & 0 deletions
73
src/main/java/org/dependencytrack/policy/VersionPolicyEvaluator.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.dependencytrack.policy; | ||
|
||
import alpine.logging.Logger; | ||
import org.dependencytrack.model.Component; | ||
import org.dependencytrack.model.Policy; | ||
import org.dependencytrack.model.PolicyCondition; | ||
import org.dependencytrack.util.ComponentVersion; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Evaluates a components version against a policy. | ||
* | ||
* @since 4.2.0 | ||
*/ | ||
public class VersionPolicyEvaluator extends AbstractPolicyEvaluator { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(VersionPolicyEvaluator.class); | ||
|
||
@Override | ||
public PolicyCondition.Subject supportedSubject() { | ||
return PolicyCondition.Subject.VERSION; | ||
} | ||
|
||
@Override | ||
public List<PolicyConditionViolation> evaluate(final Policy policy, final Component component) { | ||
final var componentVersion = new ComponentVersion(component.getVersion()); | ||
|
||
final List<PolicyConditionViolation> violations = new ArrayList<>(); | ||
|
||
for (final PolicyCondition condition : super.extractSupportedConditions(policy)) { | ||
LOGGER.debug("Evaluating component (" + component.getUuid() + ") against policy condition (" + condition.getUuid() + ")"); | ||
|
||
final var conditionVersion = new ComponentVersion(condition.getValue()); | ||
if (conditionVersion.getVersionParts().isEmpty()) { | ||
LOGGER.warn("Unable to parse version (" + condition.getValue() + " provided by condition"); | ||
continue; | ||
} | ||
|
||
if (matches(componentVersion, conditionVersion, condition.getOperator())) { | ||
violations.add(new PolicyConditionViolation(condition, component)); | ||
} | ||
} | ||
|
||
return violations; | ||
} | ||
|
||
static boolean matches(final ComponentVersion componentVersion, | ||
final ComponentVersion conditionVersion, | ||
final PolicyCondition.Operator operator) { | ||
final int comparisonResult = componentVersion.compareTo(conditionVersion); | ||
switch (operator) { | ||
case NUMERIC_EQUAL: | ||
return comparisonResult == 0; | ||
case NUMERIC_NOT_EQUAL: | ||
return comparisonResult != 0; | ||
case NUMERIC_LESS_THAN: | ||
return comparisonResult < 0; | ||
case NUMERIC_LESSER_THAN_OR_EQUAL: | ||
return comparisonResult <= 0; | ||
case NUMERIC_GREATER_THAN: | ||
return comparisonResult > 0; | ||
case NUMERIC_GREATER_THAN_OR_EQUAL: | ||
return comparisonResult >= 0; | ||
default: | ||
LOGGER.warn("Unsupported operation " + operator); | ||
break; | ||
} | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.