Skip to content

Commit

Permalink
Azure SSH: clear current identity before logging in again
Browse files Browse the repository at this point in the history
  • Loading branch information
GGonryun committed Dec 5, 2024
1 parent 0ae5e35 commit 33c3b11
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/plugins/azure/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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 });

Expand Down

0 comments on commit 33c3b11

Please sign in to comment.