From c4e99216c36d537f6a94babc7b0c9b244c441d3c Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Sat, 16 Apr 2022 09:00:25 -0400 Subject: [PATCH] fix: disallow setting account_id in named service environments Much like https://github.com/cloudflare/wrangler2/pull/641, we don't want to allow setting account_id with named service environments. This is so that we use the same account_id for multiple environments, and have them group together in the dashboard. --- .changeset/little-roses-invite.md | 7 +++ .../src/__tests__/configuration.test.ts | 55 +++++++++++++++++++ .../wrangler/src/config/validation-helpers.ts | 2 +- packages/wrangler/src/config/validation.ts | 4 +- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 .changeset/little-roses-invite.md diff --git a/.changeset/little-roses-invite.md b/.changeset/little-roses-invite.md new file mode 100644 index 000000000000..56fafa060cc1 --- /dev/null +++ b/.changeset/little-roses-invite.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: disallow setting account_id in named service environments + +Much like https://github.com/cloudflare/wrangler2/pull/641, we don't want to allow setting account_id with named service environments. This is so that we use the same account_id for multiple environments, and have them group together in the dashboard. diff --git a/packages/wrangler/src/__tests__/configuration.test.ts b/packages/wrangler/src/__tests__/configuration.test.ts index 17ae0877df14..641339a8d847 100644 --- a/packages/wrangler/src/__tests__/configuration.test.ts +++ b/packages/wrangler/src/__tests__/configuration.test.ts @@ -1763,6 +1763,61 @@ describe("normalizeAndValidateConfig()", () => { }); }); + it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => { + const rawConfig: RawConfig = { + legacy_env: false, + env: { + DEV: { + account_id: "some_account_id", + }, + }, + }; + + const { config, diagnostics } = normalizeAndValidateConfig( + rawConfig, + undefined, + { env: "DEV" } + ); + + expect(config.account_id).toBeUndefined(); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + + - \\"env.DEV\\" environment configuration + - The \\"account_id\\" field is not allowed in named service environments. + Please remove the field from this environment." + `); + expect(diagnostics.hasWarnings()).toBe(false); + }); + + it("should error if top-level config and a named environment both contain a `account_id` field", () => { + const rawConfig: RawConfig = { + account_id: "ACCOUNT_ID", + legacy_env: false, + env: { + DEV: { + account_id: "ENV_ACCOUNT_ID", + }, + }, + }; + + const { config, diagnostics } = normalizeAndValidateConfig( + rawConfig, + undefined, + { env: "DEV" } + ); + + expect(config.account_id).toEqual("ACCOUNT_ID"); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + + - \\"env.DEV\\" environment configuration + - The \\"account_id\\" field is not allowed in named service environments. + Please remove the field from this environment." + `); + expect(diagnostics.hasWarnings()).toBe(false); + }); + it("should warn for non-inherited fields that are missing in environments", () => { const vars: RawConfig["vars"] = { FOO: "foo", diff --git a/packages/wrangler/src/config/validation-helpers.ts b/packages/wrangler/src/config/validation-helpers.ts index 724b7625ecf4..e3e76d575a9c 100644 --- a/packages/wrangler/src/config/validation-helpers.ts +++ b/packages/wrangler/src/config/validation-helpers.ts @@ -82,7 +82,7 @@ export function inheritableInLegacyEnvironments( rawEnv: RawEnvironment, field: K, validate: ValidatorFn, - transformFn: TransformFn, + transformFn: TransformFn = (v) => v, defaultValue: Environment[K] ): Environment[K] { return topLevelEnv === undefined || isLegacyEnv === true diff --git a/packages/wrangler/src/config/validation.ts b/packages/wrangler/src/config/validation.ts index 87ad213bf06b..53612cb0282c 100644 --- a/packages/wrangler/src/config/validation.ts +++ b/packages/wrangler/src/config/validation.ts @@ -651,12 +651,14 @@ function normalizeAndValidateEnvironment( const environment: Environment = { // Inherited fields - account_id: inheritable( + account_id: inheritableInLegacyEnvironments( diagnostics, + isLegacyEnv, topLevelEnv, rawEnv, "account_id", isString, + undefined, undefined ), compatibility_date: inheritable(