generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 44
/
implementation.go
1058 lines (915 loc) · 30.9 KB
/
implementation.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2021 The Kubernetes 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 spdx
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
import (
"archive/tar"
"bufio"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
gitignore "github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/uuid"
"github.com/nozzle/throttler"
purl "github.com/package-url/packageurl-go"
"github.com/sirupsen/logrus"
"sigs.k8s.io/bom/pkg/license"
"sigs.k8s.io/bom/pkg/osinfo"
"sigs.k8s.io/release-utils/util"
)
//counterfeiter:generate . spdxImplementation
type spdxImplementation interface {
ExtractTarballTmp(string) (string, error)
ReadArchiveManifest(string) (*ArchiveManifest, error)
PullImagesToArchive(string, string) (*ImageReferenceInfo, error)
PackageFromImageTarball(*Options, string) (*Package, error)
PackageFromTarball(*Options, *TarballOptions, string) (*Package, error)
PackageFromDirectory(*Options, string) (*Package, error)
GetDirectoryTree(string) ([]string, error)
IgnorePatterns(string, []string, bool) ([]gitignore.Pattern, error)
ApplyIgnorePatterns([]string, []gitignore.Pattern) []string
GetGoDependencies(string, *Options) ([]*Package, error)
GetDirectoryLicense(*license.Reader, string, *Options) (*license.License, error)
LicenseReader(*Options) (*license.Reader, error)
ImageRefToPackage(string, *Options) (*Package, error)
AnalyzeImageLayer(string, *Package) error
}
type spdxDefaultImplementation struct{}
// ExtractTarballTmp extracts a tarball to a temporary directory
func (di *spdxDefaultImplementation) ExtractTarballTmp(tarPath string) (tmpDir string, err error) {
tmpDir, err = os.MkdirTemp(os.TempDir(), "spdx-tar-extract-")
if err != nil {
return tmpDir, fmt.Errorf("creating temporary directory for tar extraction: %w", err)
}
// Open the tar file
f, err := os.Open(tarPath)
if err != nil {
return tmpDir, fmt.Errorf("opening tarball: %w", err)
}
defer f.Close()
// Read the first bytes to determine if the file is compressed
var sample [3]byte
var gzipped bool
if _, err := io.ReadFull(f, sample[:]); err != nil {
return "", fmt.Errorf("sampling bytes from file header: %w", err)
}
if _, err := f.Seek(0, 0); err != nil {
return "", fmt.Errorf("rewinding read pointer: %w", err)
}
if sample[0] == 0x1f && sample[1] == 0x8b && sample[2] == 0x08 {
gzipped = true
}
var tr *tar.Reader
if gzipped {
gzipReader, err := gzip.NewReader(f)
if err != nil {
return "", err
}
tr = tar.NewReader(gzipReader)
} else {
tr = tar.NewReader(f)
}
numFiles := 0
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return tmpDir, fmt.Errorf("reading tarfile %s: %w", tarPath, err)
}
if hdr.FileInfo().IsDir() {
continue
}
if strings.HasPrefix(filepath.Base(hdr.FileInfo().Name()), ".wh") {
logrus.Info("Skipping extraction of whithout file")
continue
}
if err := os.MkdirAll(
filepath.Join(tmpDir, filepath.Dir(hdr.Name)), os.FileMode(0o755),
); err != nil {
return tmpDir, fmt.Errorf("creating image directory structure: %w", err)
}
targetFile, err := sanitizeExtractPath(tmpDir, hdr.Name)
if err != nil {
return tmpDir, err
}
f, err := os.Create(targetFile)
if err != nil {
return tmpDir, fmt.Errorf("creating image layer file: %w", err)
}
if _, err := io.CopyN(f, tr, hdr.Size); err != nil {
f.Close()
if err == io.EOF {
break
}
return tmpDir, fmt.Errorf("extracting image data: %w", err)
}
f.Close()
numFiles++
}
logrus.Infof("Successfully extracted %d files from image tarball %s", numFiles, tarPath)
return tmpDir, err
}
// fix gosec G305: File traversal when extracting zip/tar archive
// more context: https://snyk.io/research/zip-slip-vulnerability
func sanitizeExtractPath(tmpDir, filePath string) (string, error) {
destpath := filepath.Join(tmpDir, filePath)
if !strings.HasPrefix(destpath, filepath.Clean(tmpDir)+string(os.PathSeparator)) {
return "", fmt.Errorf("%s: illegal file path", filePath)
}
return destpath, nil
}
// readArchiveManifest extracts the manifest json from an image tar
// archive and returns the data as a struct
func (di *spdxDefaultImplementation) ReadArchiveManifest(manifestPath string) (manifest *ArchiveManifest, err error) {
// Check that we have the archive manifest.json file
if !util.Exists(manifestPath) {
return manifest, errors.New("unable to find manifest file " + manifestPath)
}
// Parse the json file
manifestData := []ArchiveManifest{}
manifestJSON, err := os.ReadFile(manifestPath)
if err != nil {
return manifest, fmt.Errorf("unable to read from tarfile: %w", err)
}
if err := json.Unmarshal(manifestJSON, &manifestData); err != nil {
fmt.Println(string(manifestJSON))
return manifest, fmt.Errorf("unmarshalling image manifest: %w", err)
}
return &manifestData[0], nil
}
// getImageReferences gets a reference string and returns all image
// references from it
func getImageReferences(referenceString string) (*ImageReferenceInfo, error) {
ref, err := name.ParseReference(referenceString)
if err != nil {
return nil, fmt.Errorf("parsing image reference %s: %w", referenceString, err)
}
descr, err := remote.Get(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return nil, fmt.Errorf("fetching remote descriptor: %w", err)
}
// If the reference points to an image, return it
if descr.MediaType.IsImage() {
return refInfoFromImage(descr)
} else if descr.MediaType.IsIndex() {
return refInfoFromIndex(descr)
}
return nil, fmt.Errorf("unable to recognize reference mediatype (%s)", string(descr.MediaType))
}
func refInfoFromIndex(descr *remote.Descriptor) (refinfo *ImageReferenceInfo, err error) {
refinfo = &ImageReferenceInfo{Images: []ImageReferenceInfo{}}
logrus.Infof("Reference %s points to an index", descr.Ref.String())
tag := descr.Ref.Context().Tag(descr.Ref.String())
if tag.String() == "" {
return nil, fmt.Errorf("cannot build tag from reference %s", descr.Ref.String())
}
// Get the image index
index, err := descr.ImageIndex()
if err != nil {
return nil, fmt.Errorf("getting image index for %s: %w", descr.Ref.String(), err)
}
indexDigest, err := index.Digest()
if err != nil {
return nil, fmt.Errorf("getting image index digest: %w", err)
}
// If we could not turn the reference to digest then we synthesize one
dig, err := fullDigest(tag, indexDigest)
if err != nil {
return nil, fmt.Errorf("building single image digest: %w", err)
}
refinfo.Digest = dig.String()
indexManifest, err := index.IndexManifest()
if err != nil {
return nil, fmt.Errorf("getting index manifest from %s: %w", descr.Ref.String(), err)
}
logrus.Infof("Reference image index points to %d manifests", len(indexManifest.Manifests))
refinfo.MediaType = string(indexManifest.MediaType)
// Add all the child images described in the index
// TODO: rangeValCopy: each iteration copies 136 bytes (consider pointers or indexing)
for _, manifest := range indexManifest.Manifests { //nolint: gocritic
archImgDigest, err := fullDigest(tag, manifest.Digest)
if err != nil {
return nil, fmt.Errorf("generating digest for image: %w", err)
}
arch, osid := "", ""
if manifest.Platform != nil {
arch = manifest.Platform.Architecture
osid = manifest.Platform.OS
}
logrus.Infof("Adding image %s (%s/%s)", archImgDigest, arch, osid)
refinfo.Images = append(refinfo.Images,
ImageReferenceInfo{
Digest: archImgDigest.String(),
MediaType: string(manifest.MediaType),
Arch: arch,
OS: osid,
})
}
return refinfo, nil
}
func refInfoFromImage(descr *remote.Descriptor) (refinfo *ImageReferenceInfo, err error) {
refinfo = &ImageReferenceInfo{}
logrus.Infof("Reference %s points to a single image", descr.Ref.String())
tag := descr.Ref.Context().Tag(descr.Ref.String())
if tag.String() == "" {
return nil, fmt.Errorf("cannot build tag from reference %s", descr.Ref.String())
}
// Check if we can get an image
im, err := descr.Image()
if err != nil {
return nil, fmt.Errorf("getting image from descriptor: %w", err)
}
imageDigest, err := im.Digest()
if err != nil {
return nil, fmt.Errorf("while calculating image digest: %w", err)
}
// If we could not turn the reference to digest then we synthesize one
dig, err := fullDigest(tag, imageDigest)
if err != nil {
return nil, fmt.Errorf("building single image digest: %w", err)
}
refinfo.Digest = dig.String()
mt, err := im.MediaType()
if err == nil {
refinfo.MediaType = string(mt)
}
// Get the platform data
conf, err := im.ConfigFile()
if err == nil {
refinfo.Arch = conf.Architecture
refinfo.OS = conf.OS
}
return refinfo, nil
}
// fullDigest builds a name.Digest with the registry info from tag
// and the value from hash
func fullDigest(tag name.Tag, hash v1.Hash) (name.Digest, error) {
dig, err := name.NewDigest(
fmt.Sprintf(
"%s/%s@%s:%s",
tag.RegistryStr(), tag.RepositoryStr(),
hash.Algorithm, hash.Hex,
),
)
if err != nil {
return name.Digest{}, fmt.Errorf("building digest: %w", err)
}
return dig, nil
}
func PullImageToArchive(referenceString, path string) error {
ref, err := name.ParseReference(referenceString)
if err != nil {
return fmt.Errorf("parsing reference %s: %w", referenceString, err)
}
// Get the image from the reference:
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return fmt.Errorf("getting image: %w", err)
}
if err := tarball.WriteToFile(path, ref, img); err != nil {
return fmt.Errorf("writing image to disk: %w", err)
}
return nil
}
// PullImagesToArchive takes an image reference (a tag or a digest)
// and writes it into a docker tar archive in path
func (di *spdxDefaultImplementation) PullImagesToArchive(
referenceString, path string,
) (references *ImageReferenceInfo, err error) {
// Get the image references from the index
references, err = getImageReferences(referenceString)
if err != nil {
return nil, err
}
if !util.Exists(path) {
if err := os.MkdirAll(path, os.FileMode(0o755)); err != nil {
return nil, fmt.Errorf("creating image directory: %w", err)
}
}
// If we do not have any child images we download the main reference
// as it is not an index
if len(references.Images) == 0 {
tarPath, err := createReferenceArchive(references.Digest, path)
if err != nil {
return nil, fmt.Errorf("downloading archive of image: %w", err)
}
references.Archive = tarPath
}
// Populate a new image reference set with the archive data
newrefs := *references
newrefs.Images = []ImageReferenceInfo{}
// Download 4 arches at once
t := throttler.New(4, len(references.Images))
mtx := sync.Mutex{}
for _, refData := range references.Images {
go func(r ImageReferenceInfo) {
tarPath, err := createReferenceArchive(r.Digest, path)
mtx.Lock()
r.Archive = tarPath
newrefs.Images = append(newrefs.Images, r)
mtx.Unlock()
t.Done(err)
}(refData)
t.Throttle()
}
if err := t.Err(); err != nil {
return nil, err
}
return &newrefs, nil
}
func createReferenceArchive(digest, path string) (tarPath string, err error) {
ref, err := name.ParseReference(digest)
if err != nil {
return "", fmt.Errorf("parsing reference %s: %w", digest, err)
}
d, ok := ref.(name.Digest)
if !ok {
return "", errors.New("reference is not a digest")
}
p := strings.Split(d.DigestStr(), ":")
if len(p) < 2 {
return "", fmt.Errorf("unable to parse digest string %s", d.DigestStr())
}
tarPath = filepath.Join(path, p[1]+".tar")
logrus.Debugf("Downloading %s from remote registry to %s", digest, tarPath)
// Download image from remote
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return "", fmt.Errorf("getting image from remote: %w", err)
}
// Write image to tar archive
if err := tarball.MultiWriteToFile(
tarPath, map[name.Tag]v1.Image{d.Repository.Tag(p[1]): img},
); err != nil {
return "", fmt.Errorf("writing image to disk: %w", err)
}
return tarPath, nil
}
// PackageFromTarball builds a SPDX package from the contents of a tarball
func (di *spdxDefaultImplementation) PackageFromTarball(
opts *Options, tarOpts *TarballOptions, tarFile string,
) (pkg *Package, err error) {
logrus.Infof("Generating SPDX package from tarball %s", tarFile)
if tarOpts.AddFiles {
// Estract the tarball
tmp, err := di.ExtractTarballTmp(tarFile)
if err != nil {
return nil, fmt.Errorf("extracting tarball to temporary archive: %w", err)
}
defer os.RemoveAll(tmp)
pkg, err = di.PackageFromDirectory(opts, tmp)
if err != nil {
return nil, fmt.Errorf("generating package from tar contents: %w", err)
}
} else {
pkg = NewPackage()
}
// Set the extract dir option. This makes the package to remove
// the tempdir prefix from the document paths:
pkg.Options().WorkDir = tarOpts.ExtractDir
if err := pkg.ReadSourceFile(tarFile); err != nil {
return nil, fmt.Errorf("reading source file %s: %w", tarFile, err)
}
// Build the ID and the filename from the tarball name
return pkg, nil
}
// GetDirectoryTree traverses a directory and return a slice of strings with all files
func (di *spdxDefaultImplementation) GetDirectoryTree(dirPath string) ([]string, error) {
fileList := []string{}
if err := fs.WalkDir(os.DirFS(dirPath), ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if d.Type() == os.ModeSymlink {
return nil
}
fileList = append(fileList, path)
return nil
}); err != nil {
return nil, fmt.Errorf("buiding directory tree: %w", err)
}
return fileList, nil
}
// IgnorePatterns return a list of gitignore patterns
func (di *spdxDefaultImplementation) IgnorePatterns(
dirPath string, extraPatterns []string, skipGitIgnore bool,
) ([]gitignore.Pattern, error) {
patterns := []gitignore.Pattern{}
for _, s := range extraPatterns {
patterns = append(patterns, gitignore.ParsePattern(s, nil))
}
if skipGitIgnore {
logrus.Debug("Not using patterns in .gitignore")
return patterns, nil
}
if util.Exists(filepath.Join(dirPath, gitIgnoreFile)) {
f, err := os.Open(filepath.Join(dirPath, gitIgnoreFile))
if err != nil {
return nil, fmt.Errorf("opening gitignore file: %w", err)
}
defer f.Close()
// When using .gitignore files, we alwas add the .git directory
// to match git's behavior
patterns = append(patterns, gitignore.ParsePattern(".git/", nil))
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s := scanner.Text()
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
logrus.Debugf("Loaded .gitignore pattern: >>%s<<", s)
patterns = append(patterns, gitignore.ParsePattern(s, nil))
}
}
}
logrus.Debugf(
"Loaded %d patterns from .gitignore (+ %d extra) at root of directory", len(patterns), len(extraPatterns),
)
return patterns, nil
}
// ApplyIgnorePatterns applies the gitignore patterns to a list of files, removing matched
func (di *spdxDefaultImplementation) ApplyIgnorePatterns(
fileList []string, patterns []gitignore.Pattern,
) (filteredList []string) {
logrus.Infof(
"Applying %d ignore patterns to list of %d filenames",
len(patterns), len(fileList),
)
// We will return a new file list
filteredList = []string{}
// Build the new gitignore matcher
matcher := gitignore.NewMatcher(patterns)
// Cycle all files, removing those matched:
for _, file := range fileList {
if matcher.Match(strings.Split(file, string(filepath.Separator)), false) {
logrus.Debugf("File ignored by .gitignore: %s", file)
} else {
filteredList = append(filteredList, file)
}
}
return filteredList
}
// GetGoDependencies opens a Go module and directory and returns the
// dependencies as SPDX packages.
func (di *spdxDefaultImplementation) GetGoDependencies(
path string, opts *Options,
) (spdxPackages []*Package, err error) {
// Open the directory as a go module:
mod, err := NewGoModuleFromPath(path)
if err != nil {
return nil, fmt.Errorf("creating a mod from the specified path: %w", err)
}
mod.Options().OnlyDirectDeps = opts.OnlyDirectDeps
mod.Options().ScanLicenses = opts.ScanLicenses
// Open the module
if err := mod.Open(); err != nil {
return nil, fmt.Errorf("opening new module path: %w", err)
}
defer func() {
preErr := err
err = mod.RemoveDownloads()
if preErr != nil {
err = preErr
}
}()
if opts.ScanLicenses {
if errScan := mod.ScanLicenses(); err != nil {
return nil, errScan
}
}
spdxPackages = []*Package{}
for _, goPkg := range mod.Packages {
spdxPkg, err := goPkg.ToSPDXPackage()
if err != nil {
// If a dependency cannot be converted, warn but do not die
logrus.Error(fmt.Errorf("converting go dependency to spdx package: %w", err))
continue
}
spdxPackages = append(spdxPackages, spdxPkg)
}
return spdxPackages, err
}
func (di *spdxDefaultImplementation) LicenseReader(spdxOpts *Options) (*license.Reader, error) {
opts := license.DefaultReaderOptions
opts.CacheDir = spdxOpts.LicenseCacheDir
opts.LicenseDir = spdxOpts.LicenseData
opts.LicenseListVersion = spdxOpts.LicenseListVersion
// Create the new reader
reader, err := license.NewReaderWithOptions(opts)
if err != nil {
return nil, fmt.Errorf("creating reusable license reader: %w", err)
}
return reader, nil
}
// GetDirectoryLicense takes a path and scans
// the files in it to determine licensins information
func (di *spdxDefaultImplementation) GetDirectoryLicense(
reader *license.Reader, path string, _ *Options,
) (*license.License, error) {
licenseResult, err := reader.ReadTopLicense(path)
if err != nil {
return nil, fmt.Errorf("getting directory license: %w", err)
}
if licenseResult == nil {
logrus.Warnf("License classifier could not find a license for directory: %v", err)
return nil, nil
}
return licenseResult.License, nil
}
// purlFromImage builds a purl from an image reference
func (*spdxDefaultImplementation) purlFromImage(img *ImageReferenceInfo) string {
// OCI type urls don't have a namespace ref:
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#oci
imageReference, err := name.ParseReference(img.Digest)
if err != nil {
logrus.Error(err)
return ""
}
digest := ""
// If we have the digest, skip checking it from the registry
if _, ok := imageReference.(name.Digest); ok {
p := strings.Split(imageReference.(name.Digest).String(), "@")
if len(p) < 2 {
return ""
}
digest = p[1]
} else {
digest, err = crane.Digest(img.Reference)
if err != nil {
logrus.Error(err)
return ""
}
}
url := imageReference.Context().Name()
parts := strings.Split(url, "/")
if len(parts) < 2 {
return ""
}
imageName := parts[len(parts)-1]
url = strings.TrimSuffix(url, "/"+imageName)
// Add the purl qualifgiers:
mm := map[string]string{
"repository_url": url,
}
if img.Arch != "" {
mm["arch"] = img.Arch
}
if img.OS != "" {
mm["os"] = img.OS
}
if tag, ok := imageReference.(name.Tag); ok {
mm["tag"] = tag.String()
}
if img.MediaType != "" {
mm["mediaType"] = img.MediaType
}
packageurl := purl.NewPackageURL(
purl.TypeOCI, "", imageName, digest,
purl.QualifiersFromMap(mm), "",
)
return packageurl.String()
}
// ImageRefToPackage Returns a spdx package from an OCI image reference
func (di *spdxDefaultImplementation) ImageRefToPackage(ref string, opts *Options) (*Package, error) {
tmpdir, err := os.MkdirTemp("", "doc-build-")
if err != nil {
return nil, fmt.Errorf("creating temporary workdir in: %w", err)
}
defer os.RemoveAll(tmpdir)
references, err := di.PullImagesToArchive(ref, tmpdir)
if err != nil {
return nil, fmt.Errorf("while downloading images to archive: %w", err)
}
topDigest, err := name.NewDigest(references.Digest)
if err != nil {
return nil, fmt.Errorf("parsing digest %s: %w", references.Digest, err)
}
logrus.Debugf("Reference %s produced %+v", ref, references)
// If we just got one image and that image is exactly the same
// reference, return a single package:
if len(references.Images) == 0 {
logrus.Infof("Generating single image package for %s", ref)
p, err := di.referenceInfoToPackage(opts, references)
if err != nil {
return nil, fmt.Errorf("generating image package: %w", err)
}
// Rebuild the ID to compose it with the parent element
p.Name = topDigest.DigestStr()
p.BuildID(p.Name)
return p, nil
}
// Create the package representing the image tag:
logrus.Infof("Generating SBOM for multiarch image %s", references.Digest)
pkg := &Package{}
pkg.Name = topDigest.DigestStr()
pkg.BuildID(pkg.Name)
if references.Digest != "" {
pkg.DownloadLocation = references.Digest
}
// Now, cycle each image in the index and generate a package from it
for i := range references.Images {
subpkg, err := di.referenceInfoToPackage(opts, &references.Images[i])
if err != nil {
return nil, fmt.Errorf("generating image package")
}
// Rebuild the ID to compose it with the parent element
subpkg.BuildID(pkg.Name, subpkg.Name)
// Add the package to the image
pkg.AddRelationship(&Relationship{
Peer: subpkg,
Type: CONTAINS,
FullRender: true,
Comment: "Container image lager",
})
// And add an inverse relationship to the index
subpkg.AddRelationship(&Relationship{
Peer: pkg,
Type: VARIANT_OF,
Comment: "Image index",
})
}
// Add a the topmost package purl
packageurl := di.purlFromImage(references)
if packageurl != "" {
pkg.ExternalRefs = append(pkg.ExternalRefs, ExternalRef{
Category: CatPackageManager,
Type: "purl",
Locator: packageurl,
})
}
return pkg, nil
}
func (di *spdxDefaultImplementation) referenceInfoToPackage(opts *Options, img *ImageReferenceInfo) (*Package, error) {
subpkg, err := di.PackageFromImageTarball(opts, img.Archive)
if err != nil {
return nil, fmt.Errorf("adding image variant package: %w", err)
}
imageDigest, err := name.NewDigest(img.Digest)
if err != nil {
return nil, fmt.Errorf("parsing digest %s: %w", img.Digest, err)
}
subpkg.Name = imageDigest.DigestStr()
subpkg.Checksum = map[string]string{
"SHA256": strings.TrimPrefix(imageDigest.DigestStr(), "sha256:"),
}
subpkg.FileName = ""
packageurl := di.purlFromImage(img)
if packageurl != "" {
subpkg.ExternalRefs = append(subpkg.ExternalRefs, ExternalRef{
Category: CatPackageManager,
Type: "purl",
Locator: packageurl,
})
}
return subpkg, nil
}
// PackageFromImageTarball reads an OCI image archive and produces a SPDX
// packafe describing its layers
func (di *spdxDefaultImplementation) PackageFromImageTarball(
spdxOpts *Options, tarPath string,
) (imagePackage *Package, err error) {
logrus.Infof("Generating SPDX package from image tarball %s", tarPath)
if tarPath == "" {
return nil, errors.New("tar path empty")
}
// Extract all files from tarfile
tarOpts := &TarballOptions{}
// If specified, add individual files from the tarball to the
// spdx package, unless AnalyzeLayers is set because in that
// case the individual analyzers decide to do that.
if spdxOpts.AddTarFiles && !spdxOpts.AnalyzeLayers {
tarOpts.AddFiles = true
}
tarOpts.ExtractDir, err = di.ExtractTarballTmp(tarPath)
if err != nil {
return nil, fmt.Errorf("extracting tarball to temp dir: %w", err)
}
defer os.RemoveAll(tarOpts.ExtractDir)
// Read the archive manifest json:
manifest, err := di.ReadArchiveManifest(
filepath.Join(tarOpts.ExtractDir, archiveManifestFilename),
)
if err != nil {
return nil, fmt.Errorf("while reading docker archive manifest: %w", err)
}
if len(manifest.RepoTags) == 0 {
return nil, errors.New("no RepoTags found in manifest")
}
if manifest.RepoTags[0] == "" {
return nil, errors.New(
"unable to add tar archive, manifest does not have a RepoTags entry",
)
}
logrus.Infof("Package describes image %s", manifest.RepoTags[0])
// Create the new SPDX package
imagePackage, err = di.PackageFromTarball(spdxOpts, tarOpts, tarPath)
if err != nil {
return nil, fmt.Errorf("generating package from tar archive: %w", err)
}
imagePackage.Options().WorkDir = tarOpts.ExtractDir
imagePackage.Name = filepath.Base(tarPath)
imagePackage.BuildID(manifest.RepoTags[0])
imagePackage.Comment = "Container image archive"
logrus.Infof("Image manifest lists %d layers", len(manifest.LayerFiles))
// Scan the container layers for OS information:
ct := osinfo.ContainerScanner{}
var osPackageData *[]osinfo.PackageDBEntry
var layerNum int
layerPaths := []string{}
for _, layerFile := range manifest.LayerFiles {
layerPaths = append(layerPaths, filepath.Join(tarOpts.ExtractDir, layerFile))
}
// Scan for package data if option is set
if spdxOpts.ScanImages {
layerNum, osPackageData, err = ct.ReadOSPackages(layerPaths)
if err != nil {
return nil, fmt.Errorf("getting os data from container: %w", err)
}
}
if osPackageData != nil {
logrus.Infof(
"Scan of container image returned %d OS packages in layer #%d",
len(*osPackageData), layerNum,
)
}
// Cycle all the layers from the manifest and add them as packages
for i, layerFile := range manifest.LayerFiles {
// Generate a package from a layer
pkg, err := di.PackageFromTarball(spdxOpts, tarOpts, filepath.Join(tarOpts.ExtractDir, layerFile))
if err != nil {
return nil, fmt.Errorf("building package from layer: %w", err)
}
pkg.Name = "sha256:" + pkg.Checksum["SHA256"]
pkg.Comment = "Container image layer from archive"
// Regenerate the BuildID to avoid clashes when handling multiple
// images at the same time.
pkg.BuildID(manifest.RepoTags[0], pkg.Name)
// If the option is enabled, scan the container layers
if spdxOpts.AnalyzeLayers {
if err := di.AnalyzeImageLayer(filepath.Join(tarOpts.ExtractDir, layerFile), pkg); err != nil {
return nil, fmt.Errorf("scanning layer "+pkg.ID+" :%w", err)
}
} else {
logrus.Info("Not performing deep image analysis (opts.AnalyzeLayers = false)")
}
// If we got the OS data from the scanner, add the packages:
if i == layerNum && osPackageData != nil {
for i := range *osPackageData {
ospk := NewPackage()
ospk.Name = (*osPackageData)[i].Package
ospk.Version = (*osPackageData)[i].Version
ospk.HomePage = (*osPackageData)[i].HomePage
ospk.Originator = struct {
Person string
Organization string
}{
Person: (*osPackageData)[i].MaintainerName,
}
if (*osPackageData)[i].License != "" {
ospk.LicenseDeclared = (*osPackageData)[i].License
}
ospk.Checksum = (*osPackageData)[i].Checksums
if (*osPackageData)[i].MaintainerName != "" {
ospk.Supplier.Person = (*osPackageData)[i].MaintainerName
if (*osPackageData)[i].MaintainerEmail != "" {
ospk.Supplier.Person += fmt.Sprintf(" (%s)", (*osPackageData)[i].MaintainerEmail)
}
}
if (*osPackageData)[i].PackageURL() != "" {
ospk.ExternalRefs = append(ospk.ExternalRefs, ExternalRef{
Category: CatPackageManager,
Type: "purl",
Locator: (*osPackageData)[i].PackageURL(),
})
}
ospk.BuildID(pkg.ID)
if err := pkg.AddPackage(ospk); err != nil {
return nil, fmt.Errorf("adding OS package to container layer: %w", err)
}
}
}
// Add the layer package to the image package
if err := imagePackage.AddPackage(pkg); err != nil {
return nil, fmt.Errorf("adding layer to image package: %w", err)
}
}
// return the finished package
return imagePackage, nil
}
func (di *spdxDefaultImplementation) AnalyzeImageLayer(layerPath string, pkg *Package) error {
return NewImageAnalyzer().AnalyzeLayer(layerPath, pkg)
}
// PackageFromDirectory scans a directory and returns its contents as a
// SPDX package, optionally determining the licenses found
func (di *spdxDefaultImplementation) PackageFromDirectory(opts *Options, dirPath string) (pkg *Package, err error) {
dirPath, err = filepath.Abs(dirPath)
if err != nil {
return nil, fmt.Errorf("getting absolute directory path: %w", err)
}
fileList, err := di.GetDirectoryTree(dirPath)
if err != nil {
return nil, fmt.Errorf("building directory tree: %w", err)
}
reader, err := di.LicenseReader(opts)
if err != nil {
return nil, fmt.Errorf("creating license reader: %w", err)
}
licenseTag := ""
lic, err := di.GetDirectoryLicense(reader, dirPath, opts)
if err != nil {
return nil, fmt.Errorf("scanning directory for licenses: %w", err)
}
if lic != nil {
licenseTag = lic.LicenseID
}
// Build a list of patterns from those found in the .gitignore file and
// posssibly others passed in the options:
patterns, err := di.IgnorePatterns(
dirPath, opts.IgnorePatterns, opts.NoGitignore,
)
if err != nil {
return nil, fmt.Errorf("building ignore patterns list: %w", err)
}
// Apply the ignore patterns to the list of files
fileList = di.ApplyIgnorePatterns(fileList, patterns)
if len(fileList) == 0 {
return nil, fmt.Errorf("directory %s has no files to scan", dirPath)
}
logrus.Infof("Scanning %d files and adding them to the SPDX package", len(fileList))
pkg = NewPackage()
pkg.FilesAnalyzed = true