@@ -37,53 +37,58 @@ import (
37
37
38
38
type ImageIndexResult struct {
39
39
Input string
40
- Image * v1.Image
41
40
Sbom * types.Sbom
42
41
Error error
43
42
}
44
43
45
44
func indexImageAsync (wg * sync.WaitGroup , image string , cli command.Cli , resultChan chan <- ImageIndexResult ) {
46
45
defer wg .Done ()
47
- sbom , img , err := IndexImage (image , cli )
46
+ sbom , err := IndexImage (image , cli )
48
47
cves , err := query .QueryCves (sbom , "" , "" , "" )
49
48
if err == nil {
50
49
sbom .Vulnerabilities = * cves
51
50
}
52
51
resultChan <- ImageIndexResult {
53
52
Input : image ,
54
- Image : img ,
55
53
Sbom : sbom ,
56
54
Error : err ,
57
55
}
58
56
}
59
57
60
- func IndexPath (path string , name string , cli command.Cli ) (* types.Sbom , * v1. Image , error ) {
58
+ func IndexPath (path string , name string , cli command.Cli ) (* types.Sbom , error ) {
61
59
cache , err := registry .ReadImage (name , path )
62
60
if err != nil {
63
- return nil , nil , errors .Wrap (err , "failed to read image" )
61
+ return nil , errors .Wrap (err , "failed to read image" )
64
62
}
65
63
return indexImage (cache , cli )
66
64
}
67
65
68
- func IndexImage (image string , cli command.Cli ) (* types.Sbom , * v1.Image , error ) {
66
+ func IndexImage (image string , cli command.Cli ) (* types.Sbom , error ) {
67
+ if strings .HasPrefix (image , "sha256:" ) {
68
+ configFilePath := cli .ConfigFile ().Filename
69
+ sbomFilePath := filepath .Join (filepath .Dir (configFilePath ), "sbom" , "sha256" , image [7 :], "sbom.json" )
70
+ if sbom := cachedSbom (sbomFilePath ); sbom != nil {
71
+ return sbom , nil
72
+ }
73
+ }
69
74
cache , err := registry .SaveImage (image , cli )
70
75
if err != nil {
71
- return nil , nil , errors .Wrap (err , "failed to copy image" )
76
+ return nil , errors .Wrap (err , "failed to copy image" )
72
77
}
73
78
return indexImage (cache , cli )
74
79
}
75
80
76
- func indexImage (cache * registry.ImageCache , cli command.Cli ) (* types.Sbom , * v1. Image , error ) {
81
+ func indexImage (cache * registry.ImageCache , cli command.Cli ) (* types.Sbom , error ) {
77
82
configFilePath := cli .ConfigFile ().Filename
78
83
sbomFilePath := filepath .Join (filepath .Dir (configFilePath ), "sbom" , "sha256" , cache .Digest [7 :], "sbom.json" )
79
- if sbom := cachedSbom (cache , sbomFilePath ); sbom != nil {
80
- return sbom , cache . Image , nil
84
+ if sbom := cachedSbom (sbomFilePath ); sbom != nil {
85
+ return sbom , nil
81
86
}
82
87
83
88
err := cache .StoreImage ()
84
89
defer cache .Cleanup ()
85
90
if err != nil {
86
- return nil , nil , errors .Wrapf (err , "failed to copy image" )
91
+ return nil , errors .Wrapf (err , "failed to copy image" )
87
92
}
88
93
89
94
lm := createLayerMapping (* cache .Image )
@@ -101,7 +106,7 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
101
106
trivyResult .Packages , err = types .NormalizePackages (trivyResult .Packages )
102
107
syftResult .Packages , err = types .NormalizePackages (syftResult .Packages )
103
108
if err != nil {
104
- return nil , nil , errors .Wrapf (err , "failed to normalize packagess: %s" , cache .Name )
109
+ return nil , errors .Wrapf (err , "failed to normalize packagess: %s" , cache .Name )
105
110
}
106
111
107
112
packages := types .MergePackages (syftResult , trivyResult )
@@ -119,7 +124,7 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
119
124
if cache .Name != "" {
120
125
ref , err := name .ParseReference (cache .Name )
121
126
if err != nil {
122
- return nil , nil , errors .Wrapf (err , "failed to parse reference: %s" , cache .Name )
127
+ return nil , errors .Wrapf (err , "failed to parse reference: %s" , cache .Name )
123
128
}
124
129
cache .Name = ref .Context ().String ()
125
130
if ! strings .HasPrefix (ref .Identifier (), "sha256:" ) {
@@ -162,18 +167,18 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
162
167
if err == nil {
163
168
err = os .MkdirAll (filepath .Dir (sbomFilePath ), os .ModePerm )
164
169
if err != nil {
165
- return nil , nil , errors .Wrapf (err , "failed create to sbom folder" )
170
+ return nil , errors .Wrapf (err , "failed create to sbom folder" )
166
171
}
167
172
err = os .WriteFile (sbomFilePath , js , 0644 )
168
173
if err != nil {
169
- return nil , nil , errors .Wrapf (err , "failed to write sbom" )
174
+ return nil , errors .Wrapf (err , "failed to write sbom" )
170
175
}
171
176
}
172
177
173
- return & sbom , cache . Image , nil
178
+ return & sbom , nil
174
179
}
175
180
176
- func cachedSbom (cache * registry. ImageCache , sbomFilePath string ) * types.Sbom {
181
+ func cachedSbom (sbomFilePath string ) * types.Sbom {
177
182
// see if we can re-use an existing sbom
178
183
if _ , ok := os .LookupEnv ("ATOMIST_NO_CACHE" ); ! ok {
179
184
if _ , err := os .Stat (sbomFilePath ); ! os .IsNotExist (err ) {
0 commit comments