-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsbom.go
203 lines (175 loc) · 5.06 KB
/
sbom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022 go-imageinspect authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package imageinspect
import (
"bytes"
"context"
"encoding/json"
"sort"
"strings"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/remotes"
intoto "github.com/in-toto/in-toto-golang/in_toto"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/spdx/tools-golang/jsonloader"
"github.com/spdx/tools-golang/spdx"
)
type SBOM struct {
AlpinePackages []Package `json:",omitempty"`
UnknownPackages []Package `json:",omitempty"`
}
type pkgType int
const (
pkgTypeUnknown pkgType = iota
pkgTypeAlpine
)
type Package struct {
Name string
Version string
Description string
Creator PackageCreator
DownloadURL string
HomepageURL string
License []string
Files []string
CPEs []string
}
type PackageCreator struct {
Name string // TODO: split name and e-mail
Org string `json:",omitempty"`
}
type spdxStatement struct {
intoto.StatementHeader
Predicate json.RawMessage `json:"predicate"`
}
func (l *Loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *result, subject digest.Digest, refs []digest.Digest, img *Image) error {
ctx = remotes.WithMediaTypeKeyPrefix(ctx, "application/vnd.in-toto+json", "intoto")
for _, dgst := range refs {
mfst, ok := r.manifests[dgst]
if !ok {
return errors.Errorf("referenced image %s not found", dgst)
}
for _, layer := range mfst.manifest.Layers {
if layer.MediaType == "application/vnd.in-toto+json" && layer.Annotations["in-toto.io/predicate-type"] == "https://spdx.dev/Document" {
var stmt spdxStatement
_, err := remotes.FetchHandler(l.cache, fetcher)(ctx, layer)
if err != nil {
return err
}
dt, err := content.ReadBlob(ctx, l.cache, layer)
if err != nil {
return err
}
if err := json.Unmarshal(dt, &stmt); err != nil {
return err
}
if stmt.PredicateType != "https://spdx.dev/Document" {
return errors.Errorf("unexpected predicate type %s", stmt.PredicateType)
}
subjectValidated := false
for _, s := range stmt.Subject {
for alg, hash := range s.Digest {
if alg+":"+hash == subject.String() {
subjectValidated = true
break
}
}
}
if !subjectValidated {
return errors.Errorf("unable to validate subject %s, expected %s", stmt.Subject, subject.String())
}
doc, err := decodeSPDX(stmt.Predicate)
if err != nil {
return err
}
addSPDX(img, doc)
}
}
}
normalizeSBOM(img.SBOM)
return nil
}
func addSPDX(img *Image, doc *spdx.Document2_2) {
sbom := img.SBOM
if sbom == nil {
sbom = &SBOM{}
}
for _, p := range doc.Packages {
var files []string
for _, f := range p.Files {
if f == nil {
// HACK: the SPDX parser is broken with multiple files in hasFiles
continue
}
files = append(files, f.FileName)
}
pkg := Package{
Name: p.PackageName,
Version: p.PackageVersion,
Creator: PackageCreator{Name: p.PackageOriginatorPerson, Org: p.PackageOriginatorOrganization},
Description: p.PackageDescription,
HomepageURL: p.PackageHomePage,
DownloadURL: p.PackageDownloadLocation,
License: strings.Split(p.PackageLicenseConcluded, " AND "),
Files: files,
}
typ := pkgTypeUnknown
for _, ref := range p.PackageExternalReferences {
if ref.Category == "PACKAGE_MANAGER" && ref.RefType == "purl" {
if strings.HasPrefix(ref.Locator, "pkg:alpine/") {
typ = pkgTypeAlpine
}
}
if ref.Category == "SECURITY" && ref.RefType == "cpe23Type" {
pkg.CPEs = append(pkg.CPEs, ref.Locator)
}
}
switch typ {
case pkgTypeAlpine:
sbom.AlpinePackages = append(sbom.AlpinePackages, pkg)
default:
sbom.UnknownPackages = append(sbom.UnknownPackages, pkg)
}
}
img.SBOM = sbom
}
func normalizeSBOM(sbom *SBOM) {
if sbom == nil {
return
}
for _, pkgs := range [][]Package{sbom.AlpinePackages, sbom.UnknownPackages} {
// TODO: remote duplicates
sort.Slice(pkgs, func(i, j int) bool {
if pkgs[i].Name == pkgs[j].Name {
return pkgs[i].Version < pkgs[j].Version
}
return pkgs[i].Name < pkgs[j].Name
})
}
}
func decodeSPDX(dt []byte) (s *spdx.Document2_2, err error) {
defer func() {
// The spdx tools JSON parser is reported to be panicking sometimes
if v := recover(); v != nil {
s = nil
err = errors.Errorf("an error occurred during SPDX JSON document parsing: %+v", v)
}
}()
doc, err := jsonloader.Load2_2(bytes.NewReader(dt))
if err != nil {
return nil, errors.Wrap(err, "unable to decode spdx")
}
return doc, nil
}