Skip to content

Commit 2bc3e56

Browse files
committed
Refactor
1 parent 5d58e11 commit 2bc3e56

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

jfrog-tasks-utils/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
"typings": "utils.d.ts",
1010
"dependencies": {
1111
"azure-pipelines-task-lib": "4.5.0",
12-
"azure-pipelines-tool-lib": "2.0.6",
1312
"azure-pipelines-tasks-java-common": "^2.219.1",
14-
"typed-rest-client": "^1.8.11",
15-
"sync-request": "^6.1.0"
13+
"azure-pipelines-tool-lib": "2.0.6",
14+
"typed-rest-client": "^1.8.11"
1615
},
1716
"scripts": {
1817
"test": "echo \"Error: no test specified\" && exit 1"

jfrog-tasks-utils/utils.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const buildAgent = 'jfrog-azure-devops-extension';
1919
const customFolderPath = encodePath(join(jfrogFolderPath, 'current'));
2020
const customCliPath = encodePath(join(customFolderPath, fileName)); // Optional - Customized jfrog-cli path.
2121
const jfrogCliReleasesUrl = 'https://releases.jfrog.io/artifactory/jfrog-cli/v2-jf';
22-
const request = require('sync-request');
22+
const HttpClient = require('typed-rest-client/HttpClient').HttpClient;
23+
2324
// Set by Tools Installer Task. This JFrog CLI version will be used in all tasks unless manual installation is used,
2425
// or a specific version was requested in a task. If not set, use the default CLI version.
2526
const pipelineRequestedCliVersionEnv = 'JFROG_CLI_PIPELINE_REQUESTED_VERSION_AZURE';
@@ -253,7 +254,7 @@ function configureXrayCliServer(xrayService, serverId, cliPath, buildDir) {
253254
return configureSpecificCliServer(xrayService, '--xray-url', serverId, cliPath, buildDir);
254255
}
255256

256-
function getADOIdToken(serviceConnectionID) {
257+
async function fetchAzureOidcToken(serviceConnectionID) {
257258
const uri = tl.getVariable('System.CollectionUri');
258259
const teamPrjID = tl.getVariable('System.TeamProjectId');
259260
const hub = tl.getVariable('System.HostType');
@@ -262,24 +263,34 @@ function getADOIdToken(serviceConnectionID) {
262263
const apiVersion = '7.1-preview.1';
263264

264265
const url = `${uri}${teamPrjID}/_apis/distributedtask/hubs/${hub}/plans/${planID}/jobs/${jobID}/oidctoken?api-version=${apiVersion}&serviceConnectionId=${serviceConnectionID}`;
266+
const token = tl.getVariable('System.AccessToken');
265267

266-
try {
267-
const response = request('POST', url, {
268-
headers: {
269-
'Content-Type': 'application/json',
270-
Authorization: `Bearer ${tl.getVariable('System.AccessToken')}`,
271-
},
272-
});
273-
274-
if (response.statusCode !== 200) {
275-
throw new Error(`HTTP request failed with status code ${response.statusCode}`);
276-
}
268+
if (!token) {
269+
throw new Error('System.AccessToken is not available. Make sure "Allow scripts to access OAuth token" is enabled.');
270+
}
271+
272+
const httpClient = new HttpClient('jfrog-azure-devops-extension');
273+
274+
const res = await httpClient.post(url, '', {
275+
'Content-Type': 'application/json',
276+
'Authorization': `Bearer ${token}`
277+
});
277278

278-
const parsedResponse = JSON.parse(response.getBody('utf8'));
279-
return parsedResponse.oidcToken;
280-
} catch (error) {
281-
throw new Error(`Failed to get or parse response: ${error.message}`);
279+
if (res.message.statusCode !== 200) {
280+
throw new Error(`OIDC token request failed: HTTP ${res.message.statusCode}`);
282281
}
282+
283+
const body = await res.readBody();
284+
285+
/** @type {{ oidcToken?: string }} */
286+
const parsed = JSON.parse(body);
287+
288+
if (!parsed.oidcToken) {
289+
throw new Error('OIDC token not found in response body.');
290+
}
291+
292+
tl.debug('Successfully fetched OIDC token from Azure DevOps.');
293+
return parsed.oidcToken;
283294
}
284295

285296
function configureSpecificCliServer(service, urlFlag, serverId, cliPath, buildDir) {
@@ -293,13 +304,13 @@ function configureSpecificCliServer(service, urlFlag, serverId, cliPath, buildDi
293304
let secretInStdinSupported = isStdinSecretSupported();
294305

295306
if (oidcProviderName) {
296-
const idToken = getADOIdToken(service);
297-
(cliCommand = cliJoin(
307+
const idToken = fetchAzureOidcToken(service);
308+
cliCommand = cliJoin(
298309
cliCommand,
299310
'--oidc-provider-name=' + (isWindows() ? quote(oidcProviderName) : singleQuote(oidcProviderName)),
300-
'--oidc-provider-type=' + 'Azure',
301-
)),
302-
'--oidc-token-id=' + (isWindows() ? quote(idToken) : singleQuote(idToken));
311+
'--oidc-provider-type=Azure',
312+
'--oidc-token-id=' + (isWindows() ? quote(idToken) : singleQuote(idToken))
313+
);
303314
return executeCliCommand(cliCommand, buildDir, { stdinSecret });
304315
}
305316

0 commit comments

Comments
 (0)