mirror of
https://github.com/kanidm/kanidm.git
synced 2025-05-22 08:53:57 +02:00
fix: updating rlm_python to use uv for things
This commit is contained in:
parent
90c5ab04d8
commit
da569cc1b4
1
rlm_python/.python-version
Normal file
1
rlm_python/.python-version
Normal file
|
@ -0,0 +1 @@
|
|||
3.12
|
3
rlm_python/README.md
Normal file
3
rlm_python/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# rlm_python
|
||||
|
||||
Kandim FreeRADIUS module.
|
|
@ -1,2 +1,13 @@
|
|||
[tool.ruff]
|
||||
line-length = 150
|
||||
[project]
|
||||
name = "rlm-python"
|
||||
version = "0.1.0"
|
||||
description = "FreeRADIUS Kanidm module"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = ["kanidm"]
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["mypy>=1.15.0", "pytest>=8.3.5", "ruff>=0.11.9", "ty>=0.0.0a8"]
|
||||
|
||||
[tool.uv.sources]
|
||||
kanidm = { path = "../pykanidm" }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
""" entrypoint for kanidm's RADIUS module """
|
||||
"""entrypoint for kanidm's RADIUS module"""
|
||||
|
||||
import atexit
|
||||
import os
|
||||
|
@ -7,15 +7,16 @@ import subprocess
|
|||
import shutil
|
||||
import signal
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import Any, Optional
|
||||
|
||||
# import toml
|
||||
import kanidm.radius
|
||||
from kanidm.radius import CONFIG_PATHS
|
||||
from kanidm.types import KanidmClientConfig
|
||||
from kanidm.utils import load_config
|
||||
|
||||
DEBUG = True
|
||||
if os.environ.get('DEBUG', False):
|
||||
if os.environ.get("DEBUG", False):
|
||||
DEBUG = True
|
||||
|
||||
CERT_SERVER_DEST = "/etc/raddb/certs/server.pem"
|
||||
|
@ -23,54 +24,62 @@ CERT_CA_DEST = "/etc/raddb/certs/ca.pem"
|
|||
CERT_CA_DIR = "/etc/raddb/certs/"
|
||||
CERT_DH_DEST = "/etc/raddb/certs/dh.pem"
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def _sigchild_handler(
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
""" handler for SIGCHLD call"""
|
||||
) -> None:
|
||||
"""handler for SIGCHLD call"""
|
||||
print("Received SIGCHLD ...", file=sys.stderr)
|
||||
os.waitpid(-1, os.WNOHANG)
|
||||
|
||||
|
||||
def write_clients_conf(
|
||||
kanidm_config_object: KanidmClientConfig,
|
||||
) -> None:
|
||||
""" writes out the config file """
|
||||
) -> None:
|
||||
"""writes out the config file"""
|
||||
raddb_config_file = Path("/etc/raddb/clients.conf")
|
||||
|
||||
with raddb_config_file.open('w', encoding='utf-8') as file_handle:
|
||||
with raddb_config_file.open("w", encoding="utf-8") as file_handle:
|
||||
for client in kanidm_config_object.radius_clients:
|
||||
file_handle.write(f"client {client.name} {{\n" )
|
||||
file_handle.write(f"client {client.name} {{\n")
|
||||
file_handle.write(f" ipaddr = {client.ipaddr}\n")
|
||||
file_handle.write(f" secret = {client.secret}\n" )
|
||||
file_handle.write(' proto = *\n')
|
||||
file_handle.write('}\n')
|
||||
file_handle.write(f" secret = {client.secret}\n")
|
||||
file_handle.write(" proto = *\n")
|
||||
file_handle.write("}\n")
|
||||
|
||||
|
||||
def setup_certs(
|
||||
kanidm_config_object: KanidmClientConfig,
|
||||
) -> None:
|
||||
""" sets up certificates """
|
||||
) -> None:
|
||||
"""sets up certificates"""
|
||||
|
||||
if kanidm_config_object.radius_ca_path:
|
||||
cert_ca = Path(kanidm_config_object.radius_ca_path).expanduser().resolve()
|
||||
if not cert_ca.exists():
|
||||
print(f"Failed to find radiusd ca file ({cert_ca}), quitting!", file=sys.stderr)
|
||||
print(
|
||||
f"Failed to find radiusd ca file ({cert_ca}), quitting!",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
if cert_ca != CERT_CA_DEST:
|
||||
if cert_ca != Path(CERT_CA_DEST):
|
||||
print(f"Copying {cert_ca} to {CERT_CA_DEST}")
|
||||
try:
|
||||
shutil.copyfile(cert_ca, CERT_CA_DEST)
|
||||
except shutil.SameFileError:
|
||||
pass
|
||||
|
||||
|
||||
# This dir can also contain crls!
|
||||
if kanidm_config_object.radius_ca_dir:
|
||||
cert_ca_dir = Path(kanidm_config_object.radius_ca_dir).expanduser().resolve()
|
||||
if not cert_ca_dir.exists():
|
||||
print(f"Failed to find radiusd ca dir ({cert_ca_dir}), quitting!", file=sys.stderr)
|
||||
print(
|
||||
f"Failed to find radiusd ca dir ({cert_ca_dir}), quitting!",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
if cert_ca_dir != CERT_CA_DIR:
|
||||
if cert_ca_dir != Path(CERT_CA_DIR):
|
||||
print(f"Copying {cert_ca_dir} to {CERT_CA_DIR}")
|
||||
shutil.copytree(cert_ca_dir, CERT_CA_DIR, dirs_exist_ok=True)
|
||||
|
||||
|
@ -83,7 +92,7 @@ def setup_certs(
|
|||
print(
|
||||
f"Failed to find server keyfile ({server_key}), quitting!",
|
||||
file=sys.stderr,
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
server_cert = Path(kanidm_config_object.radius_cert_path).expanduser().resolve()
|
||||
|
@ -91,18 +100,19 @@ def setup_certs(
|
|||
print(
|
||||
f"Failed to find server cert file ({server_cert}), quitting!",
|
||||
file=sys.stderr,
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
# concat key + cert into /etc/raddb/certs/server.pem
|
||||
with open(CERT_SERVER_DEST, 'w', encoding='utf-8') as file_handle:
|
||||
with open(CERT_SERVER_DEST, "w", encoding="utf-8") as file_handle:
|
||||
file_handle.write(server_cert.read_text(encoding="utf-8"))
|
||||
file_handle.write('\n')
|
||||
file_handle.write("\n")
|
||||
file_handle.write(server_key.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def kill_radius(
|
||||
proc: subprocess.Popen,
|
||||
) -> None:
|
||||
""" handler to kill the radius server once the script exits """
|
||||
proc: subprocess.Popen[Any],
|
||||
) -> None:
|
||||
"""handler to kill the radius server once the script exits"""
|
||||
if proc is None:
|
||||
pass
|
||||
else:
|
||||
|
@ -116,8 +126,9 @@ def kill_radius(
|
|||
|
||||
proc.wait()
|
||||
|
||||
def find_freeradius_bin() -> str:
|
||||
""" finds the binary """
|
||||
|
||||
def find_freeradius_bin() -> Optional[str]:
|
||||
"""finds the binary"""
|
||||
binary_paths = [
|
||||
"/usr/sbin/radiusd",
|
||||
"/usr/sbin/freeradius",
|
||||
|
@ -129,37 +140,44 @@ def find_freeradius_bin() -> str:
|
|||
print(f"Failed to find FreeRADIUS binary, looked in {lookedin}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def run_radiusd() -> None:
|
||||
""" run the server """
|
||||
"""run the server"""
|
||||
|
||||
if DEBUG:
|
||||
cmd_args = [ "-X" ]
|
||||
cmd_args = ["-X"]
|
||||
else:
|
||||
cmd_args = [ "-f", "-l", "stdout" ]
|
||||
with subprocess.Popen(
|
||||
[find_freeradius_bin()] + cmd_args,
|
||||
stderr=subprocess.STDOUT,
|
||||
cmd_args = ["-f", "-l", "stdout"]
|
||||
freeradius_bin = find_freeradius_bin()
|
||||
if freeradius_bin is None:
|
||||
print("Failed to find FreeRADIUS binary, quitting!", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
else:
|
||||
with subprocess.Popen(
|
||||
[freeradius_bin] + cmd_args,
|
||||
stderr=subprocess.STDOUT,
|
||||
) as proc:
|
||||
# print(proc, file=sys.stderr)
|
||||
atexit.register(kill_radius, proc)
|
||||
proc.wait()
|
||||
# print(proc, file=sys.stderr)
|
||||
atexit.register(kill_radius, proc)
|
||||
proc.wait()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
signal.signal(signal.SIGCHLD, _sigchild_handler)
|
||||
|
||||
config_file = kanidm.radius.find_radius_config_path()
|
||||
if config_file is None:
|
||||
print(
|
||||
"Failed to find configuration file ({config_file}), quitting!",
|
||||
f"Failed to find configuration file in ({CONFIG_PATHS}), quitting!",
|
||||
file=sys.stderr,
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
kanidm_config = KanidmClientConfig.model_validate(load_config(config_file))
|
||||
setup_certs(kanidm_config)
|
||||
write_clients_conf(kanidm_config)
|
||||
print("Configuration set up, starting...")
|
||||
try:
|
||||
run_radiusd()
|
||||
except KeyboardInterrupt as ki:
|
||||
print(ki)
|
||||
else:
|
||||
kanidm_config = KanidmClientConfig.model_validate(load_config(config_file))
|
||||
setup_certs(kanidm_config)
|
||||
write_clients_conf(kanidm_config)
|
||||
print("Configuration set up, starting...")
|
||||
try:
|
||||
run_radiusd()
|
||||
except KeyboardInterrupt as ki:
|
||||
print(ki)
|
||||
|
|
1046
rlm_python/uv.lock
Normal file
1046
rlm_python/uv.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue