Skip to content

Commit

Permalink
auth: Add logging to VSCodeAzureSubscriptionProvider (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweininger authored Dec 6, 2024
1 parent eede4ad commit 80d0550
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions auth/src/VSCodeAzureSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
private readonly onDidSignOutEmitter = new vscode.EventEmitter<void>();
private lastSignOutEventFired: number = 0;

public constructor() {
// So that customers can easily share logs, try to only log PII using trace level
public constructor(private readonly logger?: vscode.LogOutputChannel) {
const disposable = vscode.authentication.onDidChangeSessions(async e => {
// Ignore any sign in that isn't for the configured auth provider
if (e.provider.id !== getConfiguredAuthProviderId()) {
Expand Down Expand Up @@ -62,6 +63,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
* @returns A list of tenants.
*/
public async getTenants(account?: vscode.AuthenticationSessionAccountInformation): Promise<AzureTenant[]> {
const startTimeMs = Date.now();
const results: AzureTenant[] = [];
for await (account of account ? [account] : await vscode.authentication.getAccounts(getConfiguredAuthProviderId())) {
// Added check. Without this the getSubscriptionClient function throws the NotSignedInError
Expand All @@ -74,7 +76,8 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
}
}
}

const endTimeMs = Date.now();
this.logger?.debug(`auth: Got ${results.length} tenants for account "${account?.label}" in ${endTimeMs - startTimeMs}ms`);
return results;
}

Expand All @@ -92,16 +95,19 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
* the user is signed in.
*/
public async getSubscriptions(filter: boolean = true): Promise<AzureSubscription[]> {
this.logger?.debug('auth: Loading subscriptions...');
const startTime = Date.now();
const tenantIds = await this.getTenantFilters();
const shouldFilterTenants = filter && !!tenantIds.length; // If the list is empty it is treated as "no filter"

const allSubscriptions: AzureSubscription[] = [];

let accountCount: number; // only used for logging
try {
this.suppressSignInEvents = true;

// Get the list of tenants from each account
const accounts = await vscode.authentication.getAccounts(getConfiguredAuthProviderId());
accountCount = accounts.length;
for (const account of accounts) {
for (const tenant of await this.getTenants(account)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -117,7 +123,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
}

// list subscriptions for the home tenant
allSubscriptions.push(...await this.getSubscriptionsForTenant(account))
allSubscriptions.push(...await this.getSubscriptionsForTenant(account));
}
} finally {
this.suppressSignInEvents = false;
Expand All @@ -129,6 +135,9 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
allSubscriptions.forEach(sub => subscriptionMap.set(`${sub.account.id}/${sub.subscriptionId}`, sub));
const uniqueSubscriptions = Array.from(subscriptionMap.values());

const endTime = Date.now();
this.logger?.debug(`auth: Got ${uniqueSubscriptions.length} subscriptions from ${accountCount} accounts in ${endTime - startTime}ms`);

const sortSubscriptions = (subscriptions: AzureSubscription[]): AzureSubscription[] =>
subscriptions.sort((a, b) => a.name.localeCompare(b.name));

Expand Down Expand Up @@ -176,7 +185,9 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
return silentlyCheckForSession(tenantId, account);
}

return await innerIsSignedIn();
const result = await innerIsSignedIn();
this.logger?.trace(`auth: isSignedIn returned ${result} (account="${account?.label ?? 'none'}") (tenantId="${tenantId ?? 'none'}")`);
return result;
}

/**
Expand All @@ -188,7 +199,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
* @returns True if the user is signed in, false otherwise.
*/
public async signIn(tenantId?: string, account?: vscode.AuthenticationSessionAccountInformation): Promise<boolean> {

this.logger?.debug(`auth: Signing in (account="${account?.label ?? 'none'}") (tenantId="${tenantId ?? 'none'}")`);
const session = await getSessionFromVSCode([], tenantId, {
createIfNone: true,
// If no account is provided, then clear the session preference which tells VS Code to show the account picker
Expand Down

0 comments on commit 80d0550

Please sign in to comment.