From 4a1292d7e6105acf7d35aea20fc9e392b53e1da2 Mon Sep 17 00:00:00 2001 From: mehab Date: Wed, 25 Oct 2023 13:52:39 +0100 Subject: [PATCH] converting to single query for getting component meta information Signed-off-by: mehab --- .../persistence/ComponentQueryManager.java | 4 +- .../persistence/QueryManager.java | 46 +++++++++++++------ .../resources/v1/ComponentResource.java | 2 +- .../persistence/QueryManagerTest.java | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java b/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java index e4e13382d..b055a6ea0 100644 --- a/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java +++ b/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java @@ -199,7 +199,7 @@ public PaginatedResult getComponents(final Project project, final boolean includ if (RepositoryType.UNSUPPORTED != type) { final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName()); component.setRepositoryMeta(repoMetaComponent); - component.setComponentMetaInformation(getMetaInformation(purl, component.getUuid())); + component.setComponentMetaInformation(getMetaInformation(component.getUuid())); } } } @@ -331,7 +331,7 @@ public PaginatedResult getComponents(ComponentIdentity identity, Project project if (RepositoryType.UNSUPPORTED != type) { final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName()); component.setRepositoryMeta(repoMetaComponent); - component.setComponentMetaInformation(getMetaInformation(purl, component.getUuid())); + component.setComponentMetaInformation(getMetaInformation(component.getUuid())); } } } diff --git a/src/main/java/org/dependencytrack/persistence/QueryManager.java b/src/main/java/org/dependencytrack/persistence/QueryManager.java index 98448fa9f..e9d12ebdf 100644 --- a/src/main/java/org/dependencytrack/persistence/QueryManager.java +++ b/src/main/java/org/dependencytrack/persistence/QueryManager.java @@ -18,6 +18,7 @@ */ package org.dependencytrack.persistence; +import alpine.common.logging.Logger; import alpine.common.util.BooleanUtil; import alpine.model.ApiKey; import alpine.model.ConfigProperty; @@ -27,6 +28,7 @@ import alpine.persistence.AlpineQueryManager; import alpine.persistence.PaginatedResult; import alpine.resources.AlpineRequest; +import alpine.server.util.DbUtil; import com.github.packageurl.PackageURL; import com.google.common.collect.Lists; import io.github.resilience4j.retry.Retry; @@ -125,6 +127,8 @@ public class QueryManager extends AlpineQueryManager { private AlpineRequest request; + + private static final Logger LOGGER = Logger.getLogger(QueryManager.class); private BomQueryManager bomQueryManager; private ComponentQueryManager componentQueryManager; private FindingsQueryManager findingsQueryManager; @@ -1843,19 +1847,35 @@ public IntegrityAnalysis getIntegrityAnalysisByComponentUuid(UUID uuid) { return getIntegrityAnalysisQueryManager().getIntegrityAnalysisByComponentUuid(uuid); } - public ComponentMetaInformation getMetaInformation(PackageURL purl, UUID uuid) { - Date publishedAt = null; - Date lastFetched = null; - IntegrityMatchStatus integrityMatchStatus = null; - final IntegrityMetaComponent integrityMetaComponent = getIntegrityMetaComponent(purl.toString()); - final IntegrityAnalysis integrityAnalysis = getIntegrityAnalysisByComponentUuid(uuid); - if (integrityMetaComponent != null) { - publishedAt = integrityMetaComponent.getPublishedAt(); - lastFetched = integrityMetaComponent.getLastFetch(); - } - if (integrityAnalysis != null) { - integrityMatchStatus = integrityAnalysis.getIntegrityCheckStatus(); + public ComponentMetaInformation getMetaInformation(UUID uuid) { + + Connection connection = null; + PreparedStatement preparedStatement = null; + String queryString = """ + SELECT "C"."ID", "C"."PURL", "IMC"."LAST_FETCH", "IMC"."PUBLISHED_AT", "IA"."INTEGRITY_CHECK_STATUS" FROM "COMPONENT" "C" + JOIN "INTEGRITY_META_COMPONENT" "IMC" ON "C"."PURL" ="IMC"."PURL" JOIN "INTEGRITY_ANALYSIS" "IA" ON "IA"."ID" ="C"."ID" WHERE "C"."UUID" = ? + """; + try { + connection = (Connection) pm.getDataStoreConnection(); + + preparedStatement = connection.prepareStatement(queryString); + preparedStatement.setString(1, uuid.toString()); + ResultSet resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + var publishedDate = Date.from(resultSet.getTimestamp("PUBLISHED_AT").toInstant()); + Date lastFetch = Date.from(resultSet.getTimestamp("LAST_FETCH").toInstant()); + return new ComponentMetaInformation(publishedDate, + IntegrityMatchStatus.valueOf(resultSet.getString("INTEGRITY_CHECK_STATUS")), + lastFetch); + + } + } catch (Exception ex) { + LOGGER.error("error occurred while fetch component published date and integrity information", ex); + throw new RuntimeException(ex); + } finally { + DbUtil.close(preparedStatement); + DbUtil.close(connection); } - return new ComponentMetaInformation(publishedAt, integrityMatchStatus, lastFetched); + return null; } } diff --git a/src/main/java/org/dependencytrack/resources/v1/ComponentResource.java b/src/main/java/org/dependencytrack/resources/v1/ComponentResource.java index 04d475f1d..589356521 100644 --- a/src/main/java/org/dependencytrack/resources/v1/ComponentResource.java +++ b/src/main/java/org/dependencytrack/resources/v1/ComponentResource.java @@ -153,7 +153,7 @@ public Response getComponentByUuid( detachedComponent.setRepositoryMeta(repoMetaComponent); } if (includeIntegrityMetaData) { - detachedComponent.setComponentMetaInformation(qm.getMetaInformation(component.getPurl(), component.getUuid())); + detachedComponent.setComponentMetaInformation(qm.getMetaInformation(component.getUuid())); } } } diff --git a/src/test/java/org/dependencytrack/persistence/QueryManagerTest.java b/src/test/java/org/dependencytrack/persistence/QueryManagerTest.java index 589cce019..3796ba74c 100644 --- a/src/test/java/org/dependencytrack/persistence/QueryManagerTest.java +++ b/src/test/java/org/dependencytrack/persistence/QueryManagerTest.java @@ -42,7 +42,7 @@ public void testGetMetaInformation() { integrityMetaComponent.setStatus(FetchStatus.PROCESSED); qm.createIntegrityMetaComponent(integrityMetaComponent); component = qm.createComponent(component, false); - ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getPurl(), component.getUuid()); + ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getUuid()); Assert.assertEquals(HASH_MATCH_PASSED, componentMetaInformation.integrityMatchStatus()); Assert.assertEquals(integrityMetaComponent.getPublishedAt(), componentMetaInformation.publishedDate()); Assert.assertEquals(integrityMetaComponent.getLastFetch(), componentMetaInformation.lastFetched());