diff --git a/crates/countersign-gateway/src/shared_issuer.rs b/crates/countersign-gateway/src/shared_issuer.rs index 5cf39d9..837da02 100644 --- a/crates/countersign-gateway/src/shared_issuer.rs +++ b/crates/countersign-gateway/src/shared_issuer.rs @@ -18,7 +18,7 @@ use serde::Deserialize; use time::OffsetDateTime; use x509_parser::prelude::{FromDer, GeneralName, X509Certificate}; -use crate::sidecar::{sign_leaf, verified_worker_key}; +use crate::sidecar::{leaf_not_before, sign_leaf, verified_worker_key}; type Failure = Box; type Chain = Vec>; @@ -46,6 +46,7 @@ enum Source { Local { issuer: Box>, chain: Chain, + starts: OffsetDateTime, expires: OffsetDateTime, }, Remote { @@ -113,12 +114,14 @@ impl SharedIssuer { { return Err("invalid proxy CA credential".into()); } + let starts = parsed.validity.not_before.to_datetime(); let expires = parsed.validity.not_after.to_datetime(); let issuer = Box::new(Issuer::from_ca_cert_der(ca, key)?); roots.add(chain.last().ok_or("empty CA chain")?.clone())?; Source::Local { issuer, chain, + starts, expires, } } @@ -186,13 +189,20 @@ impl SharedIssuer { Source::Local { issuer, chain, + starts, expires, } => { let deadline = grant_not_after.min(*expires); if deadline.unix_timestamp() <= now.unix_timestamp() { return Err("issuer has expired".into()); } - let leaf = sign_leaf(origin, &key, now, deadline, issuer)?; + let leaf = sign_leaf( + origin, + &key, + leaf_not_before(now, *starts), + deadline, + issuer, + )?; std::iter::once(leaf).chain(chain.iter().cloned()).collect() } Source::Remote { @@ -529,11 +539,46 @@ mod tests { .issue(&site.scope, &site.origin, site.deadline, &site.csr, "g/1") .await .unwrap(); + // A client whose clock runs half a minute behind finds the leaf valid. + let (_, leaf) = x509_parser::parse_x509_certificate(&chain[0]).unwrap(); + let behind = (OffsetDateTime::now_utc() - time::Duration::seconds(30)).unix_timestamp(); + assert!(leaf.validity.not_before.timestamp() <= behind); site.tls(chain); fs::write(key, KeyPair::generate().unwrap().serialize_pem()).unwrap(); assert!(SharedIssuer::open(source, site.policy).await.is_err()); } + #[tokio::test] + async fn a_leaf_never_starts_before_the_ca_that_signs_it() { + // An Integrator's CA minted seconds ago: the leaf is backdated for a + // client whose clock runs behind, but never to before its own issuer. + let site = TestSite::new(); + let minted = OffsetDateTime::now_utc() - time::Duration::seconds(5); + let mut params = CertificateParams::default(); + params.is_ca = IsCa::Ca(BasicConstraints::Constrained(0)); + params.key_usages = vec![KeyUsagePurpose::KeyCertSign]; + params.not_before = minted; + let ca_key = KeyPair::generate().unwrap(); + let ca = params.self_signed(&ca_key).unwrap(); + let dir = tempdir().unwrap(); + let cert = dir.path().join("ca.pem"); + let key = dir.path().join("key.pem"); + fs::write(&cert, ca.pem()).unwrap(); + fs::write(&key, ca_key.serialize_pem()).unwrap(); + let issuer = SharedIssuer::open(SharedCaSource::Local { cert, key }, site.policy.clone()) + .await + .unwrap(); + let chain = issuer + .issue(&site.scope, &site.origin, site.deadline, &site.csr, "g/1") + .await + .unwrap(); + let (_, leaf) = x509_parser::parse_x509_certificate(&chain[0]).unwrap(); + assert_eq!( + leaf.validity.not_before.timestamp(), + minted.unix_timestamp() + ); + } + #[tokio::test] async fn shared_profile_selects_acm_pca_service_endpoint() { use aws_runtime::env_config::file::{EnvConfigFileKind, EnvConfigFiles}; diff --git a/crates/countersign-gateway/src/sidecar.rs b/crates/countersign-gateway/src/sidecar.rs index 5b7f3e8..473b9ad 100644 --- a/crates/countersign-gateway/src/sidecar.rs +++ b/crates/countersign-gateway/src/sidecar.rs @@ -9,7 +9,7 @@ use rcgen::{ use ring::rand::{SecureRandom, SystemRandom}; use rustls_pki_types::CertificateDer; use thiserror::Error; -use time::OffsetDateTime; +use time::{Duration, OffsetDateTime}; use x509_parser::certification_request::X509CertificationRequest; use x509_parser::cri_attributes::ParsedCriAttribute; use x509_parser::prelude::{FromDer, GeneralName, ParsedExtension, X509Version}; @@ -30,6 +30,16 @@ pub enum IssueError { RandomUnavailable, } +/// How long before it is issued a certificate's validity starts. A client +/// validates with its own clock, and one that runs behind the Gateway's would +/// otherwise find a fresh certificate not yet valid. +/// +/// Why (2026-11-20): on ECS Fargate 1.4.0, kernel 6.1.190, curl in the +/// Gateway's own task rejected a leaf issued within 10 ms after a second began. +/// OpenSSL reads `time()`, which that kernel advances once per 10 ms tick, so +/// it still showed the second before the leaf's `notBefore`. +const CLIENT_CLOCK_ALLOWANCE: Duration = Duration::minutes(1); + /// One freshly generated CA and an immutable snapshot of its destination policy. /// /// Only the public CA certificate and issued public leaves leave this object. @@ -37,6 +47,7 @@ pub struct SidecarIssuer { authority: CertifiedIssuer<'static, KeyPair>, policy: DestinationPolicy, not_before: OffsetDateTime, + ca_not_before: OffsetDateTime, not_after: OffsetDateTime, } @@ -56,8 +67,9 @@ impl SidecarIssuer { return Err(IssueError::EmptyPolicy); } + let ca_not_before = now - CLIENT_CLOCK_ALLOWANCE; let mut params = CertificateParams::default(); - params.not_before = now; + params.not_before = ca_not_before; params.not_after = not_after; params.distinguished_name = DistinguishedName::new(); params @@ -80,6 +92,7 @@ impl SidecarIssuer { authority, policy, not_before: now, + ca_not_before, not_after, }) } @@ -112,19 +125,34 @@ impl SidecarIssuer { let not_after = certificate_deadline(now, grant_not_after.min(self.not_after))?; let public_key = verified_worker_key(origin.host(), csr_der)?; - sign_leaf(origin, &public_key, now, not_after, &self.authority) + sign_leaf( + origin, + &public_key, + leaf_not_before(now, self.ca_not_before), + not_after, + &self.authority, + ) } } +/// When a leaf issued at `now` starts being valid: earlier by the allowance, +/// but never before `ca_not_before`, its issuer's own start. +pub(crate) fn leaf_not_before( + now: OffsetDateTime, + ca_not_before: OffsetDateTime, +) -> OffsetDateTime { + (now - CLIENT_CLOCK_ALLOWANCE).max(ca_not_before) +} + pub(crate) fn sign_leaf( origin: &Origin, public_key: &SubjectPublicKeyInfo, - now: OffsetDateTime, + not_before: OffsetDateTime, not_after: OffsetDateTime, authority: &rcgen::Issuer<'_, KeyPair>, ) -> Result, IssueError> { let mut params = CertificateParams::new(vec![origin.host().to_owned()])?; - params.not_before = now; + params.not_before = not_before; params.not_after = not_after; params.distinguished_name = DistinguishedName::new(); params.is_ca = IsCa::ExplicitNoCa; diff --git a/crates/countersign-gateway/tests/runtime.py b/crates/countersign-gateway/tests/runtime.py index 06c2cd1..fc7f37d 100644 --- a/crates/countersign-gateway/tests/runtime.py +++ b/crates/countersign-gateway/tests/runtime.py @@ -325,7 +325,8 @@ def launched_directly(): # Only Alice's grant worker runs. A grant worker the allow-list # kills dies of SIGSYS, so deliver that signal to it: the # supervisor cannot tell the two apart. - assert request('alice').returncode == 0 + first = request('alice') + assert first.returncode == 0, first.stderr if sandbox == 'filter': launched_directly() leaves = [pid for pid in descendants(child.pid) if not descendants(pid)] diff --git a/crates/countersign-gateway/tests/sidecar.rs b/crates/countersign-gateway/tests/sidecar.rs index 7f8bd60..f48a35f 100644 --- a/crates/countersign-gateway/tests/sidecar.rs +++ b/crates/countersign-gateway/tests/sidecar.rs @@ -117,6 +117,31 @@ fn leaf_has_only_the_authorized_host_public_key_usage_and_lifetime() { )); } +#[test] +fn a_client_whose_clock_runs_behind_accepts_the_ca_and_the_leaf() { + use x509_parser::time::ASN1Time; + + // A client validates with its own clock. Half a minute behind the Gateway + // is an ordinary host, and such a client has to find both already valid. + let behind = ASN1Time::from_timestamp( + (OffsetDateTime::now_utc() - Duration::seconds(30)).unix_timestamp(), + ) + .unwrap(); + let origin = Origin::parse("api.example.test:443").unwrap(); + let policy = policy(); + let scope = policy.scope_for(&origin).unwrap(); + let issuer = SidecarIssuer::new(policy, after(3)).unwrap(); + let (_, ca) = parse_x509_certificate(issuer.public_cert_der()).unwrap(); + let key = KeyPair::generate().unwrap(); + let der = issuer + .issue(&scope, &origin, after(1), &csr(&key, &[origin.host()])) + .unwrap(); + let (_, leaf) = parse_x509_certificate(&der).unwrap(); + + assert!(ca.validity.is_valid_at(behind), "{:?}", ca.validity); + assert!(leaf.validity.is_valid_at(behind), "{:?}", leaf.validity); +} + #[test] fn wildcard_grant_cannot_mint_leaves_for_other_ports_apex_or_overrides() { let api = Origin::parse("api.example.test:443").unwrap(); diff --git a/docs/develop.md b/docs/develop.md index c115d18..439455a 100644 --- a/docs/develop.md +++ b/docs/develop.md @@ -27,6 +27,8 @@ pnpm test To run the Gateway's tests under the filter sandbox on a host that refuses namespaces, run `scripts/filter-sandbox-container.sh`, which needs Docker. It runs the containment test and the runtime test in a container under Docker's default security profile, as the end-to-end workflow's `gateway-filter-sandbox` job does. Its two last steps use a profile with one quirk each: where the host refuses the allow-list, the filter sandbox has to fail at startup by name, and where the kernel has no `close_range`, a grant worker still has to start with no descriptor of the supervisor's. +That container has the CI runner's kernel. To run the same two tests on real ECS Fargate 1.4.0, x86-64 and arm64, use the qualification harness, `qualification/fargate/run.sh`. It needs our AWS account, and its [README](https://github.com/zeroae/countersign/blob/main/qualification/fargate/README.md) says how to deploy, run and tear it down. Run it for every release that touches the grant worker. + For the graphics and network path, run the [demo walkthrough](try.md) with `--verify` to automate the installed Companion and browser. The hosted end-to-end job also runs a separate harness that selects identities from candidate lists of different sizes, verifies the peer fingerprint and checks revocation. The native qualification jobs generate identities in disposable provider stores. On macOS and Windows use a disposable runner account for that harness because it changes user-level keychain or certificate-store state. [End-to-end workflow](https://github.com/zeroae/countersign/blob/fbb5aa7c951f8b70a238f3351140aa44a6184adf/.github/workflows/e2e.yml) ยท [Native qualification workflow](https://github.com/zeroae/countersign/blob/fbb5aa7c951f8b70a238f3351140aa44a6184adf/.github/workflows/native.yml) ## A grant worker died of SIGSYS diff --git a/qualification/fargate/Dockerfile b/qualification/fargate/Dockerfile new file mode 100644 index 0000000..8bd190a --- /dev/null +++ b/qualification/fargate/Dockerfile @@ -0,0 +1,71 @@ +# The Fargate qualification image: the Gateway, the worker sandbox containment +# test and the runtime test, with what the runtime test needs and no bubblewrap. +# It is a qualification tool. The Gateway image is distribution/Dockerfile. +# +# Build it with run.sh, from the repository root as context. Every RUN is in a +# stage that runs on the build host's architecture, and the last stage only +# copies files, so building linux/amd64 on an arm64 host (or the reverse) needs +# no emulation. +FROM --platform=$BUILDPLATFORM rust:1.99-bookworm AS build +ARG BUILDARCH +ARG TARGETARCH +WORKDIR /build +COPY Cargo.toml Cargo.lock LICENSE ./ +COPY crates ./crates +# The containment test holds the Gateway executable's path from its build, so +# the Gateway is staged at the path cargo built it at. +RUN set -eu; \ + case "$TARGETARCH" in \ + amd64) target=x86_64-unknown-linux-gnu; gnu=x86_64-linux-gnu ;; \ + arm64) target=aarch64-unknown-linux-gnu; gnu=aarch64-linux-gnu ;; \ + *) echo "unsupported architecture $TARGETARCH" >&2; exit 1 ;; \ + esac; \ + if [ "$TARGETARCH" != "$BUILDARCH" ]; then \ + apt-get update; \ + apt-get install -y --no-install-recommends "gcc-$(echo "$gnu" | tr _ -)" "libc6-dev-$TARGETARCH-cross"; \ + rustup target add "$target"; \ + export "CARGO_TARGET_$(echo "$target" | tr a-z- A-Z_)_LINKER=$gnu-gcc"; \ + fi; \ + built() { \ + cargo "$@" --locked --release --target "$target" -p countersign-gateway --message-format=json \ + >/tmp/built.json; \ + sed -n 's/.*"executable":"\([^"]*\)".*/\1/p' /tmp/built.json; \ + }; \ + gateway=$(built build --bin countersign-gateway); \ + containment=$(built test --no-run --test worker_sandbox | grep /deps/worker_sandbox-); \ + test -x "$gateway"; test -x "$containment"; \ + mkdir -p "/stage$(dirname "$gateway")" /stage/usr/local/bin /stage/qualification; \ + cp "$gateway" "/stage$gateway"; \ + ln -s "$gateway" /stage/usr/local/bin/countersign-gateway; \ + cp "$containment" /stage/usr/local/bin/worker-sandbox-containment; \ + cp crates/countersign-gateway/tests/runtime.py /stage/qualification/ + +# What the runtime test needs beyond the base image's Python and curl, fetched +# for the target architecture and unpacked, never run. +FROM --platform=$BUILDPLATFORM python:3.12-slim-bookworm AS needs +ARG TARGETARCH +RUN set -eu; \ + case "$TARGETARCH" in \ + amd64) machine=x86_64 ;; \ + arm64) machine=aarch64 ;; \ + *) echo "unsupported architecture $TARGETARCH" >&2; exit 1 ;; \ + esac; \ + pip install --no-cache-dir --target /stage/qualification/python --only-binary=:all: \ + --python-version 3.12 --implementation cp \ + --platform "manylinux_2_28_$machine" --platform "manylinux_2_17_$machine" --platform "manylinux2014_$machine" \ + cryptography==50.0.2; \ + dpkg --add-architecture "$TARGETARCH"; \ + apt-get update; \ + cd /tmp; \ + apt-get download "libnss-wrapper:$TARGETARCH"; \ + dpkg-deb -x libnss-wrapper_*.deb /stage + +FROM python:3.12-bookworm +ARG REVISION=unknown +COPY --from=build /stage/ / +COPY --from=needs /stage/ / +COPY qualification/fargate/qualify.sh /qualification/ +ENV PYTHONPATH=/qualification/python PYTHONUNBUFFERED=1 COUNTERSIGN_REVISION=$REVISION +# The Gateway image's user: not root, and with no capability. +USER 65531:65531 +ENTRYPOINT ["/qualification/qualify.sh"] diff --git a/qualification/fargate/README.md b/qualification/fargate/README.md new file mode 100644 index 0000000..d48a5f7 --- /dev/null +++ b/qualification/fargate/README.md @@ -0,0 +1,46 @@ +# Fargate qualification harness + +This harness runs the Gateway's containment test and its runtime test on real ECS Fargate 1.4.0, on x86-64 and arm64, under the [filter sandbox](../../GLOSSARY.md). CI already runs both tests in a container that refuses namespaces as Fargate does (`scripts/filter-sandbox-container.sh`), but on the CI runner's kernel. The filter sandbox's allow-list depends on the kernel, and Fargate runs an older one. So the filter sandbox is qualified here first, and again for every release that touches the grant worker. Listing Fargate on the [tested platforms](../../docs/reference/support.md) page takes more than this run: see spec #21. + +It is a qualification tool. The Gateway never depends on it, and nothing here ships. + +## What a run proves + +Each task runs `qualify.sh` as the Gateway image's user, with Fargate's defaults: no added capability, Fargate's own seccomp profile, no task role. It prints the host's kernel and confinement facts, checks that the host refuses user namespaces and that the image has no bubblewrap, then runs: + +1. the worker sandbox containment test (`crates/countersign-gateway/tests/worker_sandbox.rs`); +2. the runtime test under `"filter"` (`crates/countersign-gateway/tests/runtime.py`), with its test grant authority and mTLS site inside the task. + +The Gateway's startup line in the log names the layers applied, the Landlock ABI among them. The task exits non-zero at the first step that fails, and `run.sh` exits non-zero when any task did. + +The image holds a release build of the commit you build from. Its first log lines name that revision, with `-dirty` when the checkout had uncommitted changes. `run` fails a task whose image names another revision than the checkout's, so build before you run. + +## What you need + +- Docker with buildx. Neither architecture needs emulation: the images are cross-compiled, and the last stage only copies files. +- The AWS CLI, with credentials that may create the stack. Set `AWS_PROFILE`, and `AWS_REGION` unless the profile names a region. The stack creates one VPC, so pick a region with room for one. +- The execution role is named `PowerUserPB--exec` and carries the `PowerUserAccess` permissions boundary, as the `zeroae-code` power-user SSO roles require. IAM role names are per account, not per region, so one stack name deploys in one region at a time. + +## Deploy, run and tear down + +```bash +export AWS_PROFILE=zeroae-code/ZAEPowerUserAccess AWS_REGION=us-east-2 +qualification/fargate/run.sh # deploy the stack, push both images, run both tasks +qualification/fargate/run.sh teardown # delete the stack, images and logs included +``` + +One step at a time, or one architecture: + +```bash +qualification/fargate/run.sh deploy +qualification/fargate/run.sh build arm64 +qualification/fargate/run.sh run arm64 +``` + +`run` prints each task's platform, architecture, image digest and log, then its exit code. Record that output on the release's qualification ticket. The stack costs nothing while no task runs, apart from the images' storage, so tear it down or keep it between releases as you prefer. `COUNTERSIGN_QUALIFICATION_STACK` names the stack when you need a second one, or one in another region. + +To try the image without AWS, on this host's architecture and under Docker's default profile: + +```bash +qualification/fargate/run.sh local +``` diff --git a/qualification/fargate/qualify.sh b/qualification/fargate/qualify.sh new file mode 100755 index 0000000..08fa08e --- /dev/null +++ b/qualification/fargate/qualify.sh @@ -0,0 +1,41 @@ +#!/bin/sh +# Runs inside the qualification image, as the task's only command: says what +# host this is, then runs the Gateway's containment test and its runtime test under the +# filter sandbox. Exits non-zero at the first step that fails. +set -eu + +fact() { printf '%s: %s\n' "$1" "$2"; } +status() { sed -n "s/^$1:[[:space:]]*//p" /proc/self/status; } + +echo '== Host' +fact 'Countersign revision' "$COUNTERSIGN_REVISION" +fact 'Kernel' "$(uname -r) $(uname -m)" +fact 'uid / gid' "$(id -u) / $(id -g)" +fact 'CapEff' "$(status CapEff)" +fact 'NoNewPrivs' "$(status NoNewPrivs)" +fact 'Seccomp' "$(status Seccomp)" +fact 'Yama ptrace_scope' "$(cat /proc/sys/kernel/yama/ptrace_scope 2>/dev/null || echo unreadable)" +fact 'LSMs' "$(cat /sys/kernel/security/lsm 2>/dev/null || echo unreadable)" + +echo '== The host refuses namespaces' +# Without the program, its failure would say nothing about the host. +command -v unshare >/dev/null || { echo 'The qualification image has no unshare.' >&2; exit 1; } +if unshare --user true 2>/dev/null; then + echo 'This host can create user namespaces, so it proves nothing about the filter sandbox.' >&2 + exit 1 +fi +if command -v bwrap >/dev/null; then + echo 'The qualification image has bubblewrap.' >&2 + exit 1 +fi +echo 'refused, and the image has no bubblewrap' + +echo '== Worker sandbox containment test' +timeout 300 worker-sandbox-containment + +# The Gateway's startup log, which this prints, names the worker sandbox and +# each layer applied, the Landlock ABI among them. +echo '== Runtime test under the filter sandbox' +timeout 300 python3 /qualification/runtime.py "$(command -v countersign-gateway)" --worker-sandbox filter + +echo '== Qualified: the containment test and the runtime test passed under the filter sandbox' diff --git a/qualification/fargate/run.sh b/qualification/fargate/run.sh new file mode 100755 index 0000000..7b34af7 --- /dev/null +++ b/qualification/fargate/run.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash +# Qualify the filter sandbox on real ECS Fargate 1.4.0: run the Gateway's +# containment test and its runtime test under "filter" in a task, on x86-64 +# and arm64. README.md beside this script says what it needs and proves. +# +# Usage: qualification/fargate/run.sh [command] [architecture...] +# all deploy, build and run (the default) +# deploy create or update the stack +# build build each architecture's image and push it +# run run each architecture's task, print its log, fail if it failed +# local build this host's image and run it under Docker's default profile +# teardown delete the stack, images and logs included +# An architecture is x86_64 or arm64; with none named, both. +# Needs Docker with buildx, and for all but `local` the AWS CLI with +# credentials (AWS_PROFILE) and a region (AWS_REGION, or the profile's). +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +cd "$here/../.." + +stack=${COUNTERSIGN_QUALIFICATION_STACK:-countersign-fargate-qualification} +command=${1:-all} +shift || true +architectures=("$@") +[ ${#architectures[@]} -gt 0 ] || architectures=(x86_64 arm64) + +# Docker's name for an architecture. +platform() { + case "$1" in + x86_64) echo linux/amd64 ;; + arm64) echo linux/arm64 ;; + *) echo "unknown architecture $1: x86_64 or arm64" >&2; return 1 ;; + esac +} +for architecture in "${architectures[@]}"; do platform "$architecture" >/dev/null; done + +output() { + aws cloudformation describe-stacks --stack-name "$stack" \ + --query "Stacks[0].Outputs[?OutputKey=='$1'].OutputValue" --output text +} + +image() { # architecture, tag, then how to deliver it: --push or --load + docker buildx build --platform "$(platform "$1")" --provenance=false \ + --file qualification/fargate/Dockerfile \ + --build-arg "REVISION=$(git describe --always --dirty)" --tag "$2" "$3" . +} + +teardown() { + aws cloudformation delete-stack --stack-name "$stack" + aws cloudformation wait stack-delete-complete --stack-name "$stack" +} + +deploy() { + # A stack whose first deploy failed holds nothing and cannot be updated. + if [ "$(aws cloudformation describe-stacks --stack-name "$stack" \ + --query 'Stacks[0].StackStatus' --output text 2>/dev/null)" = ROLLBACK_COMPLETE ]; then + teardown + fi + aws cloudformation deploy --stack-name "$stack" --template-file "$here/stack.yaml" \ + --capabilities CAPABILITY_NAMED_IAM --no-fail-on-empty-changeset +} + +build() { + local repository + repository=$(output RepositoryUri) + aws ecr get-login-password | docker login --username AWS --password-stdin "${repository%%/*}" + for architecture in "${architectures[@]}"; do + image "$architecture" "$repository:$architecture" --push + done +} + +# Start every task first, then wait for each: the architectures run at once. +# A task that passed has to name this checkout's revision, so that a run +# without a build cannot pass on an image built from another commit. +run() { + local cluster network group revision failed=0 index + cluster=$(output Cluster) + group=$(output LogGroup) + network="awsvpcConfiguration={subnets=[$(output Subnet)],securityGroups=[$(output SecurityGroup)],assignPublicIp=ENABLED}" + revision=$(git describe --always --dirty) + local tasks=() + for architecture in "${architectures[@]}"; do + local started task reason + started=$(aws ecs run-task --cluster "$cluster" --launch-type FARGATE \ + --platform-version 1.4.0 --task-definition "$stack-$architecture" \ + --network-configuration "$network" \ + --query '[tasks[0].taskArn, failures[0].reason]' --output text) + read -r task reason <<<"$started" + if [ "$task" = None ]; then + echo "Fargate did not start the $architecture task: $reason" >&2 + return 1 + fi + tasks+=("$task") + echo "started $architecture: $task" + done + for index in "${!architectures[@]}"; do + local architecture=${architectures[$index]} task=${tasks[$index]} code log + # One wait gives up after ten minutes, which a task whose two tests both + # run to their timeouts outlasts. + aws ecs wait tasks-stopped --cluster "$cluster" --tasks "$task" || + aws ecs wait tasks-stopped --cluster "$cluster" --tasks "$task" || true + echo "==== Fargate $architecture" + aws ecs describe-tasks --cluster "$cluster" --tasks "$task" --output text --query \ + 'tasks[0].[join(`: `, [`Platform`, platformFamily, platformVersion]), join(`: `, [`Architecture`, attributes[?name==`ecs.cpu-architecture`].value | [0]]), join(`: `, [`Image digest`, containers[0].imageDigest || `none`]), join(`: `, [`Status`, lastStatus]), join(`: `, [`Stopped`, stoppedReason || `no reason given`])]' | + tr '\t' '\n' + # filter-log-events follows the pages of a long log, as get-log-events + # does not. + log=$(aws logs filter-log-events --log-group-name "$group" \ + --log-stream-names "$architecture/qualify/${task##*/}" \ + --query 'events[].message' --output text | tr '\t' '\n') || log='(the task left no log)' + printf '%s\n' "$log" + code=$(aws ecs describe-tasks --cluster "$cluster" --tasks "$task" \ + --query 'tasks[0].containers[0].exitCode' --output text) + echo "==== Fargate $architecture exit code: $code" + [ "$code" = 0 ] || failed=1 + if ! grep -qxF "Countersign revision: $revision" <<<"$log"; then + echo "==== Fargate $architecture did not run this checkout ($revision): build its image first" >&2 + failed=1 + fi + done + return $failed +} + +local_run() { + local architecture + architecture=$(uname -m) + [ "$architecture" = aarch64 ] && architecture=arm64 + image "$architecture" "countersign-fargate-qualification:$architecture" --load + # No --privileged, --cap-add or --security-opt: Docker's default profile, + # which refuses namespaces as Fargate does. + docker run --rm "countersign-fargate-qualification:$architecture" +} + +case "$command" in + all) deploy; build; run ;; + deploy) deploy ;; + build) build ;; + run) run ;; + local) local_run ;; + teardown) teardown ;; + *) sed -n '2,/^set -euo/{/^set -euo/d;s/^# \{0,1\}//;p}' "$0" >&2; exit 2 ;; +esac diff --git a/qualification/fargate/stack.yaml b/qualification/fargate/stack.yaml new file mode 100644 index 0000000..a055c97 --- /dev/null +++ b/qualification/fargate/stack.yaml @@ -0,0 +1,145 @@ +# The Fargate qualification harness: where run.sh runs the Gateway's containment +# test and its runtime test on real ECS Fargate, on x86-64 and arm64. It is a qualification tool, +# and the Gateway never depends on it. Deleting the stack removes every +# resource, images included. run.sh deploys and deletes it. +AWSTemplateFormatVersion: "2010-09-09" +Description: Countersign Fargate qualification harness (zeroae/countersign#24). Not product infrastructure. + +Resources: + Repository: + Type: AWS::ECR::Repository + Properties: + RepositoryName: !Ref AWS::StackName + EmptyOnDelete: true + + # A small public network with no NAT: a task needs egress only, to pull its + # image and write its log. + Vpc: + Type: AWS::EC2::VPC + Properties: + CidrBlock: 10.42.0.0/24 + Tags: [{ Key: Name, Value: !Ref AWS::StackName }] + InternetGateway: + Type: AWS::EC2::InternetGateway + GatewayAttachment: + Type: AWS::EC2::VPCGatewayAttachment + Properties: + VpcId: !Ref Vpc + InternetGatewayId: !Ref InternetGateway + Subnet: + Type: AWS::EC2::Subnet + Properties: + VpcId: !Ref Vpc + CidrBlock: 10.42.0.0/25 + AvailabilityZone: !Select [0, !GetAZs ""] + MapPublicIpOnLaunch: true + RouteTable: + Type: AWS::EC2::RouteTable + Properties: + VpcId: !Ref Vpc + DefaultRoute: + Type: AWS::EC2::Route + DependsOn: GatewayAttachment + Properties: + RouteTableId: !Ref RouteTable + DestinationCidrBlock: 0.0.0.0/0 + GatewayId: !Ref InternetGateway + SubnetRouteTable: + Type: AWS::EC2::SubnetRouteTableAssociation + Properties: + SubnetId: !Ref Subnet + RouteTableId: !Ref RouteTable + SecurityGroup: + Type: AWS::EC2::SecurityGroup + Properties: + GroupDescription: Qualification task, egress only (image pull and logs) + VpcId: !Ref Vpc + + LogGroup: + Type: AWS::Logs::LogGroup + DeletionPolicy: Delete + Properties: + LogGroupName: !Sub "/ecs/${AWS::StackName}" + RetentionInDays: 30 + + Cluster: + Type: AWS::ECS::Cluster + Properties: + ClusterName: !Ref AWS::StackName + + # Named and bounded as the zeroae-code power-user SSO roles require. + ExecutionRole: + Type: AWS::IAM::Role + Properties: + RoleName: !Sub "PowerUserPB-${AWS::StackName}-exec" + PermissionsBoundary: arn:aws:iam::aws:policy/PowerUserAccess + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: { Service: ecs-tasks.amazonaws.com } + Action: sts:AssumeRole + ManagedPolicyArns: + - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy + + # Fargate defaults throughout: the image's own non-root user, no added + # capability, Fargate's own seccomp profile and no task role. That is what an + # operator's task gets. One vCPU, because the runtime test waits on the + # Gateway with timeouts sized for a CI runner. + TaskDefinitionX8664: + Type: AWS::ECS::TaskDefinition + Properties: + Family: !Sub "${AWS::StackName}-x86_64" + RequiresCompatibilities: [FARGATE] + NetworkMode: awsvpc + Cpu: "1024" + Memory: "2048" + RuntimePlatform: + OperatingSystemFamily: LINUX + CpuArchitecture: X86_64 + ExecutionRoleArn: !GetAtt ExecutionRole.Arn + ContainerDefinitions: + - Name: qualify + Image: !Sub "${Repository.RepositoryUri}:x86_64" + Essential: true + LogConfiguration: + LogDriver: awslogs + Options: + awslogs-group: !Ref LogGroup + awslogs-region: !Ref AWS::Region + awslogs-stream-prefix: x86_64 + + TaskDefinitionArm64: + Type: AWS::ECS::TaskDefinition + Properties: + Family: !Sub "${AWS::StackName}-arm64" + RequiresCompatibilities: [FARGATE] + NetworkMode: awsvpc + Cpu: "1024" + Memory: "2048" + RuntimePlatform: + OperatingSystemFamily: LINUX + CpuArchitecture: ARM64 + ExecutionRoleArn: !GetAtt ExecutionRole.Arn + ContainerDefinitions: + - Name: qualify + Image: !Sub "${Repository.RepositoryUri}:arm64" + Essential: true + LogConfiguration: + LogDriver: awslogs + Options: + awslogs-group: !Ref LogGroup + awslogs-region: !Ref AWS::Region + awslogs-stream-prefix: arm64 + +Outputs: + RepositoryUri: + Value: !GetAtt Repository.RepositoryUri + Cluster: + Value: !Ref Cluster + Subnet: + Value: !Ref Subnet + SecurityGroup: + Value: !Ref SecurityGroup + LogGroup: + Value: !Ref LogGroup