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 policy evaluation not happening upon creation or update of individual components #4374

Merged
merged 1 commit into from
Nov 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public Response createComponent(@Parameter(description = "The UUID of the projec
// Wait for RepositoryMetaEvent after VulnerabilityAnalysisEvent,
// as both might be needed in policy evaluation
.onSuccess(new RepositoryMetaEvent(List.of(component)))
.onSuccess(new PolicyEvaluationEvent(component))
.onSuccess(new PolicyEvaluationEvent(component).project(component.getProject()))
);
return Response.status(Response.Status.CREATED).entity(component).build();
}
Expand Down Expand Up @@ -479,7 +479,7 @@ public Response updateComponent(Component jsonComponent) {
// Wait for RepositoryMetaEvent after VulnerabilityAnalysisEvent,
// as both might be needed in policy evaluation
.onSuccess(new RepositoryMetaEvent(List.of(component)))
.onSuccess(new PolicyEvaluationEvent(component))
.onSuccess(new PolicyEvaluationEvent(component).project(component.getProject()))
);
return Response.ok(component).build();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.dependencytrack.tasks;

import alpine.persistence.PaginatedResult;
import org.dependencytrack.PersistenceCapableTest;
import org.dependencytrack.event.PolicyEvaluationEvent;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Policy;
import org.dependencytrack.model.PolicyCondition;
import org.dependencytrack.model.Project;
import org.junit.Test;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class PolicyEvaluationTaskTest extends PersistenceCapableTest {

@Test
public void testPolicyEvaluationForSingleComponent() {
Project project = new Project();
project.setName("my-project");
project.setGroup("com.example");
project.setVersion("1.0.0");
qm.createProject(project, Collections.emptyList(), false);

Component component = new Component();
component.setGroup("com.example");
component.setName("my-component");
component.setVersion("1.0.0");
component.setPurl("pkg:maven/com.example/[email protected]");
component.setProject(project);
qm.createComponent(component, false);

// a policy that identifies the upper component and thus should be violated
Policy policy = qm.createPolicy("my-policy", Policy.Operator.ALL, Policy.ViolationState.FAIL);
qm.createPolicyCondition(policy, PolicyCondition.Subject.PACKAGE_URL, PolicyCondition.Operator.MATCHES, "pkg:maven/com.example/[email protected]");

PolicyEvaluationTask task = new PolicyEvaluationTask();
task.inform(new PolicyEvaluationEvent(component).project(project));

PaginatedResult policyViolations = qm.getPolicyViolations(project, false);
assertThat(policyViolations.getTotal()).isEqualTo(1);
}

}
Loading