1
1
package leeway
2
2
3
3
import (
4
- "archive/tar"
5
4
"bytes"
6
- "compress/gzip"
7
5
"context"
8
- "errors"
9
6
"fmt"
10
- "io"
11
7
"os"
12
8
"path/filepath"
13
9
"strings"
14
10
11
+ "slices"
12
+
15
13
"github.com/anchore/clio"
16
14
"github.com/anchore/grype/grype"
17
15
"github.com/anchore/grype/grype/db/v6/distribution"
@@ -34,7 +32,6 @@ import (
34
32
"github.com/anchore/syft/syft/source"
35
33
log "github.com/sirupsen/logrus"
36
34
"golang.org/x/xerrors"
37
- "slices"
38
35
)
39
36
40
37
// IgnoreRulePackage is an alias for match.IgnoreRulePackage
@@ -455,69 +452,3 @@ func loadVulnerabilityDB(p *Package, buildctx *buildContext) (vulnerability.Prov
455
452
456
453
return provider , status , nil
457
454
}
458
-
459
- // ErrNoSBOM is returned when no SBOM is found in a cached archive
460
- var ErrNoSBOM = xerrors .Errorf ("no SBOM found" )
461
-
462
- // AccessSBOMInCachedArchive provides access to the SBOM in a cached build artifact.
463
- // If no such SBOM exists, ErrNoSBOM is returned.
464
- func AccessSBOMInCachedArchive (fn string , handler func (sbom io.Reader ) error ) (err error ) {
465
- defer func () {
466
- if err != nil && ! errors .Is (err , ErrNoSBOM ) {
467
- err = xerrors .Errorf ("error extracting SBOM from %s: %w" , fn , err )
468
- }
469
- }()
470
-
471
- f , err := os .Open (fn )
472
- if err != nil {
473
- return xerrors .Errorf ("cannot open file: %w" , err )
474
- }
475
- defer func () {
476
- if closeErr := f .Close (); closeErr != nil {
477
- log .WithError (closeErr ).Warn ("failed to close file during SBOM extraction" )
478
- }
479
- }()
480
-
481
- g , err := gzip .NewReader (f )
482
- if err != nil {
483
- return xerrors .Errorf ("cannot create gzip reader: %w" , err )
484
- }
485
- defer func () {
486
- if closeErr := g .Close (); closeErr != nil {
487
- log .WithError (closeErr ).Warn ("failed to close gzip reader" )
488
- }
489
- }()
490
-
491
- var sbomFound bool
492
- a := tar .NewReader (g )
493
- var hdr * tar.Header
494
- for {
495
- hdr , err = a .Next ()
496
- if err == io .EOF {
497
- err = nil
498
- break
499
- }
500
- if err != nil {
501
- return xerrors .Errorf ("error reading tar: %w" , err )
502
- }
503
-
504
- // Look for SBOM files with any extension
505
- if ! strings .HasPrefix (hdr .Name , "./" + "sbom" + "." ) &&
506
- ! strings .HasPrefix (hdr .Name , "package/" + "sbom" + "." ) {
507
- continue
508
- }
509
-
510
- err = handler (io .LimitReader (a , hdr .Size ))
511
- if err != nil {
512
- return xerrors .Errorf ("error handling SBOM: %w" , err )
513
- }
514
- sbomFound = true
515
- break
516
- }
517
-
518
- if ! sbomFound {
519
- return ErrNoSBOM
520
- }
521
-
522
- return nil
523
- }
0 commit comments