|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { TokenCredential, GetTokenOptions, AccessToken } from "@azure/core-http"; |
| 5 | +import { createSpan } from "../util/tracing"; |
| 6 | +import { AuthenticationErrorName } from "../client/errors"; |
| 7 | +import { CanonicalCode } from "@opentelemetry/types"; |
| 8 | +import { logger } from "../util/logging"; |
| 9 | + |
| 10 | +import * as child_process from "child_process"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides the user access token and expire time |
| 14 | + * with Azure CLI command "az account get-access-token". |
| 15 | + */ |
| 16 | +export class AzureCliCredential implements TokenCredential { |
| 17 | + /** |
| 18 | + * Creates an instance of the AzureCliCredential class. |
| 19 | + */ |
| 20 | + constructor() {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Gets the access token from Azure CLI |
| 24 | + * @param resource The resource to use when getting the token |
| 25 | + */ |
| 26 | + protected async getAzureCliAccessToken(resource: string) { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + try { |
| 29 | + child_process.exec( |
| 30 | + `az account get-access-token --output json --resource ${resource}`, |
| 31 | + (error, stdout, stderr) => { |
| 32 | + resolve({ stdout: stdout, stderr: stderr }); |
| 33 | + } |
| 34 | + ); |
| 35 | + } catch (err) { |
| 36 | + reject(err); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Authenticates with Azure Active Directory and returns an access token if |
| 43 | + * successful. If authentication cannot be performed at this time, this method may |
| 44 | + * return null. If an error occurs during authentication, an {@link AuthenticationError} |
| 45 | + * containing failure details will be thrown. |
| 46 | + * |
| 47 | + * @param scopes The list of scopes for which the token will have access. |
| 48 | + * @param options The options used to configure any requests this |
| 49 | + * TokenCredential implementation might make. |
| 50 | + */ |
| 51 | + public async getToken( |
| 52 | + scopes: string | string[], |
| 53 | + options?: GetTokenOptions |
| 54 | + ): Promise<AccessToken | null> { |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + let scope: string; |
| 57 | + scope = typeof scopes === "string" ? scopes : scopes[0]; |
| 58 | + logger.info(`use the scope ${scope}`); |
| 59 | + const resource = scope.replace(/\/.default$/, ""); |
| 60 | + let responseData = ""; |
| 61 | + |
| 62 | + const { span } = createSpan("AzureCliCredential-getToken", options); |
| 63 | + this.getAzureCliAccessToken(resource) |
| 64 | + .then((obj: any) => { |
| 65 | + if (obj.stderr) { |
| 66 | + let isLoginError = obj.stderr.match("(.*)az login(.*)"); |
| 67 | + let isNotInstallError = |
| 68 | + obj.stderr.match("az:(.*)not found") || |
| 69 | + obj.stderr.startsWith("'az' is not recognized"); |
| 70 | + if (isNotInstallError) { |
| 71 | + throw new Error("Azure CLI could not be found. Please visit https://aka.ms/azure-cli for installation instructions and then, once installed, authenticate to your Azure account using 'az login'."); |
| 72 | + } else if (isLoginError) { |
| 73 | + throw new Error("Please run 'az login' from a command prompt to authenticate before using this credential."); |
| 74 | + } |
| 75 | + throw new Error(obj.stderr); |
| 76 | + } else { |
| 77 | + responseData = obj.stdout; |
| 78 | + const response: { accessToken: string; expiresOn: string } = JSON.parse(responseData); |
| 79 | + resolve({ |
| 80 | + token: response.accessToken, |
| 81 | + expiresOnTimestamp: new Date(response.expiresOn).getTime() |
| 82 | + }); |
| 83 | + } |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + const code = |
| 87 | + err.name === AuthenticationErrorName |
| 88 | + ? CanonicalCode.UNAUTHENTICATED |
| 89 | + : CanonicalCode.UNKNOWN; |
| 90 | + span.setStatus({ |
| 91 | + code, |
| 92 | + message: err.message |
| 93 | + }); |
| 94 | + reject(err); |
| 95 | + }); |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
0 commit comments