@@ -189,7 +189,25 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
189189 buildChannel := make (chan * client.SolveStatus )
190190 eg , ctx := errgroup .WithContext (ctx )
191191 eg .Go (func () error {
192- _ , err := bkClient .Build (ctx , solveOpt , copaProduct , func (ctx context.Context , c gwclient.Client ) (* gwclient.Result , error ) {
192+ var pkgType string
193+ var validatedManifest * unversioned.UpdateManifest
194+ if updates != nil {
195+ // create a new manifest with the successfully patched packages
196+ validatedManifest = & unversioned.UpdateManifest {
197+ Metadata : unversioned.Metadata {
198+ OS : unversioned.OS {
199+ Type : updates .Metadata .OS .Type ,
200+ Version : updates .Metadata .OS .Version ,
201+ },
202+ Config : unversioned.Config {
203+ Arch : updates .Metadata .Config .Arch ,
204+ },
205+ },
206+ Updates : []unversioned.UpdatePackage {},
207+ }
208+ }
209+
210+ solveResponse , err := bkClient .Build (ctx , solveOpt , copaProduct , func (ctx context.Context , c gwclient.Client ) (* gwclient.Result , error ) {
193211 // Configure buildctl/client for use by package manager
194212 config , err := buildkit .InitializeBuildkitConfig (ctx , c , imageName .String ())
195213 if err != nil {
@@ -263,38 +281,32 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
263281
264282 res .AddMeta (exptypes .ExporterImageConfigKey , config .ConfigData )
265283
266- // Currently can only validate updates if updating via scanner
267- if reportFile != "" {
268- // create a new manifest with the successfully patched packages
269- validatedManifest := & unversioned.UpdateManifest {
270- Metadata : unversioned.Metadata {
271- OS : unversioned.OS {
272- Type : updates .Metadata .OS .Type ,
273- Version : updates .Metadata .OS .Version ,
274- },
275- Config : unversioned.Config {
276- Arch : updates .Metadata .Config .Arch ,
277- },
278- },
279- Updates : []unversioned.UpdatePackage {},
280- }
284+ // for the vex document, only include updates that were successfully applied
285+ pkgType = manager .GetPackageType ()
286+ if validatedManifest != nil {
281287 for _ , update := range updates .Updates {
282288 if ! slices .Contains (errPkgs , update .Name ) {
283289 validatedManifest .Updates = append (validatedManifest .Updates , update )
284290 }
285291 }
286- // vex document must contain at least one statement
287- if output != "" && len (validatedManifest .Updates ) > 0 {
288- if err := vex .TryOutputVexDocument (validatedManifest , manager , patchedImageName , format , output ); err != nil {
289- ch <- err
290- return nil , err
291- }
292- }
293292 }
294293
295294 return res , nil
296295 }, buildChannel )
297296
297+ // Currently can only validate updates if updating via scanner
298+ if reportFile != "" && validatedManifest != nil {
299+ digest := solveResponse .ExporterResponse [exptypes .ExporterImageDigestKey ]
300+ nameDigestOrTag := getRepoNameWithDigest (patchedImageName , digest )
301+ // vex document must contain at least one statement
302+ if output != "" && len (validatedManifest .Updates ) > 0 {
303+ if err := vex .TryOutputVexDocument (validatedManifest , pkgType , nameDigestOrTag , format , output ); err != nil {
304+ ch <- err
305+ return err
306+ }
307+ }
308+ }
309+
298310 return err
299311 })
300312
@@ -320,7 +332,9 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
320332 return pipeR .Close ()
321333 })
322334
323- return eg .Wait ()
335+ err = eg .Wait ()
336+
337+ return err
324338}
325339
326340func getOSType (ctx context.Context , osreleaseBytes []byte ) (string , error ) {
@@ -390,3 +404,14 @@ func dockerLoad(ctx context.Context, pipeR io.Reader) error {
390404
391405 return cmd .Run ()
392406}
407+
408+ // e.g. "docker.io/library/nginx:1.21.6-patched".
409+ func getRepoNameWithDigest (patchedImageName , imageDigest string ) string {
410+ parts := strings .Split (patchedImageName , "/" )
411+ last := parts [len (parts )- 1 ]
412+ if idx := strings .IndexRune (last , ':' ); idx >= 0 {
413+ last = last [:idx ]
414+ }
415+ nameWithDigest := fmt .Sprintf ("%s@%s" , last , imageDigest )
416+ return nameWithDigest
417+ }
0 commit comments