inspect: parse sbom and provenance into json structs

Signed-off-by: Justin Chadwell <me@jedevc.com>
(cherry picked from commit e68c566c1c)
pull/1501/head
Justin Chadwell 2 years ago committed by Tonis Tiigi
parent fe6f697205
commit 793082f543
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39

@ -46,9 +46,9 @@ type index struct {
} }
type asset struct { type asset struct {
config *ocispec.Image config *ocispec.Image
sbom *sbomStub sbom *sbomStub
slsa *slsaStub provenance *provenanceStub
} }
type result struct { type result struct {
@ -255,7 +255,7 @@ func (l *loader) scanConfig(ctx context.Context, fetcher remotes.Fetcher, desc o
} }
type sbomStub struct { 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 { 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 { if err != nil {
return err return err
} }
var spdx struct {
Predicate interface{} `json:"predicate"`
}
if err := json.Unmarshal(dt, &spdx); err != nil {
return err
}
as.sbom = &sbomStub{ as.sbom = &sbomStub{
SPDX: dt, SPDX: spdx.Predicate,
} }
break break
} }
@ -285,8 +291,8 @@ func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul
return nil return nil
} }
type slsaStub struct { type provenanceStub struct {
Provenance json.RawMessage `json:",omitempty"` SLSA interface{} `json:",omitempty"`
} }
func (l *loader) scanProvenance(ctx context.Context, fetcher remotes.Fetcher, r *result, refs []digest.Digest, as *asset) error { 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 { if err != nil {
return err return err
} }
as.slsa = &slsaStub{ var slsa struct {
Provenance: dt, Predicate interface{} `json:"predicate"`
}
if err := json.Unmarshal(dt, &slsa); err != nil {
return err
}
as.provenance = &provenanceStub{
SLSA: slsa.Predicate,
} }
break break
} }
@ -330,16 +342,16 @@ func (r *result) Configs() map[string]*ocispec.Image {
return res return res
} }
func (r *result) SLSA() map[string]slsaStub { func (r *result) Provenance() map[string]provenanceStub {
if len(r.assets) == 0 { if len(r.assets) == 0 {
return nil return nil
} }
res := make(map[string]slsaStub) res := make(map[string]provenanceStub)
for p, a := range r.assets { for p, a := range r.assets {
if a.slsa == nil { if a.provenance == nil {
continue continue
} }
res[p] = *a.slsa res[p] = *a.provenance
} }
return res return res
} }

@ -99,7 +99,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
} }
imageconfigs := res.Configs() imageconfigs := res.Configs()
slsas := res.SLSA() provenances := res.Provenance()
sboms := res.SBOM() sboms := res.SBOM()
format := tpl.Root.String() format := tpl.Root.String()
@ -143,43 +143,43 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
default: default:
if len(res.platforms) > 1 { if len(res.platforms) > 1 {
return tpl.Execute(out, struct { return tpl.Execute(out, struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Manifest interface{} `json:"manifest,omitempty"` Manifest interface{} `json:"manifest,omitempty"`
Image map[string]*ocispecs.Image `json:"image,omitempty"` Image map[string]*ocispecs.Image `json:"image,omitempty"`
SLSA map[string]slsaStub `json:"SLSA,omitempty"` Provenance map[string]provenanceStub `json:"Provenance,omitempty"`
SBOM map[string]sbomStub `json:"SBOM,omitempty"` SBOM map[string]sbomStub `json:"SBOM,omitempty"`
}{ }{
Name: p.name, Name: p.name,
Manifest: mfst, Manifest: mfst,
Image: imageconfigs, Image: imageconfigs,
SLSA: slsas, Provenance: provenances,
SBOM: sboms, SBOM: sboms,
}) })
} }
var ic *ocispecs.Image var ic *ocispecs.Image
for _, v := range imageconfigs { for _, v := range imageconfigs {
ic = v ic = v
} }
var slsa slsaStub var provenance provenanceStub
for _, v := range slsas { for _, v := range provenances {
slsa = v provenance = v
} }
var sbom sbomStub var sbom sbomStub
for _, v := range sboms { for _, v := range sboms {
sbom = v sbom = v
} }
return tpl.Execute(out, struct { return tpl.Execute(out, struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Manifest interface{} `json:"manifest,omitempty"` Manifest interface{} `json:"manifest,omitempty"`
Image *ocispecs.Image `json:"image,omitempty"` Image *ocispecs.Image `json:"image,omitempty"`
SLSA slsaStub `json:"SLSA,omitempty"` Provenance provenanceStub `json:"Provenance,omitempty"`
SBOM sbomStub `json:"SBOM,omitempty"` SBOM sbomStub `json:"SBOM,omitempty"`
}{ }{
Name: p.name, Name: p.name,
Manifest: mfst, Manifest: mfst,
Image: ic, Image: ic,
SLSA: slsa, Provenance: provenance,
SBOM: sbom, SBOM: sbom,
}) })
} }

Loading…
Cancel
Save