Skip to content

Commit 16b92d5

Browse files
committed
non-TTY check for required variables:
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml` then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing. resolves #827
1 parent 50cbe61 commit 16b92d5

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe("publish", () => {
178178
expect(std.err).toMatchInlineSnapshot(`""`);
179179
});
180180

181-
it("should throw an error in non-TTY if 'account_id' & 'CLOUDFLARE_ACCOUNT_ID' is missing", async () => {
181+
it("should throw an error in non-TTY & there is more than one account associated with API token", async () => {
182182
setIsTTY(false);
183183
process.env = {
184184
CLOUDFLARE_API_TOKEN: "hunter2",
@@ -233,10 +233,35 @@ describe("publish", () => {
233233
await expect(runWrangler("publish index.js")).rejects.toThrowError();
234234

235235
expect(std.err).toMatchInlineSnapshot(`
236-
"X [ERROR] Missing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info: TBD
236+
"X [ERROR] In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work.
237+
Please go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.
237238
239+
"
240+
`);
241+
});
242+
it("should throw error with no account ID provided and no members retrieved", async () => {
243+
setIsTTY(false);
244+
writeWranglerToml({
245+
account_id: undefined,
246+
});
247+
process.env = {
248+
CLOUDFLARE_API_TOKEN: "picard",
249+
CLOUDFLARE_ACCOUNT_ID: undefined,
250+
};
251+
writeWorkerSource();
252+
mockSubDomainRequest();
253+
mockUploadWorkerRequest();
254+
mockOAuthServerCallback();
255+
mockGetMemberships({
256+
success: false,
257+
result: [],
258+
});
238259

239-
X [ERROR] Did not login, quitting...
260+
await expect(runWrangler("publish index.js")).rejects.toThrowError();
261+
262+
expect(std.err).toMatchInlineSnapshot(`
263+
"X [ERROR] Failed to automatically retrieve account IDs for the logged in user.
264+
In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file.
240265
241266
"
242267
`);

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
44
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
55
import { mockConsoleMethods } from "./helpers/mock-console";
66
import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
7-
import { useMockIsTTY } from "./helpers/mock-istty";
87
import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
98
import { useMockStdin } from "./helpers/mock-stdin";
109
import { runInTempDir } from "./helpers/run-in-tmp";
@@ -13,14 +12,13 @@ import { runWrangler } from "./helpers/run-wrangler";
1312
describe("wrangler secret", () => {
1413
const std = mockConsoleMethods();
1514
const { mockGetMemberships } = mockOAuthFlow();
16-
const { setIsTTY } = useMockIsTTY();
15+
1716
runInTempDir();
1817
mockAccountId();
1918
mockApiToken();
2019

2120
afterEach(() => {
2221
unsetAllMocks();
23-
setIsTTY(true);
2422
});
2523

2624
describe("put", () => {
@@ -177,11 +175,11 @@ describe("wrangler secret", () => {
177175
success: false,
178176
result: [],
179177
});
180-
await expect(
181-
runWrangler("secret put the-key --name script-name")
182-
).rejects.toThrowErrorMatchingInlineSnapshot(
183-
`"No account id found, quitting..."`
184-
);
178+
await expect(runWrangler("secret put the-key --name script-name"))
179+
.rejects.toThrowErrorMatchingInlineSnapshot(`
180+
"Failed to automatically retrieve account IDs for the logged in user.
181+
In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file."
182+
`);
185183
});
186184

187185
it("should use the account from wrangler.toml", async () => {
@@ -204,7 +202,6 @@ describe("wrangler secret", () => {
204202
});
205203

206204
it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
207-
setIsTTY(false);
208205
mockGetMemberships({
209206
success: true,
210207
result: [

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

+6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { writeAuthConfigFile } from "../user";
44
import { getUserInfo, WhoAmI } from "../whoami";
55
import { setMockResponse } from "./helpers/mock-cfetch";
66
import { mockConsoleMethods } from "./helpers/mock-console";
7+
import { useMockIsTTY } from "./helpers/mock-istty";
78
import { runInTempDir } from "./helpers/run-in-tmp";
89
import type { UserInfo } from "../whoami";
910

1011
describe("getUserInfo()", () => {
1112
runInTempDir({ homedir: "./home" });
1213
const std = mockConsoleMethods();
14+
const { setIsTTY } = useMockIsTTY();
15+
16+
beforeEach(() => {
17+
setIsTTY(true);
18+
});
1319

1420
it("should return undefined if there is no config file", async () => {
1521
const userInfo = await getUserInfo();

packages/wrangler/src/pages.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,11 @@ const createDeployment: CommandModule<
842842
const config = getConfigCache<PagesConfigCache>(
843843
PAGES_CONFIG_CACHE_FILENAME
844844
);
845-
const isInteractive = process.stdin.isTTY;
846-
const accountId = await requireAuth(config, isInteractive);
845+
const accountId = await requireAuth(config);
847846

848847
projectName ??= config.project_name;
849848

849+
const isInteractive = process.stdin.isTTY;
850850
if (!projectName && isInteractive) {
851851
const existingOrNew = await new Promise<"new" | "existing">((resolve) => {
852852
const { unmount } = render(
@@ -1605,8 +1605,8 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
16051605
const config = getConfigCache<PagesConfigCache>(
16061606
PAGES_CONFIG_CACHE_FILENAME
16071607
);
1608-
const isInteractive = process.stdin.isTTY;
1609-
const accountId = await requireAuth(config, isInteractive);
1608+
1609+
const accountId = await requireAuth(config);
16101610

16111611
const projects: Array<Project> = await listProjects({ accountId });
16121612

@@ -1650,9 +1650,9 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
16501650
const config = getConfigCache<PagesConfigCache>(
16511651
PAGES_CONFIG_CACHE_FILENAME
16521652
);
1653-
const isInteractive = process.stdin.isTTY;
1654-
const accountId = await requireAuth(config, isInteractive);
1653+
const accountId = await requireAuth(config);
16551654

1655+
const isInteractive = process.stdin.isTTY;
16561656
if (!projectName && isInteractive) {
16571657
projectName = await prompt("Enter the name of your new project:");
16581658
}
@@ -1735,11 +1735,11 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
17351735
const config = getConfigCache<PagesConfigCache>(
17361736
PAGES_CONFIG_CACHE_FILENAME
17371737
);
1738-
const isInteractive = process.stdin.isTTY;
1739-
const accountId = await requireAuth(config, isInteractive);
1738+
const accountId = await requireAuth(config);
17401739

17411740
projectName ??= config.project_name;
17421741

1742+
const isInteractive = process.stdin.isTTY;
17431743
if (!projectName && isInteractive) {
17441744
const projects = await listProjects({ accountId });
17451745
projectName = await new Promise((resolve) => {

packages/wrangler/src/user.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,9 @@ export function getAPIToken(): string | undefined {
418418
!localAPIToken &&
419419
!LocalState.accessToken?.value
420420
) {
421-
logger.error(
422-
"Missing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info: TBD"
421+
throw new Error(
422+
"In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN."
423423
);
424-
425-
return;
426424
}
427425

428426
return localAPIToken ?? LocalState.accessToken?.value;
@@ -1142,6 +1140,11 @@ export async function getAccountId(
11421140
.join("\n")
11431141
);
11441142
}
1143+
} else {
1144+
if (!isInteractive)
1145+
throw new Error(
1146+
`Failed to automatically retrieve account IDs for the logged in user. In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file.`
1147+
);
11451148
}
11461149
return accountId;
11471150
}

0 commit comments

Comments
 (0)