@@ -87,26 +87,28 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, error
8787 err := cache .StoreImage ()
8888 defer cache .Cleanup ()
8989 if err != nil {
90- return nil , errors .Wrapf (err , "failed to copy image" )
90+ return nil , errors .Wrap (err , "failed to copy image" )
9191 }
9292
93- lm := createLayerMapping (* cache .Image )
94-
93+ lm , err := createLayerMapping (cache )
94+ if err != nil {
95+ return nil , errors .Wrap (err , "failed to index image" )
96+ }
9597 s := internal .StartSpinner ("info" , "Indexing" , cli .Out ().IsTerminal ())
9698 defer s .Stop ()
9799 trivyResultChan := make (chan types.IndexResult )
98100 syftResultChan := make (chan types.IndexResult )
99- go trivySbom (cache . ImagePath , lm , trivyResultChan )
100- go syftSbom (cache . ImagePath , lm , syftResultChan )
101+ go trivySbom (cache , lm , trivyResultChan )
102+ go syftSbom (cache , lm , syftResultChan )
101103
102104 trivyResult := <- trivyResultChan
103105 syftResult := <- syftResultChan
104106
105107 if trivyResult .Error != nil {
106- return nil , errors .Wrapf (trivyResult .Error , "failed to index image" )
108+ return nil , errors .Wrap (trivyResult .Error , "failed to index image" )
107109 }
108110 if syftResult .Error != nil {
109- return nil , errors .Wrapf (syftResult .Error , "failed to index image" )
111+ return nil , errors .Wrap (syftResult .Error , "failed to index image" )
110112 }
111113
112114 trivyResult .Packages , err = types .NormalizePackages (trivyResult .Packages )
@@ -120,10 +122,14 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, error
120122 s .Stop ()
121123 skill .Log .Infof (`Indexed %d packages` , len (packages ))
122124
123- manifest , _ := (* cache .Image ).RawManifest ()
124- config , _ := (* cache .Image ).RawConfigFile ()
125- c , _ := (* cache .Image ).ConfigFile ()
126- m , _ := (* cache .Image ).Manifest ()
125+ rawManifest := cache .Source .Image .Metadata .RawManifest
126+ rawConfig := cache .Source .Image .Metadata .RawConfig
127+
128+ var manifest v1.Manifest
129+ err = json .Unmarshal (rawManifest , & manifest )
130+ if err != nil {
131+ return nil , errors .Wrap (err , "failed to unmarshal manifest" )
132+ }
127133
128134 sbom := types.Sbom {
129135 Artifacts : packages ,
@@ -132,17 +138,17 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, error
132138 Image : types.ImageSource {
133139 Name : cache .Name ,
134140 Digest : cache .Digest ,
135- Manifest : m ,
136- Config : c ,
137- RawManifest : base64 .StdEncoding .EncodeToString (manifest ),
138- RawConfig : base64 .StdEncoding .EncodeToString (config ),
141+ Manifest : & manifest ,
142+ Config : & cache . Source . Image . Metadata . Config ,
143+ RawManifest : base64 .StdEncoding .EncodeToString (rawManifest ),
144+ RawConfig : base64 .StdEncoding .EncodeToString (rawConfig ),
139145 Distro : syftResult .Distro ,
140146 Platform : types.Platform {
141- Os : c .OS ,
142- Architecture : c .Architecture ,
143- Variant : c .Variant ,
147+ Os : cache . Source . Image . Metadata . Config .OS ,
148+ Architecture : cache . Source . Image . Metadata . Config .Architecture ,
149+ Variant : cache . Source . Image . Metadata . Config .Variant ,
144150 },
145- Size : m . Config .Size ,
151+ Size : cache . Source . Image . Metadata .Size ,
146152 },
147153 },
148154 Descriptor : types.Descriptor {
@@ -160,11 +166,11 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, error
160166 if err == nil {
161167 err = os .MkdirAll (filepath .Dir (sbomFilePath ), os .ModePerm )
162168 if err != nil {
163- return nil , errors .Wrapf (err , "failed create to sbom folder" )
169+ return nil , errors .Wrap (err , "failed create to sbom folder" )
164170 }
165171 err = os .WriteFile (sbomFilePath , js , 0644 )
166172 if err != nil {
167- return nil , errors .Wrapf (err , "failed to write sbom" )
173+ return nil , errors .Wrap (err , "failed to write sbom" )
168174 }
169175 }
170176
@@ -191,30 +197,30 @@ func cachedSbom(sbomFilePath string) *types.Sbom {
191197 return nil
192198}
193199
194- func createLayerMapping (img v1.Image ) types.LayerMapping {
200+ func createLayerMapping (cache * registry.ImageCache ) (* types.LayerMapping , error ) {
201+ skill .Log .Debugf ("Creating layer mapping" )
195202 lm := types.LayerMapping {
196203 ByDiffId : make (map [string ]string , 0 ),
197204 ByDigest : make (map [string ]string , 0 ),
198205 DiffIdByOrdinal : make (map [int ]string , 0 ),
199206 DigestByOrdinal : make (map [int ]string , 0 ),
200207 OrdinalByDiffId : make (map [string ]int , 0 ),
201208 }
202- config , _ := img .ConfigFile ()
203- diffIds := config .RootFS .DiffIDs
204- manifest , _ := img .Manifest ()
205- layers := manifest .Layers
209+
210+ diffIds := cache .Source .Image .Metadata .Config .RootFS .DiffIDs
211+ layers := cache .Source .Metadata .ImageMetadata .Layers
206212
207213 for i := range layers {
208214 layer := layers [i ]
209215 diffId := diffIds [i ]
210216
211- lm .ByDiffId [diffId .String ()] = layer .Digest . String ()
212- lm .ByDigest [layer .Digest . String () ] = diffId .String ()
217+ lm .ByDiffId [diffId .String ()] = layer .Digest
218+ lm .ByDigest [layer .Digest ] = diffId .String ()
213219 lm .OrdinalByDiffId [diffId .String ()] = i
214220 lm .DiffIdByOrdinal [i ] = diffId .String ()
215- lm .DigestByOrdinal [i ] = layer .Digest . String ()
221+ lm .DigestByOrdinal [i ] = layer .Digest
216222 }
217223
218224 skill .Log .Debugf ("Created layer mapping" )
219- return lm
225+ return & lm , nil
220226}
0 commit comments