diff --git a/.github/scripts/aws-origin-tls-workflow.test.sh b/.github/scripts/aws-origin-tls-workflow.test.sh index ddf18fb41..e6e97a2c2 100644 --- a/.github/scripts/aws-origin-tls-workflow.test.sh +++ b/.github/scripts/aws-origin-tls-workflow.test.sh @@ -16,17 +16,30 @@ raise 'bounded job required' unless job['timeout-minutes'] == 25 raise 'only contents and OIDC permissions needed' unless job['permissions'] == {'contents'=>'read', 'id-token'=>'write'} steps = job.fetch('steps') credentials = steps.select { |s| s['uses'] == 'aws-actions/configure-aws-credentials@v4' } -raise 'existing role with restrictive session policy required' unless credentials.length == 1 && credentials[0]['with'] == { - 'role-to-assume'=>'${{ vars.AWS_TERRAFORM_APPLY_ROLE_ARN }}', 'aws-region'=>'us-east-1', - 'inline-session-policy'=>'${{ steps.scope.outputs.policy }}' -} +raise 'separate probe and cleanup credentials required' unless credentials.map { |s| s['id'] } == ['credentials', 'cleanup_credentials'] +['probe', 'cleanup'].each_with_index do |phase, i| + raise 'existing role with phase policy and no credential reuse required' unless credentials[i]['with'] == { + 'role-to-assume'=>'${{ vars.AWS_TERRAFORM_APPLY_ROLE_ARN }}', 'aws-region'=>'us-east-1', + 'unset-current-credentials'=>true, + 'inline-session-policy'=>"${{ steps.scope.outputs.#{phase}_policy }}" + } +end scope = steps.index { |s| s['run'] == 'node .github/scripts/aws-origin-tls.cjs session-policy' } -raise 'source gate must precede credentials' unless scope && scope < steps.index(credentials[0]) +raise 'source gate must precede credentials' unless scope && steps[scope]['id'] == 'scope' && scope < steps.index(credentials[0]) +raise 'probe credentials require the successful source gate' if credentials[0]['if'] probe = steps.find { |s| s['run'] == 'node .github/scripts/aws-origin-tls.cjs probe' } -raise 'probe timeout required' unless probe && probe['timeout-minutes'] == 10 +raise 'probe requires its successful credentials and timeout' unless probe && probe['timeout-minutes'] == 10 && + probe['if'] == "success() && steps.credentials.outcome == 'success'" +raise 'probe must run once immediately after its credentials' unless steps.count { |s| s['run'] == probe['run'] } == 1 && + steps.index(probe) == steps.index(credentials[0]) + 1 +raise 'cleanup credentials must follow probe failure or credential failure after a successful source gate' unless + credentials[1]['if'] == "always() && steps.scope.outcome == 'success'" && credentials[1]['timeout-minutes'] == 2 && + steps.index(credentials[1]) == steps.index(probe) + 1 cleanup = steps.last -raise 'bounded cleanup must always follow credentials, including probe failure' unless cleanup['run'] == 'node .github/scripts/aws-origin-tls.cjs finish' && - cleanup['if'] == "always() && steps.credentials.outcome == 'success'" && cleanup['timeout-minutes'] == 8 +raise 'bounded finalizer must require successful cleanup credentials with no earlier-session fallback' unless + cleanup['run'] == 'node .github/scripts/aws-origin-tls.cjs finish' && + cleanup['if'] == "always() && steps.cleanup_credentials.outcome == 'success'" && cleanup['timeout-minutes'] == 8 && + steps.index(cleanup) == steps.index(credentials[1]) + 1 raise 'no secret inheritance, bypasses or public artifacts' if workflow.to_s.match?(/secrets|upload-artifact|AWS_TERRAFORM_ENABLED|AWS_TLS_RECOVERY|terraform apply|deploy-aws/) jobs.each_value do |j| raise 'failures must stop execution' if j['continue-on-error'] @@ -35,5 +48,5 @@ jobs.each_value do |j| raise 'checkout credentials must not persist' if s['uses'] == 'actions/checkout@v4' && s.dig('with', 'persist-credentials') != false end end -puts 'Origin TLS workflow source gate, ordinary environment, queue and unconditional bounded cleanup checks passed' +puts 'Origin TLS workflow source gate, phase credentials, failure gating, ordinary environment and bounded cleanup checks passed' RUBY diff --git a/.github/scripts/aws-origin-tls.cjs b/.github/scripts/aws-origin-tls.cjs index 94ab5a6b2..cb0a6b0ae 100644 --- a/.github/scripts/aws-origin-tls.cjs +++ b/.github/scripts/aws-origin-tls.cjs @@ -33,18 +33,24 @@ function configuration(env) { family: `warmbly-production-origin-tls-${env.GITHUB_RUN_ID}`, startedBy: `origin-tls-${env.GITHUB_RUN_ID}` }; } -function sessionPolicy(config) { - return { Version: "2012-10-17", Statement: [ +function sessionPolicy(config, phase) { + requireThat(phase === "probe" || phase === "cleanup", "Explicit diagnostic credential phase required"); + const statements = phase === "probe" ? [ { Effect: "Allow", Action: ["elasticloadbalancing:DescribeLoadBalancers", "elasticloadbalancing:DescribeListeners", - "ec2:DescribeNetworkInterfaces", "ec2:DescribeSubnets", "ecs:RegisterTaskDefinition", "ecs:ListTaskDefinitions", "ecs:ListTasks"], Resource: "*" }, + "ec2:DescribeNetworkInterfaces", "ec2:DescribeSubnets", "ecs:RegisterTaskDefinition"], Resource: "*" }, { Effect: "Allow", Action: "ecs:DescribeServices", Resource: SERVICE }, { Effect: "Allow", Action: "ecs:RunTask", Resource: `${ECS}:task-definition/${config.family}:*`, Condition: { ArnEquals: { "ecs:cluster": CLUSTER } } }, - { Effect: "Allow", Action: "ecs:DeregisterTaskDefinition", Resource: "*" }, - { Effect: "Allow", Action: ["ecs:DescribeTasks", "ecs:StopTask"], Resource: `${ECS}:task/warmbly-production/*` }, + { Effect: "Allow", Action: "ecs:DescribeTasks", Resource: `${ECS}:task/warmbly-production/*` }, { Effect: "Allow", Action: "iam:PassRole", Resource: EXECUTION, Condition: { StringEquals: { "iam:PassedToService": "ecs-tasks.amazonaws.com" } } }, + ] : [ + { Effect: "Allow", Action: ["ecs:ListTaskDefinitions", "ecs:ListTasks"], Resource: "*" }, + { Effect: "Allow", Action: "ecs:DeregisterTaskDefinition", Resource: "*" }, + { Effect: "Allow", Action: ["ecs:DescribeTasks", "ecs:StopTask"], Resource: `${ECS}:task/warmbly-production/*` }, { Effect: "Allow", Action: "logs:GetLogEvents", Resource: `arn:aws:logs:${REGION}:${ACCOUNT}:log-group:${LOG}:log-stream:origin-tls-${config.run}/*` }, + ]; + return { Version: "2012-10-17", Statement: [...statements, { Effect: "Allow", Action: "s3:PutObject", Resource: `arn:aws:s3:::${BUCKET}/${PREFIX}/${config.run}/1/*`, Condition: { StringEquals: { "s3:x-amz-server-side-encryption": "AES256", "s3:ResourceAccount": ACCOUNT }, Bool: { "aws:SecureTransport": "true" }, Null: { "s3:if-none-match": "false" } } }, @@ -290,8 +296,9 @@ if (require.main === module) { const config = configuration(process.env); const source = spawnSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }); requireThat(source.status === 0 && source.stdout.trim() === config.source, "Checkout must match main workflow source"); - if (process.argv[2] === "session-policy") appendFileSync(process.env.GITHUB_OUTPUT, `policy=${canonical(sessionPolicy(config))}\n`); - else if (process.argv[2] === "probe") await execute(awsCLI, config); + if (process.argv[2] === "session-policy") { + for (const phase of ["probe", "cleanup"]) appendFileSync(process.env.GITHUB_OUTPUT, `${phase}_policy=${canonical(sessionPolicy(config, phase))}\n`); + } else if (process.argv[2] === "probe") await execute(awsCLI, config); else if (process.argv[2] === "finish") await finish(awsCLI, config); else throw new Error("Unknown diagnostic command"); }).catch((error) => { console.error(`Origin TLS diagnostic stopped: ${error.message}`); process.exitCode = 1; }); diff --git a/.github/scripts/aws-origin-tls.test.cjs b/.github/scripts/aws-origin-tls.test.cjs index fcdf59c84..2dd487017 100644 --- a/.github/scripts/aws-origin-tls.test.cjs +++ b/.github/scripts/aws-origin-tls.test.cjs @@ -1,5 +1,6 @@ const { test } = require("node:test"); const assert = require("node:assert/strict"); +const { spawnSync } = require("node:child_process"); const { mkdtempSync, readFileSync, rmSync, writeFileSync } = require("node:fs"); const { tmpdir } = require("node:os"); const { join } = require("node:path"); @@ -92,38 +93,96 @@ test("only fresh main dispatches under normal infrastructure configuration are a } }); -test("session policy admits only diagnostics, bound roles, cluster and encrypted conditional private archives", () => { +test("each credential phase grants exactly its required operations and resource scopes", () => { const config = configuration(env()); - const policy = sessionPolicy(config); - assert.ok(JSON.stringify(policy).length < 2048); - const all = policy.Statement.flatMap((s) => s.Action); - for (const forbidden of ["ecs:UpdateService", "ecs:ExecuteCommand", "secretsmanager:GetSecretValue", "ssm:GetParameters", - "iam:PutRolePolicy", "elasticloadbalancing:ModifyListener", "ec2:AuthorizeSecurityGroupIngress"]) assert.ok(!all.includes(forbidden)); - assert.deepEqual(policy.Statement.find((s) => s.Action === "iam:PassRole").Resource, EXECUTION); - assert.equal(policy.Statement.find((s) => s.Action === "ecs:RunTask").Condition.ArnEquals["ecs:cluster"], CLUSTER); - const put = policy.Statement.find((s) => s.Action === "s3:PutObject"); - assert.ok(put.Resource.endsWith(`/origin-tls/${config.run}/1/*`)); - assert.equal(put.Condition.StringEquals["s3:x-amz-server-side-encryption"], "AES256"); - assert.equal(put.Condition.Null["s3:if-none-match"], "false"); + const allow = (Action, Resource, Condition) => ({ Effect: "Allow", Action, Resource, ...(Condition ? { Condition } : {}) }); + const archive = allow("s3:PutObject", `arn:aws:s3:::${BUCKET}/warmbly/production/diagnostics/origin-tls/${config.run}/1/*`, { + StringEquals: { "s3:x-amz-server-side-encryption": "AES256", "s3:ResourceAccount": ACCOUNT }, + Bool: { "aws:SecureTransport": "true" }, Null: { "s3:if-none-match": "false" }, + }); + const expected = { + probe: [ + allow(["elasticloadbalancing:DescribeLoadBalancers", "elasticloadbalancing:DescribeListeners", + "ec2:DescribeNetworkInterfaces", "ec2:DescribeSubnets", "ecs:RegisterTaskDefinition"], "*"), + allow("ecs:DescribeServices", SERVICE), + allow("ecs:RunTask", `${ECS}:task-definition/${config.family}:*`, { ArnEquals: { "ecs:cluster": CLUSTER } }), + allow("ecs:DescribeTasks", `${ECS}:task/warmbly-production/*`), + allow("iam:PassRole", EXECUTION, { StringEquals: { "iam:PassedToService": "ecs-tasks.amazonaws.com" } }), + archive, + ], + cleanup: [ + allow(["ecs:ListTaskDefinitions", "ecs:ListTasks"], "*"), + allow("ecs:DeregisterTaskDefinition", "*"), + allow(["ecs:DescribeTasks", "ecs:StopTask"], `${ECS}:task/warmbly-production/*`), + allow("logs:GetLogEvents", `arn:aws:logs:us-east-1:${ACCOUNT}:log-group:${LOG}:log-stream:origin-tls-${config.run}/*`), + archive, + ], + }; + for (const phase of ["probe", "cleanup"]) { + const policy = sessionPolicy(config, phase); + assert.deepEqual(policy, { Version: "2012-10-17", Statement: expected[phase] }); + assert.ok(JSON.stringify(policy).length < 2048); + } + for (const phase of [undefined, "", "finish", "all"]) assert.throws(() => sessionPolicy(config, phase), /credential phase/); }); -test("diagnostic actions use provider-supported resource scopes", () => { - const config = configuration(env()); - const statements = sessionPolicy(config).Statement; - const granting = (action) => statements.filter((s) => [s.Action].flat().includes(action)); - assert.deepEqual(granting("ecs:DeregisterTaskDefinition"), [ - { Effect: "Allow", Action: "ecs:DeregisterTaskDefinition", Resource: "*" }, - ]); - assert.deepEqual(granting("ecs:RunTask"), [ - { Effect: "Allow", Action: "ecs:RunTask", Resource: `${ECS}:task-definition/${config.family}:*`, - Condition: { ArnEquals: { "ecs:cluster": CLUSTER } } }, - ]); - assert.deepEqual(granting("logs:GetLogEvents"), [ - { Effect: "Allow", Action: "logs:GetLogEvents", - Resource: `arn:aws:logs:us-east-1:${ACCOUNT}:log-group:${LOG}:log-stream:origin-tls-${config.run}/*` }, - ]); +test("the source gate emits distinct phase policies and refuses an unverified checkout", (t) => { + const f = fixture(t); + const revision = spawnSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }); + assert.equal(revision.status, 0); + const output = join(f.config.work, "output"); + const childEnv = { ...process.env, ...env(), WORK: f.config.work, GITHUB_OUTPUT: output, GITHUB_SHA: revision.stdout.trim() }; + const run = () => spawnSync(process.execPath, [join(__dirname, "aws-origin-tls.cjs"), "session-policy"], { env: childEnv, encoding: "utf8" }); + assert.equal(run().status, 0); + const policies = Object.fromEntries(readFileSync(output, "utf8").trim().split("\n").map((line) => { + const equals = line.indexOf("="); + return [line.slice(0, equals), JSON.parse(line.slice(equals + 1))]; + })); + assert.deepEqual(policies, { probe_policy: sessionPolicy(f.config, "probe"), cleanup_policy: sessionPolicy(f.config, "cleanup") }); + writeFileSync(output, ""); + childEnv.GITHUB_SHA = "0".repeat(40); + assert.equal(run().status, 1); + assert.equal(readFileSync(output, "utf8"), ""); }); +function phaseAWS(f, phase) { + const actions = sessionPolicy(f.config, phase).Statement.flatMap((s) => s.Action); + return (service, action, args) => { + const prefix = { elbv2: "elasticloadbalancing", s3api: "s3" }[service] || service; + const name = action.split("-").map((word) => word[0].toUpperCase() + word.slice(1)).join(""); + if (service !== "sts" || action !== "get-caller-identity") assert.ok(actions.includes(`${prefix}:${name}`), `${phase} must permit ${service} ${action}`); + return f.aws(service, action, args); + }; +} + +for (const failure of [null, "register-task-definition", "run-task", "describe-tasks"]) { + test(`phase sessions support ${failure || "successful probe"} cleanup with one registration and launch at most`, async (t) => { + let lost = false; + const f = fixture(t, (action) => { + if (action === failure && !lost) { lost = true; throw new Error("Fixture lost response"); } + }); + const oldSummary = process.env.GITHUB_STEP_SUMMARY; + process.env.GITHUB_STEP_SUMMARY = join(f.config.work, "summary"); + t.after(() => { if (oldSummary === undefined) delete process.env.GITHUB_STEP_SUMMARY; else process.env.GITHUB_STEP_SUMMARY = oldSummary; }); + const probeRun = execute(phaseAWS(f, "probe"), f.config, async () => {}); + if (failure) await assert.rejects(probeRun, /lost response/); + else await probeRun; + const boundary = f.calls.length; + if (failure && failure !== "register-task-definition") f.state.lastStatus = "RUNNING"; + const cleanup = finish(phaseAWS(f, "cleanup"), f.config, async () => {}); + if (failure) await assert.rejects(cleanup, /incomplete/); + else await cleanup; + assert.ok(!f.calls.slice(0, boundary).some((c) => c.action === "deregister-task-definition")); + assert.ok(!f.calls.slice(boundary).some((c) => ["register-task-definition", "run-task"].includes(c.action))); + assert.equal(f.calls.filter((c) => c.action === "register-task-definition").length, 1); + assert.equal(f.calls.filter((c) => c.action === "run-task").length, failure === "register-task-definition" ? 0 : 1); + assert.equal(f.evidence().cleanup.deregistered, true); + assert.equal(f.evidence().success, !failure); + assert.equal(f.calls.at(-1).action, "put-object"); + assert.deepEqual(JSON.parse(readFileSync(join(f.config.work, "result.json"))), f.evidence()); + }); +} + test("fresh definition contains only the official digest, hard-bounded TLS client and existing execution/log facilities", () => { const task = definition(configuration(env()), ["10.42.0.85", "10.42.1.12"]); assert.deepEqual(Object.keys(task).sort(), ["family", "networkMode", "requiresCompatibilities", "cpu", "memory", "executionRoleArn", "runtimePlatform", "containerDefinitions"].sort()); diff --git a/.github/workflows/probe-origin-tls-aws.yml b/.github/workflows/probe-origin-tls-aws.yml index 269934a0d..c48f5d026 100644 --- a/.github/workflows/probe-origin-tls-aws.yml +++ b/.github/workflows/probe-origin-tls-aws.yml @@ -58,14 +58,26 @@ jobs: with: role-to-assume: ${{ vars.AWS_TERRAFORM_APPLY_ROLE_ARN }} aws-region: us-east-1 - inline-session-policy: ${{ steps.scope.outputs.policy }} + unset-current-credentials: true + inline-session-policy: ${{ steps.scope.outputs.probe_policy }} - name: Run disposable secretless TLS diagnostic + if: success() && steps.credentials.outcome == 'success' timeout-minutes: 10 env: WORK: ${{ runner.temp }}/origin-tls run: node .github/scripts/aws-origin-tls.cjs probe + - name: Acquire cleanup-only diagnostic credentials + if: always() && steps.scope.outcome == 'success' + timeout-minutes: 2 + uses: aws-actions/configure-aws-credentials@v4 + id: cleanup_credentials + with: + role-to-assume: ${{ vars.AWS_TERRAFORM_APPLY_ROLE_ARN }} + aws-region: us-east-1 + unset-current-credentials: true + inline-session-policy: ${{ steps.scope.outputs.cleanup_policy }} - name: Stop, deregister and privately archive diagnostic evidence - if: always() && steps.credentials.outcome == 'success' + if: always() && steps.cleanup_credentials.outcome == 'success' timeout-minutes: 8 env: WORK: ${{ runner.temp }}/origin-tls diff --git a/deploy/terraform/aws/runtime/README.md b/deploy/terraform/aws/runtime/README.md index f47a8b7ba..accd2d84d 100644 --- a/deploy/terraform/aws/runtime/README.md +++ b/deploy/terraform/aws/runtime/README.md @@ -247,7 +247,8 @@ Operator procedure: `.github/scripts/aws-origin-tls.cjs`, plus `logs:GetLogEvents` on `/ecs/warmbly-production/validation` and conditional `s3:PutObject` on `warmbly/production/diagnostics/origin-tls//1/*` in the private state - bucket. The workflow's session policy only restricts existing permissions. + bucket. The workflow's phase-specific sessions only restrict existing + permissions. Missing access requires the normal IAM process; the workflow never expands a role or switches environments. Read-only TLS metadata does not require access to secret values, SSM parameters or application task credentials. @@ -271,6 +272,18 @@ Operator procedure: certificate chains. Success requires the complete raw handshake evidence, exact task image digest and confirmed diagnostic cleanup. +Probe credentials permit discovery, one registration and launch, task readback +and private intent archival. Cleanup uses a separate OIDC session acquired +immediately before the finalizer, with only task/definition discovery, task +readback and stop, deregistration, exact-run log retrieval and private archival. +It cannot register definitions, launch tasks or pass roles. AWS requires +`ecs:DeregisterTaskDefinition` on `Resource: "*"`; the application validates +every deregistration candidate against the exact account, region and run family +with a numeric revision. The Logs ARN names only this run's streams. +Both credential steps clear existing credentials before acquisition. Cleanup +credential acquisition runs after probe failure, and the finalizer requires its +success. Credential failures fail the workflow without reusing an earlier session. + The transport targets are freshly described private interfaces of the exact ALB, never the public API DNS address. The SNI and verified hostname stay `api.warmbly.com`; the client trusts its system public CAs. Each interface gets