Repository navigation
feat(sbom): track Go and Rust dependencies in shipped binaries #466
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
2486fa0
feat(sbom): retain Go and Rust dependencies by shipped binary content
reyreavman f4f65f8
feat(sbom): recover produced binaries from cached image stages
reyreavman 4687f02
test(sbom): verify binary evidence across imports and cache reuse
reyreavman da08270
fix(sbom): preserve complete evidence for same-layer hardlink chains
reyreavman 6f49d04
fix(sbom): skip non-produced layers during binary classification
reyreavman b6a9ee3
test(sbom): guard real stage order and cache compatibility
reyreavman 328ea41
docs(sbom): clarify persisted binary evidence contracts
reyreavman 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 |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package build | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "fmt" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/name" | ||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
|
|
||
| "github.com/werf/common-go/pkg/util" | ||
| "github.com/werf/werf/v3/pkg/build/stage" | ||
| "github.com/werf/werf/v3/pkg/image" | ||
| "github.com/werf/werf/v3/pkg/sbom/shippedscope" | ||
| ) | ||
|
|
||
| type sbomStageRecord struct { | ||
| Stage stage.StageName `json:"stage"` | ||
| StageDigest string `json:"stageDigest"` | ||
| ParentStageID string `json:"parentStageID"` | ||
| } | ||
|
|
||
| type sbomStageProvenance struct { | ||
| Layers shippedscope.ProducedLayers | ||
| DiffIDs []v1.Hash | ||
| } | ||
|
|
||
| func resolveSBOMStageProvenance(ctx context.Context, final *image.StageDesc, readConfig func(context.Context, string) (*v1.ConfigFile, error)) (sbomStageProvenance, error) { | ||
| unknown := sbomStageProvenance{} | ||
| if final == nil || final.StageID == nil || final.Info == nil || final.Info.Repository == "" { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: final stage descriptor is incomplete") | ||
| } | ||
|
|
||
| ref := final.Info.RepoDigest | ||
| if ref == "" { | ||
| ref = final.Info.Name | ||
| } | ||
| current, err := readConfig(ctx, ref) | ||
| if err != nil { | ||
| return unknown, fmt.Errorf("read SBOM stage config %q: %w", ref, err) | ||
| } | ||
| if current == nil { | ||
| return unknown, fmt.Errorf("read SBOM stage config %q: empty config", ref) | ||
| } | ||
|
|
||
| result := sbomStageProvenance{DiffIDs: slices.Clone(current.RootFS.DiffIDs)} | ||
| id := final.StageID.String() | ||
| digest := final.StageID.Digest | ||
| seen := make(map[string]bool) | ||
| stages := []stage.StageName{ | ||
| stage.From, stage.BeforeInstall, stage.DependenciesBeforeInstall, stage.GitArchive, | ||
| stage.Packages, stage.Install, stage.DependenciesAfterInstall, stage.BeforeSetup, | ||
| stage.DependenciesBeforeSetup, stage.Setup, stage.DependenciesAfterSetup, | ||
| stage.GitCache, stage.GitLatestPatch, stage.ImageSpec, stage.VerityAnnotation, stage.Sign, | ||
| } | ||
| previousPosition := len(stages) | ||
| for { | ||
| if err := ctx.Err(); err != nil { | ||
| return unknown, err | ||
| } | ||
| if seen[id] { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: cyclic parent %q", id) | ||
| } | ||
| seen[id] = true | ||
|
|
||
| var record sbomStageRecord | ||
| if err := json.Unmarshal([]byte(current.Config.Labels[image.WerfSBOMStageLabel]), &record); err != nil { | ||
| return unknown, fmt.Errorf("read SBOM stage record %q: %w", id, err) | ||
| } | ||
| if record.StageDigest != digest || record.ParentStageID != current.Config.Labels[image.WerfParentStageID] { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: record does not belong to stage %q", id) | ||
| } | ||
| position := slices.Index(stages, record.Stage) | ||
| if position < 0 || position >= previousPosition { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: unexpected stage %q at %q", record.Stage, id) | ||
| } | ||
| previousPosition = position | ||
| if record.Stage == stage.From { | ||
| result.Layers.Known = true | ||
| slices.Reverse(result.Layers.Ranges) | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| parentDigest, timestamp, ok := strings.Cut(record.ParentStageID, "-") | ||
| if !ok || parentDigest == "" { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: invalid parent %q", record.ParentStageID) | ||
| } | ||
| if _, err := hex.DecodeString(parentDigest); err != nil { | ||
| return unknown, fmt.Errorf("read SBOM parent digest %q: %w", parentDigest, err) | ||
| } | ||
| if ts, err := strconv.ParseInt(timestamp, 10, 64); err != nil || ts <= 0 { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: invalid parent timestamp %q", timestamp) | ||
| } | ||
| parentRef, err := name.NewTag(final.Info.Repository + ":" + record.ParentStageID) | ||
| if err != nil { | ||
| return unknown, fmt.Errorf("parse SBOM parent reference: %w", err) | ||
| } | ||
| parent, err := readConfig(ctx, parentRef.Name()) | ||
| if err != nil { | ||
| return unknown, fmt.Errorf("read SBOM parent config %q: %w", parentRef.Name(), err) | ||
| } | ||
| if parent == nil { | ||
| return unknown, fmt.Errorf("read SBOM parent config %q: empty config", parentRef.Name()) | ||
| } | ||
| parentLayers := parent.RootFS.DiffIDs | ||
| currentLayers := current.RootFS.DiffIDs | ||
| if len(parentLayers) > len(currentLayers) || !slices.Equal(parentLayers, currentLayers[:len(parentLayers)]) { | ||
| return unknown, fmt.Errorf("resolve SBOM stages: parent layers are not a prefix of stage %q", id) | ||
| } | ||
| if record.Stage == stage.Install || record.Stage == stage.BeforeSetup || record.Stage == stage.Setup { | ||
| result.Layers.Ranges = append(result.Layers.Ranges, shippedscope.ProducedLayerRange{Start: len(parentLayers), End: len(currentLayers)}) | ||
| } | ||
|
|
||
| current = parent | ||
| id = record.ParentStageID | ||
| digest = parentDigest | ||
| } | ||
| } | ||
|
|
||
| func sbomProvenanceChecksum(provenance sbomStageProvenance) string { | ||
| parts := []string{strconv.FormatBool(provenance.Layers.Known), "diffids"} | ||
| for _, diffID := range provenance.DiffIDs { | ||
| parts = append(parts, diffID.String()) | ||
| } | ||
| parts = append(parts, "ranges") | ||
| for _, layerRange := range provenance.Layers.Ranges { | ||
| parts = append(parts, fmt.Sprintf("%d:%d", layerRange.Start, layerRange.End)) | ||
| } | ||
|
|
||
| return util.Sha256Hash(parts...) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Major (claim). A new service label with a per-stage value is new committed config → new stage content → every SBOM-enabled stapel stage rebuilds once after upgrade. The PR body says "without invalidating image-build caches". That sentence is false and should go; the one-time rebuild itself is fine and worth a line in the release notes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cache-invalidation claim does not match the actual lookup order.
BuildPhase.onImageStagefinds a suitable cached stage and returns before callingprepareStageInstructions; the new service label is written only after a miss. A newly committed image config has different content, but that does not change the recipe/anchor key used to select an existing stage.b6a9ee3 adds regressions for both image-anchor reuse and an individual cached stage with SBOM enabled and no provenance label. Both pass, leave the old label absent and avoid rebuilding. A mutation rejecting such a cached stage fails the regression. The description now states the precise consequence: old image caches remain reusable, but their binary provenance remains unknown until a newly built chain supplies it. No release-note claim of an unavoidable rebuild was added.