|
28 | 28 | import org.dependencytrack.model.NotificationRule;
|
29 | 29 | import org.dependencytrack.model.Project;
|
30 | 30 | import org.dependencytrack.model.Tag;
|
| 31 | +import org.dependencytrack.model.Severity; |
31 | 32 | import org.dependencytrack.model.Vex;
|
32 | 33 | import org.dependencytrack.model.Vulnerability;
|
33 | 34 | import org.dependencytrack.notification.publisher.DefaultNotificationPublishers;
|
@@ -346,6 +347,85 @@ void testDisabledRule() {
|
346 | 347 | assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty();
|
347 | 348 | }
|
348 | 349 |
|
| 350 | + @Test |
| 351 | + void testNewVulnerabilityIdentifiedShouldTriggerNotification() { |
| 352 | + final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false); |
| 353 | + var componentA = new Component(); |
| 354 | + componentA.setProject(projectA); |
| 355 | + componentA.setName("Component A"); |
| 356 | + componentA = qm.createComponent(componentA, false); |
| 357 | + |
| 358 | + final Project projectB = qm.createProject("Project B", null, "1.0", null, null, null, true, false); |
| 359 | + var componentB = new Component(); |
| 360 | + componentB.setProject(projectB); |
| 361 | + componentB.setName("Component B"); |
| 362 | + componentB = qm.createComponent(componentB, false); |
| 363 | + |
| 364 | + final NotificationPublisher publisher = createMockPublisher(); |
| 365 | + |
| 366 | + final NotificationRule rule = qm.createNotificationRule("Test Rule", NotificationScope.PORTFOLIO, NotificationLevel.INFORMATIONAL, publisher); |
| 367 | + rule.setNotifyOn(Set.of(NotificationGroup.NEW_VULNERABILITY)); |
| 368 | + rule.setProjects(List.of(projectA)); |
| 369 | + |
| 370 | + // Set which severities to trigger notification |
| 371 | + rule.setNotifySeverities(List.of(Severity.HIGH)); |
| 372 | + |
| 373 | + final var notification = new Notification(); |
| 374 | + notification.setScope(NotificationScope.PORTFOLIO.name()); |
| 375 | + notification.setGroup(NotificationGroup.NEW_VULNERABILITY.name()); |
| 376 | + notification.setLevel(NotificationLevel.INFORMATIONAL); |
| 377 | + notification.setSubject(new NewVulnerabilityIdentified(null, qm.detach(componentB), Set.of(), null)); |
| 378 | + |
| 379 | + final var router = new NotificationRouter(); |
| 380 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty(); |
| 381 | + |
| 382 | + // Set a high vulnerability that should trigger a notification |
| 383 | + final Vulnerability highVuln = new Vulnerability(); |
| 384 | + highVuln.setSeverity(Severity.HIGH); |
| 385 | + notification.setSubject(new NewVulnerabilityIdentified(highVuln, qm.detach(componentA), Set.of(), null)); |
| 386 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)) |
| 387 | + .satisfiesExactly(resolvedRule -> assertThat(resolvedRule.getName()).isEqualTo("Test Rule")); |
| 388 | + } |
| 389 | + |
| 390 | + @Test |
| 391 | + void testNewVulnerabilityIdentifiedShouldNotTriggerNotification() { |
| 392 | + final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false); |
| 393 | + var componentA = new Component(); |
| 394 | + componentA.setProject(projectA); |
| 395 | + componentA.setName("Component A"); |
| 396 | + componentA = qm.createComponent(componentA, false); |
| 397 | + |
| 398 | + final Project projectB = qm.createProject("Project B", null, "1.0", null, null, null, true, false); |
| 399 | + var componentB = new Component(); |
| 400 | + componentB.setProject(projectB); |
| 401 | + componentB.setName("Component B"); |
| 402 | + componentB = qm.createComponent(componentB, false); |
| 403 | + |
| 404 | + final NotificationPublisher publisher = createMockPublisher(); |
| 405 | + |
| 406 | + final NotificationRule rule = qm.createNotificationRule("Test Rule", NotificationScope.PORTFOLIO, NotificationLevel.INFORMATIONAL, publisher); |
| 407 | + rule.setNotifyOn(Set.of(NotificationGroup.NEW_VULNERABILITY)); |
| 408 | + rule.setProjects(List.of(projectA)); |
| 409 | + |
| 410 | + // Set which severities to trigger notification (CRITICAL and HIGH only) |
| 411 | + rule.setNotifySeverities(List.of(Severity.CRITICAL, Severity.HIGH)); |
| 412 | + |
| 413 | + // Set a low severity that should NOT trigger a notification |
| 414 | + final Vulnerability lowVuln = new Vulnerability(); |
| 415 | + lowVuln.setSeverity(Severity.LOW); |
| 416 | + |
| 417 | + final var notification = new Notification(); |
| 418 | + notification.setScope(NotificationScope.PORTFOLIO.name()); |
| 419 | + notification.setGroup(NotificationGroup.NEW_VULNERABILITY.name()); |
| 420 | + notification.setLevel(NotificationLevel.INFORMATIONAL); |
| 421 | + notification.setSubject(new NewVulnerabilityIdentified(lowVuln, qm.detach(componentB), Set.of(), null)); |
| 422 | + |
| 423 | + final var router = new NotificationRouter(); |
| 424 | + |
| 425 | + // This should not trigger a notification |
| 426 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty(); |
| 427 | + } |
| 428 | + |
349 | 429 | @Test
|
350 | 430 | void testNewVulnerabilityIdentifiedLimitedToProject() {
|
351 | 431 | final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false);
|
@@ -414,6 +494,95 @@ void testNewVulnerableDependencyLimitedToProject() {
|
414 | 494 | .satisfiesExactly(resolvedRule -> assertThat(resolvedRule.getName()).isEqualTo("Test Rule"));
|
415 | 495 | }
|
416 | 496 |
|
| 497 | + @Test |
| 498 | + void testNewVulnerableDependencyThatShouldTriggerNotification() { |
| 499 | + final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false); |
| 500 | + var componentA = new Component(); |
| 501 | + componentA.setProject(projectA); |
| 502 | + componentA.setName("Component A"); |
| 503 | + componentA = qm.createComponent(componentA, false); |
| 504 | + |
| 505 | + final Project projectB = qm.createProject("Project B", null, "1.0", null, null, null, true, false); |
| 506 | + var componentB = new Component(); |
| 507 | + componentB.setProject(projectB); |
| 508 | + componentB.setName("Component B"); |
| 509 | + componentB = qm.createComponent(componentB, false); |
| 510 | + |
| 511 | + final NotificationPublisher publisher = createSlackPublisher(); |
| 512 | + |
| 513 | + final NotificationRule rule = qm.createNotificationRule("Test Rule", NotificationScope.PORTFOLIO, NotificationLevel.INFORMATIONAL, publisher); |
| 514 | + rule.setNotifyOn(Set.of(NotificationGroup.NEW_VULNERABLE_DEPENDENCY)); |
| 515 | + rule.setProjects(List.of(projectA)); |
| 516 | + |
| 517 | + // Set which severities to trigger notification |
| 518 | + rule.setNotifySeverities(List.of(Severity.HIGH)); |
| 519 | + |
| 520 | + final var notification = new Notification(); |
| 521 | + notification.setScope(NotificationScope.PORTFOLIO.name()); |
| 522 | + notification.setGroup(NotificationGroup.NEW_VULNERABLE_DEPENDENCY.name()); |
| 523 | + notification.setLevel(NotificationLevel.INFORMATIONAL); |
| 524 | + notification.setSubject(new NewVulnerableDependency(qm.detach(componentB), null)); |
| 525 | + |
| 526 | + final var router = new NotificationRouter(); |
| 527 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty(); |
| 528 | + |
| 529 | + // Set two high vulnerabilities |
| 530 | + final Vulnerability highVuln1 = new Vulnerability(); |
| 531 | + highVuln1.setSeverity(Severity.HIGH); |
| 532 | + final Vulnerability highVuln2 = new Vulnerability(); |
| 533 | + highVuln2.setSeverity(Severity.HIGH); |
| 534 | + List<Vulnerability> vulnerabilities = List.of(highVuln1, highVuln2); |
| 535 | + |
| 536 | + // Set a new vulnerable dependency that should trigger a notification |
| 537 | + notification.setSubject(new NewVulnerableDependency(qm.detach(componentA), vulnerabilities)); |
| 538 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)) |
| 539 | + .satisfiesExactly(resolvedRule -> assertThat(resolvedRule.getName()).isEqualTo("Test Rule")); |
| 540 | + } |
| 541 | + |
| 542 | + @Test |
| 543 | + void testNewVulnerableDependencyThatShouldNotTriggerNotification() { |
| 544 | + final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false); |
| 545 | + var componentA = new Component(); |
| 546 | + componentA.setProject(projectA); |
| 547 | + componentA.setName("Component A"); |
| 548 | + componentA = qm.createComponent(componentA, false); |
| 549 | + |
| 550 | + final Project projectB = qm.createProject("Project B", null, "1.0", null, null, null, true, false); |
| 551 | + var componentB = new Component(); |
| 552 | + componentB.setProject(projectB); |
| 553 | + componentB.setName("Component B"); |
| 554 | + componentB = qm.createComponent(componentB, false); |
| 555 | + |
| 556 | + final NotificationPublisher publisher = createSlackPublisher(); |
| 557 | + |
| 558 | + final NotificationRule rule = qm.createNotificationRule("Test Rule", NotificationScope.PORTFOLIO, NotificationLevel.INFORMATIONAL, publisher); |
| 559 | + rule.setNotifyOn(Set.of(NotificationGroup.NEW_VULNERABLE_DEPENDENCY)); |
| 560 | + rule.setProjects(List.of(projectA)); |
| 561 | + |
| 562 | + // Set which severities to trigger notification |
| 563 | + rule.setNotifySeverities(List.of(Severity.HIGH)); |
| 564 | + |
| 565 | + final var notification = new Notification(); |
| 566 | + notification.setScope(NotificationScope.PORTFOLIO.name()); |
| 567 | + notification.setGroup(NotificationGroup.NEW_VULNERABLE_DEPENDENCY.name()); |
| 568 | + notification.setLevel(NotificationLevel.INFORMATIONAL); |
| 569 | + notification.setSubject(new NewVulnerableDependency(qm.detach(componentB), null)); |
| 570 | + |
| 571 | + final var router = new NotificationRouter(); |
| 572 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty(); |
| 573 | + |
| 574 | + // Set two low vulnerabilities |
| 575 | + final Vulnerability highVuln1 = new Vulnerability(); |
| 576 | + highVuln1.setSeverity(Severity.LOW); |
| 577 | + final Vulnerability highVuln2 = new Vulnerability(); |
| 578 | + highVuln2.setSeverity(Severity.LOW); |
| 579 | + List<Vulnerability> vulnerabilities = List.of(highVuln1, highVuln2); |
| 580 | + |
| 581 | + // Set a new vulnerable dependency that should trigger a notification |
| 582 | + notification.setSubject(new NewVulnerableDependency(qm.detach(componentA), vulnerabilities)); |
| 583 | + assertThat(router.resolveRules(PublishContext.from(notification), notification)).isEmpty(); |
| 584 | + } |
| 585 | + |
417 | 586 | @Test
|
418 | 587 | void testBomConsumedOrProcessedLimitedToProject() {
|
419 | 588 | final Project projectA = qm.createProject("Project A", null, "1.0", null, null, null, true, false);
|
|
0 commit comments