mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-09-30 22:03:14 +00:00
PR#1 of the AWS migration. CDK TypeScript app under /infra: stacks open-swe-iam (four per-env GitHub-OIDC deploy roles) + open-swe-dev/-prod (per-env EC2 instance role + AMI cache). aws-cdk-lib pinned exact 2.260.0; kebab naming Aspect + 15 tests. IAM is synth-only (NOT deployed). Cleared the Phase-1 security gates: - T4 GPT-4.1 IAM cross-review (StringEquals trust; cdk-hnb659fds-* wildcard kept as org convention; AWS-RunShellScript timeboxed to T19). - T5 /sh-security-review: deploy roles split PER-ENV with env-scoped OIDC trust (dev=branch ref+tag dev, prod=environment:prod+tag prod) so a dev token cannot reach prod; re-verified block:false.
93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
import * as iam from "aws-cdk-lib/aws-iam";
|
|
import { Construct } from "constructs";
|
|
import { ACCOUNT, EnvName, REGION, prefix } from "../config";
|
|
|
|
/**
|
|
* Least-privilege EC2 instance role for the open-swe box (one per env).
|
|
*
|
|
* Grants exactly what the boot/runtime flow needs and NOTHING ELSE — no admin,
|
|
* no `*` resources except where the AWS action genuinely has no resource-level
|
|
* scoping. Per-env so the dev box can never read prod secrets/config and vice
|
|
* versa. Reviewed at T4 (GPT-4.1 IAM cross-review) / T5 (/sh-security-review)
|
|
* before it is ever deployed (T6).
|
|
*/
|
|
export class InstanceRole extends Construct {
|
|
public readonly role: iam.Role;
|
|
|
|
constructor(scope: Construct, id: string, env: EnvName) {
|
|
super(scope, id);
|
|
const p = prefix(env);
|
|
|
|
this.role = new iam.Role(this, "Role", {
|
|
roleName: `${p}-instance-role`,
|
|
assumedBy: new iam.ServicePrincipal("ec2.amazonaws.com"),
|
|
description: `EC2 instance role for the ${p} open-swe box (least-privilege).`,
|
|
});
|
|
|
|
// AWS-managed: lets the SSM agent register the instance and RECEIVE the
|
|
// app-deploy `ssm:SendCommand` from githubdeploy-open-swe-app. This is the
|
|
// standard Session-Manager / RunCommand grant and is the only managed
|
|
// policy on the role. DELIBERATE — flag for T4 confirmation.
|
|
this.role.addManagedPolicy(
|
|
iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonSSMManagedInstanceCore"),
|
|
);
|
|
|
|
// Read the build artifact from the env's S3 asset bucket (deploy = pull).
|
|
this.role.addToPolicy(
|
|
new iam.PolicyStatement({
|
|
sid: "ReadArtifactObjects",
|
|
actions: ["s3:GetObject"],
|
|
resources: [`arn:aws:s3:::${p}-assets/*`],
|
|
}),
|
|
);
|
|
this.role.addToPolicy(
|
|
new iam.PolicyStatement({
|
|
sid: "ListArtifactBucket",
|
|
actions: ["s3:ListBucket", "s3:GetBucketLocation"],
|
|
resources: [`arn:aws:s3:::${p}-assets`],
|
|
}),
|
|
);
|
|
|
|
// Read non-sensitive config from SSM Parameter Store under /open-swe-<env>/*.
|
|
this.role.addToPolicy(
|
|
new iam.PolicyStatement({
|
|
sid: "ReadSsmConfig",
|
|
actions: ["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParametersByPath"],
|
|
resources: [`arn:aws:ssm:${REGION}:${ACCOUNT}:parameter/${p}/*`],
|
|
}),
|
|
);
|
|
|
|
// Read secrets from Secrets Manager under open-swe-<env>/*. Secret ARNs carry
|
|
// a random 6-char suffix, hence the trailing `*`.
|
|
this.role.addToPolicy(
|
|
new iam.PolicyStatement({
|
|
sid: "ReadSecrets",
|
|
actions: ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"],
|
|
resources: [`arn:aws:secretsmanager:${REGION}:${ACCOUNT}:secret:${p}/*`],
|
|
}),
|
|
);
|
|
|
|
// NOTE (T11): SSM SecureString + Secrets Manager here are assumed to use the
|
|
// AWS-managed keys (alias/aws/ssm, alias/aws/secretsmanager) for which the
|
|
// service grants Decrypt implicitly — so NO kms:Decrypt is granted. If T11
|
|
// moves these to a customer CMK, add a scoped `kms:Decrypt` on that key ARN
|
|
// ONLY (not `*`).
|
|
|
|
// Ship application logs to CloudWatch Logs under /open-swe/<env>/*.
|
|
this.role.addToPolicy(
|
|
new iam.PolicyStatement({
|
|
sid: "PutAppLogs",
|
|
actions: [
|
|
"logs:CreateLogGroup",
|
|
"logs:CreateLogStream",
|
|
"logs:PutLogEvents",
|
|
"logs:DescribeLogStreams",
|
|
],
|
|
resources: [
|
|
`arn:aws:logs:${REGION}:${ACCOUNT}:log-group:/open-swe/${env}/*`,
|
|
`arn:aws:logs:${REGION}:${ACCOUNT}:log-group:/open-swe/${env}/*:*`,
|
|
],
|
|
}),
|
|
);
|
|
}
|
|
}
|