diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index b7800e7..cff6a50 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -11,6 +11,9 @@ // stats max_queued= max_tick_debt= commits= store_failures= // Exit: 0 drained, 2 usage, 3 drain deadline passed, 4 quarantined. // +// --pg-service (M8.3) takes host, hostaddr, port, database, user and CA from +// the [evolution] section of a libpq service file; anything else in it is refused. +// // --objects-reserve-mib (M8.3, default 1024) refuses new objects that would leave // less free space on the object volume: node-local volumes do not enforce their // requested size. A refused upload is a store failure; nothing is acknowledged. @@ -129,7 +132,15 @@ std::optional parse(int argc, char** argv) { o.pg.user = value; else if (flag == "--pg-passfile") o.pg.password_file = value; - else if (flag == "--pg-ca") + else if (flag == "--pg-service") { + // Connection from the [evolution] section of a libpq service file (M8.3); + // the password file stays separate (--pg-passfile). + auto service = persistence::read_pg_service(std::string(value), "evolution"); + if (!service) + return std::nullopt; + service->password_file = o.pg.password_file; + o.pg = *service; + } else if (flag == "--pg-ca") o.pg.ca_file = value; else if (flag == "--objects") o.objects = value; diff --git a/apps/evo-world-clone/main.cpp b/apps/evo-world-clone/main.cpp index db0b3e4..4e0cf2c 100644 --- a/apps/evo-world-clone/main.cpp +++ b/apps/evo-world-clone/main.cpp @@ -10,7 +10,8 @@ // evo-world-clone check-store --world W --region R --pg-* ... --objects DIR // [--reserve-mib N] // -// --*pg-* is host, hostaddr, port, database, user, passfile, ca. The source +// --*pg-* is host, hostaddr, port, database, user, passfile, ca, or service (a libpq +// service file whose [evolution] section supplies everything but the password file). The source // connection needs only the runtime read functions and is switched to read-only // transactions; the target connection is the target database's owner. Source and // target must be different databases reached with different users: a clone never @@ -103,6 +104,15 @@ bool pg_flag(std::string_view flag, std::string_view prefix, std::string_view va if (!flag.starts_with(prefix)) return false; const auto name = flag.substr(prefix.size()); + if (name == "service") { + // [evolution] section of a libpq service file; the password file stays separate. + auto service = persistence::read_pg_service(std::string(value), "evolution"); + if (!service) + return false; + service->password_file = pg.password_file; + pg = *service; + return true; + } std::string* field = name == "host" ? &pg.host : name == "hostaddr" ? &pg.hostaddr : name == "port" ? &pg.port diff --git a/deploy/kubernetes/postgres-migration/job.yaml.example b/deploy/kubernetes/postgres-migration/job.yaml.example index 7bbeb3d..52209a9 100644 --- a/deploy/kubernetes/postgres-migration/job.yaml.example +++ b/deploy/kubernetes/postgres-migration/job.yaml.example @@ -31,7 +31,7 @@ spec: requests: {cpu: 100m, memory: 64Mi} limits: {cpu: 500m, memory: 128Mi} env: - - {name: PGSERVICE, value: evolution-migrator} + - {name: PGSERVICE, value: evolution} - {name: PGSERVICEFILE, value: /run/postgres/pg_service.conf} - {name: EVO_PGPASS_SOURCE, value: /run/postgres/pgpass} # Operator-created NOLOGIN group role; the runtime login is its member. diff --git a/deploy/kubernetes/stores/stores.yaml.example b/deploy/kubernetes/stores/stores.yaml.example index 8e8eb4c..d971968 100644 --- a/deploy/kubernetes/stores/stores.yaml.example +++ b/deploy/kubernetes/stores/stores.yaml.example @@ -7,11 +7,11 @@ # REPLACE_RUNTIME_ROLE that environment's NOLOGIN runtime group role # REPLACE_TOOLS_DIGEST aevolution-postgres-tools image digest (sha256:...) # REPLACE_SERVER_DIGEST aevolution server-runtime image digest (sha256:...) -# Secret contract (docs/infrastructure/ENVIRONMENTS.md, never in Git): -# aevolution-db: PGHOST, PGHOSTADDR, PGPORT, PGDATABASE (only these -# keys are read; PGPASSWORD/PGUSER are never mounted) -# evolution-postgres-migrator: pg_service.conf, pgpass, ca.crt (migrate-postgres.sh) -# evolution-postgres-runtime: user, pgpass, ca.crt +# Secret contract (docs/infrastructure/ENVIRONMENTS.md, never in Git). Each login +# Secret is self-contained, with keys pg_service.conf (section [evolution], with +# sslrootcert=/run/postgres/ca.crt), pgpass and ca.crt: +# evolution-postgres-migrator the environment's migrator login (migrate-postgres.sh) +# evolution-postgres-runtime the environment's runtime login (check-store, region) # Order: apply the NetworkPolicy, run migrate, then check-store (twice: the second # run must report "probe verified", proving the volume survived a new pod). apiVersion: networking.k8s.io/v1 @@ -66,7 +66,7 @@ spec: - name: migrate image: registry.aeternalisgames.com/aeternalis/aevolution-postgres-tools@REPLACE_TOOLS_DIGEST env: - - {name: PGSERVICE, value: evolution-migrator} + - {name: PGSERVICE, value: evolution} - {name: PGSERVICEFILE, value: /run/postgres/pg_service.conf} - {name: EVO_PGPASS_SOURCE, value: /run/postgres/pgpass} - {name: EVO_PG_RUNTIME_ROLE, value: REPLACE_RUNTIME_ROLE} @@ -134,20 +134,8 @@ spec: umask 077 cp /run/postgres/pgpass /tmp/pgpass exec /usr/local/bin/evo-world-clone check-store --world 1 --region 1 \ - --objects /objects --pg-host "$DB_HOST" --pg-hostaddr "$DB_HOSTADDR" \ - --pg-port "$DB_PORT" --pg-database "$DB_NAME" --pg-user "$DB_USER" \ - --pg-passfile /tmp/pgpass --pg-ca /run/postgres/ca.crt - env: - - name: DB_HOST - valueFrom: {secretKeyRef: {name: aevolution-db, key: PGHOST}} - - name: DB_HOSTADDR - valueFrom: {secretKeyRef: {name: aevolution-db, key: PGHOSTADDR}} - - name: DB_PORT - valueFrom: {secretKeyRef: {name: aevolution-db, key: PGPORT}} - - name: DB_NAME - valueFrom: {secretKeyRef: {name: aevolution-db, key: PGDATABASE}} - - name: DB_USER - valueFrom: {secretKeyRef: {name: evolution-postgres-runtime, key: user}} + --objects /objects --pg-service /run/postgres/pg_service.conf \ + --pg-passfile /tmp/pgpass resources: requests: {cpu: 50m, memory: 64Mi} limits: {memory: 256Mi} @@ -170,5 +158,6 @@ spec: secretName: evolution-postgres-runtime defaultMode: 0440 items: + - {key: pg_service.conf, path: pg_service.conf} - {key: pgpass, path: pgpass} - {key: ca.crt, path: ca.crt} diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index 1f23a1c..2a4e73c 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -7,7 +7,7 @@ names it. Remove an item only when it is done, and record where it was done. | Item | Raised | Why deferred | Needed before | |---|---|---|---| | **World0 break-glass membership.** Decide who may act as `aevolution:world0-break-glass`, and how that group is granted: for example a short-lived client certificate with that organization, or an OIDC group. Decide also how its use is approved and recorded. The admission policy already enforces the group (ADR-0069). | M8.2, 2026-09-29 | Maintainer: figure out later | Any intentional deletion of World0 data or identity; M8.8 World0 launch | -| **Backup destination for dev/stage stores.** Decide where backups go: the AEvolution PostgreSQL VM (WAL archive or base backups) and each environment's `aevolution-objects` volume. It should be off the host for any claim beyond disposable environments. | M8.3, 2026-09-29 | Needs the maintainer's choice of target | M8.7 backup and restore; any World0 data | +| **Backup destination for dev/stage stores.** There are no backups yet (maintainer, 2026-09-29). The plan is to build backup tooling for the AEvolution PostgreSQL VM and each `aevolution-objects` volume up to a pluggable destination, then add the destination. It must be off the host for anything beyond disposable environments. | M8.3, 2026-09-29 | The maintainer will supply a destination later | M8.7 backup and restore; any World0 data | | **Storage after the RAID/disk rework.** Re-run `deploy/kubernetes/inventory/disk-probe-local-path.yaml` (or a new class), and update INVENTORY.md. | M8.1, 2026-09-29 | Maintainer is reorganizing disks | M8.3's final storage decision | ## Accepted risks (maintainer decisions) diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 7ae61be..63b248d 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -2728,3 +2728,32 @@ admission policy and the stage and World0 AppProjects, then ran tool's usage exit. It has not been built locally, because Docker is not running. - Not executed: anything in the cluster, the store Jobs, retention, rebind and rotation evidence, and the image build. + +## M8.3 service-file connections - 2026-09-29 + +- The maintainer's dev Secrets have the keys `ca.crt`, `pg_service.conf` + (section `[evolution]`) and `pgpass`, with no `user` key. The values were + pasted into the conversation, so both passwords must be rotated. That rotation + is also the M8.3 credential-rotation evidence step. The values are not recorded + anywhere in the repository. +- `read_pg_service` and `--pg-service` were added to `evo-server` and + `evo-world-clone`. + - Parser tests: a valid file with another section and CRLF lines parses. The + parser refuses: + - `password`, `passfile` and `options` keys; + - `sslmode=require`; + - a repeated key or a repeated section; + - a non-numeric timeout; + - malformed lines; + - a missing section; + - a file over 4 KiB; + - a missing file. + `sslmode=disable` is accepted only for a local socket. + - `check-store --pg-service` against the local cluster verified the probe and the + privilege boundary. +- The store Jobs now mount each login Secret whole (service file, password file, + CA) and no longer read `aevolution-db`. The migrate Job's `PGSERVICE` is now + `evolution`; the previous `evolution-migrator` did not match the maintainer's + file. +- PostgreSQL Release and ASan/UBSan passed. The analysis build passed after one + loop copy was fixed. Deploy policy: 0 failures. diff --git a/docs/infrastructure/ENVIRONMENTS.md b/docs/infrastructure/ENVIRONMENTS.md index c2f12c3..f57532a 100644 --- a/docs/infrastructure/ENVIRONMENTS.md +++ b/docs/infrastructure/ENVIRONMENTS.md @@ -18,15 +18,30 @@ manual, and none is performed by Argo CD. ## Secret references (created by an operator, never committed) +Each environment has its own **database**: dev uses `aevolution` on the PostgreSQL +VM `pg-forge-01`. Stage needs a separate database, with its own migrator login and +runtime group. Sharing one database would let one environment's runtime login act +on the other's regions, because the fenced functions are per database, not per +environment. + Each namespace gets its own Secrets, with environment-specific values: - `harbor-pull`: a pull-only Harbor robot (docker-registry type). - `evolution-postgres-migrator`: the migrator/owner login for that environment's - database. Keys: `pg_service.conf`, `pgpass`, `ca.crt`. + database. - `evolution-postgres-runtime`: the runtime login, a member of that environment's - NOLOGIN runtime group (ADR-0060). Keys: `user`, `pgpass`, `ca.crt`. -- `aevolution-db`: connection settings. Only `PGHOST`, `PGHOSTADDR` (numeric), - `PGPORT` and `PGDATABASE` are ever read, as individual variables. `PGPASSWORD` - and `PGUSER` are never mounted; the policy check forbids it. + NOLOGIN runtime group (ADR-0060). + +Both login Secrets have the keys `pg_service.conf`, `pgpass` and `ca.crt`: +- `pg_service.conf` has an `[evolution]` section with `host`, a numeric + `hostaddr`, `port`, `dbname`, `user`, `sslmode=verify-full` and + `sslrootcert=/run/postgres/ca.crt`. `connect_timeout` is allowed. `evo-server` + and `evo-world-clone` read it with `--pg-service` and refuse any other key. +- `pgpass` is mounted, then copied to private memory before use, because libpq + ignores group-readable password files. + +`aevolution-db` (the `PG*`/`POSTGRES_*` connection variables) is not used by +AEvolution workloads. `PGPASSWORD` and `PGUSER` are never placed in a container +environment, and the policy check forbids it. A World0 credential must never exist in the dev or stage namespaces, and no dev or stage credential may reach the World0 database. Rotate by replacing the Secret and @@ -58,7 +73,8 @@ Run this once per disposable environment (`dev`, then `stage`). **1. Label the store node, and apply the storage class and claim.** ```sh -# Label exactly one worker (worker-1 or worker-2; never worker-3 or the control plane). +# Label exactly one worker. The maintainer allows any worker (never the control plane); +# worker-1 is chosen (lowest measured latency; worker-3's disk is 87 % full). kubectl label node rke2-worker-1 aevolution.io/stores=true kubectl apply -f deploy/kubernetes/platform/storage/storageclass.yaml kubectl apply -k deploy/kubernetes/platform/dev # adds the aevolution-objects claim diff --git a/docs/infrastructure/POSTGRESQL.md b/docs/infrastructure/POSTGRESQL.md index 30eee1f..ac0eb58 100644 --- a/docs/infrastructure/POSTGRESQL.md +++ b/docs/infrastructure/POSTGRESQL.md @@ -191,7 +191,7 @@ epoch. Create Secret **evolution-postgres-migrator** in evolution-dev outside Git. Keys: `pg_service.conf`, `pgpass`, `ca.crt`. The service file has section -`[evolution-migrator]` with host, numeric hostaddr, port, dbname, user, +`[evolution]` (the same section name in every login Secret, M8.3) with host, numeric hostaddr, port, dbname, user, sslmode=verify-full, sslrootcert=/run/postgres/ca.crt and connect_timeout=5. pgpass uses libpq's escaped host:port:database:user:password format. Do not paste values into chat, manifests or image arguments. Configure cluster Secret RBAC and diff --git a/include/evo/persistence/postgres.hpp b/include/evo/persistence/postgres.hpp index 729d831..0a36995 100644 --- a/include/evo/persistence/postgres.hpp +++ b/include/evo/persistence/postgres.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace evo::persistence { @@ -19,6 +20,14 @@ struct PgOptions { std::string host, hostaddr, port, database, user, password_file, ca_file; }; using PgRows = std::vector>>; +// Reads one section of a libpq connection service file (M8.3): host, hostaddr, +// port, dbname, user and sslrootcert (as ca_file). sslmode may only be verify-full +// (or disable for a local socket), and connect_timeout is accepted and ignored (the +// adapter has its own deadlines). Anything else (password, passfile, options, a +// missing or repeated section, a repeated key, a file over 4 KiB) is refused with +// config. password_file stays empty: the caller supplies it. +[[nodiscard]] std::expected read_pg_service(const std::string& file, + std::string_view section); // Linux I/O-owner adapter. Query text is trusted application SQL, values are parameters. // No automatic retry. unknown means the command may have executed; connection is closed. class Postgres { diff --git a/src/persistence/postgres.cpp b/src/persistence/postgres.cpp index 144c862..fe8bfe9 100644 --- a/src/persistence/postgres.cpp +++ b/src/persistence/postgres.cpp @@ -4,11 +4,20 @@ #include #include #include +#include #include #include +#include namespace evo::persistence { namespace { +std::string_view trim(std::string_view s) { + while (!s.empty() && (s.front() == ' ' || s.front() == '\t' || s.front() == '\r')) + s.remove_prefix(1); + while (!s.empty() && (s.back() == ' ' || s.back() == '\t' || s.back() == '\r')) + s.remove_suffix(1); + return s; +} using Clock = std::chrono::steady_clock; struct Finish { void operator()(PGconn* p) const { @@ -52,6 +61,76 @@ struct Postgres::Impl { }; Postgres::Postgres(std::unique_ptr impl) : impl_(std::move(impl)) {} Postgres::~Postgres() = default; +std::expected read_pg_service(const std::string& file, + std::string_view section) { + constexpr std::size_t limit = 4096; + std::ifstream in(file, std::ios::binary); + if (!in) + return std::unexpected{PgError::config}; + std::string text(limit + 1, '\0'); + in.read(text.data(), static_cast(text.size())); + text.resize(static_cast(in.gcount())); + if (text.size() > limit || section.empty()) + return std::unexpected{PgError::config}; + PgOptions o; + std::string sslmode; + bool inside = false, found = false; + std::vector seen; + std::string_view rest = text; + while (!rest.empty()) { + const auto end = rest.find('\n'); + const auto line = trim(rest.substr(0, end)); + rest.remove_prefix(end == std::string_view::npos ? rest.size() : end + 1); + if (line.empty() || line.front() == '#') + continue; + if (line.front() == '[') { + if (line.back() != ']') + return std::unexpected{PgError::config}; + inside = line.substr(1, line.size() - 2) == section; + if (inside && found) + return std::unexpected{PgError::config}; // Repeated section. + found = found || inside; + continue; + } + if (!inside) + continue; + const auto equals = line.find('='); + if (equals == std::string_view::npos) + return std::unexpected{PgError::config}; + const std::string key{trim(line.substr(0, equals))}; + const std::string value{trim(line.substr(equals + 1))}; + if (std::ranges::find(seen, key) != seen.end()) + return std::unexpected{PgError::config}; + seen.push_back(key); + if (key == "host") + o.host = value; + else if (key == "hostaddr") + o.hostaddr = value; + else if (key == "port") + o.port = value; + else if (key == "dbname") + o.database = value; + else if (key == "user") + o.user = value; + else if (key == "sslrootcert") + o.ca_file = value; + else if (key == "sslmode") + sslmode = value; + else if (key == "connect_timeout") { + if (value.empty() || + !std::ranges::all_of(value, [](char ch) { return ch >= '0' && ch <= '9'; })) + return std::unexpected{PgError::config}; + } else { + return std::unexpected{PgError::config}; // password, passfile, options, ... + } + } + const bool local = !o.host.empty() && o.host.front() == '/'; + if (!found || + (!sslmode.empty() && sslmode != "verify-full" && !(local && sslmode == "disable"))) + return std::unexpected{PgError::config}; + return o; +} + std::expected, PgFailure> Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) { if (!duration(timeout) || o.host.empty() || o.database.empty() || o.user.empty() || diff --git a/tests/postgres_tests.cpp b/tests/postgres_tests.cpp index 5afebb6..7aadef3 100644 --- a/tests/postgres_tests.cpp +++ b/tests/postgres_tests.cpp @@ -26,7 +26,39 @@ std::string env(const char* key) { throw std::runtime_error("missing test environment"); return value; } +// M8.3: libpq service files are read strictly (section, keys, TLS mode, size). +void service_files() { + const auto path = env("PGHOST") + "/service-test.conf"; + const auto parse = [&](const std::string& text, std::string_view section = "evolution") { + std::FILE* f = std::fopen(path.c_str(), "wb"); + CHECK(f != nullptr); + CHECK(std::fwrite(text.data(), 1, text.size(), f) == text.size()); + CHECK(std::fclose(f) == 0); + return read_pg_service(path, section); + }; + auto good = parse("# comment\n[other]\nhost=elsewhere\n[evolution]\r\nhost=pg-forge-01\n" + "hostaddr=172.10.1.127\nport=5432\ndbname=aevolution\n" + "user=aevolution_runtime_dev\nsslmode=verify-full\n" + "sslrootcert=/run/postgres/ca.crt\nconnect_timeout=5\n"); + CHECK(good && good->host == "pg-forge-01" && good->hostaddr == "172.10.1.127" && + good->port == "5432" && good->database == "aevolution" && + good->user == "aevolution_runtime_dev" && good->ca_file == "/run/postgres/ca.crt" && + good->password_file.empty()); + const std::string base = "[evolution]\nhost=h\nhostaddr=10.0.0.1\nport=1\ndbname=d\nuser=u\n"; + for (const std::string& bad : + {base + "password=secret\n", base + "passfile=/x\n", base + "sslmode=require\n", + base + "options=-cx\n", base + "user=again\n", base + "[evolution]\nhost=h\n", + base + "connect_timeout=soon\n", base + "no-equals-sign\n", std::string("[evolution\n"), + std::string("[elsewhere]\nhost=h\n"), base + std::string(5000, '#')}) + CHECK(!parse(bad) && parse(bad).error() == PgError::config); + CHECK(parse("[evolution]\nhost=/var/run\nsslmode=disable\n")); // Local socket only. + CHECK(!parse("[evolution]\nhost=remote\nsslmode=disable\n")); + CHECK(!read_pg_service(env("PGHOST") + "/no-such-file", "evolution")); + std::remove(path.c_str()); +} + void run() { + service_files(); PgOptions options{env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), env("PGUSER"), {}, {}}; auto made = Postgres::connect(options, 5s); CHECK(made); diff --git a/tests/region_store_tests.cpp b/tests/region_store_tests.cpp index 6ff1699..023e962 100644 --- a/tests/region_store_tests.cpp +++ b/tests/region_store_tests.cpp @@ -162,6 +162,21 @@ void check_store_qualifies_the_stores() { const auto other = fresh("store-check-full"); auto full = check_store(other, runtime, {"--reserve-mib", "1048576"}, code); CHECK(code == 4 && has(full, "probe refused full")); + // The same check through a libpq service file, as the cluster Jobs run it. + const auto service = root / "pg_service.conf"; + { + std::FILE* f = std::fopen(service.c_str(), "wb"); + CHECK(f != nullptr); + const auto text = "[evolution]\nhost=" + env("PGHOST") + "\nport=" + env("PGPORT") + + "\ndbname=" + env("PGDATABASE") + "\nuser=" + runtime + + "\nsslmode=disable\nconnect_timeout=5\n"; + CHECK(std::fwrite(text.data(), 1, text.size(), f) == text.size()); + CHECK(std::fclose(f) == 0); + } + Child via_service{{env("EVO_WORLD_CLONE_BIN"), "check-store", "--world", "7", "--region", "84", + "--pg-service", service.string(), "--objects", root.string()}}; + CHECK(via_service.wait_exit(30s) == 0 && via_service.printed("probe verified digest=") && + via_service.printed("database tables=denied functions=reachable")); // An owner login can read tables: not an acceptable runtime identity. auto owner_run = check_store(root, env("PGUSER"), {}, code); CHECK(code == 3 && has(owner_run, "database tables=READABLE"));