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

Refactor build scan table #545

Merged
merged 2 commits into from
Aug 15, 2021
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 @@ -108,4 +108,18 @@ public void setCve(String cve) {
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

public enum IssueType {
SECURITY,
LICENSE
}

public IssueType getIssueType() {
if ("Security".equals(this.getType())) {
return IssueType.SECURITY;
} else if ("License".equals(this.getType())) {
return IssueType.LICENSE;
}
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,43 @@

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.*;
import org.jfrog.build.extractor.scan.Severity;

import java.util.*;

import static org.jfrog.build.api.util.CommonUtils.emptyIfNull;

/***
* Helper for printing build scan results as a violations table to log.
* Helper for printing build scan results as violations tables to log.
*/
@SuppressWarnings("unused")
public class BuildScanTableHelper {
private ArtifactoryXrayResponse scanResult;
private Map<Severity, Set<BuildScanTableElement>> table;
private Log log;
private int longestDisplayName = 0;
private String tableFormat = "";
public String TABLE_HEADLINE = "Xray Scan Summary:";
public List<String> TABLE_HEADERS = Arrays.asList("#", "Component", "Severity", "Type");

SecurityViolationsTable securityViolationsTable;
LicenseViolationsTable licenseViolationsTable;

@SuppressWarnings("unused")
public void PrintTable(ArtifactoryXrayResponse scanResult, Log log) {
public void printTable(ArtifactoryXrayResponse scanResult, Log log) {
this.scanResult = scanResult;
this.log = log;
securityViolationsTable = new SecurityViolationsTable(log);
licenseViolationsTable = new LicenseViolationsTable(log);
generateResultTable();
updateTableFormat();
print();
doPrintTables();
}

/***
* Prints the generated build scan table to log.
* Table is rendered with the table format.
* Prints the generated violations tables to log.
*/
private void print() {
int line = 1;
Severity[] severities = Severity.values();

// Print table headline.
log.info(TABLE_HEADLINE);
// Print column headers.
printLine(TABLE_HEADERS.toArray());

// Print lines of violations by descending severity.
for (int i = severities.length - 1; i >= 0; i--) {
Severity severity = severities[i];
Set<BuildScanTableElement> elements = table.get(severity);
if (elements == null) {
continue;
}
for (BuildScanTableElement element : elements) {
printLine(line, element.getFileDisplayName(), severity.getSeverityName(), element.getIssueType());
line++;
}
}
private void doPrintTables() {
securityViolationsTable.printTable();
log.info("");
}

private void printLine(Object... args) {
log.info(String.format(tableFormat, args));
}

/***
* Updates table format after longestDisplayName is known.
* Format aligns elements to the left.
* Padding on a column must be longer than the longest element in that column.
*/
private void updateTableFormat() {
// Index (assuming 5 digits is sufficient).
tableFormat = "%-6s"
// Display name (plus space).
+ "%-" + (longestDisplayName + 5) + "s"
// Severity (Longest is 'Information').
+ "%-15s"
// Type (Longest is 'Security').
+ "%-10s";
licenseViolationsTable.printTable();
}

/***
* Loops over all alerts and adds infected files with required information.
*/
private void generateResultTable() {
table = new HashMap<>();
for (Alert alert : emptyIfNull(scanResult.getAlerts())) {
for (Issue issue : emptyIfNull(alert.getIssues())) {
for (ImpactedArtifact impactedArtifact : emptyIfNull(issue.getImpactedArtifacts())) {
Expand All @@ -94,23 +50,20 @@ private void generateResultTable() {
}
}

/**
* Add a violation to the corresponding table.
*
* @param issue Issue that caused violation.
* @param infectedFile Infected file.
*/
private void addElement(Issue issue, InfectedFile infectedFile) {
// Create table element.
Severity severity = Severity.fromString(issue.getSeverity());
BuildScanTableElement buildScanTableElement = new BuildScanTableElement(infectedFile.getDisplayName(), infectedFile.getSha256(),
issue.getType(), issue.getSummary(), issue.getDescription());

// Add element to table.
Set<BuildScanTableElement> elements = table.get(severity);
if (elements == null) {
elements = new HashSet<>();
}
elements.add(buildScanTableElement);
table.put(severity, elements);

// Update longest display name if longer.
if (infectedFile.getDisplayName() != null && infectedFile.getDisplayName().length() > longestDisplayName) {
longestDisplayName = infectedFile.getDisplayName().length();
Issue.IssueType issueType = issue.getIssueType();
if (issueType == Issue.IssueType.SECURITY) {
securityViolationsTable.addElement(issue, infectedFile);
} else if (issueType == Issue.IssueType.LICENSE) {
licenseViolationsTable.addElement(issue, infectedFile);
} else {
throw new IllegalArgumentException(String.format("Illegal issue type '%s'. Expecting either 'Security' or 'License'", issue.getType()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.jfrog.build.extractor.buildScanTable;

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.InfectedFile;
import org.jfrog.build.client.artifactoryXrayResponse.Issue;

import java.util.Objects;

public class LicenseViolationsTable extends ScanTableBase {
public static final String LICENSE_VIOLATIONS_TABLE_HEADLINE = "License Compliance Violations";

protected LicenseViolationsTable(Log log) {
super(log);
}

protected String getHeadline() {
return LICENSE_VIOLATIONS_TABLE_HEADLINE;
}

protected String[] getHeaders() {
return new String[]{"#", "Severity", "Component"};
}

protected String getTableFormat() {
return super.getFormatBase(longestDisplayName);
}

protected String getEmptyTableLine() {
return "No license compliance violations were found";
}

protected void addElement(Issue issue, InfectedFile infectedFile) {
// Create table element.
LicenseTableElement element = new LicenseTableElement(infectedFile.getDisplayName(), infectedFile.getSha256(),
issue.getSummary(), issue.getDescription());
super.addElement(table, issue, element);
// Update the longest display name if longer.
if (infectedFile.getDisplayName() != null && infectedFile.getDisplayName().length() > longestDisplayName) {
longestDisplayName = infectedFile.getDisplayName().length();
}
}

protected void printTable() {
super.printTable(table);
}

private static class LicenseTableElement extends TableElementBase {
LicenseTableElement(String fileDisplayName, String fileSha256,
String issueSummary, String issueDescription) {
super(fileDisplayName, fileSha256, issueSummary, issueDescription);
}

@Override
protected Object[] getLineArgs(int line, String severityName) {
return new Object[]{line, severityName, this.getFileDisplayName()};
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LicenseTableElement that = (LicenseTableElement) o;
return Objects.equals(fileDisplayName, that.fileDisplayName) &&
Objects.equals(fileSha256, that.fileSha256) &&
Objects.equals(issueSummary, that.issueSummary) &&
Objects.equals(issueDescription, that.issueDescription);
}

@Override
public int hashCode() {
return Objects.hash(fileDisplayName, fileSha256, issueSummary, issueDescription);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.jfrog.build.extractor.buildScanTable;

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.Issue;
import org.jfrog.build.extractor.scan.Severity;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Base class for violations tables printed by {@link BuildScanTableHelper}
*/
abstract class ScanTableBase {
private final Log log;
protected int longestDisplayName = 0;
protected final Map<Severity, Set<TableElementBase>> table = new HashMap<>();

protected ScanTableBase(Log log) {
this.log = log;
}

protected abstract String getHeadline();

protected abstract String[] getHeaders();

protected abstract String getTableFormat();

protected abstract String getEmptyTableLine();

protected String getFormatBase(int longestDisplayName) {
// Index (assuming 5 digits is sufficient).
return "%-6s"
// Severity (Longest is 'Information').
+ "%-14s"
// Display name (plus space).
+ "%-" + (longestDisplayName + 3) + "s";
}

private void printFormattedLine(Object... args) {
log.info(String.format(getTableFormat(), args));
}

protected void addElement(Map<Severity, Set<TableElementBase>> table, Issue issue, TableElementBase element) {
Severity severity = Severity.fromString(issue.getSeverity());
Set<TableElementBase> elements = table.get(severity);
if (elements == null) {
elements = new HashSet<>();
}
elements.add(element);
table.put(severity, elements);
}

protected void printTable(Map<Severity, Set<TableElementBase>> table) {
int line = 1;
Severity[] severities = Severity.values();

// Print table headline.
log.info(getHeadline());

// If table is empty, print the no violations found line and return.
if (table.isEmpty()) {
log.info(getEmptyTableLine());
log.info("");
return;
}

// Print column headers.
printFormattedLine((Object[]) getHeaders());

// Print lines of violations by descending severity.
for (int i = severities.length - 1; i >= 0; i--) {
Severity severity = severities[i];
Set<TableElementBase> elements = table.get(severity);
if (elements == null) {
continue;
}
for (TableElementBase element : elements) {
printFormattedLine(element.getLineArgs(line, severity.getSeverityName()));
line++;
}
}
log.info("");
}

/**
* Base class for elements of the violations tables
*/
protected abstract static class TableElementBase {
protected final String fileDisplayName;
// Following fields are for the set's uniqueness only:
protected final String fileSha256;
protected final String issueSummary;
protected final String issueDescription;

protected TableElementBase(String fileDisplayName, String fileSha256,
String issueSummary, String issueDescription) {
this.fileDisplayName = fileDisplayName;
this.fileSha256 = fileSha256;
this.issueSummary = issueSummary;
this.issueDescription = issueDescription;
}

protected String getFileDisplayName() {
return fileDisplayName == null ? "" : fileDisplayName;
}

protected abstract Object[] getLineArgs(int line, String severityName);
}
}
Loading