Skip to content

Commit 0606b1a

Browse files
authored
root: support db pool (#13534)
1 parent 03d5dad commit 0606b1a

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed

authentik/lib/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ def redis_url(db: int) -> str:
356356
def django_db_config(config: ConfigLoader | None = None) -> dict:
357357
if not config:
358358
config = CONFIG
359+
360+
pool_options = False
361+
use_pool = config.get_bool("postgresql.use_pool", False)
362+
if use_pool:
363+
pool_options = config.get_dict_from_b64_json("postgresql.pool_options", True)
364+
if not pool_options:
365+
pool_options = True
366+
359367
db = {
360368
"default": {
361369
"ENGINE": "authentik.root.db",
@@ -369,6 +377,7 @@ def django_db_config(config: ConfigLoader | None = None) -> dict:
369377
"sslrootcert": config.get("postgresql.sslrootcert"),
370378
"sslcert": config.get("postgresql.sslcert"),
371379
"sslkey": config.get("postgresql.sslkey"),
380+
"pool": pool_options,
372381
},
373382
"CONN_MAX_AGE": config.get_optional_int("postgresql.conn_max_age", 0),
374383
"CONN_HEALTH_CHECKS": config.get_bool("postgresql.conn_health_checks", False),

authentik/lib/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ postgresql:
2121
user: authentik
2222
port: 5432
2323
password: "env://POSTGRES_PASSWORD"
24+
use_pool: False
2425
test:
2526
name: test_authentik
2627
default_schema: public

authentik/lib/tests/test_config.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def test_db_default(self):
217217
"HOST": "foo",
218218
"NAME": "foo",
219219
"OPTIONS": {
220+
"pool": False,
220221
"sslcert": "foo",
221222
"sslkey": "foo",
222223
"sslmode": "foo",
@@ -267,6 +268,7 @@ def test_db_read_replicas(self):
267268
"HOST": "foo",
268269
"NAME": "foo",
269270
"OPTIONS": {
271+
"pool": False,
270272
"sslcert": "foo",
271273
"sslkey": "foo",
272274
"sslmode": "foo",
@@ -285,6 +287,7 @@ def test_db_read_replicas(self):
285287
"HOST": "bar",
286288
"NAME": "foo",
287289
"OPTIONS": {
290+
"pool": False,
288291
"sslcert": "foo",
289292
"sslkey": "foo",
290293
"sslmode": "foo",
@@ -333,6 +336,7 @@ def test_db_read_replicas_pgbouncer(self):
333336
"HOST": "foo",
334337
"NAME": "foo",
335338
"OPTIONS": {
339+
"pool": False,
336340
"sslcert": "foo",
337341
"sslkey": "foo",
338342
"sslmode": "foo",
@@ -351,6 +355,7 @@ def test_db_read_replicas_pgbouncer(self):
351355
"HOST": "bar",
352356
"NAME": "foo",
353357
"OPTIONS": {
358+
"pool": False,
354359
"sslcert": "foo",
355360
"sslkey": "foo",
356361
"sslmode": "foo",
@@ -394,6 +399,7 @@ def test_db_read_replicas_pgpool(self):
394399
"HOST": "foo",
395400
"NAME": "foo",
396401
"OPTIONS": {
402+
"pool": False,
397403
"sslcert": "foo",
398404
"sslkey": "foo",
399405
"sslmode": "foo",
@@ -412,6 +418,7 @@ def test_db_read_replicas_pgpool(self):
412418
"HOST": "bar",
413419
"NAME": "foo",
414420
"OPTIONS": {
421+
"pool": False,
415422
"sslcert": "foo",
416423
"sslkey": "foo",
417424
"sslmode": "foo",
@@ -451,6 +458,7 @@ def test_db_read_replicas_diff_ssl(self):
451458
"HOST": "foo",
452459
"NAME": "foo",
453460
"OPTIONS": {
461+
"pool": False,
454462
"sslcert": "foo",
455463
"sslkey": "foo",
456464
"sslmode": "foo",
@@ -469,6 +477,7 @@ def test_db_read_replicas_diff_ssl(self):
469477
"HOST": "bar",
470478
"NAME": "foo",
471479
"OPTIONS": {
480+
"pool": False,
472481
"sslcert": "bar",
473482
"sslkey": "foo",
474483
"sslmode": "foo",
@@ -484,3 +493,87 @@ def test_db_read_replicas_diff_ssl(self):
484493
},
485494
},
486495
)
496+
497+
def test_db_pool(self):
498+
"""Test DB Config with pool"""
499+
config = ConfigLoader()
500+
config.set("postgresql.host", "foo")
501+
config.set("postgresql.name", "foo")
502+
config.set("postgresql.user", "foo")
503+
config.set("postgresql.password", "foo")
504+
config.set("postgresql.port", "foo")
505+
config.set("postgresql.test.name", "foo")
506+
config.set("postgresql.use_pool", True)
507+
conf = django_db_config(config)
508+
self.assertEqual(
509+
conf,
510+
{
511+
"default": {
512+
"ENGINE": "authentik.root.db",
513+
"HOST": "foo",
514+
"NAME": "foo",
515+
"OPTIONS": {
516+
"pool": True,
517+
"sslcert": None,
518+
"sslkey": None,
519+
"sslmode": None,
520+
"sslrootcert": None,
521+
},
522+
"PASSWORD": "foo",
523+
"PORT": "foo",
524+
"TEST": {"NAME": "foo"},
525+
"USER": "foo",
526+
"CONN_MAX_AGE": 0,
527+
"CONN_HEALTH_CHECKS": False,
528+
"DISABLE_SERVER_SIDE_CURSORS": False,
529+
}
530+
},
531+
)
532+
533+
def test_db_pool_options(self):
534+
"""Test DB Config with pool"""
535+
config = ConfigLoader()
536+
config.set("postgresql.host", "foo")
537+
config.set("postgresql.name", "foo")
538+
config.set("postgresql.user", "foo")
539+
config.set("postgresql.password", "foo")
540+
config.set("postgresql.port", "foo")
541+
config.set("postgresql.test.name", "foo")
542+
config.set("postgresql.use_pool", True)
543+
config.set(
544+
"postgresql.pool_options",
545+
base64.b64encode(
546+
dumps(
547+
{
548+
"max_size": 15,
549+
}
550+
).encode()
551+
).decode(),
552+
)
553+
conf = django_db_config(config)
554+
self.assertEqual(
555+
conf,
556+
{
557+
"default": {
558+
"ENGINE": "authentik.root.db",
559+
"HOST": "foo",
560+
"NAME": "foo",
561+
"OPTIONS": {
562+
"pool": {
563+
"max_size": 15,
564+
},
565+
"sslcert": None,
566+
"sslkey": None,
567+
"sslmode": None,
568+
"sslrootcert": None,
569+
},
570+
"PASSWORD": "foo",
571+
"PORT": "foo",
572+
"TEST": {"NAME": "foo"},
573+
"USER": "foo",
574+
"CONN_MAX_AGE": 0,
575+
"CONN_HEALTH_CHECKS": False,
576+
"DISABLE_SERVER_SIDE_CURSORS": False,
577+
}
578+
},
579+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies = [
4747
"opencontainers",
4848
"packaging",
4949
"paramiko",
50-
"psycopg[c]",
50+
"psycopg[c, pool]",
5151
"pydantic",
5252
"pydantic-scim",
5353
"pyjwt",

uv.lock

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/docs/releases/2025/v2025.4.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Previously, sessions were stored by default in the cache. Now, they are stored i
1919

2020
## New features
2121

22+
### Postgres pool
23+
2224
## Upgrading
2325

2426
This release does not introduce any new requirements. You can follow the upgrade instructions below; for more detailed information about upgrading authentik, refer to our [Upgrade documentation](../../install-config/upgrade.mdx).

0 commit comments

Comments
 (0)