From 33c3b11c696812f53d5264247a9039dd35874f70 Mon Sep 17 00:00:00 2001 From: Miguel Campos Date: Wed, 4 Dec 2024 16:27:45 -0800 Subject: [PATCH] Azure SSH: clear current identity before logging in again --- src/plugins/azure/auth.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/plugins/azure/auth.ts b/src/plugins/azure/auth.ts index db82aaf..848b698 100644 --- a/src/plugins/azure/auth.ts +++ b/src/plugins/azure/auth.ts @@ -16,6 +16,11 @@ export const azLoginCommand = () => ({ args: ["login"], }); +export const azLogoutCommand = () => ({ + command: "az", + args: ["logout"], +}); + export const azAccountSetCommand = (subscriptionId: string) => ({ command: "az", args: ["account", "set", "--subscription", subscriptionId], @@ -29,6 +34,23 @@ export const azLogin = async ( if (debug) print2("Logging in to Azure..."); + // Logging out first ensures that any cached credentials are cleared. + // https://github.com/Azure/azure-cli/issues/29161 + try { + const { command: azLogoutExe, args: azLogOutArgs } = azLogoutCommand(); + const logoutResult = await exec(azLogoutExe, azLogOutArgs, { check: true }); + + if (debug) { + print2(logoutResult.stdout); + print2(logoutResult.stderr); + } + } catch (error: any) { + if (debug) { + // ignore the error if the user is not logged in. + print2(`Skipping logout: ${error.stderr}`); + } + } + const { command: azLoginExe, args: azLoginArgs } = azLoginCommand(); const loginResult = await exec(azLoginExe, azLoginArgs, { check: true });