diff --git a/exporter/attestation/unbundle.go b/exporter/attestation/unbundle.go index 498b13624bad..a2120d7975e1 100644 --- a/exporter/attestation/unbundle.go +++ b/exporter/attestation/unbundle.go @@ -81,6 +81,7 @@ func Unbundle(ctx context.Context, s session.Group, bundled []exporter.Attestati for _, atts := range unbundled { joined = append(joined, atts...) } + joined = sort(joined) if err := Validate(joined); err != nil { return nil, err @@ -88,6 +89,33 @@ func Unbundle(ctx context.Context, s session.Group, bundled []exporter.Attestati return joined, nil } +func sort(atts []exporter.Attestation) []exporter.Attestation { + isCore := make([]bool, len(atts)) + for i, att := range atts { + name, ok := att.Metadata[result.AttestationSBOMCore] + if !ok { + continue + } + if n, _, _ := strings.Cut(att.Path, "."); n != string(name) { + continue + } + isCore[i] = true + } + + result := make([]exporter.Attestation, 0, len(atts)) + for i, att := range atts { + if isCore[i] { + result = append(result, att) + } + } + for i, att := range atts { + if !isCore[i] { + result = append(result, att) + } + } + return result +} + func unbundle(ctx context.Context, root string, bundle exporter.Attestation) ([]exporter.Attestation, error) { dir, err := fs.RootPath(root, bundle.Path) if err != nil {