Skip to content
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

dockerfile: apply no-cache to generated sboms #3414

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,14 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
if err != nil {
return nil, errors.Wrapf(err, "failed to parse sbom generator %s", src)
}

useCache := true
if v, ok := req.FrontendAttrs["no-cache"]; ok && v == "" {
// disable cache if cache is disabled for all stages
useCache = false
}
ref = reference.TagNameOnly(ref)
procs = append(procs, proc.SBOMProcessor(ref.String()))
procs = append(procs, proc.SBOMProcessor(ref.String(), useCache))
}

if attrs, ok := attests["provenance"]; ok {
Expand Down
17 changes: 10 additions & 7 deletions frontend/attestations/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
// build-contexts or multi-stage builds. Handling these separately allows the
// scanner to optionally ignore these or to mark them as such in the
// attestation.
type Scanner func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State) (result.Attestation[llb.State], error)
type Scanner func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[llb.State], error)

func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scanner string) (Scanner, error) {
if scanner == "" {
Expand All @@ -55,7 +55,7 @@ func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scan
return nil, errors.Errorf("scanner %s does not have cmd", scanner)
}

return func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State) (result.Attestation[llb.State], error) {
return func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[llb.State], error) {
var env []string
env = append(env, cfg.Config.Env...)
env = append(env, "BUILDKIT_SCAN_DESTINATION="+outDir)
Expand All @@ -64,17 +64,20 @@ func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scan
env = append(env, "BUILDKIT_SCAN_SOURCE_EXTRAS="+path.Join(srcDir, "extras/"))
}

opts := []llb.RunOption{
llb.Dir(cfg.Config.WorkingDir),
llb.Args(args),
runOpts := []llb.RunOption{
llb.WithCustomName(fmt.Sprintf("[%s] generating sbom using %s", name, scanner)),
}
for _, opt := range opts {
runOpts = append(runOpts, opt)
}
runOpts = append(runOpts, llb.Dir(cfg.Config.WorkingDir))
runOpts = append(runOpts, llb.Args(args))
for _, e := range env {
k, v, _ := strings.Cut(e, "=")
opts = append(opts, llb.AddEnv(k, v))
runOpts = append(runOpts, llb.AddEnv(k, v))
}

runscan := llb.Image(scanner).Run(opts...)
runscan := llb.Image(scanner).Run(runOpts...)
runscan.AddMount(path.Join(srcDir, "core", CoreSBOMName), ref, llb.Readonly)
for k, extra := range extras {
runscan.AddMount(path.Join(srcDir, "extras", ExtraSBOMPrefix+k), extra, llb.Readonly)
Expand Down
8 changes: 7 additions & 1 deletion frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,13 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {

if scanner != nil {
for i, p := range expPlatforms.Platforms {
att, err := scanner(ctx, p.ID, scanTargets[i].Core, scanTargets[i].Extras)
target := scanTargets[i]

var opts []llb.ConstraintsOpt
if target.IgnoreCache {
opts = append(opts, llb.IgnoreCache)
}
att, err := scanner(ctx, p.ID, target.Core, target.Extras, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type ConvertOpt struct {
type SBOMTargets struct {
Core llb.State
Extras map[string]llb.State

IgnoreCache bool
}

func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, *SBOMTargets, error) {
Expand All @@ -103,9 +105,15 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
if ds.scanContext {
sbom.Extras["context"] = ds.opt.buildContext
}
if ds.ignoreCache {
sbom.IgnoreCache = true
}
for _, dsi := range findReachable(ds) {
if ds != dsi && dsi.scanStage {
sbom.Extras[dsi.stageName] = dsi.state
if dsi.ignoreCache {
sbom.IgnoreCache = true
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions solver/llbsolver/proc/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/pkg/errors"
)

func SBOMProcessor(scannerRef string) llbsolver.Processor {
func SBOMProcessor(scannerRef string, useCache bool) llbsolver.Processor {
return func(ctx context.Context, res *llbsolver.Result, s *llbsolver.Solver, j *solver.Job) (*llbsolver.Result, error) {
// skip sbom generation if we already have an sbom
if sbom.HasSBOM(res.Result) {
Expand Down Expand Up @@ -44,7 +44,11 @@ func SBOMProcessor(scannerRef string) llbsolver.Processor {
}
st := llb.NewState(defop)

att, err := scanner(ctx, p.ID, st, nil)
var opts []llb.ConstraintsOpt
if !useCache {
opts = append(opts, llb.IgnoreCache)
}
att, err := scanner(ctx, p.ID, st, nil, opts...)
if err != nil {
return nil, err
}
Expand Down