From ec247a96e16f78ac74d96d8c5f103e44d2096812 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 10:06:10 +0000 Subject: [PATCH] test(data): PostgreSQL MD5, cleartext, trust and TLS tests can run, and must where TLS is set up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BRIEF §6.3 requires every authentication method and TLS mode against the PostgreSQL service containers, but the MD5, cleartext and trust tests always skipped in CI (no roles) and the TLS tests skipped at run time because the official images start with TLS off. .github/scripts/postgres-ci-setup.sh prepares a server container: a role per method with its pg_hba line ahead of the image's SCRAM line (the MD5 password stored as MD5), and TLS with a self-signed certificate for localhost and 127.0.0.1, enabled with ALTER SYSTEM and a reload whose rejected lines fail the script. With TITANO_TEST_PG_TLS=1 a server that refuses TLS fails the TLS tests instead of skipping them, and a new test checks that verify-full accepts the server whose certificate chains to TITANO_TEST_PG_ROOT_CERT. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .github/scripts/postgres-ci-setup.sh | 56 +++++++++++++++++++ .../Infrastructure/DatabaseTest.cs | 5 ++ .../Titano.Data.Tests/Postgres/PgWireTests.cs | 28 +++++++++- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 .github/scripts/postgres-ci-setup.sh diff --git a/.github/scripts/postgres-ci-setup.sh b/.github/scripts/postgres-ci-setup.sh new file mode 100644 index 00000000..78239047 --- /dev/null +++ b/.github/scripts/postgres-ci-setup.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Prepares a PostgreSQL service container for the wire-protocol tests that need more than the image's defaults +# (BRIEF §6.3): one role per authentication method (MD5, cleartext, trust) with its pg_hba line ahead of the image's +# catch-all SCRAM line, and TLS with a self-signed certificate valid for localhost and 127.0.0.1. +# Usage: bash .github/scripts/postgres-ci-setup.sh CONTAINER SUPERUSER DATABASE CERTIFICATE_FOLDER +# The certificate (server.crt) stays in CERTIFICATE_FOLDER: the verify-full test trusts it as its root. +set -euo pipefail +container=$1 +superuser=$2 +database=$3 +certificates=$4 + +sql() { docker exec -i "$container" psql -v ON_ERROR_STOP=1 -U "$superuser" -d "$database" -Atq "$@"; } + +mkdir -p "$certificates" +openssl req -x509 -newkey rsa:2048 -nodes -days 2 -subj "/CN=localhost" \ + -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \ + -keyout "$certificates/server.key" -out "$certificates/server.crt" 2>/dev/null + +data=$(sql -c "SHOW data_directory") +hba=$(sql -c "SHOW hba_file") +docker cp "$certificates/server.crt" "$container:$data/server.crt" +docker cp "$certificates/server.key" "$container:$data/server.key" +# The server refuses a key that others can read. +docker exec "$container" sh -c "chown postgres:postgres '$data/server.crt' '$data/server.key' && chmod 600 '$data/server.key'" + +# The first matching line wins, so these go above the image's 'host all all all scram-sha-256'. +docker exec "$container" sh -c "{ printf '%s\n' 'host all titano_md5 all md5' 'host all titano_clear all password' \ + 'host all titano_trust all trust'; cat '$hba'; } > /tmp/pg_hba.conf && cat /tmp/pg_hba.conf > '$hba'" + +sql > /dev/null <<'SQL' +SET password_encryption = 'md5'; +CREATE ROLE titano_md5 LOGIN PASSWORD 'md5-ci'; +RESET password_encryption; +CREATE ROLE titano_clear LOGIN PASSWORD 'clear-ci'; +CREATE ROLE titano_trust LOGIN; +ALTER SYSTEM SET ssl = on; +SELECT pg_reload_conf(); +SQL + +# A reload that fails keeps the old settings silently: report the rejected lines, then wait for TLS to be on. +errors=$(sql -c "SELECT line_number || ': ' || error FROM pg_hba_file_rules WHERE error IS NOT NULL + UNION ALL SELECT name || ': ' || error FROM pg_file_settings WHERE error IS NOT NULL") +if [ -n "$errors" ]; then + echo "::error title=PostgreSQL setup::$errors" + exit 1 +fi +for _ in $(seq 1 20); do + if [ "$(sql -c "SHOW ssl")" = "on" ]; then + echo "PostgreSQL in $container: MD5, cleartext and trust roles, TLS on" + exit 0 + fi + sleep 0.5 +done +echo "::error title=PostgreSQL setup::TLS did not turn on after the reload" +exit 1 diff --git a/tests/Titano.Data.Tests/Infrastructure/DatabaseTest.cs b/tests/Titano.Data.Tests/Infrastructure/DatabaseTest.cs index cb2a015f..8566872e 100644 --- a/tests/Titano.Data.Tests/Infrastructure/DatabaseTest.cs +++ b/tests/Titano.Data.Tests/Infrastructure/DatabaseTest.cs @@ -29,6 +29,11 @@ internal static class PgTestServer public static string Optional(string variable) => Environment.GetEnvironmentVariable(variable) is { Length: > 0 } cs ? cs : throw new SkipException($"{variable} not set"); + /// Set to 1 where the server was set up with TLS (CI): a server that refuses TLS then fails the TLS tests. + public const string TlsVariable = "TITANO_TEST_PG_TLS"; + + public static bool TlsExpected => Environment.GetEnvironmentVariable(TlsVariable) is "1" or "true"; + public static async Task CreateSchemaAsync(CancellationToken cancellationToken) { var name = "titano_t_" + Guid.NewGuid().ToString("N")[..16]; diff --git a/tests/Titano.Data.Tests/Postgres/PgWireTests.cs b/tests/Titano.Data.Tests/Postgres/PgWireTests.cs index 132c549b..c6552862 100644 --- a/tests/Titano.Data.Tests/Postgres/PgWireTests.cs +++ b/tests/Titano.Data.Tests/Postgres/PgWireTests.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Security.Authentication; using Titano.Data.Postgres; using Titano.Data.Tests.Infrastructure; @@ -6,7 +7,11 @@ using Titano.Testing; namespace Titano.Data.Tests.Postgres; -/// Protocol-level tests against a live server (TITANO_TEST_PG; optional TITANO_TEST_PG_MD5/_CLEARTEXT/_TRUST). +/// +/// Protocol-level tests against a live server (TITANO_TEST_PG). Optional: TITANO_TEST_PG_MD5/_CLEARTEXT/_TRUST (roles +/// of the other authentication methods), TITANO_TEST_PG_TLS=1 (the server offers TLS) and TITANO_TEST_PG_ROOT_CERT (the +/// PEM its certificate chains to). CI sets them all up (.github/scripts/postgres-ci-setup.sh). +/// [RequiresEnvironment(PgTestServer.Variable)] public sealed class PgWireTests(TestContext context) : DatabaseTest(context) { @@ -15,6 +20,14 @@ public sealed class PgWireTests(TestContext context) : DatabaseTest(context) private async Task OpenAsync(string? extra = null) => (PostgresDatabase)Own(DatabaseFactory.Open(await NewOptionsAsync(extraPostgres: extra))); + /// A server without TLS skips the TLS tests, unless it was set up with TLS (). + [DoesNotReturn] + private static void WithoutTls(DatabaseConnectionException refusal) + { + if (PgTestServer.TlsExpected) Assert.Fail($"{PgTestServer.TlsVariable} says the server offers TLS, but it refused: {refusal.Message}"); + TestContext.Skip("server has no TLS"); + } + private async Task AuthenticateAsync(string connectionString) { var database = Own(new PostgresDatabase(PostgresSettings.Parse(connectionString))); @@ -61,7 +74,7 @@ public sealed class PgWireTests(TestContext context) : DatabaseTest(context) } catch (DatabaseConnectionException ex) when (ex.Message.Contains("does not support TLS", StringComparison.Ordinal)) { - TestContext.Skip("server has no TLS"); + WithoutTls(ex); return; } await using var session = await database.OpenAsync(Ct); @@ -83,13 +96,22 @@ public sealed class PgWireTests(TestContext context) : DatabaseTest(context) } catch (DatabaseConnectionException ex) when (ex.Message.Contains("does not support TLS", StringComparison.Ordinal)) { - TestContext.Skip("server has no TLS"); + WithoutTls(ex); } catch (AuthenticationException) { } } + [Test] + public async Task VerifyFullAcceptsACertificateThatChainsToTheConfiguredRoot() + { + var root = PgTestServer.Optional("TITANO_TEST_PG_ROOT_CERT"); + var database = await OpenAsync($"SslMode=VerifyFull;Root Certificate={root}"); + await using var session = await database.OpenAsync(Ct); + Assert.True(await session.ScalarAsync("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()", [], Ct)); + } + [Test] public async Task DecodesEveryMappedResultType() {