Skip to content

Commit 02fbd22

Browse files
ascorbicdevin-ai-integration[bot]NuroDev
authored
feat(wrangler): add warning when account_id mismatch detected on auth error (#11683)
* feat(wrangler): add warning when account_id mismatch detected on auth error When a 401 authentication error occurs, display a warning if the account_id in the Wrangler configuration does not match any of the user's authenticated accounts. This helps users identify configuration issues where they may have the wrong account ID set in their wrangler.toml file. Fixes #9358 Co-Authored-By: mkane@cloudflare.com <m@mk.gg> * refactor: simplify complianceConfig initialization Move the default value assignment to the variable declaration instead of the catch block. This is a minor simplification as the full optional chaining approach doesn't work due to type constraints - ComplianceConfig doesn't include account_id, only the full Config type does. Co-Authored-By: mkane@cloudflare.com <m@mk.gg> * chore: address review feedback - Update changeset to mention both wrangler.toml and wrangler.jsonc - Convert inline comment to JSDoc for printAccountIdMismatchWarning function Co-Authored-By: mkane@cloudflare.com <m@mk.gg> * chore: add JSDoc to whoami function Address vicb's review feedback to add JSDoc documentation. Co-Authored-By: mkane@cloudflare.com <m@mk.gg> * Minor `whoami` JSDoc tweaks --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Ben <4991309+NuroDev@users.noreply.github.com>
1 parent d059f69 commit 02fbd22

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Display a warning when authentication errors occur and the `account_id` in your Wrangler configuration does not match any of your authenticated accounts. This helps identify configuration issues where you may have the wrong account ID set in your `wrangler.toml` or `wrangler.jsonc` file.

packages/wrangler/src/__tests__/whoami.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,42 @@ describe("whoami", () => {
225225
msw.use(...mswSuccessOauthHandlers, ...mswSuccessUserHandlers);
226226
});
227227

228+
it("should display a warning when account_id in config does not match authenticated accounts", async () => {
229+
writeAuthConfigFile({ oauth_token: "some-oauth-token" });
230+
const { whoami } = await import("../user/whoami");
231+
await whoami(
232+
COMPLIANCE_REGION_CONFIG_UNKNOWN,
233+
"unknownaccountid",
234+
"unknownaccountid"
235+
);
236+
expect(std.out).toContain(
237+
"The `account_id` in your Wrangler configuration (unknownaccountid) does not match any of your authenticated accounts."
238+
);
239+
expect(std.out).toContain("This may be causing the authentication error.");
240+
});
241+
242+
it("should not display a warning when account_id matches an authenticated account", async () => {
243+
writeAuthConfigFile({ oauth_token: "some-oauth-token" });
244+
const { whoami } = await import("../user/whoami");
245+
await whoami(COMPLIANCE_REGION_CONFIG_UNKNOWN, "account-1", "account-1");
246+
expect(std.out).not.toContain(
247+
"does not match any of your authenticated accounts"
248+
);
249+
});
250+
251+
it("should not display a warning when accountFilter and configAccountId don't match", async () => {
252+
writeAuthConfigFile({ oauth_token: "some-oauth-token" });
253+
const { whoami } = await import("../user/whoami");
254+
await whoami(
255+
COMPLIANCE_REGION_CONFIG_UNKNOWN,
256+
"differentaccountid",
257+
"unknownaccountid"
258+
);
259+
expect(std.out).not.toContain(
260+
"does not match any of your authenticated accounts"
261+
);
262+
});
263+
228264
it("should display membership roles if --account flag is given", async () => {
229265
writeAuthConfigFile({ oauth_token: "some-oauth-token" });
230266
msw.use(

packages/wrangler/src/core/handle-errors.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ export async function handleError(
113113
logger.log(chalk.yellow(message));
114114
}
115115
const accountTag = (e as APIError)?.accountTag;
116-
let complianceConfig: ComplianceConfig;
116+
let complianceConfig: ComplianceConfig = COMPLIANCE_REGION_CONFIG_UNKNOWN;
117+
let configAccountId: string | undefined;
117118
try {
118-
complianceConfig = await readConfig(args, {
119+
const config = await readConfig(args, {
119120
hideWarnings: true,
120121
});
122+
complianceConfig = config;
123+
configAccountId = config.account_id;
121124
} catch {
122-
complianceConfig = COMPLIANCE_REGION_CONFIG_UNKNOWN;
125+
// Ignore errors reading config
123126
}
124-
await whoami(complianceConfig, accountTag);
127+
await whoami(complianceConfig, accountTag, configAccountId);
125128
} else if (e instanceof ParseError) {
126129
e.notes.push({
127130
text: "\nIf you think this is a bug, please open an issue at: https://github.com/cloudflare/workers-sdk/issues/new/choose",

packages/wrangler/src/user/whoami.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import { DefaultScopeKeys, getAPIToken, getAuthFromEnv, getScopes } from ".";
99
import type { ApiCredentials, Scope } from ".";
1010
import type { ComplianceConfig } from "@cloudflare/workers-utils";
1111

12+
/**
13+
* Displays information about the currently authenticated user, including their
14+
* email, accounts, token permissions, and membership roles.
15+
*
16+
* When called with `accountFilter` and `configAccountId`, also checks for potential
17+
* `account_id` mismatches that could cause authentication errors.
18+
*/
1219
export async function whoami(
1320
complianceConfig: ComplianceConfig,
14-
accountFilter?: string
21+
accountFilter?: string,
22+
configAccountId?: string
1523
) {
1624
logger.log("Getting User settings...");
1725
const user = await getUserInfo(complianceConfig);
@@ -30,6 +38,7 @@ export async function whoami(
3038
}
3139
printComplianceRegion(complianceConfig);
3240
printAccountList(user);
41+
printAccountIdMismatchWarning(user, accountFilter, configAccountId);
3342
printTokenPermissions(user);
3443
await printMembershipInfo(complianceConfig, user, accountFilter);
3544
}
@@ -73,6 +82,50 @@ function printAccountList(user: UserInfo) {
7382
);
7483
}
7584

85+
/**
86+
* Prints a warning if the account_id in the Wrangler configuration does not match
87+
* any of the user's authenticated accounts.
88+
*
89+
* Only shows warning if:
90+
* 1. We have an accountFilter (the account ID from the failed request)
91+
* 2. We have a configAccountId (the account_id from the wrangler config)
92+
* 3. The accountFilter matches the configAccountId (meaning the config account_id was used)
93+
* 4. The accountFilter is NOT in the user's accounts list
94+
*/
95+
function printAccountIdMismatchWarning(
96+
user: UserInfo,
97+
accountFilter?: string,
98+
configAccountId?: string
99+
) {
100+
if (!accountFilter || !configAccountId) {
101+
return;
102+
}
103+
104+
// Check if the account ID from the failed request matches the configured account_id
105+
if (accountFilter !== configAccountId) {
106+
return;
107+
}
108+
109+
// Check if the configured account_id is in the user's accounts
110+
const accountInUserAccounts = user.accounts.some(
111+
(account) => account.id === accountFilter
112+
);
113+
114+
if (!accountInUserAccounts) {
115+
logger.log(
116+
formatMessage({
117+
text: `The \`account_id\` in your Wrangler configuration (${chalk.blue(configAccountId)}) does not match any of your authenticated accounts.`,
118+
kind: "warning",
119+
notes: [
120+
{
121+
text: "This may be causing the authentication error. Check your Wrangler configuration file and ensure the `account_id` is correct for your account.",
122+
},
123+
],
124+
})
125+
);
126+
}
127+
}
128+
76129
function printTokenPermissions(user: UserInfo) {
77130
const permissions =
78131
user.tokenPermissions?.map((scope) => scope.split(":")) ?? [];

0 commit comments

Comments
 (0)