@@ -13,76 +13,145 @@ setFailedMock.mockImplementation(() => {})
13
13
describe ( 'main' , ( ) => {
14
14
let outputs = { } as Record < string , string >
15
15
const originalEnv = process . env
16
- const issuer = 'https://token.actions.githubusercontent.com'
17
- const audience = 'nobody'
18
- const jwksPath = '/.well-known/jwks.json'
19
- const tokenPath = '/token'
20
-
21
- const claims = {
22
- iss : issuer ,
23
- aud : 'nobody' ,
24
- repository : 'owner/repo' ,
25
- ref : 'refs/heads/main' ,
26
- sha : 'babca52ab0c93ae16539e5923cb0d7403b9a093b' ,
27
- workflow_ref : 'owner/repo/.github/workflows/main.yml@main' ,
28
- job_workflow_ref : 'owner/shared/.github/workflows/build.yml@main' ,
29
- event_name : 'push' ,
30
- repository_id : 'repo-id' ,
31
- repository_owner_id : 'owner-id' ,
32
- run_id : 'run-id' ,
33
- run_attempt : 'run-attempt' ,
34
- runner_environment : 'github-hosted'
35
- }
36
-
37
- beforeEach ( async ( ) => {
16
+
17
+ beforeEach ( ( ) => {
38
18
jest . resetAllMocks ( )
39
19
40
20
setOutputMock . mockImplementation ( ( key , value ) => {
41
21
outputs [ key ] = value
42
22
} )
43
-
44
- process . env = {
45
- ...originalEnv ,
46
- ACTIONS_ID_TOKEN_REQUEST_URL : `${ issuer } ${ tokenPath } ?` ,
47
- ACTIONS_ID_TOKEN_REQUEST_TOKEN : 'token' ,
48
- GITHUB_SERVER_URL : 'https://github.com' ,
49
- GITHUB_REPOSITORY : claims . repository
50
- }
51
-
52
- // Generate JWT signing key
53
- const key = await jose . generateKeyPair ( 'PS256' )
54
-
55
- // Create JWK, JWKS, and JWT
56
- const kid = '12345'
57
- const jwk = await jose . exportJWK ( key . publicKey )
58
- const jwks = { keys : [ { ...jwk , kid } ] }
59
- const jwt = await new jose . SignJWT ( claims )
60
- . setProtectedHeader ( { alg : 'PS256' , kid } )
61
- . sign ( key . privateKey )
62
-
63
- // Mock OpenID configuration and JWKS endpoints
64
- nock ( issuer )
65
- . get ( '/.well-known/openid-configuration' )
66
- . reply ( 200 , { jwks_uri : `${ issuer } ${ jwksPath } ` } )
67
- nock ( issuer ) . get ( jwksPath ) . reply ( 200 , jwks )
68
-
69
- // Mock OIDC token endpoint for populating the provenance
70
- nock ( issuer ) . get ( tokenPath ) . query ( { audience } ) . reply ( 200 , { value : jwt } )
71
23
} )
72
24
73
25
afterEach ( ( ) => {
74
26
outputs = { }
75
27
process . env = originalEnv
76
28
} )
77
29
78
- it ( 'successfully run main' , async ( ) => {
79
- // Run the main function
80
- await main . run ( )
30
+ describe ( 'when the default OIDC issuer is used' , ( ) => {
31
+ const issuer = 'https://token.actions.githubusercontent.com'
32
+ const audience = 'nobody'
33
+ const jwksPath = '/.well-known/jwks.json'
34
+ const tokenPath = '/token'
35
+
36
+ const claims = {
37
+ iss : issuer ,
38
+ aud : 'nobody' ,
39
+ repository : 'owner/repo' ,
40
+ ref : 'refs/heads/main' ,
41
+ sha : 'babca52ab0c93ae16539e5923cb0d7403b9a093b' ,
42
+ workflow_ref : 'owner/repo/.github/workflows/main.yml@main' ,
43
+ job_workflow_ref : 'owner/shared/.github/workflows/build.yml@main' ,
44
+ event_name : 'push' ,
45
+ repository_id : 'repo-id' ,
46
+ repository_owner_id : 'owner-id' ,
47
+ run_id : 'run-id' ,
48
+ run_attempt : 'run-attempt' ,
49
+ runner_environment : 'github-hosted'
50
+ }
51
+
52
+ beforeEach ( async ( ) => {
53
+ process . env = {
54
+ ...originalEnv ,
55
+ ACTIONS_ID_TOKEN_REQUEST_URL : `${ issuer } ${ tokenPath } ?` ,
56
+ ACTIONS_ID_TOKEN_REQUEST_TOKEN : 'token' ,
57
+ GITHUB_SERVER_URL : 'https://github.com' ,
58
+ GITHUB_REPOSITORY : claims . repository
59
+ }
60
+
61
+ // Generate JWT signing key
62
+ const key = await jose . generateKeyPair ( 'PS256' )
63
+
64
+ // Create JWK, JWKS, and JWT
65
+ const kid = '12345'
66
+ const jwk = await jose . exportJWK ( key . publicKey )
67
+ const jwks = { keys : [ { ...jwk , kid } ] }
68
+ const jwt = await new jose . SignJWT ( claims )
69
+ . setProtectedHeader ( { alg : 'PS256' , kid } )
70
+ . sign ( key . privateKey )
71
+
72
+ // Mock OpenID configuration and JWKS endpoints
73
+ nock ( issuer )
74
+ . get ( '/.well-known/openid-configuration' )
75
+ . reply ( 200 , { jwks_uri : `${ issuer } ${ jwksPath } ` } )
76
+ nock ( issuer ) . get ( jwksPath ) . reply ( 200 , jwks )
77
+
78
+ // Mock OIDC token endpoint for populating the provenance
79
+ nock ( issuer ) . get ( tokenPath ) . query ( { audience } ) . reply ( 200 , { value : jwt } )
80
+ } )
81
+
82
+ it ( 'successfully run main' , async ( ) => {
83
+ // Run the main function
84
+ await main . run ( )
85
+
86
+ // Verify that outputs were set correctly
87
+ expect ( setOutputMock ) . toHaveBeenCalledTimes ( 2 )
88
+
89
+ expect ( outputs [ 'predicate' ] ) . toMatchSnapshot ( )
90
+ expect ( outputs [ 'predicate-type' ] ) . toBe ( 'https://slsa.dev/provenance/v1' )
91
+ } )
92
+ } )
93
+
94
+ describe ( 'when a non-default OIDC issuer is used' , ( ) => {
95
+ const issuer = 'https://token.actions.example-01.ghe.com'
96
+ const audience = 'nobody'
97
+ const jwksPath = '/.well-known/jwks.json'
98
+ const tokenPath = '/token'
99
+
100
+ const claims = {
101
+ iss : issuer ,
102
+ aud : 'nobody' ,
103
+ repository : 'owner/repo' ,
104
+ ref : 'refs/heads/main' ,
105
+ sha : 'babca52ab0c93ae16539e5923cb0d7403b9a093b' ,
106
+ workflow_ref : 'owner/repo/.github/workflows/main.yml@main' ,
107
+ job_workflow_ref : 'owner/shared/.github/workflows/build.yml@main' ,
108
+ event_name : 'push' ,
109
+ repository_id : 'repo-id' ,
110
+ repository_owner_id : 'owner-id' ,
111
+ run_id : 'run-id' ,
112
+ run_attempt : 'run-attempt' ,
113
+ runner_environment : 'github-hosted'
114
+ }
115
+
116
+ beforeEach ( async ( ) => {
117
+ process . env = {
118
+ ...originalEnv ,
119
+ ACTIONS_ID_TOKEN_REQUEST_URL : `${ issuer } ${ tokenPath } ?` ,
120
+ ACTIONS_ID_TOKEN_REQUEST_TOKEN : 'token' ,
121
+ GITHUB_SERVER_URL : 'https://example-01.ghe.com' ,
122
+ GITHUB_REPOSITORY : claims . repository
123
+ }
81
124
82
- // Verify that outputs were set correctly
83
- expect ( setOutputMock ) . toHaveBeenCalledTimes ( 2 )
125
+ // Generate JWT signing key
126
+ const key = await jose . generateKeyPair ( 'PS256' )
84
127
85
- expect ( outputs [ 'predicate' ] ) . toMatchSnapshot ( )
86
- expect ( outputs [ 'predicate-type' ] ) . toBe ( 'https://slsa.dev/provenance/v1' )
128
+ // Create JWK, JWKS, and JWT
129
+ const kid = '12345'
130
+ const jwk = await jose . exportJWK ( key . publicKey )
131
+ const jwks = { keys : [ { ...jwk , kid } ] }
132
+ const jwt = await new jose . SignJWT ( claims )
133
+ . setProtectedHeader ( { alg : 'PS256' , kid } )
134
+ . sign ( key . privateKey )
135
+
136
+ // Mock OpenID configuration and JWKS endpoints
137
+ nock ( issuer )
138
+ . get ( '/.well-known/openid-configuration' )
139
+ . reply ( 200 , { jwks_uri : `${ issuer } ${ jwksPath } ` } )
140
+ nock ( issuer ) . get ( jwksPath ) . reply ( 200 , jwks )
141
+
142
+ // Mock OIDC token endpoint for populating the provenance
143
+ nock ( issuer ) . get ( tokenPath ) . query ( { audience } ) . reply ( 200 , { value : jwt } )
144
+ } )
145
+
146
+ it ( 'successfully run main' , async ( ) => {
147
+ // Run the main function
148
+ await main . run ( )
149
+
150
+ // Verify that outputs were set correctly
151
+ expect ( setOutputMock ) . toHaveBeenCalledTimes ( 2 )
152
+
153
+ expect ( outputs [ 'predicate' ] ) . toMatchSnapshot ( )
154
+ expect ( outputs [ 'predicate-type' ] ) . toBe ( 'https://slsa.dev/provenance/v1' )
155
+ } )
87
156
} )
88
157
} )
0 commit comments