Skip to content

Commit c46055a

Browse files
authored
AAD: Use device code flow on CodeSpaces (#392)
also store tenantID so users don't have to login twice
1 parent 9664394 commit c46055a

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

packages/databricks-vscode/src/configuration/AzureCliCheck.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ExecUtils, WorkspaceClient} from "@databricks/databricks-sdk";
22
import {commands, Disposable, Uri, window} from "vscode";
3-
import {AuthProvider} from "./auth/AuthProvider";
3+
import {AzureCliAuthProvider} from "./auth/AuthProvider";
44

55
export type Step<S, N> = () => Promise<
66
SuccessResult<S> | NextResult<N> | ErrorResult
@@ -54,25 +54,30 @@ type AzureStepName =
5454

5555
export class AzureCliCheck implements Disposable {
5656
private disposables: Disposable[] = [];
57+
private isCodeSpaces: boolean;
58+
59+
tenantId: string | undefined;
5760

5861
constructor(
59-
private authProvider: AuthProvider,
62+
private authProvider: AzureCliAuthProvider,
6063
private azBinPath: string = "az"
61-
) {}
64+
) {
65+
this.isCodeSpaces = process.env.CODESPACES === "true";
66+
}
6267

6368
dispose() {
6469
this.disposables.forEach((i) => i.dispose());
6570
this.disposables = [];
6671
}
6772

6873
public async check(silent = false): Promise<boolean> {
69-
let tenant: string;
74+
this.tenantId = this.authProvider.tenantId;
7075

7176
const steps: Record<AzureStepName, Step<boolean, AzureStepName>> = {
7277
tryLogin: async () => {
7378
const result = await this.tryLogin(this.authProvider.host);
7479
if (typeof result === "string") {
75-
tenant = result;
80+
this.tenantId = result;
7681
return {
7782
type: "next",
7883
next: "loginAzureCli",
@@ -123,7 +128,7 @@ export class AzureCliCheck implements Disposable {
123128
};
124129
},
125130
loginAzureCli: async () => {
126-
if (await this.loginAzureCli(tenant)) {
131+
if (await this.loginAzureCli(this.tenantId)) {
127132
return {
128133
type: "next",
129134
next: "tryLogin",
@@ -233,7 +238,7 @@ export class AzureCliCheck implements Disposable {
233238
public async loginAzureCli(tenant = ""): Promise<boolean> {
234239
let message = 'You need to run "az login" to login with Azure.';
235240
if (tenant) {
236-
message = `You are logged in with the wrong tenant ID. Run "az login -t ${tenant}" to login with Azure with the correct tenant.`;
241+
message = `You need to tun "az login -t ${tenant}" to login with Azure.`;
237242
}
238243
const choice = await window.showInformationMessage(
239244
message,
@@ -245,8 +250,11 @@ export class AzureCliCheck implements Disposable {
245250
const terminal = window.createTerminal("az login");
246251
this.disposables.push(terminal);
247252
terminal.show();
253+
254+
const useDeviceCode = this.isCodeSpaces ? "--use-device-code" : "";
255+
248256
terminal.sendText(
249-
`${this.azBinPath} login ${
257+
`${this.azBinPath} login ${useDeviceCode} ${
250258
tenant ? "-t " + tenant : ""
251259
}; echo "Press any key to close the terminal and continue ..."; read; exit`
252260
);

packages/databricks-vscode/src/configuration/auth/AuthProvider.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export abstract class AuthProvider {
6666

6767
switch (json.authType as AuthType) {
6868
case "azure-cli":
69-
return new AzureCliAuthProvider(host);
69+
return new AzureCliAuthProvider(host, json.tenantId);
7070

7171
case "profile":
7272
if (!json.profile) {
@@ -114,8 +114,16 @@ export class ProfileAuthProvider extends AuthProvider {
114114
}
115115

116116
export class AzureCliAuthProvider extends AuthProvider {
117-
constructor(host: URL) {
117+
private _tenantId?: string;
118+
119+
constructor(host: URL, tenantId?: string) {
118120
super(host, "azure-cli");
121+
122+
this._tenantId = tenantId;
123+
}
124+
125+
get tenantId(): string | undefined {
126+
return this._tenantId;
119127
}
120128

121129
describe(): string {
@@ -126,6 +134,7 @@ export class AzureCliAuthProvider extends AuthProvider {
126134
return {
127135
host: this.host.toString(),
128136
authType: this.authType,
137+
tenantId: this.tenantId,
129138
};
130139
}
131140

@@ -145,6 +154,9 @@ export class AzureCliAuthProvider extends AuthProvider {
145154
}
146155

147156
async check(silent: boolean): Promise<boolean> {
148-
return await new AzureCliCheck(this).check(silent);
157+
const cliCheck = new AzureCliCheck(this);
158+
const result = await cliCheck.check(silent);
159+
this._tenantId = cliCheck.tenantId;
160+
return result;
149161
}
150162
}

0 commit comments

Comments
 (0)