1
0
Fork 0

Resolved Comments

pull/887/head
Sourav Chanduka 2021-07-29 12:17:22 +05:30
parent ff90431d27
commit 0c1cb726c3
6 changed files with 96 additions and 98 deletions

View File

@ -1,6 +1,8 @@
var httpclient = require('@actions/http-client')
var configvar = require('./../src/internal/config-variables')
var main = require('./../src/main')
function getTokenEndPoint() {
return 'https://vstoken.actions.githubusercontent.com/.well-known/openid-configuration'
}
describe('oidc-client-tests', () => {
it('Get Http Client', async () => {
@ -10,23 +12,7 @@ describe('oidc-client-tests', () => {
it('HTTP get request to get token endpoint', async () => {
const http = new httpclient.HttpClient('actions/oidc-client')
const res = await http.get(
'https://ghactionsoidc.azurewebsites.net/.well-known/openid-configuration'
)
const res = await http.get(getTokenEndPoint())
expect(res.message.statusCode).toBe(200)
})
it('Get token endpoint', async () => {
let url = await configvar.getIDTokenUrl()
expect(url).toBeDefined()
})
it('Fetch Id token', async () => {
var id_token = main.getIDToken('helloworld')
expect(id_token).toBeDefined()
})
})
/*test('HTTP get request to get token endpoint', async () => {
expect(1).toBe(1)
})*/

View File

@ -8,7 +8,7 @@ require('./sourcemap-register.js');module.exports =
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getRuntimeToken = exports.getIDTokenUrl = void 0;
exports.getIDTokenUrl = exports.getRuntimeToken = void 0;
const utils_1 = __webpack_require__(519);
function getRuntimeToken() {
const token = process.env['ACTIONS_RUNTIME_TOKEN'];
@ -100,38 +100,48 @@ const core = __importStar(__webpack_require__(186));
const actions_http_client = __importStar(__webpack_require__(925));
const utils_1 = __webpack_require__(519);
const config_variables_1 = __webpack_require__(463);
function postCall(id_token_url, audience) {
return __awaiter(this, void 0, void 0, function* () {
const httpclient = utils_1.createHttpClient();
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `);
}
core.debug(`Httpclient created ${httpclient} `); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
const additionalHeaders = {};
additionalHeaders[actions_http_client.Headers.ContentType] =
actions_http_client.MediaTypes.ApplicationJson;
additionalHeaders[actions_http_client.Headers.Accept] =
actions_http_client.MediaTypes.ApplicationJson;
core.debug(`audience is ${audience !== null ? audience : 'null'}`);
const data = audience !== null ? JSON.stringify({ aud: audience }) : '';
const response = yield httpclient.post(id_token_url, data, additionalHeaders);
if (!utils_1.isSuccessStatusCode(response.message.statusCode)) {
throw new Error(`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`);
}
let body = yield response.readBody();
return body;
});
}
function parseJson(body) {
const val = JSON.parse(body);
let id_token = '';
if ('value' in val) {
id_token = val['value'];
}
else {
throw new Error('Response json body do not have ID Token field');
}
core.debug(`id_token : ${id_token}`);
return id_token;
}
function getIDToken(audience) {
return __awaiter(this, void 0, void 0, function* () {
try {
// New ID Token is requested from action service
let id_token_url = config_variables_1.getIDTokenUrl();
core.debug(`ID token url is ${id_token_url}`);
const httpclient = utils_1.createHttpClient();
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `);
}
core.debug(`Httpclient created ${httpclient} `); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
const additionalHeaders = {};
additionalHeaders[actions_http_client.Headers.ContentType] =
actions_http_client.MediaTypes.ApplicationJson;
additionalHeaders[actions_http_client.Headers.Accept] =
actions_http_client.MediaTypes.ApplicationJson;
core.debug(`audience is ${(audience !== null) ? audience : "null"}`);
const data = (audience !== null) ? JSON.stringify({ aud: audience }) : '';
const response = yield httpclient.post(id_token_url, data, additionalHeaders);
if (!utils_1.isSuccessStatusCode(response.message.statusCode)) {
throw new Error(`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`);
}
const body = yield response.readBody();
const val = JSON.parse(body);
let id_token = '';
if ('value' in val) {
id_token = val['value'];
}
else {
throw new Error('Response json body do not have ID Token field');
}
core.debug(`id_token : ${id_token}`);
let body = yield postCall(id_token_url, audience);
let id_token = parseJson(body);
return id_token;
}
catch (error) {

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
import {getApiVersion} from './utils'
function getRuntimeToken(): string {
export function getRuntimeToken(){
const token = process.env['ACTIONS_RUNTIME_TOKEN']
if (!token) {
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
@ -8,13 +8,10 @@ function getRuntimeToken(): string {
return token
}
function getIDTokenUrl(): string {
export function getIDTokenUrl(){
const runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
if (!runtimeUrl) {
throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable')
}
return runtimeUrl + '?api-version=' + getApiVersion()
}
export {getIDTokenUrl, getRuntimeToken}
}

View File

@ -2,21 +2,19 @@ import {HttpClient} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/auth'
import {getRuntimeToken} from './config-variables'
function isSuccessStatusCode(statusCode?: number): boolean {
export function isSuccessStatusCode(statusCode?: number): boolean {
if (!statusCode) {
return false
}
return statusCode >= 200 && statusCode < 300
}
function createHttpClient(): HttpClient {
export function createHttpClient(): HttpClient {
return new HttpClient('actions/oidc-client', [
new BearerCredentialHandler(getRuntimeToken())
])
}
function getApiVersion(): string {
export function getApiVersion(): string {
return '2.0'
}
export {isSuccessStatusCode,createHttpClient,getApiVersion}
}

View File

@ -4,6 +4,50 @@ import {IHeaders} from '@actions/http-client/interfaces'
import {createHttpClient, isSuccessStatusCode} from './internal/utils'
import {getIDTokenUrl} from './internal/config-variables'
async function postCall(
id_token_url: string,
audience: string
): Promise<string> {
const httpclient = createHttpClient()
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `)
}
core.debug(`Httpclient created ${httpclient} `) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
const additionalHeaders: IHeaders = {}
additionalHeaders[actions_http_client.Headers.ContentType] =
actions_http_client.MediaTypes.ApplicationJson
additionalHeaders[actions_http_client.Headers.Accept] =
actions_http_client.MediaTypes.ApplicationJson
core.debug(`audience is ${audience !== null ? audience : 'null'}`)
const data: string = audience !== null ? JSON.stringify({aud: audience}) : ''
const response = await httpclient.post(id_token_url, data, additionalHeaders)
if (!isSuccessStatusCode(response.message.statusCode)) {
throw new Error(
`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`
)
}
let body: string = await response.readBody()
return body
}
function parseJson(body: string): string {
const val = JSON.parse(body)
let id_token = ''
if ('value' in val) {
id_token = val['value']
} else {
throw new Error('Response json body do not have ID Token field')
}
core.debug(`id_token : ${id_token}`)
return id_token
}
export async function getIDToken(audience: string): Promise<string> {
try {
// New ID Token is requested from action service
@ -11,46 +55,9 @@ export async function getIDToken(audience: string): Promise<string> {
core.debug(`ID token url is ${id_token_url}`)
const httpclient = createHttpClient()
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `)
}
let body: string = await postCall(id_token_url, audience)
core.debug(`Httpclient created ${httpclient} `) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
const additionalHeaders: IHeaders = {}
additionalHeaders[actions_http_client.Headers.ContentType] =
actions_http_client.MediaTypes.ApplicationJson
additionalHeaders[actions_http_client.Headers.Accept] =
actions_http_client.MediaTypes.ApplicationJson
core.debug(`audience is ${audience !== null ? audience : 'null'}`)
const data: string =
audience !== null ? JSON.stringify({aud: audience}) : ''
const response = await httpclient.post(
id_token_url,
data,
additionalHeaders
)
if (!isSuccessStatusCode(response.message.statusCode)) {
throw new Error(
`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`
)
}
const body: string = await response.readBody()
const val = JSON.parse(body)
let id_token = ''
if ('value' in val) {
id_token = val['value']
} else {
throw new Error('Response json body do not have ID Token field')
}
core.debug(`id_token : ${id_token}`)
let id_token = parseJson(body)
return id_token
} catch (error) {