-
Notifications
You must be signed in to change notification settings - Fork 1.4k
BUILDKIT_SBOM_SCAN build args #3249
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
Changes from all commits
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package attest | ||
| package sbom | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,8 +44,16 @@ const ( | |
| emptyImageName = "scratch" | ||
| defaultContextLocalName = "context" | ||
| historyComment = "buildkit.dockerfile.v0" | ||
|
|
||
| sbomScanContext = "BUILDKIT_SBOM_SCAN_CONTEXT" | ||
| sbomScanStage = "BUILDKIT_SBOM_SCAN_STAGE" | ||
| ) | ||
|
|
||
| var nonEnvArgs = map[string]struct{}{ | ||
| sbomScanContext: {}, | ||
| sbomScanStage: {}, | ||
| } | ||
|
|
||
| type ConvertOpt struct { | ||
| Target string | ||
| MetaResolver llb.ImageMetaResolver | ||
|
|
@@ -77,12 +85,36 @@ type ConvertOpt struct { | |
| ContextByName func(ctx context.Context, name, resolveMode string, p *ocispecs.Platform) (*llb.State, *Image, error) | ||
| } | ||
|
|
||
| func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, error) { | ||
| type SBOMTargets struct { | ||
| Core llb.State | ||
| Extras map[string]llb.State | ||
|
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. Is the name in this map retrievable from the final attestation (descriptor)?
Member
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. So by default this is the last part of the path component - I think we should probably document this, so that scanners can use this as the name in the SBOM. |
||
| } | ||
|
|
||
| func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, *SBOMTargets, error) { | ||
| ds, err := toDispatchState(ctx, dt, opt) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| sbom := SBOMTargets{ | ||
| Core: ds.state, | ||
| Extras: map[string]llb.State{}, | ||
| } | ||
| return &ds.state, &ds.image, nil | ||
| if ds.scanContext { | ||
| sbom.Extras["context"] = ds.opt.buildContext | ||
| } | ||
| for dsi := ds; dsi != nil; dsi = dsi.base { | ||
| if ds != dsi && dsi.scanStage { | ||
| sbom.Extras["stage:"+dsi.stageName] = dsi.state | ||
| } | ||
| for dsi2 := range dsi.deps { | ||
| if dsi2.scanStage { | ||
| sbom.Extras["stage:"+dsi2.stageName] = dsi2.state | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return &ds.state, &ds.image, &sbom, nil | ||
| } | ||
|
|
||
| func Dockefile2Outline(ctx context.Context, dt []byte, opt ConvertOpt) (*outline.Outline, error) { | ||
|
|
@@ -533,10 +565,23 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS | |
| return nil, parser.WithLocation(err, cmd.Location()) | ||
| } | ||
| } | ||
| d.opt = opt | ||
|
|
||
| for p := range d.ctxPaths { | ||
| ctxPaths[p] = struct{}{} | ||
| } | ||
|
|
||
| locals := []instructions.KeyValuePairOptional{} | ||
| locals = append(locals, d.opt.metaArgs...) | ||
| locals = append(locals, d.buildArgs...) | ||
| for _, a := range locals { | ||
| switch a.Key { | ||
| case sbomScanStage: | ||
| d.scanStage = isEnabledForStage(d.stageName, a.ValueString()) | ||
| case sbomScanContext: | ||
| d.scanContext = isEnabledForStage(d.stageName, a.ValueString()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(opt.Labels) != 0 && target.image.Config.Labels == nil { | ||
|
|
@@ -749,6 +794,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error { | |
| } | ||
|
|
||
| type dispatchState struct { | ||
| opt dispatchOpt | ||
| state llb.State | ||
| image Image | ||
| platform *ocispecs.Platform | ||
|
|
@@ -769,6 +815,8 @@ type dispatchState struct { | |
| buildInfo binfotypes.BuildInfo | ||
| outline outlineCapture | ||
| epoch *time.Time | ||
| scanStage bool | ||
| scanContext bool | ||
| } | ||
|
|
||
| type dispatchStates struct { | ||
|
|
@@ -1363,7 +1411,9 @@ func dispatchArg(d *dispatchState, c *instructions.ArgCommand, metaArgs []instru | |
| ai := argInfo{definition: arg, location: c.Location()} | ||
|
|
||
| if buildArg.Value != nil { | ||
| d.state = d.state.AddEnv(buildArg.Key, *buildArg.Value) | ||
| if _, ok := nonEnvArgs[buildArg.Key]; !ok { | ||
| d.state = d.state.AddEnv(buildArg.Key, *buildArg.Value) | ||
| } | ||
| ai.value = *buildArg.Value | ||
| } | ||
|
|
||
|
|
@@ -1735,3 +1785,17 @@ func clampTimes(img Image, tm *time.Time) Image { | |
| func isHTTPSource(src string) bool { | ||
| return strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") | ||
| } | ||
|
|
||
| func isEnabledForStage(stage string, value string) bool { | ||
| if enabled, err := strconv.ParseBool(value); err == nil { | ||
| return enabled | ||
| } | ||
|
|
||
| vv := strings.Split(value, ",") | ||
| for _, v := range vv { | ||
| if v == stage { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.