diff --git a/infra/lib/own-account-read.ts b/infra/lib/own-account-read.ts index dbc1cdb91..c4116a8b4 100644 --- a/infra/lib/own-account-read.ts +++ b/infra/lib/own-account-read.ts @@ -85,8 +85,12 @@ export function ownAccountStatements(roleArn: string): Statement[] { /** Gives `fn`'s role this release's read-only permissions in its own account, and tells it so. */ export function grantOwnAccountRead(scope: Construct, id: string, fn: cdk.aws_lambda.Function): void { const role = fn.role!; + // The description is frozen as released (test/fixtures/released-managed-policies.json). + // Changing it replaces the policy, the replacement keeps the generated name, + // and every install's update fails with "already exists". What the policy + // covers now (Bedrock knowledge bases too) is in the statements, not here. new iam.CfnManagedPolicy(scope, id, { - description: "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS, Bedrock knowledge bases), read-only; kept current by each version update", + description: "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS), read-only; kept current by each version update", roles: [role.roleName], policyDocument: { Version: "2012-10-17", Statement: ownAccountStatements(role.roleArn) }, }); diff --git a/infra/test/fixtures/released-managed-policies.json b/infra/test/fixtures/released-managed-policies.json new file mode 100644 index 000000000..3a48b3223 --- /dev/null +++ b/infra/test/fixtures/released-managed-policies.json @@ -0,0 +1,66 @@ +{ + "coderhelm.json/Boundary": { + "Description": "CoderHelm: the most any CoderHelm role can do (docs/self-host/security.md)", + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-boundary-stack" + }, + "Path": null + }, + "coderhelm.json/DeployBoundary": { + "Description": "CoderHelm: the auto-update role's boundary, the same as every CoderHelm role's (docs/self-host/security.md)", + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-deploy-boundary-stack" + }, + "Path": null + }, + "Api/GatewayServiceRoleOverflowPolicy11BB532A1": { + "Description": { + "Fn::Sub": "Part of the policies for coderhelm-${Stage}-api/Gateway/ServiceRole" + }, + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-OverflowPolicy1-81668949" + }, + "Path": "/" + }, + "Api/GatewayServiceRoleOverflowPolicy23FA68F4F": { + "Description": { + "Fn::Sub": "Part of the policies for coderhelm-${Stage}-api/Gateway/ServiceRole" + }, + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-OverflowPolicy2-e68ee111" + }, + "Path": "/" + }, + "Api/GatewayOwnAccountRead": { + "Description": "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS), read-only; kept current by each version update", + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-GatewayOwnAccountRead-aa934871" + }, + "Path": null + }, + "Worker/WorkerServiceRoleOverflowPolicy15A2EF9BB": { + "Description": { + "Fn::Sub": "Part of the policies for coderhelm-${Stage}-worker/Worker/ServiceRole" + }, + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-OverflowPolicy1-2ccac3d5" + }, + "Path": "/" + }, + "Worker/WorkerServiceRoleOverflowPolicy29FC0A908": { + "Description": { + "Fn::Sub": "Part of the policies for coderhelm-${Stage}-worker/Worker/ServiceRole" + }, + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-OverflowPolicy2-b95bf6c8" + }, + "Path": "/" + }, + "Worker/WorkerOwnAccountRead": { + "Description": "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS), read-only; kept current by each version update", + "ManagedPolicyName": { + "Fn::Sub": "coderhelm-${Stage}-WorkerOwnAccountRead-e10f242c" + }, + "Path": null + } +} diff --git a/infra/test/native.test.ts b/infra/test/native.test.ts index f93736da6..06172ce39 100644 --- a/infra/test/native.test.ts +++ b/infra/test/native.test.ts @@ -21,6 +21,7 @@ import * as path from "node:path"; import { buildApp } from "../lib/app"; import { resolveConfig } from "../lib/config"; import { buildNative, nativeStamp } from "../lib/native/build"; +import { repoPath } from "../lib/paths"; import { ASSETS_BUCKET_SENTINEL, REGION_SENTINEL, STAGE_SENTINEL, nativeConfig } from "../lib/native/config"; import { emailEnvironment } from "../lib/service-env"; import { decision } from "./iam-eval"; @@ -627,3 +628,47 @@ test("every resource type in the release is one the simulator checked the auto-u } assert.deepEqual([...missing], []); }); + +// A released managed policy's Description, ManagedPolicyName and Path can't +// change. Each forces CloudFormation to replace the policy, the replacement +// keeps the name CloudFormation generated for it, the create comes before the +// delete, and IAM refuses it: "A policy called ... already exists", so the +// update rolls back on every install (v0.8.57, GatewayOwnAccountRead). +// test/fixtures/released-managed-policies.json holds those properties as +// released. A new policy is added there; an entry already there never changes. +test("no released managed policy is replaced on update: Description, ManagedPolicyName and Path stay as released (test/fixtures/released-managed-policies.json)", () => { + const FROZEN = ["Description", "ManagedPolicyName", "Path"] as const; + const fixture = repoPath("infra", "test", "fixtures", "released-managed-policies.json"); + const released: Record> = JSON.parse(fs.readFileSync(fixture, "utf8")); + const changed: string[] = []; + const unpinned: string[] = []; + for (const [name, t] of all()) { + for (const [id, r] of Object.entries(t.Resources)) { + if (r.Type !== "AWS::IAM::ManagedPolicy") continue; + const key = `${name}/${id}`; + const now = Object.fromEntries(FROZEN.map((k) => [k, r.Properties?.[k] ?? null])); + const was = released[key]; + if (!was) { + unpinned.push(`${key}: ${JSON.stringify(now)}`); + continue; + } + for (const k of FROZEN) { + if (JSON.stringify(now[k]) !== JSON.stringify(was[k] ?? null)) { + changed.push(`${key} ${k}: released ${JSON.stringify(was[k] ?? null)}, now ${JSON.stringify(now[k])}`); + } + } + } + } + assert.deepEqual( + changed, + [], + "Changing a released managed policy's Description, ManagedPolicyName or Path replaces the policy, and every install's update " + + "fails with \"A policy called ... already exists\" and rolls back. Restore the released value; put new wording in a code " + + "comment or in the policy statements' Sid, never in the description.", + ); + assert.deepEqual( + unpinned, + [], + `A new managed policy: add it to ${fixture} as shown, so its Description, ManagedPolicyName and Path are pinned from its first release.`, + ); +});