Skip to content

Commit b6ccc05

Browse files
authored
Azure - create a custom chained token credential to place the AzureCLICredential prior to the ManagedIdentityCredential (#1009)
1 parent 33daacd commit b6ccc05

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/auth.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AzureCliCredential, AzureDeveloperCliCredential, AzurePowerShellCredential, ChainedTokenCredential, EnvironmentCredential, ManagedIdentityCredential } from "@azure/identity";
2+
3+
function createChainedTokenCredential(): ChainedTokenCredential {
4+
return new ChainedTokenCredential(
5+
new EnvironmentCredential(),
6+
new AzureCliCredential(),
7+
new ManagedIdentityCredential({ clientId: process.env.AZURE_CLIENT_ID }),
8+
new AzurePowerShellCredential({ tenantId: process.env.AZURE_TENANT_ID }),
9+
new AzureDeveloperCliCredential({ tenantId: process.env.AZURE_TENANT_ID })
10+
);
11+
}
12+
13+
export async function getAzureCredentialAccessToken(): Promise<string> {
14+
try {
15+
const credential = createChainedTokenCredential()
16+
const token = await credential.getToken('499b84ac-1321-427f-aa17-267ca6975798/.default', {
17+
tenantId: process.env.AZURE_TENANT_ID
18+
});
19+
20+
return token.token;
21+
} catch (error) {
22+
throw new Error('Can not acquire a Microsoft Entra ID access token. Additional information:\n\n' + error)
23+
}
24+
}

src/publish.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { ExtensionQueryFlags, PublishedExtension } from 'azure-devops-node-api/i
55
import { pack, readManifest, versionBump, prepublish, signPackage, createSignatureArchive } from './package';
66
import * as tmp from 'tmp';
77
import { IVerifyPatOptions, getPublisher } from './store';
8-
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest, getAzureCredentialAccessToken } from './util';
8+
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest } from './util';
99
import { Manifest } from './manifest';
1010
import { readVSIXPackage } from './zip';
1111
import { validatePublisher } from './validation';
1212
import { GalleryApi } from 'azure-devops-node-api/GalleryApi';
1313
import FormData from 'form-data';
1414
import { basename } from 'path';
1515
import { IterableBackoff, handleWhen, retry } from 'cockatiel';
16+
import { getAzureCredentialAccessToken } from './auth';
1617

1718
const tmpName = promisify(tmp.tmpName);
1819

src/util.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { PublicGalleryAPI } from './publicgalleryapi';
77
import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi';
88
import { Manifest } from './manifest';
99
import { EOL } from 'os';
10-
import { DefaultAzureCredential } from '@azure/identity';
1110

1211
const __read = promisify<_read.Options, string>(_read);
1312
export function read(prompt: string, options: _read.Options = {}): Promise<string> {
@@ -51,16 +50,6 @@ export function getPublicGalleryAPI() {
5150
return new PublicGalleryAPI(marketplaceUrl, '3.0-preview.1');
5251
}
5352

54-
export async function getAzureCredentialAccessToken(): Promise<string> {
55-
try {
56-
const credential = new DefaultAzureCredential();
57-
const token = await credential.getToken('499b84ac-1321-427f-aa17-267ca6975798/.default');
58-
return token.token;
59-
} catch (error) {
60-
throw new Error('Can not acquire a Microsoft Entra ID access token. Additional information:\n\n' + error)
61-
}
62-
}
63-
6453
export function normalize(path: string): string {
6554
return path.replace(/\\/g, '/');
6655
}

0 commit comments

Comments
 (0)