|
| 1 | +/** |
| 2 | + * Copyright 2026 actions-toolkit authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {beforeAll, describe, expect, jest, it} from '@jest/globals'; |
| 18 | +import * as path from 'path'; |
| 19 | + |
| 20 | +import {Buildx} from '../../src/buildx/buildx'; |
| 21 | +import {Build} from '../../src/buildx/build'; |
| 22 | +import {Install as CosignInstall} from '../../src/cosign/install'; |
| 23 | +import {Docker} from '../../src/docker/docker'; |
| 24 | +import {Exec} from '../../src/exec'; |
| 25 | +import {Sigstore} from '../../src/sigstore/sigstore'; |
| 26 | + |
| 27 | +const fixturesDir = path.join(__dirname, '..', '.fixtures'); |
| 28 | + |
| 29 | +const runTest = process.env.GITHUB_ACTIONS && process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu'); |
| 30 | + |
| 31 | +const maybeIdToken = runTest && process.env.ACTIONS_ID_TOKEN_REQUEST_URL ? describe : describe.skip; |
| 32 | + |
| 33 | +// needs current GitHub repo info |
| 34 | +jest.unmock('@actions/github'); |
| 35 | + |
| 36 | +beforeAll(async () => { |
| 37 | + const cosignInstall = new CosignInstall(); |
| 38 | + const cosignBinPath = await cosignInstall.download({ |
| 39 | + version: 'v3.0.2' |
| 40 | + }); |
| 41 | + await cosignInstall.install(cosignBinPath); |
| 42 | +}, 100000); |
| 43 | + |
| 44 | +maybeIdToken('signAttestationManifests', () => { |
| 45 | + it('build, sign and verify', async () => { |
| 46 | + const buildx = new Buildx(); |
| 47 | + const build = new Build({buildx: buildx}); |
| 48 | + const imageName = 'ghcr.io/docker/actions-toolkit/test'; |
| 49 | + |
| 50 | + await expect( |
| 51 | + (async () => { |
| 52 | + await Docker.getExecOutput(['login', '--password-stdin', '--username', process.env.GITHUB_REPOSITORY_OWNER || 'docker', 'ghcr.io'], { |
| 53 | + input: Buffer.from(process.env.GITHUB_TOKEN || '') |
| 54 | + }); |
| 55 | + })() |
| 56 | + ).resolves.not.toThrow(); |
| 57 | + |
| 58 | + await expect( |
| 59 | + (async () => { |
| 60 | + // prettier-ignore |
| 61 | + const buildCmd = await buildx.getCommand([ |
| 62 | + '--builder', process.env.CTN_BUILDER_NAME ?? 'default', |
| 63 | + 'build', |
| 64 | + '-f', path.join(fixturesDir, 'hello.Dockerfile'), |
| 65 | + '--provenance=mode=max', |
| 66 | + '--tag', `${imageName}:sigstore-itg`, |
| 67 | + '--platform', 'linux/amd64,linux/arm64', |
| 68 | + '--push', |
| 69 | + '--metadata-file', build.getMetadataFilePath(), |
| 70 | + fixturesDir |
| 71 | + ]); |
| 72 | + await Exec.exec(buildCmd.command, buildCmd.args); |
| 73 | + })() |
| 74 | + ).resolves.not.toThrow(); |
| 75 | + |
| 76 | + const metadata = build.resolveMetadata(); |
| 77 | + expect(metadata).toBeDefined(); |
| 78 | + const buildDigest = build.resolveDigest(metadata); |
| 79 | + expect(buildDigest).toBeDefined(); |
| 80 | + |
| 81 | + const sigstore = new Sigstore(); |
| 82 | + const signResults = await sigstore.signAttestationManifests({ |
| 83 | + imageNames: [imageName], |
| 84 | + imageDigest: buildDigest! |
| 85 | + }); |
| 86 | + expect(Object.keys(signResults).length).toEqual(2); |
| 87 | + |
| 88 | + const verifyResults = await sigstore.verifySignedManifests(signResults, { |
| 89 | + certificateIdentityRegexp: `^https://github.com/docker/actions-toolkit/.github/workflows/test.yml.*$` |
| 90 | + }); |
| 91 | + expect(Object.keys(verifyResults).length).toEqual(2); |
| 92 | + }, 100000); |
| 93 | +}); |
0 commit comments