Skip to content

Commit 02e2c1e

Browse files
add warning about env not specified to potentially risky wrangler commands (#9344)
* add warning about env not specified to potentially risky wrangler commands * Apply suggestions from code review Co-authored-by: Pete Bacon Darwin <[email protected]> * remove skip committed by mistake * remove extra debugging logs * use configPath instead * remove unused import * remove unnecessary displayName meta property * [wrangler] allow passing an empty string to the `-e|--env` flag to target the top-level environment * Update packages/wrangler/src/__tests__/deploy.test.ts Co-authored-by: Pete Bacon Darwin <[email protected]> * revert silly change * fix incorrect code change --------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent ddeeefd commit 02e2c1e

File tree

21 files changed

+658
-46
lines changed

21 files changed

+658
-46
lines changed

.changeset/some-cloths-move.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
add warning about env not specified to potentially risky wrangler commands
6+
7+
add a warning suggesting users to specify their target environment (via `-e` or `--env`)
8+
when their wrangler config file contains some environments and they are calling one
9+
of the following commands:
10+
11+
- wrangler deploy
12+
- wrangler versions upload
13+
- wrangler versions deploy
14+
- wrangler versions secret bulk
15+
- wrangler versions secret put
16+
- wrangler versions secret delete
17+
- wrangler secret bulk
18+
- wrangler secret put
19+
- wrangler secret delete
20+
- wrangler triggers deploy (TODO: manually test)
21+
22+
this is a measure we're putting in place to try to prevent developers from accidentally applying
23+
changes to an incorrect (potentially even production) environment

.changeset/spotty-worlds-rest.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+
allow passing an empty string to the `-e|--env` flag to target the top-level environment

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13006,6 +13006,65 @@ export default{
1300613006
await runWrangler("deploy ./index.js");
1300713007
});
1300813008
});
13009+
13010+
describe("multi-env warning", () => {
13011+
it("should warn if the wrangler config contains environments but none was specified in the command", async () => {
13012+
writeWorkerSource();
13013+
writeWranglerConfig({
13014+
main: "./index.js",
13015+
env: {
13016+
test: {},
13017+
},
13018+
});
13019+
mockSubDomainRequest();
13020+
mockUploadWorkerRequest();
13021+
13022+
await runWrangler("deploy");
13023+
13024+
expect(std.warn).toMatchInlineSnapshot(`
13025+
"▲ [WARNING] Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the deploy command.
13026+
13027+
To avoid unintentional changes to the wrong environment, it is recommended to explicitly specify
13028+
the target environment using the \`-e|--env\` flag.
13029+
If your intention is to use the top-level environment of your configuration simply pass an empty
13030+
string to the flag to target such environment. For example \`--env=\\"\\"\`.
13031+
13032+
"
13033+
`);
13034+
});
13035+
13036+
it("should not warn if the wrangler config contains environments and one was specified in the command", async () => {
13037+
writeWorkerSource();
13038+
writeWranglerConfig({
13039+
main: "./index.js",
13040+
env: {
13041+
test: {},
13042+
},
13043+
});
13044+
mockSubDomainRequest();
13045+
mockUploadWorkerRequest({
13046+
env: "test",
13047+
legacyEnv: true,
13048+
});
13049+
13050+
await runWrangler("deploy -e test");
13051+
13052+
expect(std.warn).toMatchInlineSnapshot(`""`);
13053+
});
13054+
13055+
it("should not warn if the wrangler config doesn't contain environments and none was specified in the command", async () => {
13056+
writeWorkerSource();
13057+
writeWranglerConfig({
13058+
main: "./index.js",
13059+
});
13060+
mockSubDomainRequest();
13061+
mockUploadWorkerRequest();
13062+
13063+
await runWrangler("deploy");
13064+
13065+
expect(std.warn).toMatchInlineSnapshot(`""`);
13066+
});
13067+
});
1300913068
});
1301013069

1301113070
/** Write mock assets to the file system so they can be uploaded. */

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

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useMockStdin } from "./helpers/mock-stdin";
1414
import { msw } from "./helpers/msw";
1515
import { runInTempDir } from "./helpers/run-in-tmp";
1616
import { runWrangler } from "./helpers/run-wrangler";
17+
import { writeWranglerConfig } from "./helpers/write-wrangler-config";
1718
import type { Interface } from "node:readline";
1819

1920
function createFetchResult(
@@ -458,6 +459,49 @@ describe("wrangler secret", () => {
458459
`);
459460
});
460461
});
462+
463+
describe("multi-env warning", () => {
464+
it("should warn if the wrangler config contains environments but none was specified in the command", async () => {
465+
writeWranglerConfig({
466+
env: {
467+
test: {},
468+
},
469+
});
470+
mockStdIn.send("the-secret");
471+
mockPutRequest({ name: "the-key", text: "the-secret" });
472+
await runWrangler("secret put the-key --name script-name");
473+
expect(std.warn).toMatchInlineSnapshot(`
474+
"▲ [WARNING] Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the secret put command.
475+
476+
To avoid unintentional changes to the wrong environment, it is recommended to explicitly specify
477+
the target environment using the \`-e|--env\` flag.
478+
If your intention is to use the top-level environment of your configuration simply pass an empty
479+
string to the flag to target such environment. For example \`--env=\\"\\"\`.
480+
481+
"
482+
`);
483+
});
484+
485+
it("should not warn if the wrangler config contains environments and one was specified in the command", async () => {
486+
writeWranglerConfig({
487+
env: {
488+
test: {},
489+
},
490+
});
491+
mockStdIn.send("the-secret");
492+
mockPutRequest({ name: "the-key", text: "the-secret" }, "test", true);
493+
await runWrangler("secret put the-key --name script-name -e test");
494+
expect(std.warn).toMatchInlineSnapshot(`""`);
495+
});
496+
497+
it("should not warn if the wrangler config doesn't contain environments and none was specified in the command", async () => {
498+
writeWranglerConfig();
499+
mockStdIn.send("the-secret");
500+
mockPutRequest({ name: "the-key", text: "the-secret" });
501+
await runWrangler("secret put the-key --name script-name");
502+
expect(std.warn).toMatchInlineSnapshot(`""`);
503+
});
504+
});
461505
});
462506

463507
it("should error if the latest version is not deployed", async () => {
@@ -640,6 +684,62 @@ describe("wrangler secret", () => {
640684
`[Error: Required Worker name missing. Please specify the Worker name in your Wrangler configuration file, or pass it as an argument with \`--name <worker-name>\`]`
641685
);
642686
});
687+
688+
describe("multi-env warning", () => {
689+
it("should warn if the wrangler config contains environments but none was specified in the command", async () => {
690+
writeWranglerConfig({
691+
env: {
692+
test: {},
693+
},
694+
});
695+
mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" });
696+
mockConfirm({
697+
text: "Are you sure you want to permanently delete the secret the-key on the Worker script-name?",
698+
result: true,
699+
});
700+
await runWrangler("secret delete the-key --name script-name");
701+
expect(std.warn).toMatchInlineSnapshot(`
702+
"▲ [WARNING] Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the secret delete command.
703+
704+
To avoid unintentional changes to the wrong environment, it is recommended to explicitly specify
705+
the target environment using the \`-e|--env\` flag.
706+
If your intention is to use the top-level environment of your configuration simply pass an empty
707+
string to the flag to target such environment. For example \`--env=\\"\\"\`.
708+
709+
"
710+
`);
711+
});
712+
713+
it("should not warn if the wrangler config contains environments and one was specified in the command", async () => {
714+
writeWranglerConfig({
715+
env: {
716+
test: {},
717+
},
718+
});
719+
mockDeleteRequest(
720+
{ scriptName: "script-name", secretName: "the-key" },
721+
"test",
722+
true
723+
);
724+
mockConfirm({
725+
text: "Are you sure you want to permanently delete the secret the-key on the Worker script-name-test?",
726+
result: true,
727+
});
728+
await runWrangler("secret delete the-key --name script-name -e test");
729+
expect(std.warn).toMatchInlineSnapshot(`""`);
730+
});
731+
732+
it("should not warn if the wrangler config doesn't contain environments and none was specified in the command", async () => {
733+
writeWranglerConfig();
734+
mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" });
735+
mockConfirm({
736+
text: "Are you sure you want to permanently delete the secret the-key on the Worker script-name?",
737+
result: true,
738+
});
739+
await runWrangler("secret delete the-key --name script-name");
740+
expect(std.warn).toMatchInlineSnapshot(`""`);
741+
});
742+
});
643743
});
644744

645745
describe("list", () => {
@@ -1240,5 +1340,63 @@ describe("wrangler secret", () => {
12401340
✨ 2 secrets successfully uploaded"
12411341
`);
12421342
});
1343+
1344+
describe("multi-env warning", () => {
1345+
it("should warn if the wrangler config contains environments but none was specified in the command", async () => {
1346+
writeWranglerConfig({
1347+
name: "test-name",
1348+
main: "./index.js",
1349+
env: {
1350+
test: {},
1351+
},
1352+
});
1353+
writeFileSync("secret.json", JSON.stringify({}));
1354+
1355+
mockBulkRequest();
1356+
1357+
await runWrangler("secret bulk ./secret.json --name script-name");
1358+
expect(std.warn).toMatchInlineSnapshot(`
1359+
"▲ [WARNING] Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the secret bulk command.
1360+
1361+
To avoid unintentional changes to the wrong environment, it is recommended to explicitly specify
1362+
the target environment using the \`-e|--env\` flag.
1363+
If your intention is to use the top-level environment of your configuration simply pass an empty
1364+
string to the flag to target such environment. For example \`--env=\\"\\"\`.
1365+
1366+
"
1367+
`);
1368+
});
1369+
1370+
it("should not warn if the wrangler config contains environments and one was specified in the command", async () => {
1371+
writeWranglerConfig({
1372+
name: "test-name",
1373+
main: "./index.js",
1374+
env: {
1375+
test: {},
1376+
},
1377+
});
1378+
writeFileSync("secret.json", JSON.stringify({}));
1379+
1380+
mockBulkRequest();
1381+
1382+
await runWrangler(
1383+
"secret bulk ./secret.json --name script-name -e test"
1384+
);
1385+
expect(std.warn).toMatchInlineSnapshot(`""`);
1386+
});
1387+
1388+
it("should not warn if the wrangler config doesn't contain environments and none was specified in the command", async () => {
1389+
writeWranglerConfig({
1390+
name: "test-name",
1391+
main: "./index.js",
1392+
});
1393+
writeFileSync("secret.json", JSON.stringify({}));
1394+
1395+
mockBulkRequest();
1396+
1397+
await runWrangler("secret bulk ./secret.json --name script-name");
1398+
expect(std.warn).toMatchInlineSnapshot(`""`);
1399+
});
1400+
});
12431401
});
12441402
});

packages/wrangler/src/__tests__/versions/secrets/bulk.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,66 @@ describe("versions secret bulk", () => {
279279
);
280280
expect(std.err).toMatchInlineSnapshot(`""`);
281281
});
282+
283+
describe("multi-env warning", () => {
284+
it("should warn if the wrangler config contains environments but none was specified in the command", async () => {
285+
vi.spyOn(readline, "createInterface").mockImplementation(
286+
() =>
287+
// `readline.Interface` is an async iterator: `[Symbol.asyncIterator](): AsyncIterableIterator<string>`
288+
JSON.stringify({
289+
SECRET_1: "secret-1",
290+
}) as unknown as Interface
291+
);
292+
293+
writeWranglerConfig({ env: { test: {} } });
294+
mockSetupApiCalls();
295+
mockPostVersion();
296+
297+
await runWrangler(`versions secret bulk --name script-name`);
298+
expect(std.warn).toMatchInlineSnapshot(`
299+
"▲ [WARNING] Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the versions secret bulk command.
300+
301+
To avoid unintentional changes to the wrong environment, it is recommended to explicitly specify
302+
the target environment using the \`-e|--env\` flag.
303+
If your intention is to use the top-level environment of your configuration simply pass an empty
304+
string to the flag to target such environment. For example \`--env=\\"\\"\`.
305+
306+
"
307+
`);
308+
});
309+
310+
it("should not warn if the wrangler config contains environments and one was specified in the command", async () => {
311+
vi.spyOn(readline, "createInterface").mockImplementation(
312+
() =>
313+
// `readline.Interface` is an async iterator: `[Symbol.asyncIterator](): AsyncIterableIterator<string>`
314+
JSON.stringify({
315+
SECRET_1: "secret-1",
316+
}) as unknown as Interface
317+
);
318+
319+
writeWranglerConfig({ env: { test: {} } });
320+
mockSetupApiCalls();
321+
mockPostVersion();
322+
323+
await runWrangler(`versions secret bulk --name script-name --env test`);
324+
expect(std.warn).toMatchInlineSnapshot(`""`);
325+
});
326+
327+
it("should not warn if the wrangler config doesn't contain environments and none was specified in the command", async () => {
328+
vi.spyOn(readline, "createInterface").mockImplementation(
329+
() =>
330+
// `readline.Interface` is an async iterator: `[Symbol.asyncIterator](): AsyncIterableIterator<string>`
331+
JSON.stringify({
332+
SECRET_1: "secret-1",
333+
}) as unknown as Interface
334+
);
335+
336+
writeWranglerConfig();
337+
mockSetupApiCalls();
338+
mockPostVersion();
339+
340+
await runWrangler(`versions secret bulk --name script-name`);
341+
expect(std.warn).toMatchInlineSnapshot(`""`);
342+
});
343+
});
282344
});

0 commit comments

Comments
 (0)