|
| 1 | +'use strict' |
| 2 | +const t = require('tap') |
| 3 | +const MockRegistry = require('./index') |
| 4 | +const mockGlobals = require('@npmcli/mock-globals') |
| 5 | + |
| 6 | +class MockProvenance { |
| 7 | + static successfulNock ({ |
| 8 | + oidcURL, |
| 9 | + requestToken, |
| 10 | + workflowPath, |
| 11 | + repository, |
| 12 | + serverUrl, |
| 13 | + ref, |
| 14 | + sha, |
| 15 | + runID, |
| 16 | + runAttempt, |
| 17 | + runnerEnv, |
| 18 | + }) { |
| 19 | + mockGlobals(t, { |
| 20 | + 'process.env': { |
| 21 | + CI: true, |
| 22 | + GITHUB_ACTIONS: true, |
| 23 | + ACTIONS_ID_TOKEN_REQUEST_URL: oidcURL, |
| 24 | + ACTIONS_ID_TOKEN_REQUEST_TOKEN: requestToken, |
| 25 | + GITHUB_WORKFLOW_REF: `${repository}/${workflowPath}@${ref}`, |
| 26 | + GITHUB_REPOSITORY: repository, |
| 27 | + GITHUB_SERVER_URL: serverUrl, |
| 28 | + GITHUB_REF: ref, |
| 29 | + GITHUB_SHA: sha, |
| 30 | + GITHUB_RUN_ID: runID, |
| 31 | + GITHUB_RUN_ATTEMPT: runAttempt, |
| 32 | + RUNNER_ENVIRONMENT: runnerEnv, |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + // Data for mocking the OIDC token request |
| 37 | + const oidcClaims = { |
| 38 | + iss: 'https://oauth2.sigstore.dev/auth', |
| 39 | + email: 'foo@bar.com', |
| 40 | + } |
| 41 | + const idToken = `.${Buffer.from(JSON.stringify(oidcClaims)).toString('base64')}.` |
| 42 | + |
| 43 | + // Data for mocking Fulcio certifcate request |
| 44 | + const fulcioURL = 'https://mock.fulcio' |
| 45 | + const leafCertificate = `-----BEGIN CERTIFICATE-----\nabc\n-----END CERTIFICATE-----\n` |
| 46 | + const rootCertificate = `-----BEGIN CERTIFICATE-----\nxyz\n-----END CERTIFICATE-----\n` |
| 47 | + const certificateResponse = { |
| 48 | + signedCertificateEmbeddedSct: { |
| 49 | + chain: { |
| 50 | + certificates: [leafCertificate, rootCertificate], |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + // Data for mocking Rekor upload |
| 56 | + const rekorURL = 'https://mock.rekor' |
| 57 | + const signature = 'ABC123' |
| 58 | + const b64Cert = Buffer.from(leafCertificate).toString('base64') |
| 59 | + const logIndex = 2513258 |
| 60 | + const uuid = |
| 61 | + '69e5a0c1663ee4452674a5c9d5050d866c2ee31e2faaf79913aea7cc27293cf6' |
| 62 | + |
| 63 | + const signatureBundle = { |
| 64 | + kind: 'hashedrekord', |
| 65 | + apiVersion: '0.0.1', |
| 66 | + spec: { |
| 67 | + signature: { |
| 68 | + content: signature, |
| 69 | + publicKey: { content: b64Cert }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + const rekorEntry = { |
| 75 | + [uuid]: { |
| 76 | + body: Buffer.from(JSON.stringify(signatureBundle)).toString( |
| 77 | + 'base64' |
| 78 | + ), |
| 79 | + integratedTime: 1654015743, |
| 80 | + logID: |
| 81 | + 'c0d23d6ad406973f9559f3ba2d1ca01f84147d8ffc5b8445c224f98b9591801d', |
| 82 | + logIndex, |
| 83 | + verification: { |
| 84 | + /* eslint-disable-next-line max-len */ |
| 85 | + signedEntryTimestamp: 'MEUCIQD6CD7ZNLUipFoxzmSL/L8Ewic4SRkXN77UjfJZ7d/wAAIgatokSuX9Rg0iWxAgSfHMtcsagtDCQalU5IvXdQ+yLEA=', |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + const oidcSrv = MockRegistry.tnock(t, oidcURL) |
| 91 | + oidcSrv.get('/?audience=sigstore', undefined, { |
| 92 | + authorization: `Bearer ${requestToken}`, |
| 93 | + }).reply(200, { value: idToken }) |
| 94 | + |
| 95 | + const fulcioSrv = MockRegistry.tnock(t, fulcioURL) |
| 96 | + fulcioSrv.matchHeader('Content-Type', 'application/json') |
| 97 | + .post('/api/v2/signingCert', { |
| 98 | + credentials: { oidcIdentityToken: idToken }, |
| 99 | + publicKeyRequest: { |
| 100 | + publicKey: { |
| 101 | + algorithm: 'ECDSA', |
| 102 | + content: /.+/i, |
| 103 | + }, |
| 104 | + proofOfPossession: /.+/i, |
| 105 | + }, |
| 106 | + }) |
| 107 | + .reply(200, certificateResponse) |
| 108 | + |
| 109 | + const rekorSrv = MockRegistry.tnock(t, rekorURL) |
| 110 | + rekorSrv |
| 111 | + .matchHeader('Accept', 'application/json') |
| 112 | + .matchHeader('Content-Type', 'application/json') |
| 113 | + .post('/api/v1/log/entries') |
| 114 | + .reply(201, rekorEntry) |
| 115 | + |
| 116 | + return { |
| 117 | + fulcioURL: fulcioURL, |
| 118 | + rekorURL: rekorURL, |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +module.exports = { |
| 124 | + MockProvenance, |
| 125 | +} |
0 commit comments