Skip to content

Commit 0775fb3

Browse files
authored
Add GitHub context variables to OIDC token exchange payload (jfrog#283)
1 parent 29fa519 commit 0775fb3

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

lib/oidc-utils.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class OidcUtils {
9797
throw new Error('Missing one or more required fields: OIDC provider name, token ID, or JFrog Platform URL.');
9898
}
9999
const args = ['eot', creds.oidcProviderName, creds.oidcTokenId, '--url', creds.jfrogUrl];
100-
if (creds.oidcAudience !== "") {
100+
if (creds.oidcAudience !== '') {
101101
args.push('--oidc-audience', creds.oidcAudience);
102102
}
103103
core.debug('Running CLI command: ' + args.join(' '));
@@ -223,7 +223,7 @@ class OidcUtils {
223223
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
224224
}
225225
static buildOidcTokenExchangePayload(jwt, providerName, applicationKey) {
226-
var _a, _b, _c, _d;
226+
var _a, _b, _c, _d, _e, _f, _g, _h;
227227
return {
228228
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
229229
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token',
@@ -233,9 +233,23 @@ class OidcUtils {
233233
gh_job_id: (_b = process.env.GITHUB_JOB) !== null && _b !== void 0 ? _b : '',
234234
gh_run_id: (_c = process.env.GITHUB_RUN_ID) !== null && _c !== void 0 ? _c : '',
235235
gh_repo: (_d = process.env.GITHUB_REPOSITORY) !== null && _d !== void 0 ? _d : '',
236+
gh_revision: (_e = process.env.GITHUB_SHA) !== null && _e !== void 0 ? _e : '',
237+
gh_branch: (_f = process.env.GITHUB_REF_NAME) !== null && _f !== void 0 ? _f : '',
236238
application_key: applicationKey,
239+
context: {
240+
vcs_commit: {
241+
vcs_url: this.buildVcsUrl(),
242+
branch: (_g = process.env.GITHUB_REF_NAME) !== null && _g !== void 0 ? _g : '',
243+
revision: (_h = process.env.GITHUB_SHA) !== null && _h !== void 0 ? _h : '',
244+
},
245+
},
237246
};
238247
}
248+
static buildVcsUrl() {
249+
const serverUrl = process.env.GITHUB_SERVER_URL;
250+
const repo = process.env.GITHUB_REPOSITORY;
251+
return serverUrl && repo ? `${serverUrl}/${repo}` : '';
252+
}
239253
/**
240254
* Retrieves the application key from .jfrog/config file.
241255
*

src/oidc-utils.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export class OidcUtils {
7474
throw new Error('Missing one or more required fields: OIDC provider name, token ID, or JFrog Platform URL.');
7575
}
7676

77-
const args = ['eot', creds.oidcProviderName, creds.oidcTokenId, '--url', creds.jfrogUrl];
78-
if (creds.oidcAudience !== "") {
77+
const args: string[] = ['eot', creds.oidcProviderName, creds.oidcTokenId, '--url', creds.jfrogUrl];
78+
if (creds.oidcAudience !== '') {
7979
args.push('--oidc-audience', creds.oidcAudience);
8080
}
8181
core.debug('Running CLI command: ' + args.join(' '));
@@ -211,7 +211,7 @@ export class OidcUtils {
211211
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
212212
}
213213

214-
private static buildOidcTokenExchangePayload(jwt: string, providerName: string, applicationKey: string): Record<string, string> {
214+
private static buildOidcTokenExchangePayload(jwt: string, providerName: string, applicationKey: string): Record<string, any> {
215215
return {
216216
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
217217
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token',
@@ -221,10 +221,25 @@ export class OidcUtils {
221221
gh_job_id: process.env.GITHUB_JOB ?? '',
222222
gh_run_id: process.env.GITHUB_RUN_ID ?? '',
223223
gh_repo: process.env.GITHUB_REPOSITORY ?? '',
224+
gh_revision: process.env.GITHUB_SHA ?? '',
225+
gh_branch: process.env.GITHUB_REF_NAME ?? '',
224226
application_key: applicationKey,
227+
context: {
228+
vcs_commit: {
229+
vcs_url: this.buildVcsUrl(),
230+
branch: process.env.GITHUB_REF_NAME ?? '',
231+
revision: process.env.GITHUB_SHA ?? '',
232+
},
233+
},
225234
};
226235
}
227236

237+
private static buildVcsUrl(): string {
238+
const serverUrl: string | undefined = process.env.GITHUB_SERVER_URL;
239+
const repo: string | undefined = process.env.GITHUB_REPOSITORY;
240+
return serverUrl && repo ? `${serverUrl}/${repo}` : '';
241+
}
242+
228243
/**
229244
* Retrieves the application key from .jfrog/config file.
230245
*

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface JfrogCredentials {
1212
accessToken?: string;
1313
oidcProviderName?: string;
1414
oidcTokenId?: string;
15-
oidcAudience : string;
15+
oidcAudience: string;
1616
}
1717

1818
/**

test/main.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('Collect JFrog Credentials from env vars exceptions', () => {
127127
expect(jfrogCredentials.accessToken).toBeUndefined();
128128
expect(jfrogCredentials.username).toBeUndefined();
129129
expect(jfrogCredentials.password).toBeUndefined();
130-
expect(jfrogCredentials.oidcAudience).toEqual("")
130+
expect(jfrogCredentials.oidcAudience).toEqual('');
131131
});
132132
});
133133

test/oidc-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('OidcUtils', (): void => {
8888
it('should throw if creds are missing required fields', async (): Promise<void> => {
8989
const incompleteCreds: JfrogCredentials = {
9090
jfrogUrl: 'https://example.jfrog.io',
91-
oidcAudience: ''
91+
oidcAudience: '',
9292
// missing provider and token ID
9393
};
9494

0 commit comments

Comments
 (0)