-
-
Notifications
You must be signed in to change notification settings - Fork 793
Issue-2557 : add vulnerability id in policy condition #2570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nscuro
merged 3 commits into
DependencyTrack:master
from
Citi:Issue-2557-vuln-id-in-policy-condition
Mar 8, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -76,7 +76,8 @@ public enum Subject { | |
| SWID_TAGID, | ||
| VERSION, | ||
| COMPONENT_HASH, | ||
| CWE | ||
| CWE, | ||
| VULNERABILITY_ID | ||
| } | ||
|
|
||
| @PrimaryKey | ||
|
|
||
This file contains hidden or 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
68 changes: 68 additions & 0 deletions
68
src/main/java/org/dependencytrack/policy/VulnerabilityIdPolicyEvaluator.java
This file contains hidden or 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,68 @@ | ||
| /* | ||
| * This file is part of Dependency-Track. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright (c) Steve Springett. All Rights Reserved. | ||
| */ | ||
| package org.dependencytrack.policy; | ||
|
|
||
| import alpine.common.logging.Logger; | ||
| import org.dependencytrack.model.Component; | ||
| import org.dependencytrack.model.Policy; | ||
| import org.dependencytrack.model.PolicyCondition; | ||
| import org.dependencytrack.model.Vulnerability; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Evaluates the component vulnerabilities against a policy based on vulnerability ID. | ||
| */ | ||
| public class VulnerabilityIdPolicyEvaluator extends AbstractPolicyEvaluator { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(VulnerabilityIdPolicyEvaluator.class); | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public PolicyCondition.Subject supportedSubject() { | ||
| return PolicyCondition.Subject.VULNERABILITY_ID; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public List<PolicyConditionViolation> evaluate(final Policy policy, final Component component) { | ||
| final List<PolicyConditionViolation> violations = new ArrayList<>(); | ||
| final List<PolicyCondition> policyConditions = super.extractSupportedConditions(policy); | ||
| for (final Vulnerability vulnerability : qm.getAllVulnerabilities(component, false)) { | ||
| for (final PolicyCondition condition: policyConditions) { | ||
| LOGGER.debug("Evaluating component (" + component.getUuid() + ") against policy condition (" + condition.getUuid() + ")"); | ||
| if (PolicyCondition.Operator.IS == condition.getOperator()) { | ||
| if (vulnerability.getVulnId().equals(condition.getValue())) { | ||
| violations.add(new PolicyConditionViolation(condition, component)); | ||
| } | ||
| } else if (PolicyCondition.Operator.IS_NOT == condition.getOperator()) { | ||
| if (! vulnerability.getVulnId().equals(condition.getValue())) { | ||
| violations.add(new PolicyConditionViolation(condition, component)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return violations; | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
src/test/java/org/dependencytrack/policy/VulnerabilityIdPolicyEvaluatorTest.java
This file contains hidden or 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,105 @@ | ||
| /* | ||
| * This file is part of Dependency-Track. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright (c) Steve Springett. All Rights Reserved. | ||
| */ | ||
| package org.dependencytrack.policy; | ||
|
|
||
| import org.dependencytrack.PersistenceCapableTest; | ||
| import org.dependencytrack.model.Component; | ||
| import org.dependencytrack.model.Policy; | ||
| import org.dependencytrack.model.PolicyCondition; | ||
| import org.dependencytrack.model.Project; | ||
| import org.dependencytrack.model.Vulnerability; | ||
| import org.dependencytrack.tasks.scanners.AnalyzerIdentity; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class VulnerabilityIdPolicyEvaluatorTest extends PersistenceCapableTest { | ||
|
|
||
| @Test | ||
| public void hasMatch() { | ||
| Policy policy = qm.createPolicy("Test Policy", Policy.Operator.ANY, Policy.ViolationState.INFO); | ||
| PolicyCondition condition = qm.createPolicyCondition(policy, PolicyCondition.Subject.VULNERABILITY_ID, PolicyCondition.Operator.IS, "test-123"); | ||
| Project project = new Project(); | ||
| project.setName("My Project"); | ||
| Component component = new Component(); | ||
| component.setName("Test Component"); | ||
| component.setProject(project); | ||
| Vulnerability vulnerability = new Vulnerability(); | ||
| vulnerability.setVulnId("test-123"); | ||
| vulnerability.setSource(Vulnerability.Source.INTERNAL); | ||
| qm.persist(project); | ||
| qm.persist(component); | ||
| qm.persist(vulnerability); | ||
| qm.addVulnerability(vulnerability, component, AnalyzerIdentity.INTERNAL_ANALYZER); | ||
| PolicyEvaluator evaluator = new VulnerabilityIdPolicyEvaluator(); | ||
| evaluator.setQueryManager(qm); | ||
| List<PolicyConditionViolation> violations = evaluator.evaluate(policy, component); | ||
| Assert.assertEquals(1, violations.size()); | ||
| PolicyConditionViolation violation = violations.get(0); | ||
| Assert.assertEquals(component.getId(), violation.getComponent().getId()); | ||
| Assert.assertEquals(condition, violation.getPolicyCondition()); | ||
| } | ||
|
|
||
| @Test | ||
| public void noMatch() { | ||
| Policy policy = qm.createPolicy("Test Policy", Policy.Operator.ANY, Policy.ViolationState.INFO); | ||
| qm.createPolicyCondition(policy, PolicyCondition.Subject.VULNERABILITY_ID, PolicyCondition.Operator.IS, "test-123"); | ||
| Project project = new Project(); | ||
| project.setName("My Project"); | ||
| Component component = new Component(); | ||
| component.setName("Test Component"); | ||
| component.setVersion("1.0"); | ||
| component.setProject(project); | ||
| Vulnerability vulnerability = new Vulnerability(); | ||
| vulnerability.setVulnId("test-789"); | ||
| vulnerability.setSource(Vulnerability.Source.INTERNAL); | ||
| qm.persist(project); | ||
| qm.persist(component); | ||
| qm.persist(vulnerability); | ||
| qm.addVulnerability(vulnerability, component, AnalyzerIdentity.INTERNAL_ANALYZER); | ||
| PolicyEvaluator evaluator = new VulnerabilityIdPolicyEvaluator(); | ||
| evaluator.setQueryManager(qm); | ||
| List<PolicyConditionViolation> violations = evaluator.evaluate(policy, component); | ||
| Assert.assertEquals(0, violations.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void noMatchIsNotOperator() { | ||
| Policy policy = qm.createPolicy("Test Policy", Policy.Operator.ANY, Policy.ViolationState.INFO); | ||
| qm.createPolicyCondition(policy, PolicyCondition.Subject.VULNERABILITY_ID, PolicyCondition.Operator.IS_NOT, "test-123"); | ||
| Project project = new Project(); | ||
| project.setName("My Project"); | ||
| Component component = new Component(); | ||
| component.setName("Test Component"); | ||
| component.setVersion("1.0"); | ||
| component.setProject(project); | ||
| Vulnerability vulnerability = new Vulnerability(); | ||
| vulnerability.setVulnId("test-123"); | ||
| vulnerability.setSource(Vulnerability.Source.INTERNAL); | ||
| qm.persist(project); | ||
| qm.persist(component); | ||
| qm.persist(vulnerability); | ||
| qm.addVulnerability(vulnerability, component, AnalyzerIdentity.INTERNAL_ANALYZER); | ||
| PolicyEvaluator evaluator = new VulnerabilityIdPolicyEvaluator(); | ||
| evaluator.setQueryManager(qm); | ||
| List<PolicyConditionViolation> violations = evaluator.evaluate(policy, component); | ||
| Assert.assertEquals(0, violations.size()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.