|
24 | 24 | import alpine.notification.NotificationLevel; |
25 | 25 | import org.apache.commons.io.FileUtils; |
26 | 26 | import org.dependencytrack.model.Analysis; |
| 27 | +import org.dependencytrack.model.AnalysisState; |
27 | 28 | import org.dependencytrack.model.Bom; |
28 | 29 | import org.dependencytrack.model.Component; |
29 | 30 | import org.dependencytrack.model.ComponentIdentity; |
|
32 | 33 | import org.dependencytrack.model.NotificationPublisher; |
33 | 34 | import org.dependencytrack.model.Policy; |
34 | 35 | import org.dependencytrack.model.PolicyCondition; |
| 36 | +import org.dependencytrack.model.PolicyCondition.Operator; |
35 | 37 | import org.dependencytrack.model.PolicyViolation; |
36 | 38 | import org.dependencytrack.model.Project; |
| 39 | +import org.dependencytrack.model.Severity; |
37 | 40 | import org.dependencytrack.model.Tag; |
| 41 | +import org.dependencytrack.model.Vex; |
38 | 42 | import org.dependencytrack.model.ViolationAnalysis; |
39 | 43 | import org.dependencytrack.model.ViolationAnalysisState; |
40 | 44 | import org.dependencytrack.model.Vulnerability; |
|
65 | 69 | import java.io.IOException; |
66 | 70 | import java.net.URLDecoder; |
67 | 71 | import java.nio.file.Path; |
| 72 | +import java.util.Date; |
68 | 73 | import java.util.HashMap; |
69 | 74 | import java.util.HashSet; |
70 | 75 | import java.util.List; |
71 | 76 | import java.util.Map; |
72 | 77 | import java.util.Optional; |
| 78 | +import java.util.Set; |
| 79 | +import java.util.UUID; |
73 | 80 |
|
74 | 81 | import static java.nio.charset.StandardCharsets.UTF_8; |
75 | 82 |
|
@@ -634,4 +641,105 @@ public static String generateNotificationTitle(String messageType, Project proje |
634 | 641 | } |
635 | 642 | return messageType; |
636 | 643 | } |
| 644 | + |
| 645 | + public static Object generateSubject(String group) { |
| 646 | + final Project project = createProject(); |
| 647 | + final Vulnerability vuln = createVulnerability(); |
| 648 | + final Component component = createComponent(project); |
| 649 | + final Analysis analysis = createAnalysis(component, vuln); |
| 650 | + final PolicyViolation policyViolation = createPolicyViolation(component, project); |
| 651 | + |
| 652 | + switch (group) { |
| 653 | + case "BOM_CONSUMED": |
| 654 | + return new BomConsumedOrProcessed(project, "bomContent", Bom.Format.CYCLONEDX, "1.5"); |
| 655 | + case "BOM_PROCESSED": |
| 656 | + return new BomConsumedOrProcessed(project, "bomContent", Bom.Format.CYCLONEDX, "1.5"); |
| 657 | + case "BOM_PROCESSING_FAILED": |
| 658 | + return new BomProcessingFailed(project, "bomContent", "cause", Bom.Format.CYCLONEDX, "1.5"); |
| 659 | + case "BOM_VALIDATION_FAILED": |
| 660 | + return new BomValidationFailed(project, "bomContent", List.of("TEST"), Bom.Format.CYCLONEDX); |
| 661 | + case "VEX_CONSUMED": |
| 662 | + return new VexConsumedOrProcessed(project, "", Vex.Format.CYCLONEDX, ""); |
| 663 | + case "VEX_PROCESSED": |
| 664 | + return new VexConsumedOrProcessed(project, "", Vex.Format.CYCLONEDX, ""); |
| 665 | + case "NEW_VULNERABILITY": |
| 666 | + return new NewVulnerabilityIdentified(vuln, component, Set.of(project), VulnerabilityAnalysisLevel.BOM_UPLOAD_ANALYSIS); |
| 667 | + case "NEW_VULNERABLE_DEPENDENCY": |
| 668 | + return new NewVulnerableDependency(component, List.of(vuln)); |
| 669 | + case "POLICY_VIOLATION": |
| 670 | + return new PolicyViolationIdentified(policyViolation, component, project); |
| 671 | + case "PROJECT_CREATED": |
| 672 | + return NotificationUtil.toJson(project); |
| 673 | + case "PROJECT_AUDIT_CHANGE": |
| 674 | + return new AnalysisDecisionChange(vuln, component, project, analysis); |
| 675 | + default: |
| 676 | + return null; |
| 677 | + } |
| 678 | + } |
| 679 | + |
| 680 | + private static Project createProject() { |
| 681 | + final Project project = new Project(); |
| 682 | + project.setUuid(UUID.fromString("c9c9539a-e381-4b36-ac52-6a7ab83b2c95")); |
| 683 | + project.setName("projectName"); |
| 684 | + project.setVersion("projectVersion"); |
| 685 | + project.setPurl("pkg:maven/org.acme/projectName@projectVersion"); |
| 686 | + return project; |
| 687 | + } |
| 688 | + |
| 689 | + private static Vulnerability createVulnerability() { |
| 690 | + final Vulnerability vuln = new Vulnerability(); |
| 691 | + vuln.setUuid(UUID.fromString("bccec5d5-ec21-4958-b3e8-22a7a866a05a")); |
| 692 | + vuln.setVulnId("INT-001"); |
| 693 | + vuln.setSource(Vulnerability.Source.INTERNAL); |
| 694 | + vuln.setSeverity(Severity.MEDIUM); |
| 695 | + return vuln; |
| 696 | + } |
| 697 | + |
| 698 | + private static Component createComponent(Project project) { |
| 699 | + final Component component = new Component(); |
| 700 | + component.setProject(project); |
| 701 | + component.setUuid(UUID.fromString("94f87321-a5d1-4c2f-b2fe-95165debebc6")); |
| 702 | + component.setName("componentName"); |
| 703 | + component.setVersion("componentVersion"); |
| 704 | + return component; |
| 705 | + } |
| 706 | + |
| 707 | + private static Analysis createAnalysis(Component component, Vulnerability vuln) { |
| 708 | + final Analysis analysis = new Analysis(); |
| 709 | + analysis.setComponent(component); |
| 710 | + analysis.setVulnerability(vuln); |
| 711 | + analysis.setAnalysisState(AnalysisState.FALSE_POSITIVE); |
| 712 | + analysis.setSuppressed(true); |
| 713 | + return analysis; |
| 714 | + } |
| 715 | + |
| 716 | + private static PolicyViolation createPolicyViolation(Component component, Project project) { |
| 717 | + final Policy policy = new Policy(); |
| 718 | + policy.setId(1); |
| 719 | + policy.setName("test"); |
| 720 | + policy.setOperator(Policy.Operator.ALL); |
| 721 | + policy.setProjects(List.of(project)); |
| 722 | + policy.setUuid(UUID.randomUUID()); |
| 723 | + policy.setViolationState(Policy.ViolationState.INFO); |
| 724 | + |
| 725 | + final PolicyCondition condition = new PolicyCondition(); |
| 726 | + condition.setId(1); |
| 727 | + condition.setUuid(UUID.randomUUID()); |
| 728 | + condition.setOperator(Operator.NUMERIC_EQUAL); |
| 729 | + condition.setSubject(PolicyCondition.Subject.AGE); |
| 730 | + condition.setValue("1"); |
| 731 | + condition.setPolicy(policy); |
| 732 | + |
| 733 | + final PolicyViolation policyViolation = new PolicyViolation(); |
| 734 | + policyViolation.setId(1); |
| 735 | + policyViolation.setPolicyCondition(condition); |
| 736 | + policyViolation.setComponent(component); |
| 737 | + policyViolation.setText("test"); |
| 738 | + policyViolation.setType(PolicyViolation.Type.SECURITY); |
| 739 | + policyViolation.setAnalysis(new ViolationAnalysis()); |
| 740 | + policyViolation.setUuid(UUID.randomUUID()); |
| 741 | + policyViolation.setTimestamp(new Date(System.currentTimeMillis())); |
| 742 | + return policyViolation; |
| 743 | + } |
| 744 | + |
637 | 745 | } |
0 commit comments