-
Notifications
You must be signed in to change notification settings - Fork 513
/
syft_provider.go
85 lines (70 loc) · 2.23 KB
/
syft_provider.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
package pkg
import (
"context"
"errors"
"github.com/anchore/go-collections"
"github.com/anchore/grype/internal/log"
"github.com/anchore/stereoscope"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/syft/source/sourceproviders"
)
func syftProvider(userInput string, config ProviderConfig) ([]Package, Context, *sbom.SBOM, error) {
src, err := getSource(userInput, config)
if err != nil {
return nil, Context{}, nil, err
}
defer func() {
if src != nil {
if err := src.Close(); err != nil {
log.Tracef("unable to close source: %+v", err)
}
}
}()
s, err := syft.CreateSBOM(context.Background(), src, config.SBOMOptions)
if err != nil {
return nil, Context{}, nil, err
}
if s == nil {
return nil, Context{}, nil, errors.New("no SBOM provided")
}
pkgCatalog := removePackagesByOverlap(s.Artifacts.Packages, s.Relationships, s.Artifacts.LinuxDistribution)
srcDescription := src.Describe()
packages := FromCollection(pkgCatalog, config.SynthesisConfig)
pkgCtx := Context{
Source: &srcDescription,
Distro: s.Artifacts.LinuxDistribution,
}
return packages, pkgCtx, s, nil
}
func getSource(userInput string, config ProviderConfig) (source.Source, error) {
if config.SBOMOptions.Search.Scope == "" {
return nil, errDoesNotProvide
}
var err error
var platform *image.Platform
if config.Platform != "" {
platform, err = image.NewPlatform(config.Platform)
if err != nil {
return nil, err
}
}
var sources []string
schemeSource, newUserInput := stereoscope.ExtractSchemeSource(userInput, allSourceTags()...)
if schemeSource != "" {
sources = []string{schemeSource}
userInput = newUserInput
}
return syft.GetSource(context.Background(), userInput, syft.DefaultGetSourceConfig().
WithSources(sources...).
WithDefaultImagePullSource(config.DefaultImagePullSource).
WithAlias(source.Alias{Name: config.Name}).
WithRegistryOptions(config.RegistryOptions).
WithPlatform(platform).
WithExcludeConfig(source.ExcludeConfig{Paths: config.Exclusions}))
}
func allSourceTags() []string {
return collections.TaggedValueSet[source.Provider]{}.Join(sourceproviders.All("", nil)...).Tags()
}