-
Notifications
You must be signed in to change notification settings - Fork 11
fix(dockerfile): analyze all FROM lines via batch analysis #546
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
base: main
Are you sure you want to change the base?
Changes from all commits
bf0257f
63309e2
3b43c23
76a3a72
bdc3e41
4ed519a
22e15c1
fe2c973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,7 @@ public final class ExhortApi implements Api { | |
| public static final String S_API_V5_LICENSES = "%s/api/v5/licenses/%s"; | ||
| public static final String S_API_V5_LICENSES_IDENTIFY = "%s/api/v5/licenses/identify"; | ||
| private static final String TRUSTIFY_DA_LICENSE_CHECK = "TRUSTIFY_DA_LICENSE_CHECK"; | ||
| private static final String TRUSTIFY_DA_RECOMMEND = "TRUSTIFY_DA_RECOMMEND"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JS client uses
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is from an older worktree (TC-4694). The JS client's main branch has already been updated to use TRUSTIFY_DA_RECOMMEND
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [sdlc-workflow/verify-pr] Classified as: Question Investigation: Both Java and JS clients use |
||
|
|
||
| private String endpoint; | ||
|
|
||
|
|
@@ -296,10 +297,36 @@ public CompletableFuture<byte[]> stackAnalysisHtml(final String manifestFile) th | |
| public CompletableFuture<AnalysisReport> stackAnalysis(final String manifestFile) | ||
| throws IOException { | ||
| String exClientTraceId = commonHookBeginning(false); | ||
| var content = resolveStackContent(manifestFile); | ||
| var uri = resolveAnalysisUri(content); | ||
| var request = buildRequest(content, uri, MediaType.APPLICATION_JSON, "Stack Analysis"); | ||
| if (content.batch) { | ||
| return this.client | ||
| .sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
| .thenApply( | ||
| response -> { | ||
| RequestManager.getInstance().addClientTraceIdToRequest(exClientTraceId); | ||
| if (debugLoggingIsNeeded()) { | ||
| logExhortRequestId(response); | ||
| } | ||
| Map<String, AnalysisReport> reports = getBatchStackAnalysisReports(response); | ||
| commonHookAfterExhortResponse(); | ||
| return reports.isEmpty() | ||
| ? new AnalysisReport() | ||
| : reports.values().iterator().next(); | ||
| }) | ||
| .exceptionally( | ||
| exception -> { | ||
| LOG.severe( | ||
| String.format( | ||
| "failed to invoke stackAnalysis (batch) for getting the json report," | ||
| + " received message= %s ", | ||
| exception.getMessage())); | ||
| return new AnalysisReport(); | ||
| }); | ||
| } | ||
| return this.client | ||
| .sendAsync( | ||
| this.buildStackRequest(manifestFile, MediaType.APPLICATION_JSON), | ||
| HttpResponse.BodyHandlers.ofString()) | ||
| .sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
| .thenApply( | ||
| response -> | ||
| getAnalysisReportFromResponse(response, "StackAnalysis", "json", exClientTraceId)) | ||
|
|
@@ -361,15 +388,26 @@ public static boolean debugLoggingIsNeeded() { | |
| return Environment.getBoolean("TRUSTIFY_DA_DEBUG", false); | ||
| } | ||
|
|
||
| private static URI buildAnalysisUri(String template, String endpoint) { | ||
| String base = String.format(template, endpoint); | ||
| if (!Environment.getBoolean(TRUSTIFY_DA_RECOMMEND, true)) { | ||
| return URI.create(base + "?recommend=false"); | ||
| } | ||
| return URI.create(base); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<AnalysisReport> componentAnalysis( | ||
| final String manifest, final byte[] manifestContent) throws IOException { | ||
| String exClientTraceId = commonHookBeginning(false); | ||
| var manifestPath = Path.of(manifest); | ||
| var provider = Ecosystem.getProvider(manifestPath); | ||
| var uri = URI.create(String.format(S_API_V_5_ANALYSIS, getEndpoint())); | ||
| var content = provider.provideComponent(); | ||
| commonHookAfterProviderCreatedSbomAndBeforeExhort(); | ||
| var uri = resolveAnalysisUri(content); | ||
| if (content.batch) { | ||
| return getBatchAnalysisReportForComponent(uri, content, exClientTraceId); | ||
| } | ||
| return getAnalysisReportForComponent(uri, content, exClientTraceId); | ||
| } | ||
|
|
||
|
|
@@ -411,9 +449,12 @@ public CompletableFuture<AnalysisReport> componentAnalysis(String manifestFile) | |
| String exClientTraceId = commonHookBeginning(false); | ||
| var manifestPath = Path.of(manifestFile); | ||
| var provider = Ecosystem.getProvider(manifestPath); | ||
| var uri = URI.create(String.format(S_API_V_5_ANALYSIS, getEndpoint())); | ||
| var content = provider.provideComponent(); | ||
| commonHookAfterProviderCreatedSbomAndBeforeExhort(); | ||
| var uri = resolveAnalysisUri(content); | ||
| if (content.batch) { | ||
| return getBatchAnalysisReportForComponent(uri, content, exClientTraceId); | ||
| } | ||
| return getAnalysisReportForComponent(uri, content, exClientTraceId); | ||
| } | ||
|
|
||
|
|
@@ -440,8 +481,48 @@ private CompletableFuture<AnalysisReport> getAnalysisReportForComponent( | |
| }); | ||
| } | ||
|
|
||
| /** Resolves the analysis URI based on whether the content is batch or single. */ | ||
| private URI resolveAnalysisUri(Provider.Content content) { | ||
| if (content.batch) { | ||
| return buildAnalysisUri(S_API_V_5_BATCH_ANALYSIS, getEndpoint()); | ||
| } | ||
| return buildAnalysisUri(S_API_V_5_ANALYSIS, getEndpoint()); | ||
| } | ||
|
|
||
| /** | ||
| * Build an HTTP request wrapper for sending to the Backend API for Stack Analysis only. | ||
| * Sends batch content to the batch-analysis endpoint and returns the first analysis report from | ||
| * the batch response map. | ||
| */ | ||
| private CompletableFuture<AnalysisReport> getBatchAnalysisReportForComponent( | ||
| URI uri, Provider.Content content, String exClientTraceId) { | ||
| return this.client | ||
| .sendAsync( | ||
| this.buildRequest(content, uri, MediaType.APPLICATION_JSON, "Batch Component Analysis"), | ||
| HttpResponse.BodyHandlers.ofString()) | ||
| .thenApply( | ||
| response -> { | ||
| RequestManager.getInstance().addClientTraceIdToRequest(exClientTraceId); | ||
| if (debugLoggingIsNeeded()) { | ||
| logExhortRequestId(response); | ||
| } | ||
| Map<String, AnalysisReport> reports = getBatchStackAnalysisReports(response); | ||
| commonHookAfterExhortResponse(); | ||
| return reports.isEmpty() ? new AnalysisReport() : reports.values().iterator().next(); | ||
| }) | ||
| .exceptionally( | ||
| exception -> { | ||
| LOG.severe( | ||
| String.format( | ||
| "failed to invoke Batch Component Analysis for getting the json report," | ||
| + " received message= %s ", | ||
| exception.getMessage())); | ||
| return new AnalysisReport(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Build an HTTP request wrapper for sending to the Backend API for Stack Analysis only. Uses the | ||
| * batch-analysis endpoint when the provider returns batch content. | ||
| * | ||
| * @param manifestFile the path for the manifest file | ||
| * @param acceptType the type of requested content | ||
|
|
@@ -450,13 +531,21 @@ private CompletableFuture<AnalysisReport> getAnalysisReportForComponent( | |
| */ | ||
| private HttpRequest buildStackRequest(final String manifestFile, final MediaType acceptType) | ||
| throws IOException { | ||
| var content = resolveStackContent(manifestFile); | ||
| var uri = resolveAnalysisUri(content); | ||
| return buildRequest(content, uri, acceptType, "Stack Analysis"); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the provider for the given manifest file and produces the stack content. Also tracks | ||
| * timing via {@link #commonHookAfterProviderCreatedSbomAndBeforeExhort()}. | ||
| */ | ||
| private Provider.Content resolveStackContent(String manifestFile) throws IOException { | ||
| var manifestPath = Path.of(manifestFile); | ||
| var provider = Ecosystem.getProvider(manifestPath); | ||
| var uri = URI.create(String.format(S_API_V_5_ANALYSIS, getEndpoint())); | ||
| var content = provider.provideStack(); | ||
| commonHookAfterProviderCreatedSbomAndBeforeExhort(); | ||
|
|
||
| return buildRequest(content, uri, acceptType, "Stack Analysis"); | ||
| return content; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -539,7 +628,7 @@ <H, T> CompletableFuture<T> performBatchAnalysis( | |
| final String analysisName) | ||
| throws IOException { | ||
| String exClientTraceId = commonHookBeginning(false); | ||
| var uri = URI.create(String.format(S_API_V_5_BATCH_ANALYSIS, getEndpoint())); | ||
| var uri = buildAnalysisUri(S_API_V_5_BATCH_ANALYSIS, getEndpoint()); | ||
| var sboms = sbomsGenerator.get(); | ||
| var content = | ||
| new Provider.Content( | ||
|
|
@@ -601,27 +690,33 @@ public CompletableFuture<ComponentAnalysisResult> componentAnalysisWithLicense( | |
| String exClientTraceId = commonHookBeginning(false); | ||
| var manifestPath = Path.of(manifestFile); | ||
| var provider = Ecosystem.getProvider(manifestPath); | ||
| var uri = URI.create(String.format(S_API_V_5_ANALYSIS, getEndpoint())); | ||
| var content = provider.provideComponent(); | ||
| String sbomJson = new String(content.buffer); | ||
| commonHookAfterProviderCreatedSbomAndBeforeExhort(); | ||
| return getAnalysisReportForComponent(uri, content, exClientTraceId) | ||
| .thenCompose( | ||
| report -> { | ||
| if (!isLicenseCheckEnabled()) { | ||
| return CompletableFuture.completedFuture(new ComponentAnalysisResult(report, null)); | ||
| } | ||
| return LicenseCheck.runLicenseCheck(this, provider, manifestPath, sbomJson, report) | ||
| .thenApply(summary -> new ComponentAnalysisResult(report, summary)) | ||
| .exceptionally( | ||
| ex -> { | ||
| LOG.warning( | ||
| String.format( | ||
| "License check failed, continuing without it: %s", | ||
| ex.getMessage())); | ||
| return new ComponentAnalysisResult(report, null); | ||
| }); | ||
| }); | ||
| var uri = resolveAnalysisUri(content); | ||
| CompletableFuture<AnalysisReport> reportFuture = | ||
| content.batch | ||
| ? getBatchAnalysisReportForComponent(uri, content, exClientTraceId) | ||
| : getAnalysisReportForComponent(uri, content, exClientTraceId); | ||
| return reportFuture.thenCompose( | ||
| report -> { | ||
| if (!isLicenseCheckEnabled() || content.batch) { | ||
| LOG.fine( | ||
| String.format( | ||
| "Skipping license check: enabled=%b, batch=%b", | ||
| isLicenseCheckEnabled(), content.batch)); | ||
| return CompletableFuture.completedFuture(new ComponentAnalysisResult(report, null)); | ||
| } | ||
| return LicenseCheck.runLicenseCheck(this, provider, manifestPath, sbomJson, report) | ||
| .thenApply(summary -> new ComponentAnalysisResult(report, summary)) | ||
| .exceptionally( | ||
| ex -> { | ||
| LOG.warning( | ||
| String.format( | ||
| "License check failed, continuing without it: %s", ex.getMessage())); | ||
| return new ComponentAnalysisResult(report, null); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.