Skip to content
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

Fix failing analysis updates #769

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/org/dependencytrack/model/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class Analysis implements Serializable {
@Persistent
@Column(name = "VULNERABILITY_POLICY_ID", allowsNull = "true")
@JsonIgnore
private VulnerabilityPolicy vulnerabilityPolicy;
private Long vulnerabilityPolicyId;

public long getId() {
return id;
Expand Down Expand Up @@ -270,11 +270,11 @@ public void setOwaspScore(BigDecimal owaspScore) {
this.owaspScore = owaspScore;
}

public VulnerabilityPolicy getVulnerabilityPolicy() {
return vulnerabilityPolicy;
public Long getVulnerabilityPolicyId() {
return vulnerabilityPolicyId;
}

public void setVulnerabilityPolicy(VulnerabilityPolicy vulnerabilityPolicy) {
this.vulnerabilityPolicy = vulnerabilityPolicy;
public void setVulnerabilityPolicyId(Long vulnerabilityPolicyId) {
this.vulnerabilityPolicyId = vulnerabilityPolicyId;
}
}
200 changes: 0 additions & 200 deletions src/main/java/org/dependencytrack/model/VulnerabilityPolicy.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ public Project clone(UUID from, String newVersion, boolean includeTags, boolean
analysis.setAnalysisJustification(sourceAnalysis.getAnalysisJustification());
analysis.setAnalysisState(sourceAnalysis.getAnalysisState());
analysis.setAnalysisDetails(sourceAnalysis.getAnalysisDetails());
analysis.setVulnerabilityPolicyId(sourceAnalysis.getVulnerabilityPolicyId());
analysis = persist(analysis);
if (sourceAnalysis.getAnalysisComments() != null) {
for (final AnalysisComment sourceComment : sourceAnalysis.getAnalysisComments()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.common.ConfigKey;
import org.dependencytrack.event.VulnerabilityPolicyFetchEvent;
import org.dependencytrack.model.VulnerabilityPolicy;
import org.dependencytrack.model.WorkflowState;
import org.dependencytrack.model.WorkflowStatus;
import org.dependencytrack.model.WorkflowStep;
import org.dependencytrack.persistence.QueryManager;
import org.dependencytrack.policy.vulnerability.VulnerabilityPolicy;
import org.dependencytrack.policy.vulnerability.VulnerabilityPolicyProvider;
import org.dependencytrack.policy.vulnerability.VulnerabilityPolicyProviderFactory;
import org.dependencytrack.resources.v1.openapi.PaginatedApi;
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<class>org.dependencytrack.model.VulnerableSoftware</class>
<class>org.dependencytrack.model.WorkflowState</class>
<class>org.dependencytrack.model.IntegrityAnalysis</class>
<class>org.dependencytrack.model.VulnerabilityPolicy</class>
<class>org.dependencytrack.model.VulnerabilityPolicyBundle</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ public void analysisThroughPolicyResetOnNoMatchTest() {
assertThat(v.getVulnId()).isEqualTo("CVE-100");
assertThat(qm.getAnalysis(component, v)).satisfies(a -> {
assertThat(a.getAnalysisState()).isEqualTo(AnalysisState.NOT_SET);
assertThat(a.getVulnerabilityPolicy()).isNull();
assertThat(a.getVulnerabilityPolicyId()).isNull();
assertThat(a.getAnalysisComments()).extracting(AnalysisComment::getCommenter).containsOnly("[Policy{None}]");
assertThat(a.getAnalysisComments()).extracting(AnalysisComment::getComment).containsExactlyInAnyOrder(
"No longer covered by any policy",
Expand Down Expand Up @@ -1267,7 +1267,7 @@ public void analysisThroughPolicyResetOnNoMatchTest() {
assertThat(a.getCvssV2Score()).isNull();
assertThat(a.getCvssV3Vector()).isNull();
assertThat(a.getCvssV3Score()).isNull();
assertThat(a.getVulnerabilityPolicy()).isNull();
assertThat(a.getVulnerabilityPolicyId()).isNull();
assertThat(a.getAnalysisComments()).isEmpty();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void testVulnerabilityPolicyIsUnassignedAndDeleted() throws Exception {
assertThat(analysis.getAnalysisJustification()).isNull();
assertThat(analysis.getAnalysisResponse()).isNull();
assertThat(analysis.getAnalysisDetails()).isNull();
assertThat(analysis.getVulnerabilityPolicy()).isNull();
assertThat(analysis.getVulnerabilityPolicyId()).isNull();
assertThat(analysis.getAnalysisComments()).extracting(AnalysisComment::getCommenter).containsOnly("[Policy{Name=name}]");
assertThat(analysis.getAnalysisComments()).extracting(AnalysisComment::getComment).containsExactlyInAnyOrder(
"Policy removed",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
import org.dependencytrack.model.AnalysisJustification;
import org.dependencytrack.model.AnalysisResponse;
import org.dependencytrack.model.AnalysisState;
import org.dependencytrack.model.AnalyzerIdentity;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Severity;
import org.dependencytrack.model.Vulnerability;
import org.dependencytrack.notification.NotificationConstants;
import org.dependencytrack.persistence.jdbi.VulnerabilityPolicyDao;
import org.dependencytrack.policy.vulnerability.VulnerabilityPolicy;
import org.dependencytrack.policy.vulnerability.VulnerabilityPolicyAnalysis;
import org.dependencytrack.proto.notification.v1.Notification;
import org.dependencytrack.resources.v1.vo.AnalysisRequest;
import org.dependencytrack.util.NotificationUtil;
Expand All @@ -55,6 +59,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.dependencytrack.assertion.Assertions.assertConditionWithTimeout;
import static org.dependencytrack.persistence.jdbi.JdbiFactory.useJdbiHandle;
import static org.dependencytrack.persistence.jdbi.JdbiFactory.withJdbiHandle;
import static org.dependencytrack.proto.notification.v1.Group.GROUP_PROJECT_AUDIT_CHANGE;
import static org.dependencytrack.proto.notification.v1.Level.LEVEL_INFORMATIONAL;
import static org.dependencytrack.proto.notification.v1.Scope.SCOPE_PORTFOLIO;
Expand Down Expand Up @@ -767,4 +773,64 @@ public void updateAnalysisUnauthorizedTest() {
assertThat(response.getHeaderString(TOTAL_COUNT_HEADER)).isNull();
}

@Test
public void updateAnalysisWithAssociatedVulnerabilityPolicyTest() {
initializeWithPermissions(Permissions.VULNERABILITY_ANALYSIS);

final var project = new Project();
project.setName("acme-app");
qm.persist(project);

final var component = new Component();
component.setProject(project);
component.setName("acme-lib");
qm.persist(component);

final var vuln = new Vulnerability();
vuln.setVulnId("CVE-123");
vuln.setSource(Vulnerability.Source.NVD);
qm.persist(vuln);

qm.addVulnerability(vuln, component, AnalyzerIdentity.INTERNAL_ANALYZER);
final Analysis analysis = qm.makeAnalysis(component, vuln, AnalysisState.NOT_AFFECTED, null, null, null, true);

final VulnerabilityPolicy vulnPolicy = withJdbiHandle(handle -> {
final var policyAnalysis = new VulnerabilityPolicyAnalysis();
policyAnalysis.setState(VulnerabilityPolicyAnalysis.State.EXPLOITABLE);

final var policy = new VulnerabilityPolicy();
policy.setName("foo");
policy.setAnalysis(policyAnalysis);
policy.setConditions(List.of("true"));
return handle.attach(VulnerabilityPolicyDao.class).create(policy);
});

useJdbiHandle(handle -> handle.createUpdate("""
WITH "VULN_POLICY" AS (
SELECT "ID"
FROM "VULNERABILITY_POLICY"
WHERE "NAME" = :policyName
)
UPDATE "ANALYSIS"
SET "VULNERABILITY_POLICY_ID" = (SELECT "ID" FROM "VULN_POLICY")
WHERE "ID" = :analysisId
""")
.bind("policyName", vulnPolicy.getName())
.bind("analysisId", analysis.getId())
.execute());

final Response response = jersey.target(V1_ANALYSIS)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity("""
{
"project": "%s",
"component": "%s",
"vulnerability": "%s",
"comment": "foo"
}
""".formatted(project.getUuid(), component.getUuid(), vuln.getUuid()), MediaType.APPLICATION_JSON));
assertThat(response.getStatus()).isEqualTo(200);
}

}
Loading