-
Notifications
You must be signed in to change notification settings - Fork 52
Add manual tenant sign-in fallback for Conditional Access tenants #1511
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
Open
CoffeeKanzler
wants to merge
10
commits into
microsoft:main
Choose a base branch
from
CoffeeKanzler:issue-1332-azure-tenant-setting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+308
−4
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66a06aa
Add azure tenant setting for sign-in
CoffeeKanzler 451dc18
Use configured tenant for provider authentication
CoffeeKanzler 9567f03
Remove local asset ignore entries
CoffeeKanzler 7d46ce5
Address tenant auth review feedback
CoffeeKanzler 144eebe
Add manual tenant sign-in fallback
CoffeeKanzler 19bcdf8
Persist manual tenant for subscription discovery
CoffeeKanzler 793aa99
Address manual tenant sign-in review feedback
CoffeeKanzler 1cabd47
Potential fix for pull request finding
CoffeeKanzler 2cb8247
Sort tenant picks by label and test empty-discovery fallback
CoffeeKanzler 9cff715
Replace globalState persistence with azure.tenant setting
CoffeeKanzler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureAccount, AzureSubscriptionProvider, AzureTenant, isNotSignedInError, SignInOptions, TenantIdAndAccount } from '@microsoft/vscode-azext-azureauth'; | ||
| import { IActionContext, IAzureQuickPickItem } from '@microsoft/vscode-azext-utils'; | ||
| import { setManualSignInTenant } from '../../utils/manualSignInTenant'; | ||
| import { localize } from '../../utils/localize'; | ||
|
|
||
| type ManualTenantSignIn = Partial<TenantIdAndAccount> & Pick<TenantIdAndAccount, 'tenantId'>; | ||
| type TenantSignIn = TenantIdAndAccount | ManualTenantSignIn; | ||
| type ManualTenantSubscriptionProvider = Omit<AzureSubscriptionProvider, 'signIn'> & { | ||
| signIn(tenant?: Partial<TenantIdAndAccount>, options?: SignInOptions): Promise<boolean>; | ||
| }; | ||
|
|
||
| export async function signInToTenant(context: IActionContext, subscriptionProvider: ManualTenantSubscriptionProvider, account?: AzureAccount): Promise<void> { | ||
| const tenantPick = await pickTenant(context, subscriptionProvider, account); | ||
| if (tenantPick) { | ||
| const signedIn = await subscriptionProvider.signIn(tenantPick.tenant); | ||
| if (signedIn && tenantPick.isManual) { | ||
| await setManualSignInTenant(tenantPick.tenant.tenantId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| interface TenantPick { | ||
| tenant: TenantSignIn; | ||
| isManual: boolean; | ||
| } | ||
|
|
||
| async function pickTenant(context: IActionContext, subscriptionProvider: ManualTenantSubscriptionProvider, account?: AzureAccount): Promise<TenantPick | undefined> { | ||
| try { | ||
| const picks = await getPicks(subscriptionProvider, account); | ||
| if (picks.length) { | ||
| const pick = await context.ui.showQuickPick(picks, { | ||
| placeHolder: localize('selectTenantPlaceholder', 'Select a Tenant (Directory) to Sign In To'), | ||
| matchOnDescription: true, | ||
| }); | ||
| return { tenant: pick.data, isManual: false }; | ||
| } | ||
|
|
||
| } catch (err) { | ||
| if (!isNotSignedInError(err)) { | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| const tenant = await promptForTenantId(context); | ||
| return tenant ? { tenant, isManual: true } : undefined; | ||
| } | ||
|
|
||
| async function getPicks(subscriptionProvider: AzureSubscriptionProvider, account?: AzureAccount): Promise<IAzureQuickPickItem<TenantIdAndAccount>[]> { | ||
| const unauthenticatedTenants: AzureTenant[] = []; | ||
| const accounts = account ? [account] : await subscriptionProvider.getAccounts(); | ||
| for (const account of accounts) { | ||
| unauthenticatedTenants.push(...await subscriptionProvider.getUnauthenticatedTenantsForAccount(account)); | ||
| } | ||
|
|
||
| const duplicateTenants = new Set(unauthenticatedTenants | ||
| .filter((tenant, index, self) => index !== self.findIndex(t => t.tenantId === tenant.tenantId)) | ||
| .map(tenant => tenant.tenantId)); | ||
| const isDuplicate = (tenantId: string) => duplicateTenants.has(tenantId); | ||
| const tenantLabel = (tenant: AzureTenant) => tenant.displayName ?? tenant.defaultDomain ?? tenant.tenantId; | ||
|
|
||
| return unauthenticatedTenants | ||
| .sort((a, b) => tenantLabel(a).localeCompare(tenantLabel(b))) | ||
| .map(tenant => ({ | ||
| label: tenantLabel(tenant), | ||
| description: `${tenant.tenantId}${isDuplicate(tenant.tenantId) ? ` (${tenant.account.label})` : ''}`, | ||
| detail: tenant.defaultDomain, | ||
| data: tenant, | ||
| })); | ||
| } | ||
|
|
||
| async function promptForTenantId(context: IActionContext): Promise<ManualTenantSignIn | undefined> { | ||
| const tenantId = await context.ui.showInputBox({ | ||
| placeHolder: localize('enterTenantIdPlaceholder', 'Tenant ID or domain'), | ||
| prompt: localize('enterTenantIdPrompt', 'Enter the tenant ID or domain to sign in to.'), | ||
| validateInput: value => value?.trim() ? undefined : localize('enterTenantIdValidation', 'Enter a tenant ID or domain.'), | ||
| }); | ||
|
|
||
| return tenantId ? { tenantId: tenantId.trim() } : undefined; | ||
| } | ||
|
Comment on lines
+44
to
+54
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { ext } from '../extensionVariables'; | ||
|
|
||
| const ManualSignInTenantKey = 'manualSignInTenant'; | ||
| let testingManualSignInTenant: string | undefined; | ||
|
|
||
| export function normalizeTenant(tenant: string | undefined): string | undefined { | ||
| const trimmedTenant = tenant?.trim(); | ||
| return trimmedTenant ? trimmedTenant : undefined; | ||
| } | ||
|
|
||
| export function getManualSignInTenant(): string | undefined { | ||
| if (testingManualSignInTenant !== undefined) { | ||
| return testingManualSignInTenant; | ||
| } | ||
|
|
||
| if (!ext.context) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return normalizeTenant(ext.context.globalState.get<string>(ManualSignInTenantKey)); | ||
| } | ||
|
|
||
| export async function setManualSignInTenant(tenant: string): Promise<void> { | ||
| if (!ext.context) { | ||
| testingManualSignInTenant = normalizeTenant(tenant); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| await ext.context.globalState.update(ManualSignInTenantKey, normalizeTenant(tenant)); | ||
| } | ||
|
|
||
| export function getTenantIdForAuthentication(tenantId: string | undefined): string | undefined { | ||
| return normalizeTenant(tenantId) ?? getManualSignInTenant(); | ||
| } | ||
|
|
||
| export async function resetManualSignInTenantForTests(): Promise<void> { | ||
| testingManualSignInTenant = undefined; | ||
| if (ext.context) { | ||
| await ext.context.globalState.update(ManualSignInTenantKey, undefined); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make the changes in the auth package in
microsoft/vscode-azuretoolsrather than duplicating the code there. Potentially we add a tenant or tenant ID arg to that function? @alexweininger thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CoffeeKanzler thanks for opening the PR!
getPickshere duplicates the auth package's existing picker, can we instead just delegate to the auth package'ssignInToTenant(provider)and only fall back to the manual tenant input box when it throwsNotSignedInError? That avoids the duplication and any auth-package changes. Also, do we actually need themanualSignInTenantpersistence +getSubscriptionContextoverride?For example: main...alexweininger/sign-in-to-tenant-manual-entry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to refactor the picker to delegate to the auth package's signInToTenant(provider) with an input-box fallback on NotSignedInError, @alexweininger.
On whether we still need the manualSignInTenant persistence + getSubscriptionContext override: I tested exactly this. The manual sign-in itself succeeds, but resources never populate afterward.
The reason: subscription enumeration goes through getSubscriptionContext({ account, tenantId: undefined }) → listTenants(), and with tenantId undefined the silent token request hits the common/organizations
endpoint. So no subscriptions come back and the tree stays empty. The override re-injects the signed-in tenant into that account-level context, which scopes the silent token correctly and makes resources load.
It has to be persisted (not just in-memory) because the same enumeration re-runs on every reload.
@bwateratmsft — to your MSAL-stickiness question, that's what I was hoping too, but in practice the broker is only sticky for the interactive sign-in, not for the silent enumeration token. Without the persisted
tenant, a reload gives an empty tree.
So my proposal: Refactor the delegation (drop the duplicated picker) and keep the tenant persistence + context override (necessary for resources to actually load).
If you'd rather that re-scoping live in the auth package instead like e.g. a "default/sticky tenant" the provider applies to account-level requests then I'd need to further study the auth package after I am back.
Heads up on timing: I'm traveling in about an hour and won't be back until Friday at the earliest, so apologies in advance for the slow follow-up. I'll pick this up as soon as I'm back.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to go back and forth on it, but now that we know we need to persist that value, then I think it's best kept as a setting like your original design.
manualSignInTenantglobalState, use an advanced setting instead.getTenantsForAccount(we already override it, and it gets theaccount): when ARM/tenantsreturns empty/throws for a conditional-access user, injectAzureTenant { tenantId: <setting>, account }. The normal per-tenant flow then lists subscriptions via that tenant's session, so we can drop thegetSubscriptionContextoverride entirely.signIn({ tenantId }).summary: one setting + the existing public override, no globalState, no internal coupling. Clients consume our discovered subscriptions via the Resources API, so this covers the whole extension family too.