From 793082f5438cff21519658d61b795ca075868548 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Mon, 9 Jan 2023 18:09:43 +0000 Subject: [PATCH] inspect: parse sbom and provenance into json structs Signed-off-by: Justin Chadwell (cherry picked from commit e68c566c1c37dfbe77cd479a8bf2743ff4dee969) --- util/imagetools/loader.go | 38 +++++++++++++++++++---------- util/imagetools/printers.go | 48 ++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/util/imagetools/loader.go b/util/imagetools/loader.go index 5f9f3928..076db5f2 100644 --- a/util/imagetools/loader.go +++ b/util/imagetools/loader.go @@ -46,9 +46,9 @@ type index struct { } type asset struct { - config *ocispec.Image - sbom *sbomStub - slsa *slsaStub + config *ocispec.Image + sbom *sbomStub + provenance *provenanceStub } type result struct { @@ -255,7 +255,7 @@ func (l *loader) scanConfig(ctx context.Context, fetcher remotes.Fetcher, desc o } type sbomStub struct { - SPDX json.RawMessage `json:",omitempty"` + SPDX interface{} `json:",omitempty"` } func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *result, refs []digest.Digest, as *asset) error { @@ -275,8 +275,14 @@ func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul if err != nil { return err } + var spdx struct { + Predicate interface{} `json:"predicate"` + } + if err := json.Unmarshal(dt, &spdx); err != nil { + return err + } as.sbom = &sbomStub{ - SPDX: dt, + SPDX: spdx.Predicate, } break } @@ -285,8 +291,8 @@ func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul return nil } -type slsaStub struct { - Provenance json.RawMessage `json:",omitempty"` +type provenanceStub struct { + SLSA interface{} `json:",omitempty"` } func (l *loader) scanProvenance(ctx context.Context, fetcher remotes.Fetcher, r *result, refs []digest.Digest, as *asset) error { @@ -306,8 +312,14 @@ func (l *loader) scanProvenance(ctx context.Context, fetcher remotes.Fetcher, r if err != nil { return err } - as.slsa = &slsaStub{ - Provenance: dt, + var slsa struct { + Predicate interface{} `json:"predicate"` + } + if err := json.Unmarshal(dt, &slsa); err != nil { + return err + } + as.provenance = &provenanceStub{ + SLSA: slsa.Predicate, } break } @@ -330,16 +342,16 @@ func (r *result) Configs() map[string]*ocispec.Image { return res } -func (r *result) SLSA() map[string]slsaStub { +func (r *result) Provenance() map[string]provenanceStub { if len(r.assets) == 0 { return nil } - res := make(map[string]slsaStub) + res := make(map[string]provenanceStub) for p, a := range r.assets { - if a.slsa == nil { + if a.provenance == nil { continue } - res[p] = *a.slsa + res[p] = *a.provenance } return res } diff --git a/util/imagetools/printers.go b/util/imagetools/printers.go index a0b1560e..e1768353 100644 --- a/util/imagetools/printers.go +++ b/util/imagetools/printers.go @@ -99,7 +99,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error { } imageconfigs := res.Configs() - slsas := res.SLSA() + provenances := res.Provenance() sboms := res.SBOM() format := tpl.Root.String() @@ -143,43 +143,43 @@ func (p *Printer) Print(raw bool, out io.Writer) error { default: if len(res.platforms) > 1 { return tpl.Execute(out, struct { - Name string `json:"name,omitempty"` - Manifest interface{} `json:"manifest,omitempty"` - Image map[string]*ocispecs.Image `json:"image,omitempty"` - SLSA map[string]slsaStub `json:"SLSA,omitempty"` - SBOM map[string]sbomStub `json:"SBOM,omitempty"` + Name string `json:"name,omitempty"` + Manifest interface{} `json:"manifest,omitempty"` + Image map[string]*ocispecs.Image `json:"image,omitempty"` + Provenance map[string]provenanceStub `json:"Provenance,omitempty"` + SBOM map[string]sbomStub `json:"SBOM,omitempty"` }{ - Name: p.name, - Manifest: mfst, - Image: imageconfigs, - SLSA: slsas, - SBOM: sboms, + Name: p.name, + Manifest: mfst, + Image: imageconfigs, + Provenance: provenances, + SBOM: sboms, }) } var ic *ocispecs.Image for _, v := range imageconfigs { ic = v } - var slsa slsaStub - for _, v := range slsas { - slsa = v + var provenance provenanceStub + for _, v := range provenances { + provenance = v } var sbom sbomStub for _, v := range sboms { sbom = v } return tpl.Execute(out, struct { - Name string `json:"name,omitempty"` - Manifest interface{} `json:"manifest,omitempty"` - Image *ocispecs.Image `json:"image,omitempty"` - SLSA slsaStub `json:"SLSA,omitempty"` - SBOM sbomStub `json:"SBOM,omitempty"` + Name string `json:"name,omitempty"` + Manifest interface{} `json:"manifest,omitempty"` + Image *ocispecs.Image `json:"image,omitempty"` + Provenance provenanceStub `json:"Provenance,omitempty"` + SBOM sbomStub `json:"SBOM,omitempty"` }{ - Name: p.name, - Manifest: mfst, - Image: ic, - SLSA: slsa, - SBOM: sbom, + Name: p.name, + Manifest: mfst, + Image: ic, + Provenance: provenance, + SBOM: sbom, }) }