-
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
Open
a-oren
wants to merge
5
commits into
guacsec:main
Choose a base branch
from
a-oren:TC-5071
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf0257f
feat(providers): add Dockerfile/Containerfile provider for image anal…
a-oren 63309e2
feat(api): add TRUSTIFY_DA_RECOMMEND env var to disable recommendations
a-oren 3b43c23
fix(dockerfile): analyze all FROM lines via batch analysis
a-oren 76a3a72
fix(dockerfile): use case-insensitive scratch comparison
a-oren bdc3e41
fix(cli): route Dockerfile analysis through imageAnalysis() for per-i…
a-oren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
||
|
|
||
| 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()); | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * 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,29 @@ 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) { | ||
| 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); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.