Skip to content

Commit 19e464b

Browse files
authored
Merge branch 'main' into push-oxmlnmluomut
2 parents edf742e + 1a58bc3 commit 19e464b

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

.changeset/thick-meals-cheat.md

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+
Make Wrangler warn more loudly if you're missing auth scopes

packages/vite-plugin-cloudflare/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
### Minor Changes
66

7+
- [#9535](https://github.com/cloudflare/workers-sdk/pull/9535) [`56dc5c4`](https://github.com/cloudflare/workers-sdk/commit/56dc5c4946417df12688dd6b2374835f60c14be6) Thanks [@penalosa](https://github.com/penalosa)! - In 2023 we announced [breakpoint debugging support](https://blog.cloudflare.com/debugging-cloudflare-workers/) for Workers, which meant that you could easily debug your Worker code in Wrangler's built-in devtools (accessible via the `[d]` hotkey) as well as multiple other devtools clients, [including VSCode](https://developers.cloudflare.com/workers/observability/dev-tools/breakpoints/). For most developers, breakpoint debugging via VSCode is the most natural flow, but until now it's required [manually configuring a `launch.json` file](https://developers.cloudflare.com/workers/observability/dev-tools/breakpoints/#setup-vs-code-to-use-breakpoints), running `wrangler dev`, and connecting via VSCode's built-in debugger.
8+
9+
Now, using VSCode's built-in [JavaScript Debug Terminals](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_javascript-debug-terminal), there are just two steps: open a JS debug terminal and run `vite dev` or `vite preview`. VSCode will automatically connect to your running Worker (even if you're running multiple Workers at once!) and start a debugging session.
10+
711
- [#9803](https://github.com/cloudflare/workers-sdk/pull/9803) [`df04528`](https://github.com/cloudflare/workers-sdk/commit/df0452892dc85133c557c4daff68508d7fdee77a) Thanks [@penalosa](https://github.com/penalosa)! - Support Workers Analytics Engine & Rate Limiting bindings
812

913
### Patch Changes

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,30 @@ describe("whoami", () => {
246246
├─┼─┤
247247
│ Account Three │ account-3 │
248248
└─┴─┘
249-
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
249+
🔓 Token Permissions:
250250
Scope (Access)
251+
252+
▲ [WARNING] Wrangler is missing some expected Oauth scopes. To fix this, run \`wrangler login\` to refresh your token. The missing scopes are:
253+
254+
- account:read
255+
- user:read
256+
- workers:write
257+
- workers_kv:write
258+
- workers_routes:write
259+
- workers_scripts:write
260+
- workers_tail:read
261+
- d1:write
262+
- pages:write
263+
- zone:read
264+
- ssl_certs:write
265+
- ai:write
266+
- queues:write
267+
- pipelines:write
268+
- secrets_store:write
269+
- containers:write
270+
- cloudchamber:write
271+
272+
251273
🎢 Membership roles in \\"Account Two\\": Contact account super admin to change your permissions.
252274
- Test role"
253275
`);
@@ -280,8 +302,30 @@ describe("whoami", () => {
280302
├─┼─┤
281303
│ Account Three │ account-3 │
282304
└─┴─┘
283-
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
305+
🔓 Token Permissions:
284306
Scope (Access)
307+
308+
▲ [WARNING] Wrangler is missing some expected Oauth scopes. To fix this, run \`wrangler login\` to refresh your token. The missing scopes are:
309+
310+
- account:read
311+
- user:read
312+
- workers:write
313+
- workers_kv:write
314+
- workers_routes:write
315+
- workers_scripts:write
316+
- workers_tail:read
317+
- d1:write
318+
- pages:write
319+
- zone:read
320+
- ssl_certs:write
321+
- ai:write
322+
- queues:write
323+
- pipelines:write
324+
- secrets_store:write
325+
- containers:write
326+
- cloudchamber:write
327+
328+
285329
🎢 Unable to get membership roles. Make sure you have permissions to read the account. Are you missing the \`User->Memberships->Read\` permission?"
286330
`);
287331
});

packages/wrangler/src/cloudchamber/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export async function buildAndMaybePush(
190190
return { image: imageTag, pushed: pushed };
191191
} catch (error) {
192192
if (error instanceof Error) {
193-
throw new UserError(error.message);
193+
throw new UserError(error.message, { cause: error });
194194
}
195195
throw new UserError("An unknown error occurred");
196196
}

packages/wrangler/src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import assert from "node:assert";
12
import os from "node:os";
23
import { setTimeout } from "node:timers/promises";
4+
import { ApiError } from "@cloudflare/containers-shared";
35
import chalk from "chalk";
46
import { ProxyAgent, setGlobalDispatcher } from "undici";
57
import makeCLI from "yargs";
@@ -1508,10 +1510,22 @@ export async function main(argv: string[]): Promise<void> {
15081510
// The workaround is to re-run the parsing with an additional `--help` flag, which will result in the correct help message being displayed.
15091511
// The `wrangler` object is "frozen"; we cannot reuse that with different args, so we must create a new CLI parser to generate the help message.
15101512
await createCLIParser([...argv, "--help"]).parse();
1511-
} else if (isAuthenticationError(e)) {
1513+
} else if (
1514+
isAuthenticationError(e) ||
1515+
// Is this a Containers/Cloudchamber-based auth error?
1516+
// This is different because it uses a custom OpenAPI-based generated client
1517+
(e instanceof UserError &&
1518+
e.cause instanceof ApiError &&
1519+
e.cause.status === 403)
1520+
) {
15121521
mayReport = false;
15131522
errorType = "AuthenticationError";
1514-
logger.log(formatMessage(e));
1523+
if (e.cause instanceof ApiError) {
1524+
logger.error(e.cause);
1525+
} else {
1526+
assert(isAuthenticationError(e));
1527+
logger.log(formatMessage(e));
1528+
}
15151529
const envAuth = getAuthFromEnv();
15161530
if (envAuth !== undefined && "apiToken" in envAuth) {
15171531
const message =

packages/wrangler/src/user/whoami.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { fetchPagedListResult, fetchResult } from "../cfetch";
33
import { isAuthenticationError } from "../deploy/deploy";
44
import { getCloudflareComplianceRegion } from "../environment-variables/misc-variables";
55
import { logger } from "../logger";
6+
import { formatMessage } from "../parse";
67
import { fetchMembershipRoles } from "./membership";
7-
import { getAPIToken, getAuthFromEnv, getScopes } from ".";
8-
import type { ApiCredentials } from ".";
8+
import { DefaultScopeKeys, getAPIToken, getAuthFromEnv, getScopes } from ".";
9+
import type { ApiCredentials, Scope } from ".";
910
import type { ComplianceConfig } from "../environment-variables/misc-variables";
1011

1112
export async function whoami(
@@ -80,13 +81,30 @@ function printTokenPermissions(user: UserInfo) {
8081
`🔓 To see token permissions visit https://dash.cloudflare.com/${user.authType === "User API Token" ? "profile" : user.accounts[0].id}/api-tokens.`
8182
);
8283
}
83-
logger.log(
84-
`🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.`
85-
);
84+
logger.log(`🔓 Token Permissions:`);
8685
logger.log(`Scope (Access)`);
86+
87+
// This Set contains all the scopes we expect to see (that Wrangler requests by default)
88+
const expectedScopes = new Set(DefaultScopeKeys);
8789
for (const [scope, access] of permissions) {
90+
// We'll remove scopes from the set of scopes that we expect to see when we see them in the API response
91+
expectedScopes.delete(`${scope}:${access}` as Scope);
8892
logger.log(`- ${scope} ${access ? `(${access})` : ``}`);
8993
}
94+
95+
// If we've iterated through all scopes in the API response and there are still expected scopes remaining,
96+
// then we know that Wrangler may not behave as expected since the current token doesn't have all the expected scopes
97+
// Warn, and tell the user how to fix it
98+
if (expectedScopes.size > 0) {
99+
logger.log("");
100+
logger.log(
101+
formatMessage({
102+
text: "Wrangler is missing some expected Oauth scopes. To fix this, run `wrangler login` to refresh your token. The missing scopes are:",
103+
kind: "warning",
104+
notes: [...expectedScopes.values()].map((s) => ({ text: `- ${s}` })),
105+
})
106+
);
107+
}
90108
}
91109

92110
async function printMembershipInfo(

0 commit comments

Comments
 (0)