Skip to content

Commit 04f7c52

Browse files
Move azCliUtils from artifacts-common to azure-arm-rest (#340)
* Moved azCliUtils from artifacts-common to azure-arm-rest.
1 parent 8eb6d84 commit 04f7c52

File tree

8 files changed

+363
-172
lines changed

8 files changed

+363
-172
lines changed

common-npm-packages/artifacts-common/azCliUtils.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

common-npm-packages/artifacts-common/package-lock.json

Lines changed: 281 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common-npm-packages/artifacts-common/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-tasks-artifacts-common",
3-
"version": "2.241.1",
3+
"version": "2.242.0",
44
"description": "Azure Artifacts common code (for new authentication tasks)",
55
"scripts": {
66
"build": "cd ../build-scripts && npm install && cd ../artifacts-common && node make.js"
@@ -15,7 +15,7 @@
1515
"@types/fs-extra": "8.0.0",
1616
"@types/mocha": "^5.2.6",
1717
"@types/node": "^16.11.39",
18-
"azure-devops-node-api": "12.0.0",
18+
"azure-devops-node-api": "14.0.1",
1919
"azure-pipelines-task-lib": "^4.13.0",
2020
"fs-extra": "8.1.0",
2121
"semver": "6.3.0"

common-npm-packages/artifacts-common/webapi.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ import * as api from 'azure-devops-node-api';
22
import { IRequestOptions } from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
33
export declare function getWebApiWithProxy(serviceUri: string, accessToken: string, options?: IRequestOptions): api.WebApi;
44
export declare function getSystemAccessToken(): string;
5-
export declare function getFederatedToken(connectedServiceName: string): Promise<string>;
Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import Q = require('q');
21
import path = require("path");
32
import * as api from 'azure-devops-node-api';
4-
import { getHandlerFromToken, WebApi } from "azure-devops-node-api";
5-
import { ITaskApi } from "azure-devops-node-api/TaskApi";
6-
import { TaskHubOidcToken } from "azure-devops-node-api/interfaces/TaskAgentInterfaces";
73
import { IRequestOptions } from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
84
import * as tl from 'azure-pipelines-task-lib/task';
95

@@ -33,63 +29,3 @@ export function getSystemAccessToken(): string {
3329
}
3430
}
3531

36-
export async function getFederatedToken(connectedServiceName: string): Promise<string> {
37-
const projectId: string = tl.getVariable("System.TeamProjectId");
38-
const hub: string = tl.getVariable("System.HostType");
39-
const planId: string = tl.getVariable('System.PlanId');
40-
const jobId: string = tl.getVariable('System.JobId');
41-
let uri = tl.getVariable("System.CollectionUri");
42-
if (!uri) {
43-
uri = tl.getVariable("System.TeamFoundationServerUri");
44-
}
45-
46-
const token = getSystemAccessToken();
47-
const authHandler = getHandlerFromToken(token);
48-
const connection = new WebApi(uri, authHandler);
49-
const oidc_token: string = await initOIDCToken(
50-
connection,
51-
projectId,
52-
hub,
53-
planId,
54-
jobId,
55-
connectedServiceName,
56-
0,
57-
2000);
58-
59-
tl.setSecret(oidc_token);
60-
61-
return oidc_token;
62-
}
63-
64-
function initOIDCToken(connection: WebApi, projectId: string, hub: string, planId: string, jobId: string, serviceConnectionId: string, retryCount: number, timeToWait: number): Q.Promise<string> {
65-
var deferred = Q.defer<string>();
66-
connection.getTaskApi().then(
67-
(taskApi: ITaskApi) => {
68-
taskApi.createOidcToken({}, projectId, hub, planId, jobId, serviceConnectionId).then(
69-
(response: TaskHubOidcToken) => {
70-
if (response != null) {
71-
tl.debug('Got OIDC token');
72-
deferred.resolve(response.oidcToken);
73-
}
74-
else if (response.oidcToken == null) {
75-
if (retryCount < 3) {
76-
let waitedTime = timeToWait;
77-
retryCount += 1;
78-
setTimeout(() => {
79-
deferred.resolve(initOIDCToken(connection, projectId, hub, planId, jobId, serviceConnectionId, retryCount, waitedTime));
80-
}, waitedTime);
81-
}
82-
else {
83-
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD'));
84-
}
85-
}
86-
},
87-
(error) => {
88-
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD') + " " + error);
89-
}
90-
);
91-
}
92-
);
93-
94-
return deferred.promise;
95-
}

common-npm-packages/artifacts-common/azCliUtils.ts renamed to common-npm-packages/azure-arm-rest/azCliUtility.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import fs = require("fs");
22
import path = require("path");
33
import * as tl from 'azure-pipelines-task-lib/task';
44
import { IExecSyncResult } from 'azure-pipelines-task-lib/toolrunner';
5-
import { getFederatedToken } from './webapi';
5+
import { getHandlerFromToken, WebApi } from "azure-devops-node-api";
6+
import { ITaskApi } from "azure-devops-node-api/TaskApi";
7+
import { TaskHubOidcToken } from "azure-devops-node-api/interfaces/TaskAgentInterfaces";
8+
import Q = require('q');
69

710
tl.setResourcePath(path.join(__dirname, 'module.json'), true);
811

@@ -73,4 +76,77 @@ function throwIfError(resultOfToolExecution: IExecSyncResult, errormsg?: string)
7376
}
7477
throw resultOfToolExecution;
7578
}
79+
}
80+
81+
function getSystemAccessToken(): string {
82+
tl.debug('Getting credentials for account feeds');
83+
let auth = tl.getEndpointAuthorization('SYSTEMVSSCONNECTION', false);
84+
if (auth && auth.scheme === 'OAuth') {
85+
tl.debug('Got auth token, setting it as secret so it does not print in console log');
86+
tl.setSecret(auth.parameters['AccessToken']);
87+
return auth.parameters['AccessToken'];
88+
}
89+
tl.warning(tl.loc('FeedTokenUnavailable'));
90+
return '';
91+
}
92+
93+
async function getFederatedToken(connectedServiceName: string): Promise<string> {
94+
const projectId: string = tl.getVariable("System.TeamProjectId");
95+
const hub: string = tl.getVariable("System.HostType");
96+
const planId: string = tl.getVariable('System.PlanId');
97+
const jobId: string = tl.getVariable('System.JobId');
98+
let uri = tl.getVariable("System.CollectionUri");
99+
if (!uri) {
100+
uri = tl.getVariable("System.TeamFoundationServerUri");
101+
}
102+
103+
const token = getSystemAccessToken();
104+
const authHandler = getHandlerFromToken(token);
105+
const connection = new WebApi(uri, authHandler);
106+
const oidc_token: string = await initOIDCToken(
107+
connection,
108+
projectId,
109+
hub,
110+
planId,
111+
jobId,
112+
connectedServiceName,
113+
0,
114+
2000);
115+
116+
tl.setSecret(oidc_token);
117+
118+
return oidc_token;
119+
}
120+
121+
function initOIDCToken(connection: WebApi, projectId: string, hub: string, planId: string, jobId: string, serviceConnectionId: string, retryCount: number, timeToWait: number): Q.Promise<string> {
122+
var deferred = Q.defer<string>();
123+
connection.getTaskApi().then(
124+
(taskApi: ITaskApi) => {
125+
taskApi.createOidcToken({}, projectId, hub, planId, jobId, serviceConnectionId).then(
126+
(response: TaskHubOidcToken) => {
127+
if (response != null) {
128+
tl.debug('Got OIDC token');
129+
deferred.resolve(response.oidcToken);
130+
}
131+
else if (response.oidcToken == null) {
132+
if (retryCount < 3) {
133+
let waitedTime = timeToWait;
134+
retryCount += 1;
135+
setTimeout(() => {
136+
deferred.resolve(initOIDCToken(connection, projectId, hub, planId, jobId, serviceConnectionId, retryCount, waitedTime));
137+
}, waitedTime);
138+
}
139+
else {
140+
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD'));
141+
}
142+
}
143+
},
144+
(error) => {
145+
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD') + " " + error);
146+
}
147+
);
148+
}
149+
);
150+
151+
return deferred.promise;
76152
}

common-npm-packages/azure-arm-rest/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common-npm-packages/azure-arm-rest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-tasks-azure-arm-rest",
3-
"version": "3.242.0",
3+
"version": "3.242.1",
44
"description": "Common Lib for Azure ARM REST apis",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)