-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathspdx.go
670 lines (595 loc) · 19.9 KB
/
spdx.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright 2022, 2023 Chainguard, Inc.
//
// 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 spdx
import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"unicode/utf8"
apkfs "github.com/chainguard-dev/go-apk/pkg/fs"
purl "github.com/package-url/packageurl-go"
"gitlab.alpinelinux.org/alpine/go/pkg/repository"
"sigs.k8s.io/release-utils/version"
"chainguard.dev/apko/pkg/sbom/options"
)
// https://spdx.github.io/spdx-spec/3-package-information/#32-package-spdx-identifier
var validIDCharsRe = regexp.MustCompile(`[^a-zA-Z0-9-.]+`)
const (
NOASSERTION = "NOASSERTION"
ExtRefPackageManager = "PACKAGE-MANAGER"
ExtRefTypePurl = "purl"
apkSBOMdir = "/var/lib/db/sbom"
)
type SPDX struct {
fs apkfs.FullFS
}
func New(fs apkfs.FullFS) SPDX {
return SPDX{fs}
}
func (sx *SPDX) Key() string {
return "spdx"
}
func (sx *SPDX) Ext() string {
return "spdx.json"
}
func stringToIdentifier(in string) (out string) {
in = strings.ReplaceAll(in, ":", "-")
return validIDCharsRe.ReplaceAllStringFunc(in, func(s string) string {
r := ""
for i := 0; i < len(s); i++ {
uc, _ := utf8.DecodeRuneInString(string(s[i]))
r = fmt.Sprintf("%sC%d", r, uc)
}
return r
})
}
// Generate writes an SPDX SBOM in path
func (sx *SPDX) Generate(opts *options.Options, path string) error {
// The default document name makes no attempt to avoid
// clashes. Ensuring a unique name requires a digest
documentName := "sbom"
if opts.ImageInfo.LayerDigest != "" {
documentName += "-" + opts.ImageInfo.LayerDigest
}
doc := &Document{
ID: "SPDXRef-DOCUMENT",
Name: documentName,
Version: "SPDX-2.3",
CreationInfo: CreationInfo{
Created: opts.ImageInfo.SourceDateEpoch.Format(time.RFC3339),
Creators: []string{
fmt.Sprintf("Tool: apko (%s)", version.GetVersionInfo().GitVersion),
"Organization: Chainguard, Inc",
},
LicenseListVersion: "3.16",
},
DataLicense: "CC0-1.0",
Namespace: "https://spdx.org/spdxdocs/apko/",
Packages: []Package{},
Files: []File{},
Relationships: []Relationship{},
}
var imagePackage *Package
layerPackage := sx.layerPackage(opts)
doc.DocumentDescribes = []string{layerPackage.ID}
if opts.ImageInfo.ImageDigest != "" {
imagePackage = sx.imagePackage(opts)
doc.DocumentDescribes = []string{imagePackage.ID}
doc.Packages = append(doc.Packages, *imagePackage)
// Add to the relationships list
doc.Relationships = append(doc.Relationships, Relationship{
Element: imagePackage.ID,
Type: "CONTAINS",
Related: layerPackage.ID,
})
}
if opts.ImageInfo.VCSUrl != "" {
if opts.ImageInfo.ImageDigest != "" {
addSourcePackage(opts.ImageInfo.VCSUrl, doc, imagePackage)
} else {
addSourcePackage(opts.ImageInfo.VCSUrl, doc, layerPackage)
}
}
doc.Packages = append(doc.Packages, *layerPackage)
for _, pkg := range opts.Packages {
// add the package
p := sx.apkPackage(opts, pkg)
// Add the layer to the ID to avoid clashes
p.ID = stringToIdentifier(fmt.Sprintf(
"SPDXRef-Package-%s-%s-%s", layerPackage.ID, pkg.Name, pkg.Version,
))
doc.Packages = append(doc.Packages, p)
// Add to the relationships list
doc.Relationships = append(doc.Relationships, Relationship{
Element: layerPackage.ID,
Type: "CONTAINS",
Related: p.ID,
})
// Check to see if the apk contains an sbom describing itself
if err := sx.ProcessInternalApkSBOM(opts, doc, &p); err != nil {
return fmt.Errorf("parsing internal apk SBOM: %w", err)
}
}
if err := renderDoc(doc, path); err != nil {
return fmt.Errorf("rendering document: %w", err)
}
return nil
}
// replacePackage replaces a package with ID originalID with newID
func replacePackage(doc *Document, originalID, newID string) {
// First check if package is described at the top of the SBOM
for i := range doc.DocumentDescribes {
if doc.DocumentDescribes[i] == originalID {
doc.DocumentDescribes[i] = newID
break
}
}
// Now, look at all relationships and replace
for i := range doc.Relationships {
if doc.Relationships[i].Element == originalID {
doc.Relationships[i].Element = newID
}
if doc.Relationships[i].Related == originalID {
doc.Relationships[i].Related = newID
}
}
// Remove the old ID from the package list
newPackages := []Package{}
replaced := false
for _, r := range doc.Packages {
if r.ID != originalID {
newPackages = append(newPackages, r)
replaced = true
}
}
if replaced {
doc.Packages = newPackages
}
}
// locateApkSBOM returns the SBOM
func locateApkSBOM(fsys apkfs.FullFS, p *Package) (string, error) {
re := regexp.MustCompile(`-r\d+$`)
for _, s := range []string{
fmt.Sprintf("%s/%s-%s.spdx.json", apkSBOMdir, p.Name, p.Version),
fmt.Sprintf("%s/%s-%s.spdx.json", apkSBOMdir, p.Name, re.ReplaceAllString(p.Version, "")),
fmt.Sprintf("%s/%s.spdx.json", apkSBOMdir, p.Name),
} {
info, err := fsys.Stat(s)
if err != nil {
if os.IsNotExist(err) {
continue
}
}
if info.IsDir() {
return "", fmt.Errorf("directory found at SBOM path %s", s)
}
return s, nil
}
return "", nil
}
func (sx *SPDX) ProcessInternalApkSBOM(opts *options.Options, doc *Document, p *Package) error {
// Check if apk installed an SBOM
path, err := locateApkSBOM(sx.fs, p)
if err != nil {
return fmt.Errorf("inspecting FS for internal apk SBOM: %w", err)
}
if path == "" {
return nil
}
// TODO: Logf("composing packages from %s into image SBOM", path)
internalDoc, err := sx.ParseInternalSBOM(opts, path)
if err != nil {
// TODO: Log error parsing apk SBOM
return nil
}
targetElementIDs := []string{}
// Cycle the top level elements...
for _, elementID := range internalDoc.DocumentDescribes {
// ... searching for a 1st level package
for _, pkg := range internalDoc.Packages {
// that matches the name
if pkg.ID == elementID && p.Name == pkg.Name {
targetElementIDs = append(targetElementIDs, pkg.ID)
// TODO: Logf("Found package %s describing %s", pkg.ID, p.Name)
}
}
// Copy the targetElementIDs
copiedElements := &map[string]struct{}{}
for _, id := range targetElementIDs {
if err := copySBOMElement(id, internalDoc, doc, copiedElements); err != nil {
return fmt.Errorf("copying element: %w", err)
}
// Search for a package in the new SBOM describing the same thing
for _, pkg := range doc.Packages {
// TODO: Think if we need to match version too
if pkg.Name == p.Name {
replacePackage(doc, pkg.ID, id)
break
}
}
}
}
return nil
}
func copySBOMElement(spdxid string, sourceDoc, targetDoc *Document, copiedElements *map[string]struct{}) error {
if _, ok := (*copiedElements)[spdxid]; ok {
return nil
}
// TODO: Logf(" Copying SBOM element %s to targetSBOM", spdxid)
// Check if we're dealing with a package
copied := false
for _, p := range sourceDoc.Packages {
if p.ID == spdxid {
targetDoc.Packages = append(targetDoc.Packages, p)
copied = true
break
}
}
if !copied {
for _, f := range sourceDoc.Files {
if f.ID == spdxid {
targetDoc.Files = append(targetDoc.Files, f)
copied = true
break
}
}
}
if !copied {
return fmt.Errorf("unable to find element %s in source document", spdxid)
}
(*copiedElements)[spdxid] = struct{}{}
// Now tranfer all related elements.
for _, r := range sourceDoc.Relationships {
if r.Element == spdxid {
if err := copySBOMElement(r.Related, sourceDoc, targetDoc, copiedElements); err != nil {
return fmt.Errorf("copying element to target SBOM: %w", err)
}
// If successful add the relationships to the target doc
targetDoc.Relationships = append(targetDoc.Relationships, r)
}
}
return nil
}
// ParseInternalSBOM opens an SBOM inside apks and
func (sx *SPDX) ParseInternalSBOM(opts *options.Options, path string) (*Document, error) {
internalSBOM := &Document{}
data, err := sx.fs.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("opening sbom file %s: %w", path, err)
}
if err := json.Unmarshal(data, internalSBOM); err != nil {
return nil, fmt.Errorf("parsing internal apk sbom: %w", err)
}
return internalSBOM, nil
}
// renderDoc marshals a document to json and writes it to disk
func renderDoc(doc *Document, path string) error {
out, err := os.Create(path)
if err != nil {
return fmt.Errorf("opening SBOM path %s for writing: %w", path, err)
}
defer out.Close()
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
enc.SetEscapeHTML(true)
if err := enc.Encode(doc); err != nil {
return fmt.Errorf("encoding spdx sbom: %w", err)
}
return nil
}
func (sx *SPDX) imagePackage(opts *options.Options) (p *Package) {
return &Package{
ID: stringToIdentifier(fmt.Sprintf(
"SPDXRef-Package-%s", opts.ImageInfo.ImageDigest,
)),
Name: opts.ImageInfo.ImageDigest,
DownloadLocation: NOASSERTION,
PrimaryPurpose: "CONTAINER",
FilesAnalyzed: false,
Description: "apko container image",
Checksums: []Checksum{
{
Algorithm: "SHA256",
Value: strings.TrimPrefix(opts.ImageInfo.ImageDigest, "sha256:"),
},
},
ExternalRefs: []ExternalRef{
{
Category: ExtRefPackageManager,
Type: ExtRefTypePurl,
Locator: purl.NewPackageURL(
purl.TypeOCI, "", opts.ImagePurlName(), opts.ImageInfo.ImageDigest,
nil, "",
).String() + "?" + opts.ImagePurlQualifiers().String(),
},
},
}
}
// apkPackage returns a SPDX package describing an apk
func (sx *SPDX) apkPackage(opts *options.Options, pkg *repository.Package) Package {
return Package{
ID: stringToIdentifier(fmt.Sprintf(
"SPDXRef-Package-%s-%s", pkg.Name, pkg.Version,
)),
Name: pkg.Name,
Version: pkg.Version,
FilesAnalyzed: false,
LicenseConcluded: pkg.License,
Description: pkg.Description,
DownloadLocation: pkg.URL,
Originator: fmt.Sprintf("Person: %s", pkg.Maintainer),
SourceInfo: "Package info from apk database",
Checksums: []Checksum{
{
Algorithm: "SHA1",
Value: fmt.Sprintf("%x", pkg.Checksum),
},
},
ExternalRefs: []ExternalRef{
{
Category: ExtRefPackageManager,
Locator: purl.NewPackageURL(
"apk", opts.OS.ID, pkg.Name, pkg.Version,
purl.QualifiersFromMap(
map[string]string{"arch": opts.ImageInfo.Arch.ToAPK()},
), "").String(),
Type: ExtRefTypePurl,
},
},
}
}
// LayerPackage returns a package describing the layer
func (sx *SPDX) layerPackage(opts *options.Options) *Package {
layerPackageName := opts.ImageInfo.LayerDigest
mainPkgID := stringToIdentifier(layerPackageName)
return &Package{
ID: fmt.Sprintf("SPDXRef-Package-%s", mainPkgID),
Name: layerPackageName,
Version: opts.OS.Version,
FilesAnalyzed: false,
Description: "apko operating system layer",
DownloadLocation: NOASSERTION,
Originator: "",
Checksums: []Checksum{},
ExternalRefs: []ExternalRef{
{
Category: ExtRefPackageManager,
Type: ExtRefTypePurl,
Locator: purl.NewPackageURL(
purl.TypeOCI, "", opts.ImagePurlName(), opts.ImageInfo.LayerDigest,
nil, "",
).String() + "?" + opts.LayerPurlQualifiers().String(),
},
},
}
}
type Document struct {
ID string `json:"SPDXID"`
Name string `json:"name"`
Version string `json:"spdxVersion"`
CreationInfo CreationInfo `json:"creationInfo"`
DataLicense string `json:"dataLicense"`
Namespace string `json:"documentNamespace"`
DocumentDescribes []string `json:"documentDescribes"`
Files []File `json:"files,omitempty"`
Packages []Package `json:"packages"`
Relationships []Relationship `json:"relationships"`
ExternalDocumentRefs []ExternalDocumentRef `json:"externalDocumentRefs,omitempty"`
}
type ExternalDocumentRef struct {
Checksum Checksum `json:"checksum"`
ExternalDocumentID string `json:"externalDocumentId"`
SPDXDocument string `json:"spdxDocument"`
}
type CreationInfo struct {
Created string `json:"created"` // Date
Creators []string `json:"creators"`
LicenseListVersion string `json:"licenseListVersion"`
}
type File struct {
ID string `json:"SPDXID"`
Name string `json:"fileName"`
CopyrightText string `json:"copyrightText,omitempty"`
NoticeText string `json:"noticeText,omitempty"`
LicenseConcluded string `json:"licenseConcluded,omitempty"`
Description string `json:"description,omitempty"`
FileTypes []string `json:"fileTypes,omitempty"`
LicenseInfoInFile []string `json:"licenseInfoInFiles,omitempty"` // List of licenses
Checksums []Checksum `json:"checksums,omitempty"`
}
type Package struct {
ID string `json:"SPDXID"`
Name string `json:"name"`
Version string `json:"versionInfo,omitempty"`
FilesAnalyzed bool `json:"filesAnalyzed"`
HasFiles []string `json:"hasFiles,omitempty"`
LicenseInfoFromFiles []string `json:"licenseInfoFromFiles,omitempty"`
LicenseConcluded string `json:"licenseConcluded,omitempty"`
LicenseDeclared string `json:"licenseDeclared,omitempty"`
Description string `json:"description,omitempty"`
DownloadLocation string `json:"downloadLocation,omitempty"`
Originator string `json:"originator,omitempty"`
SourceInfo string `json:"sourceInfo,omitempty"`
CopyrightText string `json:"copyrightText,omitempty"`
PrimaryPurpose string `json:"primaryPackagePurpose,omitempty"`
Checksums []Checksum `json:"checksums,omitempty"`
ExternalRefs []ExternalRef `json:"externalRefs,omitempty"`
VerificationCode *PackageVerificationCode `json:"packageVerificationCode,omitempty"`
}
type PackageVerificationCode struct {
Value string `json:"packageVerificationCodeValue,omitempty"`
ExcludedFiles []string `json:"packageVerificationCodeExcludedFiles,omitempty"`
}
type Checksum struct {
Algorithm string `json:"algorithm"`
Value string `json:"checksumValue"`
}
type ExternalRef struct {
Category string `json:"referenceCategory"`
Locator string `json:"referenceLocator"`
Type string `json:"referenceType"`
}
type Relationship struct {
Element string `json:"spdxElementId"`
Type string `json:"relationshipType"`
Related string `json:"relatedSpdxElement"`
}
func (sx *SPDX) GenerateIndex(opts *options.Options, path string) error {
if opts.ImageInfo.Images == nil || len(opts.ImageInfo.Images) == 0 {
return errors.New("unable to render index sbom, no architecture images found")
}
documentName := "sbom"
if opts.ImageInfo.IndexDigest.DeepCopy().String() != "" {
documentName = "sbom-" + opts.ImageInfo.IndexDigest.DeepCopy().String()
}
doc := &Document{
ID: "SPDXRef-DOCUMENT",
Name: documentName,
Version: "SPDX-2.3",
CreationInfo: CreationInfo{
Created: opts.ImageInfo.SourceDateEpoch.Format(time.RFC3339),
Creators: []string{
fmt.Sprintf("Tool: apko (%s)", version.GetVersionInfo().GitVersion),
"Organization: Chainguard, Inc",
},
LicenseListVersion: "3.16",
},
DataLicense: "CC0-1.0",
Namespace: "https://spdx.org/spdxdocs/apko/",
Packages: []Package{},
Relationships: []Relationship{},
}
// Create the index package
indexPackage := Package{
ID: "SPDXRef-Package-" + stringToIdentifier(opts.ImageInfo.IndexDigest.DeepCopy().String()),
Name: opts.ImageInfo.IndexDigest.DeepCopy().String(),
FilesAnalyzed: false,
Description: "Multi-arch image index",
SourceInfo: "Generated at image build time by apko",
DownloadLocation: NOASSERTION,
PrimaryPurpose: "CONTAINER",
Checksums: []Checksum{
{
Algorithm: "SHA256",
Value: opts.ImageInfo.IndexDigest.DeepCopy().Hex,
},
},
ExternalRefs: []ExternalRef{
{
Category: ExtRefPackageManager,
Type: ExtRefTypePurl,
Locator: purl.NewPackageURL(
purl.TypeOCI, "", opts.IndexPurlName(), opts.ImageInfo.IndexDigest.DeepCopy().String(),
nil, "",
).String() + "?" + opts.IndexPurlQualifiers().String(),
},
},
}
doc.Packages = append(doc.Packages, indexPackage)
doc.DocumentDescribes = append(doc.DocumentDescribes, indexPackage.ID)
for i, info := range opts.ImageInfo.Images {
imagePackageID := "SPDXRef-Package-" + stringToIdentifier(info.Digest.DeepCopy().String())
doc.Packages = append(doc.Packages, Package{
ID: imagePackageID,
Name: fmt.Sprintf("sha256:%s", info.Digest.DeepCopy().Hex),
FilesAnalyzed: false,
DownloadLocation: NOASSERTION,
PrimaryPurpose: "CONTAINER",
Checksums: []Checksum{
{
Algorithm: "SHA256",
Value: info.Digest.DeepCopy().Hex,
},
},
ExternalRefs: []ExternalRef{
{
Category: ExtRefPackageManager,
Type: ExtRefTypePurl,
Locator: purl.NewPackageURL(
purl.TypeOCI, "", opts.ImagePurlName(), info.Digest.DeepCopy().String(),
nil, "",
).String() + "?" + opts.ArchImagePurlQualifiers(&opts.ImageInfo.Images[i]).String(),
},
},
})
doc.Relationships = append(doc.Relationships, Relationship{
Element: stringToIdentifier(indexPackage.ID),
Type: "VARIANT_OF",
Related: imagePackageID,
})
}
addSourcePackage(opts.ImageInfo.VCSUrl, doc, &indexPackage)
if err := renderDoc(doc, path); err != nil {
return fmt.Errorf("rendering document: %w", err)
}
return nil
}
// addSourcePackage creates a package describing the source code
func addSourcePackage(vcsURL string, doc *Document, parent *Package) {
version := ""
checksums := []Checksum{}
packageName := vcsURL
if url, commitHash, found := strings.Cut(vcsURL, "@"); found {
checksums = append(checksums, Checksum{
Algorithm: "SHA1",
Value: commitHash,
})
version = commitHash
packageName = url
}
// Trim the schemas from the url for the package name
packageName = strings.TrimPrefix(packageName, "git+ssh://")
packageName = strings.TrimPrefix(packageName, "git://")
packageName = strings.TrimPrefix(packageName, "https://")
sourcePackage := Package{
ID: fmt.Sprintf("SPDXRef-Package-%s", stringToIdentifier(vcsURL)),
Name: packageName,
Version: version,
FilesAnalyzed: false,
HasFiles: []string{},
LicenseInfoFromFiles: []string{},
PrimaryPurpose: "SOURCE",
Description: "Image configuration source",
DownloadLocation: vcsURL,
Checksums: checksums,
ExternalRefs: []ExternalRef{},
}
// If this is a github package, add a purl to it:
if strings.HasPrefix(packageName, "github.com/") {
slug := strings.TrimPrefix(packageName, "github.com/")
org, user, ok := strings.Cut(slug, "/")
if ok {
sourcePackage.ExternalRefs = []ExternalRef{
{
Category: ExtRefPackageManager,
Type: ExtRefTypePurl,
Locator: purl.NewPackageURL(
purl.TypeGithub, org, strings.TrimSuffix(user, ".git"), version,
nil, "",
).String(),
},
}
}
}
doc.Packages = append(doc.Packages, sourcePackage)
doc.Relationships = append(doc.Relationships, Relationship{
Element: parent.ID,
Type: "GENERATED_FROM",
Related: sourcePackage.ID,
})
}