Skip to content

Commit 245e055

Browse files
committed
test for provenance content
Signed-off-by: Brian DeHamer <bdehamer@github.com>
1 parent 128c542 commit 245e055

File tree

3 files changed

+49
-93
lines changed

3 files changed

+49
-93
lines changed

workspaces/libnpmpublish/lib/provenance.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const BUILD_TYPE_VERSION = 'v2'
1010

1111
const generateProvenance = async (subject, opts) => {
1212
const { env } = process
13+
/* istanbul ignore next - not covering missing env var case */
1314
const [workflowPath] = (env.GITHUB_WORKFLOW_REF || '')
1415
.replace(env.GITHUB_REPOSITORY + '/', '')
1516
.split('@')

workspaces/libnpmpublish/test/provenance.js

-92
This file was deleted.

workspaces/libnpmpublish/test/publish.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,47 @@ t.test('other error code', async t => {
599599
})
600600

601601
t.test('publish existing package with provenance in gha', async t => {
602+
// Environment variables
602603
const oidcURL = 'https://mock.oidc'
603604
const requestToken = 'decafbad'
605+
const workflowPath = '.github/workflows/publish.yml'
606+
const repository = 'github/foo'
607+
const serverUrl = 'https://github.com'
608+
const ref = 'refs/heads/main'
609+
const sha = 'deadbeef'
610+
const runID = '123456'
611+
const runAttempt = '1'
612+
604613
// Set-up GHA environment variables
605614
mockGlobals(t, {
606615
'process.env': {
607616
CI: true,
608617
GITHUB_ACTIONS: true,
609618
ACTIONS_ID_TOKEN_REQUEST_URL: oidcURL,
610619
ACTIONS_ID_TOKEN_REQUEST_TOKEN: requestToken,
620+
GITHUB_WORKFLOW_REF: `${repository}/${workflowPath}@${ref}`,
621+
GITHUB_REPOSITORY: repository,
622+
GITHUB_SERVER_URL: serverUrl,
623+
GITHUB_REF: ref,
624+
GITHUB_SHA: sha,
625+
GITHUB_RUN_ID: runID,
626+
GITHUB_RUN_ATTEMPT: runAttempt,
611627
},
612628
})
629+
630+
const expectedSubject = {
631+
name: 'pkg:npm/%40npmcli/libnpmpublish-test@1.0.0',
632+
digest: {
633+
sha512: integrity.sha512[0].hexDigest(),
634+
},
635+
}
636+
637+
const expectedConfigSource = {
638+
uri: `git+${serverUrl}/${repository}@${ref}`,
639+
digest: { sha1: sha },
640+
entryPoint: workflowPath,
641+
}
642+
613643
const { publish } = t.mock('..', { 'ci-info': t.mock('ci-info') })
614644
const registry = new MockRegistry({
615645
tap: t,
@@ -732,7 +762,24 @@ t.test('publish existing package with provenance in gha', async t => {
732762

733763
registry.getVisibility({ spec, visibility: { public: true } })
734764
registry.nock.put(`/${spec.escapedName}`, body => {
735-
return t.match(body, packument, 'posted packument matches expectations')
765+
const bundleAttachment = body._attachments['@npmcli/libnpmpublish-test-1.0.0.sigstore']
766+
const bundle = JSON.parse(bundleAttachment.data)
767+
const provenance = JSON.parse(Buffer.from(bundle.dsseEnvelope.payload, 'base64').toString())
768+
769+
t.hasStrict(body, packument, 'posted packument matches expectations')
770+
t.hasStrict(provenance.subject[0],
771+
expectedSubject,
772+
'provenance subject matches expectations')
773+
t.hasStrict(provenance.predicate.buildType,
774+
'https://github.com/npm/cli/gha/v2',
775+
'provenance subject matches expectations')
776+
t.hasStrict(provenance.predicate.builder.id,
777+
'https://github.com/actions/runner',
778+
'provenance subject matches expectations')
779+
t.hasStrict(provenance.predicate.invocation.configSource,
780+
expectedConfigSource,
781+
'configSource matches expectations')
782+
return true
736783
}).reply(201, {})
737784

738785
const ret = await publish(manifest, tarData, {

0 commit comments

Comments
 (0)