-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-30569: Add SBOM Scanning REST API to Central #18484
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4bf97a
rename SBOM generation http handler
dcaravel aa9a937
SBOM Scanning rails
dcaravel fa508f2
populate scanner version
dcaravel 8b4017e
add more fields to fake vuln report
dcaravel cb814ce
remove extra whitespace in sbom proto comment
dcaravel 3c99419
review updates
dcaravel 09f9e5f
fix style and unit test failures
dcaravel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| v1 "github.com/stackrox/rox/generated/api/v1" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/env" | ||
| "github.com/stackrox/rox/pkg/features" | ||
| "github.com/stackrox/rox/pkg/httputil" | ||
| "github.com/stackrox/rox/pkg/images/integration" | ||
| "github.com/stackrox/rox/pkg/ioutils" | ||
| scannerTypes "github.com/stackrox/rox/pkg/scanners/types" | ||
| "github.com/stackrox/rox/pkg/set" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/protobuf/encoding/protojson" | ||
| ) | ||
|
|
||
| var ( | ||
| supportedMediaTypes = set.NewFrozenStringSet( | ||
| "text/spdx+json", // Used by Sigstore/Cosign, not IANA registered. | ||
| "application/spdx+json", // IANA registered type for SPDX JSON. | ||
| ) | ||
| ) | ||
|
|
||
| type sbomScanHttpHandler struct { | ||
| integrations integration.Set | ||
| } | ||
|
|
||
| var _ http.Handler = (*sbomScanHttpHandler)(nil) | ||
|
|
||
| func SBOMScanHandler(integrations integration.Set) http.Handler { | ||
| return sbomScanHttpHandler{ | ||
| integrations: integrations, | ||
| } | ||
| } | ||
|
|
||
| func (s sbomScanHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| // Verify Scanner V4 is enabled. | ||
| if !features.ScannerV4.Enabled() { | ||
| httputil.WriteGRPCStyleError(w, codes.Unimplemented, errors.New("Scanner V4 is disabled.")) | ||
| return | ||
| } | ||
|
|
||
| if !features.SBOMScanning.Enabled() { | ||
| httputil.WriteGRPCStyleError(w, codes.Unimplemented, errors.New("SBOM Scanning is disabled.")) | ||
| return | ||
| } | ||
|
|
||
| // Only POST requests are supported. | ||
| if r.Method != http.MethodPost { | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| // Validate the media type is supported. | ||
| contentType := r.Header.Get("Content-Type") | ||
| err := s.validateMediaType(contentType) | ||
| if err != nil { | ||
| httputil.WriteGRPCStyleError(w, codes.InvalidArgument, fmt.Errorf("validating media type: %w", err)) | ||
| return | ||
| } | ||
|
|
||
| // Enforce maximum uncompressed request size to prevent excessive memory usage. | ||
| // MaxBytesReader returns an error if the request body exceeds the limit. | ||
| maxReqSizeBytes := env.SBOMScanMaxReqSizeBytes.IntegerSetting() | ||
| limitedBody := http.MaxBytesReader(w, r.Body, int64(maxReqSizeBytes)) | ||
|
|
||
| // Add cancellation safety to prevent partial/corrupted data on interruption. | ||
| // InterruptibleReader: Ensures clean termination without partial reads. | ||
| body, interrupt := ioutils.NewInterruptibleReader(limitedBody) | ||
| defer interrupt() | ||
dcaravel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // ContextBoundReader: Ensures reads fail fast when request context is canceled. | ||
| // This prevents hanging reads on connection interruption | ||
| readCtx, cancel := context.WithCancel(r.Context()) | ||
| defer cancel() | ||
| body = ioutils.NewContextBoundReader(readCtx, body) | ||
|
|
||
| sbomScanResponse, err := s.scanSBOM(readCtx, body, contentType) | ||
| if err != nil { | ||
| // Check if error is due to request body exceeding size limit. | ||
| var maxBytesErr *http.MaxBytesError | ||
| if errors.As(err, &maxBytesErr) { | ||
| httputil.WriteGRPCStyleError(w, codes.InvalidArgument, fmt.Errorf("request body exceeds maximum size of %d bytes", maxBytesErr.Limit)) | ||
| return | ||
| } | ||
dcaravel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| httputil.WriteGRPCStyleError(w, codes.Internal, fmt.Errorf("scanning SBOM: %w", err)) | ||
| return | ||
| } | ||
|
|
||
| // Serialize the scan result to JSON using protojson for proper protobuf handling. | ||
| // protojson handles protobuf-specific types (enums, oneof, etc.) correctly. | ||
| jsonBytes, err := protojson.MarshalOptions{Multiline: true}.Marshal(sbomScanResponse) | ||
| if err != nil { | ||
| httputil.WriteGRPCStyleError(w, codes.Internal, fmt.Errorf("serializing SBOM scan response: %w", err)) | ||
| return | ||
| } | ||
|
|
||
| // Set response headers and write JSON response. | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if _, err := w.Write(jsonBytes); err != nil { | ||
| log.Warnw("writing SBOM scan response: %v", err) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // scanSBOM will request a scan of the SBOM from Scanner V4. | ||
| func (s sbomScanHttpHandler) scanSBOM(ctx context.Context, limitedReader io.Reader, contentType string) (*v1.SBOMScanResponse, error) { | ||
| // Get reference to Scanner V4. | ||
| scannerV4, dataSource, err := s.getScannerV4Integration() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getting Scanner V4 integration: %w", err) | ||
| } | ||
|
|
||
| // Scan the SBOM. | ||
| sbomScanResponse, err := scannerV4.ScanSBOM(ctx, limitedReader, contentType) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("scanning sbom: %w", err) | ||
| } | ||
| // Set the scan DataSource used to do the scan. | ||
| if sbomScanResponse.GetScan() != nil { | ||
| sbomScanResponse.GetScan().DataSource = dataSource | ||
| } | ||
|
|
||
| return sbomScanResponse, nil | ||
| } | ||
|
|
||
| // getScannerV4Integration returns the SBOM interface of Scanner V4. | ||
| func (s sbomScanHttpHandler) getScannerV4Integration() (scannerTypes.SBOMer, *storage.DataSource, error) { | ||
| sbomer, dataSource, err := getScannerV4SBOMIntegration(s.integrations.ScannerSet()) | ||
| return sbomer, dataSource, err | ||
| } | ||
dcaravel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // validateMediaType validates the media type from the content type header is supported. | ||
| func (s sbomScanHttpHandler) validateMediaType(contentType string) error { | ||
| // Strip any parameters (e.g., charset) from the media type | ||
| mediaType := strings.TrimSpace(strings.Split(contentType, ";")[0]) | ||
| if !supportedMediaTypes.Contains(mediaType) { | ||
| return fmt.Errorf("unsupported media type %q, supported types %v", mediaType, supportedMediaTypes.AsSlice()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.