Skip to content
Open
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 @@ -22,6 +22,9 @@ properties:
version:
type: string
maxLength: 255
latest_version:
type: string
maxLength: 255
group:
type: string
maxLength: 255
Expand All @@ -30,12 +33,19 @@ properties:
maxLength: 255
hashes:
$ref: "./hashes.yaml"
integrity_check_status:
type: string
maxLength: 255
cpe:
type: string
maxLength: 255
purl:
type: string
maxLength: 1024
published:
$ref: "./timestamp.yaml"
last_fetched:
$ref: "./timestamp.yaml"
swid_tag_id:
type: string
maxLength: 255
Expand All @@ -44,6 +54,9 @@ properties:
copyright:
type: string
maxLength: 255
integrity_repo_url:
type: string
maxLength: 1024
license:
type: string
maxLength: 255
Expand All @@ -62,6 +75,30 @@ properties:
last_inherited_risk_score:
type: number
format: double
vulnerabilities:
type: integer
format: int32
minimum: 0
critical:
type: integer
format: int32
minimum: 0
high:
type: integer
format: int32
minimum: 0
medium:
type: integer
format: int32
minimum: 0
low:
type: integer
format: int32
minimum: 0
unassigned:
type: integer
format: int32
minimum: 0
uuid:
type: string
format: uuid
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentOccurrence;
import org.dependencytrack.model.IntegrityMatchStatus;
import org.dependencytrack.model.License;
import org.dependencytrack.persistence.pagination.Page;
import org.jdbi.v3.core.mapper.RowMapper;
Expand All @@ -35,6 +36,8 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -88,21 +91,23 @@ AND LOWER("LOCATION") LIKE ('%' || LOWER(${apiFilterParameter}) || '%')
""")
Long getComponentId(@Bind UUID componentUuid);

default Page<Component> listProjectComponents(final long projectId, final Boolean onlyOutdated,
default Page<ComponentRow> listProjectComponents(final long projectId, final Boolean onlyOutdated,
final Boolean onlyDirect, final int limit, final String pageToken) {
final var decodedPageToken = decodePageToken(getHandle(), pageToken, ListComponentPageToken.class);

final List<Component> rows = listProjectComponents(projectId, limit + 1, onlyOutdated, onlyDirect,
final List<ComponentRow> rows = listProjectComponents(projectId, limit + 1, onlyOutdated, onlyDirect,
decodedPageToken != null ? decodedPageToken.lastName() : null,
decodedPageToken != null ? decodedPageToken.lastVersion() : null,
decodedPageToken != null ? decodedPageToken.lastId() : null);

final List<Component> resultRows = rows.size() > 1
final List<ComponentRow> resultRows = rows.size() > 1
? rows.subList(0, Math.min(rows.size(), limit))
: rows;

final ListComponentPageToken nextPageToken = rows.size() > limit
? new ListComponentPageToken(resultRows.getLast().getName(), resultRows.getLast().getVersion(), resultRows.getLast().getId())
? new ListComponentPageToken(resultRows.getLast().component.getName(),
resultRows.getLast().component.getVersion(),
resultRows.getLast().component.getId())
: null;

return new Page<>(resultRows, encodePageToken(getHandle(), nextPageToken));
Expand Down Expand Up @@ -149,9 +154,38 @@ record ListComponentPageToken(String lastName, String lastVersion, Long lastId)
"L"."ISOSIAPPROVED",
"L"."UUID" AS "licenseUuid",
"L"."NAME" AS "licenseName",
"INTEGRITY_META_COMPONENT"."PUBLISHED_AT" AS "published",
"INTEGRITY_META_COMPONENT"."LAST_FETCH" AS "lastFetched",
"INTEGRITY_META_COMPONENT"."REPOSITORY_URL" AS "integrityRepoUrl",
"INTEGRITY_ANALYSIS"."INTEGRITY_CHECK_STATUS" AS "integrityCheckStatus",
"DEPENDENCYMETRICS"."VULNERABILITIES" AS "vulnCount",
"DEPENDENCYMETRICS"."CRITICAL" AS "critical",
"DEPENDENCYMETRICS"."HIGH" AS "high",
"DEPENDENCYMETRICS"."MEDIUM" AS "medium",
"DEPENDENCYMETRICS"."LOW" AS "low",
"DEPENDENCYMETRICS"."UNASSIGNED_SEVERITY" AS "unassigned",
"R"."LATEST_VERSION" AS "latestVersion",
(SELECT COUNT(*) FROM "COMPONENT_OCCURRENCE" WHERE "COMPONENT_ID" = "C"."ID") AS "occurrenceCount"
FROM "COMPONENT" "C"
INNER JOIN "PROJECT" ON "C"."PROJECT_ID" = "PROJECT"."ID"
LEFT JOIN "INTEGRITY_META_COMPONENT" ON "C"."PURL" = "INTEGRITY_META_COMPONENT"."PURL"
LEFT JOIN "INTEGRITY_ANALYSIS" ON "C"."ID" = "INTEGRITY_ANALYSIS"."COMPONENT_ID"
LEFT JOIN (
SELECT DISTINCT ON ("COMPONENT_ID")
"COMPONENT_ID",
"VULNERABILITIES",
"CRITICAL",
"HIGH",
"MEDIUM",
"LOW",
"UNASSIGNED_SEVERITY"
FROM "DEPENDENCYMETRICS"
ORDER BY "COMPONENT_ID", "LAST_OCCURRENCE" DESC
) "DEPENDENCYMETRICS" ON "C"."ID" = "DEPENDENCYMETRICS"."COMPONENT_ID"
LEFT JOIN "REPOSITORY_META_COMPONENT" "R"
ON "R"."NAME" = "C"."NAME"
AND ("R"."NAMESPACE" = "C"."GROUP" OR "R"."NAMESPACE" IS NULL OR "C"."GROUP" IS NULL)
AND "C"."PURL" LIKE (('pkg:' || LOWER("R"."REPOSITORY_TYPE")) || '/%') ESCAPE E'\\\\'
LEFT OUTER JOIN "LICENSE" "L" ON "C"."LICENSE_ID" = "L"."ID"
WHERE ${apiProjectAclCondition}
AND "C"."PROJECT_ID" = :projectId
Expand All @@ -177,7 +211,7 @@ AND NOT (NOT EXISTS (
@DefineNamedBindings
@DefineApiProjectAclCondition(projectIdColumn = "\"PROJECT_ID\"")
@RegisterRowMapper(ComponentListRowMapper.class)
List<Component> listProjectComponents(
List<ComponentRow> listProjectComponents(
@Bind long projectId,
@Bind int limit,
@Bind Boolean onlyOutdated,
Expand All @@ -187,12 +221,28 @@ List<Component> listProjectComponents(
@Bind Long lastId
);

class ComponentListRowMapper implements RowMapper<Component> {
record ComponentRow(
Component component,
String latestVersion,
Instant published,
Instant lastFetched,
IntegrityMatchStatus integrityCheckStatus,
String integrityRepoUrl,
int vulnerabilities,
int critical,
int medium,
int high,
int low,
int unassigned
) {
}

class ComponentListRowMapper implements RowMapper<ComponentRow> {

private final RowMapper<Component> componentRowMapper = BeanMapper.of(Component.class);

@Override
public Component map(final ResultSet rs, final StatementContext ctx) throws SQLException {
public ComponentRow map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final Component component = componentRowMapper.map(rs, ctx);
maybeSet(rs, "componentPurl", ResultSet::getString, component::setPurl);
if (rs.getString("licenseUuid") != null) {
Expand All @@ -206,7 +256,29 @@ public Component map(final ResultSet rs, final StatementContext ctx) throws SQLE
component.setResolvedLicense(license);
}
maybeSet(rs, "occurrenceCount", ResultSet::getLong, component::setOccurrenceCount);
return component;

final Timestamp publishedTs = rs.getTimestamp("published");
final Instant published = (publishedTs != null ? publishedTs.toInstant() : null);
final Timestamp lastFetchedTs = rs.getTimestamp("lastFetched");
final Instant lastFetched = (lastFetchedTs != null ? lastFetchedTs.toInstant() : null);
final String integrityCheckStatus = rs.getString("integrityCheckStatus");
final IntegrityMatchStatus integrityStatus =
(integrityCheckStatus != null ? IntegrityMatchStatus.valueOf(integrityCheckStatus) : null);

return new ComponentRow(
component,
rs.getString("latestVersion"),
published,
lastFetched,
integrityStatus,
rs.getString("integrityRepoUrl"),
rs.getInt("vulnCount"),
rs.getInt("critical"),
rs.getInt("high"),
rs.getInt("medium"),
rs.getInt("low"),
rs.getInt("unassigned")
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.dependencytrack.api.v2.model.ListComponentsResponse;
import org.dependencytrack.api.v2.model.ListComponentsResponseItem;
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.model.Component;
import org.dependencytrack.persistence.jdbi.ComponentDao;
import org.dependencytrack.persistence.jdbi.ProjectDao;
import org.dependencytrack.persistence.pagination.Page;
Expand Down Expand Up @@ -56,30 +55,41 @@ public Response listProjectComponents(UUID uuid, Boolean onlyOutdated, Boolean o
throw new NotFoundException();
}
requireProjectAccess(handle, UUID.fromString(String.valueOf(uuid)));
final Page<Component> componentsPage = handle.attach(ComponentDao.class)
final Page<ComponentDao.ComponentRow> componentsPage = handle.attach(ComponentDao.class)
.listProjectComponents(projectId, onlyOutdated, onlyDirect, limit, pageToken);

final var response = ListComponentsResponse.builder()
.components(componentsPage.items().stream()
.<ListComponentsResponseItem>map(
componentRow -> ListComponentsResponseItem.builder()
.name(componentRow.getName())
.hashes(mapHashes(componentRow))
.classifier(componentRow.getClassifier() != null ? componentRow.getClassifier().name() : null)
.copyright(componentRow.getCopyright())
.cpe(componentRow.getCpe())
.group(componentRow.getGroup())
.internal(componentRow.isInternal())
.lastInheritedRiskScore(componentRow.getLastInheritedRiskScore())
.license(componentRow.getLicense())
.licenseExpression(componentRow.getLicenseExpression())
.licenseUrl(componentRow.getLicenseUrl())
.resolvedLicense(mapLicense(componentRow.getResolvedLicense()))
.occurrenceCount(componentRow.getOccurrenceCount())
.purl(componentRow.getPurl().toString())
.swidTagId(componentRow.getSwidTagId())
.uuid(componentRow.getUuid())
.version(componentRow.getVersion())
.name(componentRow.component().getName())
.hashes(mapHashes(componentRow.component()))
.classifier(componentRow.component().getClassifier() != null ? componentRow.component().getClassifier().name() : null)
.copyright(componentRow.component().getCopyright())
.cpe(componentRow.component().getCpe())
.group(componentRow.component().getGroup())
.internal(componentRow.component().isInternal())
.lastInheritedRiskScore(componentRow.component().getLastInheritedRiskScore())
.license(componentRow.component().getLicense())
.licenseExpression(componentRow.component().getLicenseExpression())
.licenseUrl(componentRow.component().getLicenseUrl())
.resolvedLicense(mapLicense(componentRow.component().getResolvedLicense()))
.occurrenceCount(componentRow.component().getOccurrenceCount())
.purl(componentRow.component().getPurl().toString())
.swidTagId(componentRow.component().getSwidTagId())
.uuid(componentRow.component().getUuid())
.version(componentRow.component().getVersion())
.published(componentRow.published() != null ? componentRow.published().getEpochSecond() : null)
.lastFetched(componentRow.lastFetched() != null ? componentRow.lastFetched().getEpochSecond() : null)
.latestVersion(componentRow.latestVersion())
.integrityCheckStatus(componentRow.integrityCheckStatus() != null ? componentRow.integrityCheckStatus().name() : null)
.integrityRepoUrl(componentRow.integrityRepoUrl())
.vulnerabilities(componentRow.vulnerabilities())
.critical(componentRow.critical())
.high(componentRow.high())
.medium(componentRow.medium())
.low(componentRow.low())
.unassigned(componentRow.unassigned())
.build())
.toList())
.pagination(createPaginationMetadata(uriInfo, componentsPage))
Expand Down
Loading
Loading