Skip to content

Commit dfee490

Browse files
committed
sources/kerberos: use new python-kadmin implementation
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
1 parent 05b5987 commit dfee490

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

authentik/sources/kerberos/models.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from typing import Any
77

88
import gssapi
9-
import kadmin
109
import pglock
1110
from django.db import connection, models
1211
from django.db.models.fields import b64decode
1312
from django.http import HttpRequest
1413
from django.shortcuts import reverse
1514
from django.templatetags.static import static
1615
from django.utils.translation import gettext_lazy as _
16+
from kadmin import KAdmin
17+
from kadmin.exceptions import PyKAdminException
1718
from rest_framework.serializers import Serializer
1819
from structlog.stdlib import get_logger
1920

@@ -30,9 +31,8 @@
3031
LOGGER = get_logger()
3132

3233

33-
# python-kadmin leaks file descriptors. As such, this global is used to reuse
34-
# existing kadmin connections instead of creating new ones, which results in less to no file
35-
# descriptors leaks
34+
# Creating kadmin connections is expensive. As such, this global is used to reuse
35+
# existing kadmin connections instead of creating new ones
3636
_kadmin_connections: dict[str, Any] = {}
3737

3838

@@ -198,13 +198,13 @@ def krb5_conf_path(self) -> str | None:
198198
conf_path.write_text(self.krb5_conf)
199199
return str(conf_path)
200200

201-
def _kadmin_init(self) -> "kadmin.KAdmin | None":
201+
def _kadmin_init(self) -> KAdmin | None:
202202
# kadmin doesn't use a ccache for its connection
203203
# as such, we don't need to create a separate ccache for each source
204204
if not self.sync_principal:
205205
return None
206206
if self.sync_password:
207-
return kadmin.init_with_password(
207+
return KAdmin.with_password(
208208
self.sync_principal,
209209
self.sync_password,
210210
)
@@ -215,18 +215,18 @@ def _kadmin_init(self) -> "kadmin.KAdmin | None":
215215
keytab_path.touch(mode=0o600)
216216
keytab_path.write_bytes(b64decode(self.sync_keytab))
217217
keytab = f"FILE:{keytab_path}"
218-
return kadmin.init_with_keytab(
218+
return KAdmin.with_keytab(
219219
self.sync_principal,
220220
keytab,
221221
)
222222
if self.sync_ccache:
223-
return kadmin.init_with_ccache(
223+
return KAdmin.with_ccache(
224224
self.sync_principal,
225225
self.sync_ccache,
226226
)
227227
return None
228228

229-
def connection(self) -> "kadmin.KAdmin | None":
229+
def connection(self) -> KAdmin | None:
230230
"""Get kadmin connection"""
231231
if str(self.pk) not in _kadmin_connections:
232232
kadm = self._kadmin_init()
@@ -246,7 +246,7 @@ def check_connection(self) -> dict[str, str]:
246246
status["status"] = "no connection"
247247
return status
248248
status["principal_exists"] = kadm.principal_exists(self.sync_principal)
249-
except kadmin.KAdminError as exc:
249+
except PyKAdminException as exc:
250250
status["status"] = str(exc)
251251
return status
252252

authentik/sources/kerberos/signals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""authentik kerberos source signals"""
22

3-
import kadmin
43
from django.db.models.signals import post_save
54
from django.dispatch import receiver
5+
from kadmin.exceptions import PyKAdminException
66
from rest_framework.serializers import ValidationError
77
from structlog.stdlib import get_logger
88

@@ -48,7 +48,7 @@ def kerberos_sync_password(sender, user: User, password: str, **_):
4848
source.connection().getprinc(user_source_connection.identifier).change_password(
4949
password
5050
)
51-
except kadmin.KAdminError as exc:
51+
except PyKAdminException as exc:
5252
LOGGER.warning("failed to set Kerberos password", exc=exc, source=source)
5353
Event.new(
5454
EventAction.CONFIGURATION_ERROR,

authentik/sources/kerberos/sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from typing import Any
44

5-
import kadmin
65
from django.core.exceptions import FieldError
76
from django.db import IntegrityError, transaction
7+
from kadmin import KAdmin
88
from structlog.stdlib import BoundLogger, get_logger
99

1010
from authentik.core.expression.exceptions import (
@@ -30,7 +30,7 @@ class KerberosSync:
3030

3131
_source: KerberosSource
3232
_logger: BoundLogger
33-
_connection: "kadmin.KAdmin"
33+
_connection: KAdmin
3434
mapper: SourceMapper
3535
user_manager: PropertyMappingManager
3636
group_manager: PropertyMappingManager
@@ -161,7 +161,7 @@ def sync(self) -> int:
161161

162162
user_count = 0
163163
with Krb5ConfContext(self._source):
164-
for principal in self._connection.principals():
164+
for principal in self._connection.list_principals("*"):
165165
if self._handle_principal(principal):
166166
user_count += 1
167167
return user_count

authentik/sources/kerberos/tests/test_auth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def setUp(self):
2323
)
2424
self.user = User.objects.create(username=generate_id())
2525
self.user.set_unusable_password()
26+
self.user.save()
2627
UserKerberosSourceConnection.objects.create(
2728
source=self.source, user=self.user, identifier=self.realm.user_princ
2829
)

poetry.lock

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

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ pydantic-scim = "*"
131131
pyjwt = "*"
132132
pyrad = "*"
133133
python = "~3.12"
134-
# Fork of python-kadmin with compilation fixes as it's unmaintained
135-
python-kadmin = { git = "https://github.com/authentik-community/python-kadmin.git", tag = "v0.2.0" }
134+
python-kadmin-rs = "0.0.4"
136135
pyyaml = "*"
137136
requests-oauthlib = "*"
138137
scim2-filter-parser = "*"

0 commit comments

Comments
 (0)