This commit is contained in:
phoff 2022-08-03 16:18:57 -07:00
Родитель 12877a14f7
Коммит 718660703f
4 изменённых файлов: 57 добавлений и 9 удалений

Просмотреть файл

@ -0,0 +1,23 @@
import { HttpClient, HttpOperationResponse, WebResourceLike } from "@azure/ms-rest-js";
import { ext } from "../extensionVariables";
export class LoggingHttpClient implements HttpClient {
constructor(private readonly innerClient: HttpClient) {
}
async sendRequest(httpRequest: WebResourceLike): Promise<HttpOperationResponse> {
ext.outputChannel.appendLine(`Azure SDK: ${httpRequest.method} ${httpRequest.url}...`);
try {
const response = await this.innerClient.sendRequest(httpRequest);
ext.outputChannel.appendLine(`Azure SDK: ${httpRequest.method} ${httpRequest.url} response: ${response.status}`);
return response;
} catch (error) {
ext.outputChannel.appendLine(`Azure SDK: ${httpRequest.method} ${httpRequest.url} failed: ${error}`);
throw error;
}
}
}

Просмотреть файл

@ -5,6 +5,7 @@
import { SubscriptionClient, SubscriptionModels } from "@azure/arm-subscriptions";
import { Environment } from "@azure/ms-rest-azure-env";
import { DefaultHttpClient } from "@azure/ms-rest-js";
import { DeviceTokenCredentials as DeviceTokenCredentials2 } from '@azure/ms-rest-nodeauth';
import { AuthenticationContext, MemoryCache, TokenResponse, UserCodeInfo } from "adal-node";
import { clientId, commonTenantId } from "../../constants";
@ -14,6 +15,7 @@ import { listAll } from "../../utils/arrayUtils";
import { localize } from "../../utils/localize";
import { logErrorMessage } from "../../utils/logErrorMessage";
import { isADFS } from "../environments";
import { LoggingHttpClient } from "../LoggingHttpClient";
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, import/no-internal-modules
const CacheDriver = require('adal-node/lib/cache-driver');
@ -102,7 +104,12 @@ export async function tokensFromToken(environment: Environment, firstTokenRespon
const tokenCache: MemoryCache = new MemoryCache();
await addTokenToCache(environment, tokenCache, firstTokenResponse);
const credentials: DeviceTokenCredentials2 = new DeviceTokenCredentials2(clientId, undefined, firstTokenResponse.userId, undefined, environment, tokenCache);
const client: SubscriptionClient = new SubscriptionClient(credentials, { baseUri: environment.resourceManagerEndpointUrl });
const client: SubscriptionClient = new SubscriptionClient(
credentials,
{
baseUri: environment.resourceManagerEndpointUrl,
httpClient: new LoggingHttpClient(new DefaultHttpClient())
});
const tenants: SubscriptionModels.TenantIdDescription[] = await listAll(client.tenants, client.tenants.list());
const responses: TokenResponse[] = <TokenResponse[]>(await Promise.all<TokenResponse | null>(tenants.map((tenant) => {
if (tenant.tenantId === firstTokenResponse.tenantId) {

Просмотреть файл

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { SubscriptionClient } from "@azure/arm-subscriptions";
import { HttpOperationResponse, RequestPrepareOptions } from "@azure/ms-rest-js";
import { DefaultHttpClient, HttpOperationResponse, RequestPrepareOptions } from "@azure/ms-rest-js";
import { callWithTelemetryAndErrorHandling, IActionContext, parseError } from "@microsoft/vscode-azext-utils";
import { AzureSubscription } from "../azure-account.api";
import { cacheKey } from "../constants";
@ -12,6 +12,7 @@ import { ext } from "../extensionVariables";
import { listAll } from "../utils/arrayUtils";
import { AzureSessionInternal } from "./AzureSessionInternal";
import { getSelectedEnvironment } from "./environments";
import { LoggingHttpClient } from "./LoggingHttpClient";
import { SubscriptionTenantCache } from "./subscriptionTypes";
import { TenantIdDescription } from "./TenantIdDescription";
@ -53,7 +54,12 @@ async function loadTenants(context: IActionContext): Promise<TenantIdDescription
const knownTenants: TenantIdDescription[] = [];
for (const session of ext.loginHelper.api.sessions) {
const client: SubscriptionClient = new SubscriptionClient(session.credentials2, { baseUri: session.environment.resourceManagerEndpointUrl });
const client: SubscriptionClient = new SubscriptionClient(
session.credentials2,
{
baseUri: session.environment.resourceManagerEndpointUrl,
httpClient: new LoggingHttpClient(new DefaultHttpClient())
});
const environment = await getSelectedEnvironment();
const resourceManagerEndpointUrl: string = environment.resourceManagerEndpointUrl.endsWith('/') ?
environment.resourceManagerEndpointUrl :
@ -107,7 +113,12 @@ async function loadTenants(context: IActionContext): Promise<TenantIdDescription
async function loadSubscriptions(context: IActionContext): Promise<AzureSubscription[]> {
const lists: AzureSubscription[][] = await Promise.all(ext.loginHelper.api.sessions.map(session => {
const client: SubscriptionClient = new SubscriptionClient(session.credentials2, { baseUri: session.environment.resourceManagerEndpointUrl });
const client: SubscriptionClient = new SubscriptionClient(
session.credentials2,
{
baseUri: session.environment.resourceManagerEndpointUrl,
httpClient: new LoggingHttpClient(new DefaultHttpClient())
});
return listAll(client.subscriptions, client.subscriptions.list())
.then(list => list.map(subscription => ({
session,

Просмотреть файл

@ -4,14 +4,21 @@
*--------------------------------------------------------------------------------------------*/
import fetch, { RequestInfo, RequestInit, Response } from 'node-fetch';
import { ext } from '../extensionVariables';
export { Response };
export default async function fetchUrl(url: RequestInfo, init?: RequestInit): Promise<Response> {
console.log(`Fetching ${url}...`);
ext.outputChannel.append(`Fetching ${url}...`);
const response = await fetch(url, init);
try {
const response = await fetch(url, init);
ext.outputChannel.append(`Fetching ${url} response: ${response.status}`);
return response;
} catch (error) {
ext.outputChannel.append(`Fetching ${url} failed: ${error}`);
console.log(`Returned ${response.status}.`);
return response;
throw error;
}
}