1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
import subprocess
import time
import psycopg
import pytest
from .utils import (
DIRECT_TLS_SUPPORT,
MACOS,
PG_MAJOR_VERSION,
PG_SUPPORTS_SCRAM,
TEST_DIR,
TLS_SUPPORT,
WINDOWS,
Bouncer,
wait_until,
)
if not TLS_SUPPORT:
pytest.skip(allow_module_level=True)
# XXX: These test use psql to connect using sslmode=verify-full instead of
# using psycopg. The reason for this is that psycopg has a bug on Apple
# silicon when enabling SSL: https://github.com/psycopg/psycopg/discussions/270
# replace regular bouncer fixture with one that uses the special SSL config
@pytest.fixture
async def bouncer_tls(pg, tmp_path):
bouncer_tls = Bouncer(
pg, tmp_path / "bouncer", base_ini_path=TEST_DIR / "ssl" / "test.ini"
)
await bouncer_tls.start()
yield bouncer_tls
await bouncer_tls.cleanup()
def test_server_ssl(pg, bouncer_tls, cert_dir):
bouncer_tls.admin("set server_tls_sslmode = require")
pg.ssl_access("all", "trust")
pg.configure("ssl=on")
root = cert_dir / "TestCA1" / "ca.crt"
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test()
def test_server_ssl_set_disable(pg, bouncer_tls, cert_dir):
bouncer_tls.admin("set server_tls_sslmode = require")
pg.ssl_access("all", "trust")
pg.configure("ssl=on")
root = cert_dir / "TestCA1" / "ca.crt"
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test()
pg.reset_hba()
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test() # connection is still cached
bouncer_tls.admin("reconnect")
with pytest.raises(
psycopg.OperationalError,
match="no pg_hba.conf entry for .*, (SSL encryption|SSL on)",
):
bouncer_tls.test()
# XXX: It would be nice if this reset server_login_retry, but it currently
# doesn't. So we have server_login_retry=1 in the ini file.
bouncer_tls.admin("set server_tls_sslmode = disable")
bouncer_tls.test()
def test_server_ssl_set_enable(pg, bouncer_tls, cert_dir):
bouncer_tls.admin("set server_tls_sslmode = disable")
pg.configure("ssl=on")
root = cert_dir / "TestCA1" / "ca.crt"
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test()
pg.nossl_access("all", "reject")
pg.ssl_access("all", "trust")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test() # connection is still cached
bouncer_tls.admin("reconnect")
with pytest.raises(
psycopg.OperationalError,
match="pg_hba.conf rejects connection for .*, (no encryption|SSL off)",
):
bouncer_tls.test()
# XXX: It would be nice if this reset server_login_retry, but it currently
# doesn't. So we have server_login_retry=1 in the ini file.
bouncer_tls.admin("set server_tls_sslmode = require")
bouncer_tls.test()
def test_server_ssl_verify(pg, bouncer_tls, cert_dir):
bouncer_tls.admin("set server_tls_sslmode = 'verify-full'")
root = cert_dir / "TestCA1" / "ca.crt"
wrong_root = cert_dir / "TestCA2" / "ca.crt"
bouncer_tls.admin(f"set server_tls_ca_file = '{wrong_root}'")
pg.ssl_access("all", "trust")
pg.configure("ssl=on")
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
with bouncer_tls.log_contains(r"certificate verify failed"):
with pytest.raises(
psycopg.OperationalError,
match="connection timeout expired",
):
bouncer_tls.test(connect_timeout=4)
bouncer_tls.admin(f"set server_tls_ca_file = '{root}'")
bouncer_tls.test()
bouncer_tls.psql_test(dbname="hostlistsslverify")
def test_server_ssl_auth(pg, bouncer_tls, cert_dir):
bouncer_tls.admin("set server_tls_sslmode = 'verify-full'")
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "02-bouncer.key"
cert = cert_dir / "TestCA1" / "sites" / "02-bouncer.crt"
bouncer_tls.admin(f"set server_tls_ca_file = '{root}'")
bouncer_tls.admin(f"set server_tls_key_file = '{key}'")
bouncer_tls.admin(f"set server_tls_cert_file = '{cert}'")
pg.ssl_access("all", "cert")
pg.configure("ssl=on")
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
bouncer_tls.test()
def test_client_ssl(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.admin(f"set client_tls_key_file = '{key}'")
bouncer_tls.admin(f"set client_tls_cert_file = '{cert}'")
bouncer_tls.admin(f"set client_tls_ca_file = '{root}'")
bouncer_tls.admin(f"set client_tls_sslmode = require")
bouncer_tls.psql_test(host="localhost", sslmode="require")
def test_client_ssl_set_enable_disable(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.admin(f"set client_tls_key_file = '{key}'")
bouncer_tls.admin(f"set client_tls_cert_file = '{cert}'")
bouncer_tls.admin(f"set client_tls_ca_file = '{root}'")
bouncer_tls.admin(f"set client_tls_sslmode = require")
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.admin(f"set client_tls_sslmode = disable")
bouncer_tls.test(sslmode="disable")
bouncer_tls.admin(f"set client_tls_sslmode = require")
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
def test_client_ssl_set_change_ca(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.admin(f"set client_tls_key_file = '{key}'")
bouncer_tls.admin(f"set client_tls_cert_file = '{cert}'")
bouncer_tls.admin(f"set client_tls_ca_file = '{root}'")
bouncer_tls.admin(f"set client_tls_sslmode = require")
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
new_root = cert_dir / "TestCA2" / "ca.crt"
new_key = cert_dir / "TestCA2" / "sites" / "01-localhost.key"
new_cert = cert_dir / "TestCA2" / "sites" / "01-localhost.crt"
bouncer_tls.admin(f"set client_tls_key_file = '{new_key}'")
bouncer_tls.admin(f"set client_tls_cert_file = '{new_cert}'")
bouncer_tls.admin(f"set client_tls_ca_file = '{new_root}'")
with pytest.raises(
subprocess.CalledProcessError,
):
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=new_root)
@pytest.mark.skipif("WINDOWS", reason="Windows does not have SIGHUP")
def test_client_ssl_sighup_enable_disable(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.sighup()
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.write_ini(f"client_tls_sslmode = disable")
bouncer_tls.sighup()
bouncer_tls.test(sslmode="disable")
@pytest.mark.skipif("WINDOWS", reason="Windows does not have SIGHUP")
def test_client_ssl_sighup_change_ca(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.sighup()
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
new_root = cert_dir / "TestCA2" / "ca.crt"
new_key = cert_dir / "TestCA2" / "sites" / "01-localhost.key"
new_cert = cert_dir / "TestCA2" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {new_key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {new_cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {new_root}")
bouncer_tls.sighup()
with pytest.raises(
subprocess.CalledProcessError,
):
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=new_root)
def test_client_ssl_reload_enable_disable(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.admin("reload")
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.write_ini(f"client_tls_sslmode = disable")
bouncer_tls.admin("reload")
bouncer_tls.test(sslmode="disable")
def test_client_ssl_reload_change_ca(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.admin("reload")
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
new_root = cert_dir / "TestCA2" / "ca.crt"
new_key = cert_dir / "TestCA2" / "sites" / "01-localhost.key"
new_cert = cert_dir / "TestCA2" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {new_key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {new_cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {new_root}")
bouncer_tls.admin("reload")
with pytest.raises(
subprocess.CalledProcessError,
):
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=root)
bouncer_tls.psql_test(host="localhost", sslmode="verify-full", sslrootcert=new_root)
def test_client_ssl_auth(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = verify-full")
bouncer_tls.write_ini(f"auth_type = cert")
bouncer_tls.admin("reload")
client_key = cert_dir / "TestCA1" / "sites" / "02-bouncer.key"
client_cert = cert_dir / "TestCA1" / "sites" / "02-bouncer.crt"
bouncer_tls.psql_test(
host="localhost",
sslmode="verify-full",
user="bouncer",
sslrootcert=root,
sslkey=client_key,
sslcert=client_cert,
)
@pytest.mark.skipif("not PG_SUPPORTS_SCRAM")
def test_client_ssl_scram(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.write_ini(f"auth_type = scram-sha-256")
bouncer_tls.admin("reload")
bouncer_tls.psql_test(
host="localhost",
user="bouncer",
password="zzzz",
sslmode="verify-full",
sslrootcert=root,
)
def test_ssl_replication(pg, bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"server_tls_sslmode = verify-full")
bouncer_tls.write_ini(f"server_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.admin("reload")
pg.ssl_access("all", "trust")
pg.ssl_access("replication", "trust", user="postgres")
pg.configure("ssl=on")
pg.configure(f"ssl_ca_file='{root}'")
if PG_MAJOR_VERSION < 10 or WINDOWS:
pg.restart()
else:
pg.reload()
# Logical rep
connect_args = {
"host": "localhost",
"dbname": "p7a",
"replication": "database",
"user": "postgres",
"application_name": "abc",
"sslmode": "verify-full",
"sslrootcert": root,
}
bouncer_tls.psql("IDENTIFY_SYSTEM", **connect_args)
# physical rep
connect_args["replication"] = "true"
bouncer_tls.psql("IDENTIFY_SYSTEM", **connect_args)
def test_servers_no_disconnect_on_reload_with_no_tls_change(bouncer_tls, pg, cert_dir):
bouncer_tls.default_db = "pTxnPool"
with bouncer_tls.cur() as cur:
assert pg.connection_count(dbname="p0") == 1
with bouncer_tls.log_contains(
r"pTxnPool.*database configuration changed|pTxnPool.*obsolete connection", 0
):
# change nothing and RELOAD
bouncer_tls.admin("RELOAD")
# keep cursor open for > full_maint_period
# full_maint_period = 3x/s https://github.com/pgbouncer/pgbouncer/blob/master/src/janitor.c#L28
time.sleep(0.5)
assert pg.connection_count(dbname="p0") == 1
cur.execute("SELECT 1")
def test_servers_disconnect_when_changing_tls_config(bouncer_tls, pg, cert_dir):
bouncer_tls.default_db = "pTxnPool"
bouncer_tls.write_ini(f"server_tls_protocols = tlsv1.0")
bouncer_tls.admin("RELOAD")
with bouncer_tls.cur() as cur:
assert pg.connection_count(dbname="p0") == 1
bouncer_tls.write_ini(f"server_tls_protocols = secure")
with bouncer_tls.log_contains(
r"pTxnPool.*database configuration changed|pTxnPool.*obsolete connection", 1
):
bouncer_tls.admin("RELOAD")
for _ in wait_until("Did not close connection"):
if pg.connection_count(dbname="p0") == 0:
break
cur.execute("SELECT 1")
def test_servers_disconnect_when_enabling_ssl(bouncer_tls, pg, cert_dir):
bouncer_tls.default_db = "pTxnPool"
bouncer_tls.write_ini(f"server_tls_sslmode = disable")
bouncer_tls.admin("RELOAD")
with bouncer_tls.cur() as cur:
assert pg.connection_count(dbname="p0") == 1
bouncer_tls.write_ini(f"server_tls_sslmode = allow")
with bouncer_tls.log_contains(
r"pTxnPool.*database configuration changed|pTxnPool.*obsolete connection"
):
bouncer_tls.admin("RELOAD")
cur.execute("SELECT 1")
def test_servers_disconnect_when_changing_sslmode(bouncer_tls, pg, cert_dir):
bouncer_tls.default_db = "pTxnPool"
with bouncer_tls.cur() as cur:
assert pg.connection_count(dbname="p0") == 1
bouncer_tls.write_ini(f"server_tls_sslmode = allow")
with bouncer_tls.log_contains(
r"pTxnPool.*database configuration changed|pTxnPool.*obsolete connection"
):
bouncer_tls.admin("RELOAD")
cur.execute("SELECT 1")
def test_client_ssl_set_ciphers_for_tls_v1_3(bouncer_tls, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer_tls.write_ini(f"client_tls_key_file = {key}")
bouncer_tls.write_ini(f"client_tls_cert_file = {cert}")
bouncer_tls.write_ini(f"client_tls_ca_file = {root}")
bouncer_tls.write_ini(f"client_tls_sslmode = require")
bouncer_tls.write_ini("client_tls_protocols=tlsv1.3")
bouncer_tls.write_ini("client_tls13_ciphers=TLS_CHACHA20_POLY1305_SHA256")
bouncer_tls.admin("reload")
with bouncer_tls.log_contains(r"tls=TLSv1.3/TLS_CHACHA20_POLY1305_SHA256"):
bouncer_tls.psql_test(host="localhost", sslmode="require")
bouncer_tls.admin("set client_tls13_ciphers='TLS_AES_256_GCM_SHA384'")
with bouncer_tls.log_contains(r"tls=TLSv1.3/TLS_AES_256_GCM_SHA384"):
bouncer_tls.psql_test(host="localhost", sslmode="require")
with bouncer_tls.log_contains(r"failed to set the TLSv1.3 cipher suites"):
bouncer_tls.admin(f"set client_tls13_ciphers = 'unknown'")
@pytest.mark.skipif(
"not DIRECT_TLS_SUPPORT", reason="Direct TLS is introduced in PG 17"
)
def test_client_direct_ssl(bouncer, cert_dir):
root = cert_dir / "TestCA1" / "ca.crt"
key = cert_dir / "TestCA1" / "sites" / "01-localhost.key"
cert = cert_dir / "TestCA1" / "sites" / "01-localhost.crt"
bouncer.admin(f"set client_tls_key_file = '{key}'")
bouncer.admin(f"set client_tls_cert_file = '{cert}'")
bouncer.admin(f"set client_tls_ca_file = '{root}'")
bouncer.admin("set client_tls_sslmode = require")
bouncer.psql_test(host="localhost", sslmode="require", sslnegotiation="direct")
|