Skip to content

Commit

Permalink
Replace InvalidBomProblemDetails with Errors
Browse files Browse the repository at this point in the history
Instead of adding the whole problemDetails object, just storing the errors now

Signed-off-by: Aravind Parappil <[email protected]>
  • Loading branch information
aravindparappil46 committed Jun 2, 2024
1 parent 12bc76d commit 543cd8c
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
*/
package org.dependencytrack.notification.vo;

import java.util.List;
import org.dependencytrack.model.Bom;
import org.dependencytrack.model.Project;
import org.dependencytrack.resources.v1.problems.InvalidBomProblemDetails;

public class BomValidationFailed {

private Project project;
private String bom;
private InvalidBomProblemDetails problemDetails;
private List<String> errors;
private Bom.Format format;

public BomValidationFailed(final Project project, final String bom, final InvalidBomProblemDetails problemDetails, final Bom.Format format) {
public BomValidationFailed(final Project project, final String bom, final List<String> errors, final Bom.Format format) {
this.project = project;
this.bom = bom;
this.problemDetails = problemDetails;
this.errors = errors;
this.format = format;
}

Expand All @@ -44,8 +44,8 @@ public String getBom() {
return bom;
}

public InvalidBomProblemDetails getProblemDetails() {
return problemDetails;
public List<String> getErrors() {
return errors;
}

public Bom.Format getFormat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ static void validate(final byte[] bomBytes, final Project project) {

final var bomEncoded = Base64.getEncoder()
.encodeToString(bomBytes);
dispatchBomValidationFailedNotification(project, bomEncoded, problemDetails, Format.CYCLONEDX);
dispatchBomValidationFailedNotification(project, bomEncoded, problemDetails.getErrors(), Format.CYCLONEDX);

throw new WebApplicationException(response);
} catch (RuntimeException e) {
Expand All @@ -550,15 +550,14 @@ static void validate(final byte[] bomBytes, final Project project) {
}


private static void dispatchBomValidationFailedNotification(final Project project, final String bom,
final InvalidBomProblemDetails problemDetails, final Bom.Format bomFormat) {
private static void dispatchBomValidationFailedNotification(final Project project, final String bom, final List<String> errors, final Bom.Format bomFormat) {
Notification.dispatch(new Notification()
.scope(NotificationScope.PORTFOLIO)
.group(NotificationGroup.BOM_VALIDATION_FAILED)
.level(NotificationLevel.ERROR)
.title(Title.BOM_VALIDATION_FAILED)
.content("An error occurred during BOM Validation")
.subject(new BomValidationFailed(project, bom, problemDetails, bomFormat)));
.subject(new BomValidationFailed(project, bom, errors, bomFormat)));
}

}
30 changes: 5 additions & 25 deletions src/main/java/org/dependencytrack/util/NotificationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,6 @@ public static JsonObject toJson(final Component component) {
return componentBuilder.build();
}

public static JsonObject toJson(final InvalidBomProblemDetails problemDetails) {
final JsonObjectBuilder builder = Json.createObjectBuilder();
final var errors = problemDetails.getErrors();

if (problemDetails.getType() != null) {
builder.add("type", problemDetails.getType().toString());
}
JsonUtil.add(builder, "status", problemDetails.getStatus().toString());
JsonUtil.add(builder, "title", problemDetails.getTitle());
JsonUtil.add(builder, "detail", problemDetails.getDetail());

if (errors != null && !errors.isEmpty()) {
final var commaSeparatedErrors = String.join(",", errors);
JsonUtil.add(builder, "errors", commaSeparatedErrors);
}

if (problemDetails.getInstance() != null) {
JsonUtil.add(builder, "instance", problemDetails.getInstance().toString());
}
return builder.build();
}

public static JsonObject toJson(final Vulnerability vulnerability) {
final JsonObjectBuilder vulnerabilityBuilder = Json.createObjectBuilder();
vulnerabilityBuilder.add("uuid", vulnerability.getUuid().toString());
Expand Down Expand Up @@ -505,7 +483,7 @@ public static JsonObject toJson(final BomProcessingFailed vo) {
}

public static JsonObject toJson(final BomValidationFailed vo) {
final JsonObjectBuilder builder = Json.createObjectBuilder();
final var builder = Json.createObjectBuilder();
if (vo.getProject() != null) {
builder.add("project", toJson(vo.getProject()));
}
Expand All @@ -516,8 +494,10 @@ public static JsonObject toJson(final BomValidationFailed vo) {
.build()
);
}
if (vo.getProblemDetails() != null) {
builder.add("problemDetails", toJson(vo.getProblemDetails()));
final var errors = vo.getErrors();
if (errors != null && !errors.isEmpty()) {
final var commaSeparatedErrors = String.join(",", errors);
JsonUtil.add(builder, "errors", commaSeparatedErrors);
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Project: {{ subject.project.name }}
Version: {{ subject.project.version }}
Description: {{ subject.project.description }}
Project URL: {{ baseUrl }}/projects/{{ subject.project.uuid }}
Errors: {{ subject.errors }}
{% elseif notification.group == "BOM_PROCESSED" %}
Project: {{ subject.project.name }}
Version: {{ subject.project.version }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@
"name": "Project URL",
"value": "{{ baseUrl }}/projects/{{ subject.project.uuid | escape(strategy='json') }}"
},
{
"name": "Summary",
"value": "{{ subject.problemDetails.title.toString | escape(strategy='json') }} - {{ subject.problemDetails.detail.toString | escape(strategy='json') }}"
},
{
"name": "Errors",
"value": "{{ subject.problemDetails.errors.toString | escape(strategy='json') }}"
"value": "{{ subject.errors.toString | escape(strategy='json') }}"
}
],
{% else %}
Expand Down
11 changes: 2 additions & 9 deletions src/main/resources/templates/notification/publisher/slack.peb
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,14 @@
{
"type": "section",
"text": {
"text": "{{ notification.content | escape(strategy="json") }} | {{ subject.problemDetails.title | escape(strategy="json") }}",
"type": "plain_text"
}
},
{
"type": "section",
"text": {
"text": "{{ subject.problemDetails.detail.toString | escape(strategy="json") }}",
"text": "{{ notification.content | escape(strategy="json") }}",
"type": "plain_text"
}
},
{
"type": "section",
"text": {
"text": "{{ subject.problemDetails.errors.toString | escape(strategy="json") }}",
"text": "{{ subject.errors.toString | escape(strategy="json") }}",
"type": "plain_text"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public void testInformWithBomProcessingFailedNotification() {

@Test
public void testInformWithBomValidationFailedNotification() {
final var subject = new BomValidationFailed(createProject(), "bomContent", createInvalidBomProblemDetails(), Bom.Format.CYCLONEDX);
final var errorsSample = List.of(
"$.components[928].externalReferences[1].url: does not match the iri-reference pattern must be a valid RFC 3987 IRI-reference");
final var subject = new BomValidationFailed(createProject(), "bomContent", errorsSample, Bom.Format.CYCLONEDX);

final var notification = new Notification()
.scope(NotificationScope.PORTFOLIO)
Expand Down Expand Up @@ -238,15 +240,6 @@ private static Project createProject() {
return project;
}

private static InvalidBomProblemDetails createInvalidBomProblemDetails() {
final var invalidBomProblemDetails = new InvalidBomProblemDetails();
invalidBomProblemDetails.setTitle("The uploaded BOM is invalid");
invalidBomProblemDetails.setDetail("Schema validation failed");
invalidBomProblemDetails.setStatus(400);
invalidBomProblemDetails.setErrors(List.of("$.components[928].externalReferences[1].url: does not match the iri-reference pattern must be a valid RFC 3987 IRI-reference"));
return invalidBomProblemDetails;
}

private static Vulnerability createVulnerability() {
final var alias = new org.dependencytrack.model.VulnerabilityAlias();
alias.setInternalId("INT-001");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ public void testInformWithBomValidationFailedNotification() {
"name": "Project URL",
"value": "https://example.com/projects/c9c9539a-e381-4b36-ac52-6a7ab83b2c95"
},
{
"name": "Summary",
"value": "The uploaded BOM is invalid - Schema validation failed"
},
{
"name": "Errors",
"value": "[$.components[928].externalReferences[1].url: does not match the iri-reference pattern must be a valid RFC 3987 IRI-reference]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void testInformWithBomValidationFailedNotification() {
Version: projectVersion
Description: projectDescription
Project URL: /projects/c9c9539a-e381-4b36-ac52-6a7ab83b2c95
Errors: [$.components[928].externalReferences[1].url: does not match the iri-reference pattern must be a valid RFC 3987 IRI-reference]
--------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,10 @@ public void testInformWithBomValidationFailedNotification() {
{
"type": "section",
"text": {
"text": "An error occurred during BOM Validation | The uploaded BOM is invalid",
"text": "An error occurred during BOM Validation",
"type": "plain_text"
}
},
{
"type" : "section",
"text" : {
"text" : "Schema validation failed",
"type" : "plain_text"
}
},
{
"type" : "section",
"text" : {
Expand Down

0 comments on commit 543cd8c

Please sign in to comment.