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
23 changes: 18 additions & 5 deletions infra/lib/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ export const OWN_READ_ROLES = ["CoderHelmLogReader", "CoderhelmReadOnly"];
*/
export const RUNTIME_ROLE_TAG = "coderhelm:role";
export const RUNTIME_ROLES = ["gateway", "worker"];
/**
* The log analyzer's tag value (infra/lib/log-analyzer-stack.ts). It reads
* logs through the log reader here: without it a same-account install never
* analyzes its logs, and no finding ever resolves. The boundary lets it take
* on CoderHelm's read roles like the gateway and worker (one statement keeps
* the boundary under IAM's 6,144 characters); its own role policy allows
* sts:AssumeRole on CoderHelmLogReader only, so it never takes on the
* data-source role (selfhost/check-access.mjs checks both together).
*/
export const LOG_ANALYZER_ROLE = "log-analyzer";
/** Every tag value the boundary lets take on the read roles in this account. */
export const READ_ROLE_CALLERS = [...RUNTIME_ROLES, LOG_ANALYZER_ROLE];
/** STS actions on a role in this account: all denied, except sts:AssumeRole on the read roles above. */
const ROLE_SESSION_ACTIONS = ["sts:AssumeRole", "sts:AssumeRoleWithWebIdentity", "sts:AssumeRoleWithSAML", "sts:TagSession", "sts:SetSourceIdentity"];

Expand Down Expand Up @@ -331,14 +343,15 @@ export function guardrails(n: AccessNames, otherBoundaries: string[] = []): Stat
Resource: a.ownReadRoles,
},
{
// And only by the gateway's and the worker's roles (their tag; a role
// without it is refused). Each read role's trust policy still requires
// the team's external id.
Sid: "ReadRolesHereOnlyGatewayAndWorker",
// And only by CoderHelm's own functions that read through them (their
// tag; a role without it is refused): the gateway, the worker and the
// log analyzer. Each read role's trust policy still requires the
// team's external id.
Sid: "ReadRolesHereOnlyCoderHelmRuntime",
Effect: "Deny",
Action: "sts:AssumeRole",
Resource: a.ownReadRoles,
Condition: { StringNotEquals: { [`aws:PrincipalTag/${RUNTIME_ROLE_TAG}`]: RUNTIME_ROLES } },
Condition: { StringNotEquals: { [`aws:PrincipalTag/${RUNTIME_ROLE_TAG}`]: READ_ROLE_CALLERS } },
},
{
// That tag is set and removed only by CloudFormation through the
Expand Down
7 changes: 7 additions & 0 deletions infra/lib/log-analyzer-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as s3 from "aws-cdk-lib/aws-s3";
import * as s3deploy from "aws-cdk-lib/aws-s3-deployment";
import * as sqs from "aws-cdk-lib/aws-sqs";
import { Construct } from "constructs";
import { LOG_ANALYZER_ROLE, RUNTIME_ROLE_TAG } from "./access";
import { ResolvedConfig } from "./config";
import { repoPath } from "./paths";
import { modelProviderEnvironment, modelPolicyStatements } from "./service-env";
Expand Down Expand Up @@ -120,6 +121,12 @@ export class LogAnalyzerStack extends cdk.Stack {
})
);

// On a hardened install in the same account as the logs it reads, the
// boundary lets this tag take on the read roles there
// (infra/lib/access.ts READ_ROLE_CALLERS); the statement above keeps it
// to the log reader.
cdk.Tags.of(analyzerFunction.role!).add(RUNTIME_ROLE_TAG, LOG_ANALYZER_ROLE);

// --- EventBridge Schedule: every 6 hours ---
new events.Rule(this, "AnalyzerSchedule", {
ruleName: `${prefix}-log-analyzer-schedule`,
Expand Down
2 changes: 1 addition & 1 deletion infra/lib/worker-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class WorkerStack extends cdk.Stack {
);
// On a hardened install the boundary lets only the gateway's and the
// worker's roles take on these two roles in the install's own account
// (infra/lib/access.ts, ReadRolesHereOnlyGatewayAndWorker): this tag
// (infra/lib/access.ts, ReadRolesHereOnlyCoderHelmRuntime): this tag
// says which they are.
cdk.Tags.of(this.workerFunction.role!).add(RUNTIME_ROLE_TAG, "worker");
// MCP servers: read their settings; write back a remote server's renewed
Expand Down
10 changes: 9 additions & 1 deletion infra/scripts/deploy-role-sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
4. Simulates what the boundary leaves the install's functions in its own
account (`runtime`): sts actions on CoderHelm's two read roles, on
lookalike names, the CDK bootstrap, the deploy role and the install's own
roles, by the gateway's and worker's roles (their coderhelm:role tag), by
roles, by the gateway's, worker's and log analyzer's roles (their
coderhelm:role tag), by
another role and by an untagged one; and who may set that tag. The
identity policy allows all of it, so only the boundary decides.
5. Writes infra/test/fixtures/deploy-role-sim.json, which
Expand Down Expand Up @@ -665,6 +666,13 @@ def grouped(simulated):
for (const [p, v] of Object.entries(rel.parent.Resources[part].Properties.Parameters)) {
const g = v["Fn::GetAtt"];
if (!g) continue;
if (!rel.children[g[0]]) {
// A parent resource, not a part: its role's ARN (the auto-update role).
const r = rel.parent.Resources[g[0]];
if (!r || r.Type !== "AWS::IAM::Role" || g[1] !== "Arn") throw new Error("unresolved GetAtt " + JSON.stringify(g));
imports[part + "/" + p] = "arn:aws:iam::" + ACCT + ":role/" + resolveValue(r.Properties.RoleName, { values });
continue;
}
const exp = rel.children[g[0]].Outputs[g[1].replace("Outputs.", "")].Export.Name;
imports[part + "/" + p] = resolveValue(exp, { values });
}
Expand Down
19 changes: 13 additions & 6 deletions infra/test/access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as path from "node:path";
import * as cdk from "aws-cdk-lib";
import { buildApp } from "../lib/app";
import { resolveConfig, ResolvedConfig, CoderhelmConfig } from "../lib/config";
import { AccessNames, accessNames, boundaryPolicy, BOUNDARY_SERVICES, OWN_READ_ROLES, RUNTIME_ROLE_TAG, RUNTIME_ROLES, deployPolicy, INSTALL_TAG, PolicyDocument, Statement, WINDOW_EXPIRES, WINDOW_USER_ID, windowPolicy, windowPolicyParts } from "../lib/access";
import { AccessNames, accessNames, boundaryPolicy, BOUNDARY_SERVICES, LOG_ANALYZER_ROLE, OWN_READ_ROLES, READ_ROLE_CALLERS, RUNTIME_ROLE_TAG, RUNTIME_ROLES, deployPolicy, INSTALL_TAG, PolicyDocument, Statement, WINDOW_EXPIRES, WINDOW_USER_ID, windowPolicy, windowPolicyParts } from "../lib/access";
import { allowed, glob, list, Request } from "./iam-eval";
import { installName } from "../lib/hardening";
import { buildSpecSha256, canonicalJson, updateBuildSpecObject, UPDATE_BUILD_VARIABLES } from "../lib/updates-stack";
Expand Down Expand Up @@ -123,15 +123,20 @@ test("boundary: even a role whose own policy allows everything stays inside it",
// An install in the same AWS account as the data it reads takes on
// CoderHelm's own read roles there. The boundary allows exactly that: only
// sts:AssumeRole, only on those two names, only by the gateway's and the
// worker's roles (their coderhelm:role tag).
test("boundary: in its own account, only the gateway and worker may assume only CoderHelm's two read roles", () => {
// worker's roles, plus the log analyzer's on the log reader alone (their
// coderhelm:role tag). Without the analyzer's exception a same-account
// install never analyzes its logs, so no finding ever resolves.
test("boundary: in its own account, only the gateway, worker and log analyzer may assume only CoderHelm's read roles", () => {
const sts: PolicyDocument = { Version: "2012-10-17", Statement: [{ Sid: "AllSts", Effect: "Allow", Action: "sts:*", Resource: "*" }] };
const can = (action: string, resource: string, tag?: string) =>
allowed([sts], BOUNDARY, { action, resource, context: { "aws:ResourceAccount": resource.split(":")[4], ...(tag ? { [`aws:PrincipalTag/${RUNTIME_ROLE_TAG}`]: tag } : {}) } });
for (const r of ["CoderHelmLogReader", "CoderhelmReadOnly"]) {
for (const t of RUNTIME_ROLES) assert.ok(can("sts:AssumeRole", role(r), t), `${t} may assume ${r} here`);
// The log analyzer too (its own role policy keeps it to the log reader).
assert.ok(can("sts:AssumeRole", role(r), LOG_ANALYZER_ROLE), `log-analyzer may assume ${r} here`);
// Any other caller: another function's role, a build, an untagged role, a lookalike tag value.
for (const t of ["log-analyzer", "update-build", "Gateway", "gateway-x", "", undefined]) assert.ok(!can("sts:AssumeRole", role(r), t), `${t ?? "no tag"} may not assume ${r}`);
for (const t of ["update-build", "Gateway", "gateway-x", "Log-Analyzer", "log-analyzer-x", "", undefined]) assert.ok(!can("sts:AssumeRole", role(r), t), `${t ?? "no tag"} may not assume ${r}`);
for (const a of ["sts:TagSession", "sts:SetSourceIdentity", "sts:AssumeRoleWithWebIdentity"]) assert.ok(!can(a, role(r), LOG_ANALYZER_ROLE), `log-analyzer: ${a} on ${r}`);
// Only AssumeRole: no other STS action on them, even from the gateway.
for (const a of ["sts:TagSession", "sts:SetSourceIdentity", "sts:AssumeRoleWithWebIdentity", "sts:AssumeRoleWithSAML"]) assert.ok(!can(a, role(r), "gateway"), `${a} on ${r}`);
}
Expand All @@ -141,7 +146,7 @@ test("boundary: in its own account, only the gateway and worker may assume only
"CoderhelmReadOnly-2", "CoderhelmReadOnlyAdmin", "CoderHelmLogReaderAdmin", "team/CoderhelmReadOnly", "coderhelmreadonly", "CoderHelmReadOnly",
"cdk-hnb659fds-cfn-exec-role-111122223333-us-west-2", n.deployRoleName, "coderhelm-prod-ServiceRole-ab12cd34", "coderhelm-prod-update-build", "OrganizationAccountAccessRole",
]) {
for (const t of RUNTIME_ROLES) {
for (const t of [...RUNTIME_ROLES, LOG_ANALYZER_ROLE]) {
for (const a of ["sts:AssumeRole", "sts:TagSession", "sts:SetSourceIdentity", "sts:AssumeRoleWithWebIdentity"]) assert.ok(!can(a, role(r), t), `${t}: ${a} on ${r}`);
}
}
Expand Down Expand Up @@ -188,7 +193,9 @@ test("boundary: the read role names are the ones CoderHelm's own template and se
tagged[tag.Value] = fn?.Properties.FunctionName;
}
}
assert.deepEqual(tagged, { gateway: "coderhelm-prod-gateway", worker: "coderhelm-prod-worker" });
assert.deepEqual(tagged, { gateway: "coderhelm-prod-gateway", worker: "coderhelm-prod-worker", "log-analyzer": "coderhelm-prod-log-analyzer" });
// Each tag the boundary lets in is one a CoderHelm function carries.
for (const c of READ_ROLE_CALLERS) assert.ok(c in tagged, c);
});

test("boundary: allows every action every synthesized CoderHelm role is given", () => {
Expand Down
5 changes: 4 additions & 1 deletion infra/test/deploy-role-sim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test("the deploy role may make every call CloudFormation makes for a self-host i
// (infra/lib/access.ts, NoOtherRolesHere and the read-role exception), as
// AWS's simulator saw it: an identity policy allowing every sts action and
// role tag, so the boundary alone decides.
test("own-account roles: the evaluator gives the simulator's verdict, and only the gateway and worker may assume only the two read roles", () => {
test("own-account roles: the evaluator gives the simulator's verdict, and only the gateway, worker and log analyzer may assume only the two read roles", () => {
const { identity, rows } = FIXTURE.runtime;
const { boundary } = FIXTURE.policies;
const wrong = rows.flatMap((r) => Object.entries(r.actions).filter(([action, v]) => decision([identity], boundary, { action, resource: r.resource, context: r.context }) !== v).map(([a]) => `${a} on ${r.resource} as ${r.caller}`));
Expand All @@ -117,6 +117,9 @@ test("own-account roles: the evaluator gives the simulator's verdict, and only t
"deploy role: iam:UntagRole on 111122223333:role/coderhelm-prod-ServiceRole-ab12cd34 tags other",
"gateway: sts:AssumeRole on 111122223333:role/CoderHelmLogReader",
"gateway: sts:AssumeRole on 111122223333:role/CoderhelmReadOnly",
// The log analyzer reads logs here; its own role policy keeps it to the log reader.
"log-analyzer: sts:AssumeRole on 111122223333:role/CoderHelmLogReader",
"log-analyzer: sts:AssumeRole on 111122223333:role/CoderhelmReadOnly",
"untagged: sts:AssumeRole on 999988887777:role/CoderHelmLogReader",
"worker: sts:AssumeRole on 111122223333:role/CoderHelmLogReader",
"worker: sts:AssumeRole on 111122223333:role/CoderhelmReadOnly",
Expand Down
9 changes: 5 additions & 4 deletions infra/test/fixtures/deploy-role-sim.json
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@
]
},
{
"Sid": "ReadRolesHereOnlyGatewayAndWorker",
"Sid": "ReadRolesHereOnlyCoderHelmRuntime",
"Effect": "Deny",
"Action": "sts:AssumeRole",
"Resource": [
Expand All @@ -711,7 +711,8 @@
"StringNotEquals": {
"aws:PrincipalTag/coderhelm:role": [
"gateway",
"worker"
"worker",
"log-analyzer"
]
}
}
Expand Down Expand Up @@ -1348,12 +1349,12 @@
"rows": [
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "gateway", "context": {"aws:PrincipalTag/coderhelm:role": "gateway", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "worker", "context": {"aws:PrincipalTag/coderhelm:role": "worker", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "log-analyzer", "context": {"aws:PrincipalTag/coderhelm:role": "log-analyzer", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "log-analyzer", "context": {"aws:PrincipalTag/coderhelm:role": "log-analyzer", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "lookalike tag", "context": {"aws:PrincipalTag/coderhelm:role": "Gateway", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "untagged", "context": {"aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderHelmLogReader"},
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "gateway", "context": {"aws:PrincipalTag/coderhelm:role": "gateway", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "worker", "context": {"aws:PrincipalTag/coderhelm:role": "worker", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "log-analyzer", "context": {"aws:PrincipalTag/coderhelm:role": "log-analyzer", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "allowed", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "log-analyzer", "context": {"aws:PrincipalTag/coderhelm:role": "log-analyzer", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "lookalike tag", "context": {"aws:PrincipalTag/coderhelm:role": "Gateway", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "untagged", "context": {"aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly"},
{"actions": {"sts:AssumeRole": "explicitDeny", "sts:AssumeRoleWithSAML": "explicitDeny", "sts:AssumeRoleWithWebIdentity": "explicitDeny", "sts:SetSourceIdentity": "explicitDeny", "sts:TagSession": "explicitDeny"}, "caller": "gateway", "context": {"aws:PrincipalTag/coderhelm:role": "gateway", "aws:ResourceAccount": "111122223333"}, "resource": "arn:aws:iam::111122223333:role/CoderhelmReadOnly-2"},
Expand Down
Loading
Loading