Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions __tests__/buildx/bake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,113 @@ describe('hasGitAuthTokenSecret', () => {
expect(Bake.hasGitAuthTokenSecret(def)).toEqual(expected);
});
});

describe('hasProvenanceAttestation', () => {
// prettier-ignore
test.each([
[
{
"target": {
"build": {
"attest": [
{
"type": "provenance",
"mode": "max"
}
]
},
}
} as unknown as BakeDefinition,
true
],
[
{
"target": {
"build": {
"attest": [
{
"type": "sbom"
}
]
},
}
} as unknown as BakeDefinition,
false
],
[
{
"target": {
"build": {
"attest": [
{
"type": "sbom"
},
{
"type": "provenance",
"mode": "max"
}
]
},
}
} as unknown as BakeDefinition,
true
]
])('given %o returns %p', async (def: BakeDefinition, expected: boolean) => {
expect(Bake.hasProvenanceAttestation(def)).toEqual(expected);
});
});

describe('hasSBOMAttestation', () => {
// prettier-ignore
test.each([
[
{
"target": {
"build": {
"attest": [
{
"type": "provenance",
"mode": "max"
}
]
},
}
} as unknown as BakeDefinition,
false
],
[
{
"target": {
"build": {
"attest": [
{
"type": "sbom"
}
]
},
}
} as unknown as BakeDefinition,
true
],
[
{
"target": {
"build": {
"attest": [
{
"type": "sbom"
},
{
"type": "provenance",
"mode": "max"
}
]
},
}
} as unknown as BakeDefinition,
true
]
])('given %o returns %p', async (def: BakeDefinition, expected: boolean) => {
expect(Bake.hasSBOMAttestation(def)).toEqual(expected);
});
});
30 changes: 30 additions & 0 deletions src/buildx/bake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,34 @@ export class Bake {
}
return false;
}

public static hasProvenanceAttestation(def: BakeDefinition): boolean {
return Bake.hasAttestationType('provenance', Bake.attestations(def));
}

public static hasSBOMAttestation(def: BakeDefinition): boolean {
return Bake.hasAttestationType('sbom', Bake.attestations(def));
}

public static hasAttestationType(name: string, attestations: Array<AttestEntry>): boolean {
for (const attestation of attestations) {
if (attestation.type == name) {
return true;
}
}
return false;
}

private static attestations(def: BakeDefinition): Array<AttestEntry> {
const attestations = new Array<AttestEntry>();
for (const key in def.target) {
const target = def.target[key];
if (target.attest) {
for (const attest of target.attest) {
attestations.push(Bake.parseAttestEntry(attest));
}
}
}
return attestations;
}
}