Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure - create a custom chained token credential to place the AzureCLICredential prior to the ManagedIdentityCredential #1009

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AzureCliCredential, AzureDeveloperCliCredential, AzurePowerShellCredential, ChainedTokenCredential, EnvironmentCredential, ManagedIdentityCredential } from "@azure/identity";

function createChainedTokenCredential(): ChainedTokenCredential {
return new ChainedTokenCredential(
new EnvironmentCredential(),
new AzureCliCredential(),
new ManagedIdentityCredential({ clientId: process.env.AZURE_CLIENT_ID }),
new AzurePowerShellCredential({ tenantId: process.env.AZURE_TENANT_ID }),
new AzureDeveloperCliCredential({ tenantId: process.env.AZURE_TENANT_ID })
);
}

export async function getAzureCredentialAccessToken(): Promise<string> {
try {
const credential = createChainedTokenCredential()
const token = await credential.getToken('499b84ac-1321-427f-aa17-267ca6975798/.default', {
tenantId: process.env.AZURE_TENANT_ID
});

return token.token;
} catch (error) {
throw new Error('Can not acquire a Microsoft Entra ID access token. Additional information:\n\n' + error)
}
}
3 changes: 2 additions & 1 deletion src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { ExtensionQueryFlags, PublishedExtension } from 'azure-devops-node-api/i
import { pack, readManifest, versionBump, prepublish, signPackage, createSignatureArchive } from './package';
import * as tmp from 'tmp';
import { IVerifyPatOptions, getPublisher } from './store';
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest, getAzureCredentialAccessToken } from './util';
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest } from './util';
import { Manifest } from './manifest';
import { readVSIXPackage } from './zip';
import { validatePublisher } from './validation';
import { GalleryApi } from 'azure-devops-node-api/GalleryApi';
import FormData from 'form-data';
import { basename } from 'path';
import { IterableBackoff, handleWhen, retry } from 'cockatiel';
import { getAzureCredentialAccessToken } from './auth';

const tmpName = promisify(tmp.tmpName);

Expand Down
11 changes: 0 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { PublicGalleryAPI } from './publicgalleryapi';
import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi';
import { Manifest } from './manifest';
import { EOL } from 'os';
import { DefaultAzureCredential } from '@azure/identity';

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

export async function getAzureCredentialAccessToken(): Promise<string> {
try {
const credential = new DefaultAzureCredential();
const token = await credential.getToken('499b84ac-1321-427f-aa17-267ca6975798/.default');
return token.token;
} catch (error) {
throw new Error('Can not acquire a Microsoft Entra ID access token. Additional information:\n\n' + error)
}
}

export function normalize(path: string): string {
return path.replace(/\\/g, '/');
}
Expand Down