Skip to content
Open
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
16 changes: 13 additions & 3 deletions app/ferroehr/tests/it/definition_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,19 @@ async fn the_lanes_boot_on_the_seeded_default_definitions() {
for (lane, text) in lane_configs() {
let file = assert_fs::NamedTempFile::new("ferroehr.toml").expect("temp file");
file.write_str(&text).expect("write");
let config =
ferroehr::config::assemble(Some(file.path()), &std::collections::HashMap::new(), &[])
.unwrap_or_else(|e| panic!("{lane} assembles: {e:?}"));
// A lane that names a signing passphrase file reads the secret its
// deployment mounts there, so a throwaway one stands in for it.
let passphrase = assert_fs::NamedTempFile::new("signing-passphrase").expect("temp file");
passphrase.write_str("lane").expect("write");
let mut env = std::collections::HashMap::new();
if text.contains("key_passphrase_file") {
env.insert(
"FERROEHR__RECORD__SIGNING__KEY_PASSPHRASE_FILE".to_owned(),
passphrase.path().display().to_string(),
);
}
let config = ferroehr::config::assemble(Some(file.path()), &env, &[])
.unwrap_or_else(|e| panic!("{lane} assembles: {e:?}"));
let binding = &config.authz.party_binding;
assert_eq!(
(
Expand Down
1 change: 1 addition & 0 deletions changelog.d/6694-sandbox-signing-key.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The hosted sandbox signs every version with an ML-DSA-65+Ed25519 key of its own (`[record.signing] mode = "pgp"`), made on the box and held there as two compose secrets beside `.env`, and publishes its access-log checkpoints outside the database, so its demo records carry OpenPGP version signatures and its access log is sealed into signed checkpoints. The hosted compose file refuses to start while `.env` names no `FERROEHR_SIGNING_KEY_FILE` or `FERROEHR_SIGNING_PASSPHRASE_FILE`.
81 changes: 81 additions & 0 deletions deploy/hosted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,87 @@ references them; the coded text those examples do carry (`openehr`, ISO 639-1,
ISO 3166-1, plus external SNOMED CT references) resolves against the server's
own in-process terminology bundle.

## The signing key

The sandbox signs every version with an ML-DSA-65+Ed25519 key of its own
(RFC 9980 algorithm ID 30), `[record.signing] mode = "pgp"` in
`ferroehr.sandbox.toml`, and seals the access log into signed checkpoints
with it. The key is made on the app box and never leaves it: no copy goes
into the repository, the image, CI, a log or a message. The committed test
key under `docker/signing/` signs in the development lanes and never here.

The key and its passphrase are two files under
`/opt/ferroehr-sandbox/secrets/`, a directory only `deploy` may enter. Both
are owned by 65532, the user the CDR image runs as, with mode `0400`, so
neither `deploy` nor any other account but root reads them. The compose
file mounts them into the CDR as the secrets `ferroehr-signing-key` and
`ferroehr-signing-passphrase` (at `/run/secrets/<name>`, the paths the
configuration names), and the passphrase alone into the `ferroehr-grant`,
`ferroehr-hold` and `ferroehr-hold-clinician` one-shots, whose configuration
loader reads it. `.env`
names the two files:

```sh
FERROEHR_SIGNING_KEY_FILE=./secrets/signing-key.asc
FERROEHR_SIGNING_PASSPHRASE_FILE=./secrets/signing-passphrase
```

Without either line `docker compose config` fails, so `deploy.sh` refuses
the posture before it installs anything or stops the running CDR. With a
line that names a missing file, `up -d` fails after the old CDR is removed,
so make the files first and add the lines after.

### Making the key

GnuPG cannot make an ML-DSA-65+Ed25519 key. `ferrohealth-sop`, the Stateless
OpenPGP command line of `ferrohealth-openpgp-pq` at the version
`docs/VERSIONS.md` pins, can. The box carries no Rust toolchain, so it is
built in the Rust image `docker/Dockerfile` builds the server with, and the
key is made in that image with no network. As `deploy` in
`/opt/ferroehr-sandbox`:

```sh
RUST_IMAGE=rust:<version>-slim-trixie@sha256:<digest> # the builder pin of docker/Dockerfile
install -d -m 0700 secrets tools
docker run --rm -v "$PWD/tools:/out" "$RUST_IMAGE" \
cargo install ferrohealth-openpgp-pq --version =0.1.0 --locked --root /out/sop
docker run --rm --network none -v "$PWD/tools/sop:/sop:ro" -v "$PWD/secrets:/s" \
"$RUST_IMAGE" sh -euc '
umask 077
head -c 32 /dev/urandom | base64 | tr -d "\n" > /s/signing-passphrase
/sop/bin/ferrohealth-sop generate-key --signing-only \
--with-key-password=/s/signing-passphrase \
"FerroEHR hosted sandbox <sandbox@ferroehr.eu>" > /s/signing-key.asc
/sop/bin/ferrohealth-sop extract-cert < /s/signing-key.asc > /s/signing-cert.asc
echo probe | /sop/bin/ferrohealth-sop sign \
--with-key-password=/s/signing-passphrase /s/signing-key.asc > /tmp/probe.sig
echo probe | /sop/bin/ferrohealth-sop verify /tmp/probe.sig /s/signing-cert.asc > /dev/null
chown 65532:65532 /s/signing-key.asc /s/signing-passphrase
chmod 0400 /s/signing-key.asc /s/signing-passphrase
chmod 0444 /s/signing-cert.asc'
sha256sum secrets/signing-cert.asc | cut -c1-12
```

The passphrase is 32 random bytes in base64, written without a line ending
and never printed. The probe signs and verifies one line with the new pair
before anything uses it. `signing-cert.asc` is the public half: the only
file of the three that may be shown, and the log names its 12-digit SHA-256
fingerprint. Then add the two `.env` lines above (keep a copy of the old
`.env` as `.env.prev`).

### Rotating the key

Make the new pair beside the old one under `.new` names with the same two
containers, move the old three files to `secrets/retired-<date>/`, move the
new ones into place, and run the reseed at once (`sandbox-reseed.yml` by
`workflow_dispatch`): its wipe empties the store and its `deploy` recreates
the CDR on the new key. Until then a CDR that restarts reads the new key and
refuses what the old one signed. The wiped store holds no version the old
key signed, so the configuration lists no `retired_key_files`. A rotation without a wipe (a key taken out of service
mid-day) lists the old certificate's public half there, in the posture, in
the same release. Delete a retired directory only once nothing it signed
is kept anywhere.

## Bootstrap and recovery

The first posture on a fresh box (or recovery from a broken one) is manual by
Expand Down
31 changes: 31 additions & 0 deletions deploy/hosted/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ services:
DATABASE_URL: ${DATABASE_URL:?DATABASE_URL must be set in /opt/ferroehr-sandbox/.env}
volumes:
- ./ferroehr.sandbox.toml:/etc/ferroehr/ferroehr.toml:ro
# The sandbox's own signing key and its passphrase ([record.signing]),
# mounted at /run/secrets/<name> from the files `.env` names (the
# `secrets` block at the end says why a deploy refuses without them).
secrets:
- ferroehr-signing-key
- ferroehr-signing-passphrase
# [audit.access_log] publish_directory. A tmpfs, so a recreate after the
# nightly wipe empties it with the database; 1777 because the server runs
# as 65532 and a tmpfs mounts owned by root.
tmpfs:
- /var/lib/ferroehr/access-log:mode=1777,size=16m
# The issuer's namespace (the header says why); Caddy and the viewer reach
# the CDR as ferroehr:8080, the name the issuer answers to.
network_mode: service:identity-issuer
Expand Down Expand Up @@ -163,6 +174,9 @@ services:
FERROEHR__USAGE_REPORT__ENABLED: "false"
volumes:
- ./ferroehr.sandbox.toml:/etc/ferroehr/ferroehr.toml:ro
# The loader reads the passphrase file of every configuration it loads.
secrets:
- ferroehr-signing-passphrase
command:
- party-binding
- grant
Expand Down Expand Up @@ -203,6 +217,9 @@ services:
FERROEHR__USAGE_REPORT__ENABLED: "false"
volumes:
- ./ferroehr.sandbox.toml:/etc/ferroehr/ferroehr.toml:ro
# The loader reads the passphrase file of every configuration it loads.
secrets:
- ferroehr-signing-passphrase
command:
- party-binding
- grant
Expand Down Expand Up @@ -240,6 +257,9 @@ services:
FERROEHR__USAGE_REPORT__ENABLED: "false"
volumes:
- ./ferroehr.sandbox.toml:/etc/ferroehr/ferroehr.toml:ro
# The loader reads the passphrase file of every configuration it loads.
secrets:
- ferroehr-signing-passphrase
command:
- party-binding
- grant
Expand Down Expand Up @@ -500,6 +520,17 @@ configs:
ferropix-data-key:
content: ${FERROPIX_DATA_KEY:?FERROPIX_DATA_KEY must be set in /opt/ferroehr-sandbox/.env}

# The sandbox's signing key and its passphrase, generated on the box and kept
# beside `.env`, never in the repository or the image (deploy/hosted/README.md,
# "The signing key"). `.env` names the two files. Unset, `docker compose config`
# fails, so deploy.sh refuses the posture before it installs it or stops the
# running CDR.
secrets:
ferroehr-signing-key:
file: ${FERROEHR_SIGNING_KEY_FILE:?FERROEHR_SIGNING_KEY_FILE must name the signing key in /opt/ferroehr-sandbox/.env}
ferroehr-signing-passphrase:
file: ${FERROEHR_SIGNING_PASSPHRASE_FILE:?FERROEHR_SIGNING_PASSPHRASE_FILE must name the signing key passphrase in /opt/ferroehr-sandbox/.env}

volumes:
caddy_data:
caddy_config:
Expand Down
9 changes: 9 additions & 0 deletions deploy/hosted/env.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ FERROEHR_VIEWER_IMAGE=ghcr.io/ferrohealth/ferroehr-viewer:latest
# without either.
# FERROPIX_DATABASE_URL=postgres://ferropix:CHANGE_ME@10.0.0.2:5432/ferropix
# FERROPIX_DATA_KEY=CHANGE_ME

# The sandbox's own ML-DSA-65+Ed25519 signing key and its passphrase (#6694),
# made on this box and kept under ./secrets/, never anywhere else
# (deploy/hosted/README.md, "The signing key"). Compose substitution names: the
# compose file mounts each file into the CDR as a secret and no container
# receives the names. Unset, a deploy refuses the posture, so set them
# only once both files exist.
# FERROEHR_SIGNING_KEY_FILE=./secrets/signing-key.asc
# FERROEHR_SIGNING_PASSPHRASE_FILE=./secrets/signing-passphrase
20 changes: 20 additions & 0 deletions deploy/hosted/ferroehr.sandbox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ scope = "system/Patient.rs"
[privacy.cohort]
subjects = true

# The sandbox signs with an ML-DSA-65+Ed25519 key of its own (RFC 9980
# algorithm ID 30), generated on the box and held there alone: the key and its
# passphrase are compose secrets beside the box's `.env`, which the compose
# file mounts at these paths (deploy/hosted/README.md says how they are made
# and rotated). Every version then carries an OpenPGP signature, and the
# access log is sealed into signed checkpoints. The committed test key under
# docker/signing/ never signs here.
[record.signing]
mode = "pgp"
key_file = "/run/secrets/ferroehr-signing-key"
key_passphrase_file = "/run/secrets/ferroehr-signing-passphrase"

# The newest access-log checkpoint is published outside the database every 30
# seconds, so the access_log_checkpoints indicator reads UP. The directory is a
# tmpfs of the CDR container: the nightly wipe empties the database and
# recreates the container, so the published checkpoints and the chain they
# seal start again together.
[audit.access_log]
publish_directory = "/var/lib/ferroehr/access-log"

# Any origin may read responses: development and the demo only.
[api.cors]
allow_origins = ["*"]
32 changes: 28 additions & 4 deletions scripts/checks/shipped-configs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,20 @@ check() {
if [[ -n "${FERROEHR_CONFIG_IMAGE:-}" || -n "${FERROEHR_BIN:-}" ]]; then
# ── The hosted sandbox ───────────────────────────────────────────────────────
printf 'DATABASE_URL=%s\n' "$dsn" >"$work/hosted.env"
# The box mounts its signing key's passphrase as a compose secret, and the
# loader reads that file: a throwaway one stands in for it.
passphrase="$(yq -p toml -o yaml '.record.signing.key_passphrase_file // ""' deploy/hosted/ferroehr.sandbox.toml)"
if [[ -n "$passphrase" ]]; then
printf 'config-check' >"$work/signing-passphrase"
chmod 0644 "$work/signing-passphrase"
if [[ -n "${FERROEHR_CONFIG_IMAGE:-}" ]]; then
mounts=(-v "$work/signing-passphrase:$passphrase:ro")
else
echo "FERROEHR__RECORD__SIGNING__KEY_PASSPHRASE_FILE=$work/signing-passphrase" >>"$work/hosted.env"
fi
fi
check deploy/hosted/ferroehr.sandbox.toml deploy/hosted/ferroehr.sandbox.toml "$work/hosted.env"
mounts=()

# ── Every overlay on the quickstart ──────────────────────────────────────────
for overlay in docker-compose.*.yml; do
Expand Down Expand Up @@ -285,6 +298,18 @@ viewer_compose() {
viewer_check "$label" "$work/viewer.env" "$file"
}

# ── The hosted sandbox's compose file ────────────────────────────────────────
# Resolved on every run, whatever program the check is given, with every
# profile. The box's .env supplies these; a placeholder stands in for each.
if ! COMPOSE_PROFILES="*" DATABASE_URL="$dsn" FERROPIX_DATABASE_URL="$dsn" FERROPIX_DATA_KEY=placeholder \
FERROEHR_SIGNING_KEY_FILE="$work/placeholder" FERROEHR_SIGNING_PASSPHRASE_FILE="$work/placeholder" \
"${compose[@]}" -f deploy/hosted/docker-compose.yml config --format json </dev/null >"$work/hosted.json" 2>"$work/hosted.err"; then
echo "::error file=deploy/hosted/docker-compose.yml::docker compose cannot resolve the hosted sandbox's compose file:" >&2
cat "$work/hosted.err" >&2
fail=1
: >"$work/hosted.json"
fi

if [[ -n "${FERROEHR_VIEWER_BIN:-}" ]]; then
# The viewer sits behind a compose profile; every profile is resolved.
mounts_in=()
Expand All @@ -294,10 +319,9 @@ if [[ -n "${FERROEHR_VIEWER_BIN:-}" ]]; then
export FERROSMART_SECRETS="$work/ferrosmart-secrets"
bash docker/ferrosmart/secrets.sh "$FERROSMART_SECRETS" >/dev/null
# ── The hosted sandbox's viewer ────────────────────────────────────────────
# The box's .env supplies these; a placeholder stands in for each.
DATABASE_URL="$dsn" FERROPIX_DATABASE_URL="$dsn" FERROPIX_DATA_KEY=placeholder \
"${compose[@]}" -f deploy/hosted/docker-compose.yml config --format json </dev/null >"$work/hosted.json"
viewer_compose deploy/hosted/docker-compose.yml "$work/hosted.json" "$PWD/deploy/hosted"
if [[ -s "$work/hosted.json" ]]; then
viewer_compose deploy/hosted/docker-compose.yml "$work/hosted.json" "$PWD/deploy/hosted"
fi

# ── The quickstart's viewer, alone and with each overlay ───────────────────
"${compose[@]}" -f docker-compose.yml config --format json </dev/null >"$work/quickstart.json"
Expand Down
Loading