@@ -19,7 +19,7 @@ import {Exec} from '../exec';
1919
2020import { Manifest as ImageToolsManifest } from '../types/buildx/imagetools' ;
2121import { Image } from '../types/oci/config' ;
22- import { Descriptor } from '../types/oci/descriptor' ;
22+ import { Descriptor , Platform } from '../types/oci/descriptor' ;
2323import { Digest } from '../types/oci/digest' ;
2424
2525export interface ImageToolsOpts {
@@ -83,15 +83,39 @@ export class ImageTools {
8383 } ) ;
8484 }
8585
86- public async attestationDescriptors ( name : string ) : Promise < Array < Descriptor > > {
86+ public async attestationDescriptors ( name : string , platform ?: Platform ) : Promise < Array < Descriptor > > {
8787 const manifest = await this . inspectManifest ( name ) ;
88- if ( typeof manifest === 'object' && manifest !== null && 'manifests' in manifest && Array . isArray ( manifest . manifests ) ) {
89- return manifest . manifests . filter ( m => m . annotations && m . annotations [ 'vnd.docker.reference.type' ] === 'attestation-manifest' ) ;
88+
89+ if ( typeof manifest !== 'object' || manifest === null || ! ( 'manifests' in manifest ) || ! Array . isArray ( manifest . manifests ) ) {
90+ throw new Error ( `No descriptor found for ${ name } ` ) ;
91+ }
92+
93+ const attestations = manifest . manifests . filter ( m => m . annotations ?. [ 'vnd.docker.reference.type' ] === 'attestation-manifest' ) ;
94+ if ( ! platform ) {
95+ return attestations ;
96+ }
97+
98+ const manifestByDigest = new Map < string , Descriptor > ( ) ;
99+ for ( const m of manifest . manifests ) {
100+ if ( m . digest ) {
101+ manifestByDigest . set ( m . digest , m ) ;
102+ }
90103 }
91- throw new Error ( `No attestation descriptors found for ${ name } ` ) ;
104+
105+ return attestations . filter ( attestation => {
106+ const refDigest = attestation . annotations ?. [ 'vnd.docker.reference.digest' ] ;
107+ if ( ! refDigest ) {
108+ return false ;
109+ }
110+ const referencedManifest = manifestByDigest . get ( refDigest ) ;
111+ if ( ! referencedManifest ) {
112+ return false ;
113+ }
114+ return referencedManifest . platform ?. os === platform . os && referencedManifest . platform ?. architecture === platform . architecture && ( referencedManifest . platform ?. variant ?? '' ) === ( platform . variant ?? '' ) ;
115+ } ) ;
92116 }
93117
94- public async attestationDigests ( name : string ) : Promise < Array < Digest > > {
95- return ( await this . attestationDescriptors ( name ) ) . map ( attestation => attestation . digest ) ;
118+ public async attestationDigests ( name : string , platform ?: Platform ) : Promise < Array < Digest > > {
119+ return ( await this . attestationDescriptors ( name , platform ) ) . map ( attestation => attestation . digest ) ;
96120 }
97121}
0 commit comments