Skip to content

Commit

Permalink
Merge pull request #861 from Timothyw0/main
Browse files Browse the repository at this point in the history
fix (#859): Add check for azureProfile.json to prevent double login
  • Loading branch information
Timothyw0 authored Jul 18, 2024
2 parents 81adcab + 6eabbcd commit 4efa278
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/cli/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { existsSync, promises as fs } from "node:fs";
import path from "node:path";
import { logger, logGitHubIssueMessageAndExit } from "../../../core/utils/logger.js";
import { authenticateWithAzureIdentity, listSubscriptions, listTenants } from "../../../core/account.js";
import { ENV_FILENAME } from "../../../core/constants.js";
import { AZURE_LOGIN_CONFIG, ENV_FILENAME } from "../../../core/constants.js";
import { pathExists, safeReadJson } from "../../../core/utils/file.js";
import { updateGitIgnore } from "../../../core/git.js";
import { chooseSubscription, chooseTenant } from "../../../core/prompts.js";
import { Environment } from "../../../core/swa-cli-persistence-plugin/impl/azure-environment.js";
Expand Down Expand Up @@ -43,6 +44,7 @@ export async function login(options: SWACLIConfig): Promise<any> {
logger.log(chalk.green(`✔ Successfully logged into Azure!`));
}

options = await tryGetAzTenantAndSubscription(options);
return await setupProjectCredentials(options, credentialChain);
}

Expand Down Expand Up @@ -149,3 +151,24 @@ async function storeProjectCredentialsInEnvFile(
await updateGitIgnore(ENV_FILENAME);
}
}

async function tryGetAzTenantAndSubscription(options: SWACLIConfig) {
const doesAzureConfigExist = await pathExists(AZURE_LOGIN_CONFIG);
if (!doesAzureConfigExist) {
return options;
} else {
logger.silly(`Found an existing Azure config file, getting Tenant and Subscription Id from ${AZURE_LOGIN_CONFIG}`);

const azureProfile = await safeReadJson(AZURE_LOGIN_CONFIG);
if (azureProfile) {
const allSubscriptions = (azureProfile as AzureProfile).subscriptions;
const defaultAzureInfo = allSubscriptions.find((subscription) => subscription.isDefault == true);
if (defaultAzureInfo) {
options.tenantId = defaultAzureInfo.tenantId;
options.subscriptionId = defaultAzureInfo.id;
}
}

return options;
}
}
5 changes: 5 additions & 0 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,8 @@ export function IS_DATA_API_DEV_SERVER() {
export function SWA_CLI_DATA_API_URI() {
return IS_DATA_API_DEV_SERVER() ? DEFAULT_CONFIG.dataApiLocation : address(DEFAULT_CONFIG.host, DEFAULT_CONFIG.dataApiPort, "http");
}

// Constants related to swa login
const azureFolderName = ".azure";
const azureProfileFilename = "azureProfile.json";
export const AZURE_LOGIN_CONFIG = path.join(os.homedir(), azureFolderName, azureProfileFilename);
2 changes: 1 addition & 1 deletion src/core/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function safeReadJson(path: string): Promise<JsonData | undefined>
try {
let contents = await fs.readFile(path, "utf8");
contents = stripJsonComments(contents);
return JSON.parse(contents) as JsonData;
return JSON.parse(contents.trim()) as JsonData;
} catch (error) {
logger.warn(`Failed to read JSON file at: ${path}`);
return undefined;
Expand Down
24 changes: 24 additions & 0 deletions src/swa.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,27 @@ declare type RolesSourceClaim = {
typ: string;
val: string;
};

declare type SubscriptionState = "Enabled" | "Warned" | "PastDue" | "Disabled" | "Deleted";

declare type AzureLoginInfo = {
id: string;
name: string;
state: SubscriptionState;
user: {
name: string;
type: string;
};
isDefault: boolean;
tenantId: string;
environmentName: string;
homeTenantId: string;
tenantDefaultDomain: string;
tenantDisplayName: string;
managedByTenants: string[];
};

declare interface AzureProfile {
installationId: string;
subscriptions: AzureLoginInfo[];
}

0 comments on commit 4efa278

Please sign in to comment.