Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion infra/lib/own-account-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
});
Expand Down
66 changes: 66 additions & 0 deletions infra/test/fixtures/released-managed-policies.json
Original file line number Diff line number Diff line change
@@ -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
}
}
45 changes: 45 additions & 0 deletions infra/test/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, Record<string, unknown>> = 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<any>(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.`,
);
});
Loading