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

auth: Implement fetch with https.request instead + revert package lock to v… #1618

Closed
wants to merge 1 commit into from
Closed
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
58 changes: 4 additions & 54 deletions auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@types/html-to-text": "^8.1.0",
"@types/mocha": "^7.0.2",
"@types/node": "^18.18.7",
"@types/node-fetch": "2.6.7",
"@types/semver": "^7.3.9",
"@types/uuid": "^9.0.1",
"@types/vscode": "1.76.0",
Expand All @@ -53,7 +52,6 @@
},
"dependencies": {
"@azure/arm-subscriptions": "^5.1.0",
"@azure/ms-rest-azure-env": "^2.0.0",
"node-fetch": "2.6.7"
"@azure/ms-rest-azure-env": "^2.0.0"
}
}
31 changes: 25 additions & 6 deletions auth/src/VSCodeAzureSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import type { SubscriptionClient, TenantIdDescription } from '@azure/arm-subscriptions'; // Keep this as `import type` to avoid actually loading the package before necessary
import type { TokenCredential } from '@azure/core-auth'; // Keep this as `import type` to avoid actually loading the package (at all, this one is dev-only)
import fetch from 'node-fetch'; // have to explicitly use node-fetch v2.6.7 otherwise when @azure/client-core makes a streaming request, it fails on windows
import { get } from 'https';
import * as vscode from 'vscode';
import type { AzureAuthentication } from './AzureAuthentication';
import type { AzureSubscription, SubscriptionId, TenantId } from './AzureSubscription';
Expand Down Expand Up @@ -60,12 +60,31 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
* @returns A list of tenants.
*/
public async getTenants(): Promise<TenantIdDescription[]> {
const listTenantsResponse = await fetch('https://management.azure.com/tenants?api-version=2022-12-01', {
headers: {
Authorization: `Bearer ${await this.getToken()}`,
}
const bearerToken = `Bearer ${await this.getToken()}`;
return new Promise(function async(resolve, reject) {
const req = get('https://management.azure.com/tenants?api-version=2022-12-01', {
headers: {
Authorization: bearerToken,
}
}, function (res) {
const body: unknown[] = [];
res.on('data', function (chunk: unknown) {
body.push(chunk);
});
// resolve on end
res.on('end', function () {
let response: TenantIdDescription[] = [];
try {
response = (JSON.parse(Buffer.concat(body as Uint8Array[]).toString()) as { value: TenantIdDescription[] }).value;
} catch (e) {
reject(e);
}
resolve(response);
});
});

req.end();
});
return (await listTenantsResponse.json() as { value: TenantIdDescription[] }).value;
}

/**
Expand Down