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
49 changes: 47 additions & 2 deletions crates/countersign-gateway/src/shared_issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error + Send + Sync>;
type Chain = Vec<CertificateDer<'static>>;
Expand Down Expand Up @@ -46,6 +46,7 @@ enum Source {
Local {
issuer: Box<Issuer<'static, KeyPair>>,
chain: Chain,
starts: OffsetDateTime,
expires: OffsetDateTime,
},
Remote {
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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};
Expand Down
38 changes: 33 additions & 5 deletions crates/countersign-gateway/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -30,13 +30,24 @@ 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.
pub struct SidecarIssuer {
authority: CertifiedIssuer<'static, KeyPair>,
policy: DestinationPolicy,
not_before: OffsetDateTime,
ca_not_before: OffsetDateTime,
not_after: OffsetDateTime,
}

Expand All @@ -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
Expand All @@ -80,6 +92,7 @@ impl SidecarIssuer {
authority,
policy,
not_before: now,
ca_not_before,
not_after,
})
}
Expand Down Expand Up @@ -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<CertificateDer<'static>, 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;
Expand Down
3 changes: 2 additions & 1 deletion crates/countersign-gateway/tests/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
25 changes: 25 additions & 0 deletions crates/countersign-gateway/tests/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions docs/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions qualification/fargate/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
46 changes: 46 additions & 0 deletions qualification/fargate/README.md
Original file line number Diff line number Diff line change
@@ -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-<stack>-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
```
Loading
Loading