Debian Codegraph @

buildkit/frontend/attestations/sbom/sbom.go

raw
1 package sbom
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "path"
8 "strings"
9
10 intoto "github.com/in-toto/in-toto-golang/in_toto"
11 "github.com/moby/buildkit/client/llb"
12 "github.com/moby/buildkit/client/llb/sourceresolver"
13 gatewaypb "github.com/moby/buildkit/frontend/gateway/pb"
14 "github.com/moby/buildkit/solver/result"
15 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
16 "github.com/pkg/errors"
17 )
18
19 const (
20 CoreSBOMName = "sbom"
21 ExtraSBOMPrefix = CoreSBOMName + "-"
22
23 srcDir = "/run/src/"
24 outDir = "/run/out/"
25 )
26
27 // Scanner is a function type for scanning the contents of a state and
28 // returning a new attestation and state representing the scan results.
29 //
30 // A scanner is designed a scan a single state, however, additional states can
31 // also be attached, for attaching additional information, such as scans of
32 // build-contexts or multi-stage builds. Handling these separately allows the
33 // scanner to optionally ignore these or to mark them as such in the
34 // attestation.
35 type Scanner func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[*llb.State], error)
36
37 func CreateSBOMScanner(ctx context.Context, resolver sourceresolver.MetaResolver, scanner string, resolveOpt sourceresolver.Opt, params map[string]string) (Scanner, error) {
38 if scanner == "" {
39 return nil, nil
40 }
41
42 imr := sourceresolver.NewImageMetaResolver(resolver)
43 scanner, _, dt, err := imr.ResolveImageConfig(ctx, scanner, resolveOpt)
44 if err != nil {
45 return nil, err
46 }
47
48 var cfg ocispecs.Image
49 if err := json.Unmarshal(dt, &cfg); err != nil {
50 return nil, err
51 }
52
53 var args []string
54 args = append(args, cfg.Config.Entrypoint...)
55 args = append(args, cfg.Config.Cmd...)
56 if len(args) == 0 {
57 return nil, errors.Errorf("scanner %s does not have cmd", scanner)
58 }
59
60 return func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[*llb.State], error) {
61 var env []string
62 env = append(env, cfg.Config.Env...)
63 env = append(env, "BUILDKIT_SCAN_DESTINATION="+outDir)
64 env = append(env, "BUILDKIT_SCAN_SOURCE="+path.Join(srcDir, "core", CoreSBOMName))
65 if len(extras) > 0 {
66 env = append(env, "BUILDKIT_SCAN_SOURCE_EXTRAS="+path.Join(srcDir, "extras/"))
67 }
68
69 for k, v := range params {
70 env = append(env, "BUILDKIT_SCAN_"+k+"="+v)
71 }
72
73 runOpts := []llb.RunOption{
74 llb.WithCustomName(fmt.Sprintf("[%s] generating sbom using %s", name, scanner)),
75 }
76 for _, opt := range opts {
77 runOpts = append(runOpts, opt)
78 }
79 runOpts = append(runOpts, llb.Dir(cfg.Config.WorkingDir))
80 runOpts = append(runOpts, llb.Args(args))
81 for _, e := range env {
82 k, v, _ := strings.Cut(e, "=")
83 runOpts = append(runOpts, llb.AddEnv(k, v))
84 }
85
86 runscan := llb.Image(scanner).Run(runOpts...)
87 runscan.AddMount("/tmp", llb.Scratch(), llb.Tmpfs())
88
89 runscan.AddMount(path.Join(srcDir, "core", CoreSBOMName), ref, llb.Readonly)
90 for k, extra := range extras {
91 runscan.AddMount(path.Join(srcDir, "extras", ExtraSBOMPrefix+k), extra, llb.Readonly)
92 }
93
94 stsbom := runscan.AddMount(outDir, llb.Scratch())
95 return result.Attestation[*llb.State]{
96 Kind: gatewaypb.AttestationKind_Bundle,
97 Ref: &stsbom,
98 Metadata: map[string][]byte{
99 result.AttestationReasonKey: []byte(result.AttestationReasonSBOM),
100 result.AttestationSBOMCore: []byte(CoreSBOMName),
101 },
102 InToto: result.InTotoAttestation{
103 PredicateType: intoto.PredicateSPDX,
104 },
105 }, nil
106 }, nil
107 }
108
109 func HasSBOM[T comparable](res *result.Result[T]) bool {
110 for _, as := range res.Attestations {
111 for _, a := range as {
112 if a.InToto.PredicateType == intoto.PredicateSPDX {
113 return true
114 }
115 }
116 }
117 return false
118 }