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 is_dependency_of query template #485

Merged
merged 1 commit into from
Dec 13, 2023
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 @@ -408,7 +408,7 @@ private static boolean isDependencyOf(final Component leafComponent, final Compo
-- Do not consider other leaf nodes (typically the majority of components).
-- Because we're looking for parent nodes, they MUST have direct dependencies defined.
AND "DIRECT_DEPENDENCIES" IS NOT NULL
AND <filters>
AND ${filters}
),
"CTE_DEPENDENCIES" ("UUID", "PROJECT_ID", "FOUND", "PATH") AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,37 +845,37 @@ public void testEvaluateProjectWithFuncProjectDependsOnComponent() {
assertThat(qm.getAllPolicyViolations(componentB)).hasSize(1);
}

// @Test
// public void testEvaluateProjectWithFuncComponentIsDependencyOfComponent() {
// final var policy = qm.createPolicy("policy", Policy.Operator.ANY, Policy.ViolationState.FAIL);
// qm.createPolicyCondition(policy, PolicyCondition.Subject.EXPRESSION, PolicyCondition.Operator.MATCHES, """
// component.is_dependency_of(org.dependencytrack.policy.v1.Component{name: "acme-lib-a"})
// """, PolicyViolation.Type.OPERATIONAL);
//
// final var project = new Project();
// project.setName("acme-app");
// qm.persist(project);
//
// final var componentA = new Component();
// componentA.setProject(project);
// componentA.setName("acme-lib-a");
// qm.persist(componentA);
//
// final var componentB = new Component();
// componentB.setProject(project);
// componentB.setName("acme-lib-b");
// qm.persist(componentB);
//
// project.setDirectDependencies("[%s]".formatted(new ComponentIdentity(componentA).toJSON()));
// qm.persist(project);
// componentA.setDirectDependencies("[%s]".formatted(new ComponentIdentity(componentB).toJSON()));
// qm.persist(componentA);
//
// new CelPolicyEngine().evaluateProject(project.getUuid());
//
// assertThat(qm.getAllPolicyViolations(componentA)).isEmpty();
// assertThat(qm.getAllPolicyViolations(componentB)).hasSize(1);
// }
@Test
public void testEvaluateProjectWithFuncComponentIsDependencyOfComponent() {
final var policy = qm.createPolicy("policy", Policy.Operator.ANY, Policy.ViolationState.FAIL);
qm.createPolicyCondition(policy, PolicyCondition.Subject.EXPRESSION, PolicyCondition.Operator.MATCHES, """
component.is_dependency_of(org.dependencytrack.policy.v1.Component{name: "acme-lib-a"})
""", PolicyViolation.Type.OPERATIONAL);

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

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

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

project.setDirectDependencies("[%s]".formatted(new ComponentIdentity(componentA).toJSON()));
qm.persist(project);
componentA.setDirectDependencies("[%s]".formatted(new ComponentIdentity(componentB).toJSON()));
qm.persist(componentA);

new CelPolicyEngine().evaluateProject(project.getUuid());

assertThat(qm.getAllPolicyViolations(componentA)).isEmpty();
assertThat(qm.getAllPolicyViolations(componentB)).hasSize(1);
}

@Test
public void testEvaluateProjectWithFuncMatchesRange() {
Expand Down