
* remove pyright Signed-off-by: Jens Langhammer <jens@goauthentik.io> * remove pylint Signed-off-by: Jens Langhammer <jens@goauthentik.io> * replace pylint with ruff Signed-off-by: Jens Langhammer <jens@goauthentik.io> * ruff fix Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space> * fix UP038 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix DJ012 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix default arg Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix UP031 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * rename stage type to view Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix DJ008 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix remaining upgrade Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix PLR2004 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix B904 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix PLW2901 Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix remaining issues Signed-off-by: Jens Langhammer <jens@goauthentik.io> * prevent ruff from breaking the code Signed-off-by: Jens Langhammer <jens@goauthentik.io> * stages/prompt: refactor field building Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space> * fix tests Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix lint Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fully remove isort Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io> Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space> Co-authored-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
"""Common logic for reading MMDB files"""
|
|
|
|
from pathlib import Path
|
|
|
|
from geoip2.database import Reader
|
|
from structlog.stdlib import get_logger
|
|
|
|
from authentik.events.context_processors.base import EventContextProcessor
|
|
|
|
|
|
class MMDBContextProcessor(EventContextProcessor):
|
|
"""Common logic for reading MaxMind DB files, including re-loading if the file has changed"""
|
|
|
|
def __init__(self):
|
|
self.reader: Reader | None = None
|
|
self._last_mtime: float = 0.0
|
|
self.logger = get_logger()
|
|
self.open()
|
|
|
|
def path(self) -> str | None:
|
|
"""Get the path to the MMDB file to load"""
|
|
raise NotImplementedError
|
|
|
|
def open(self):
|
|
"""Get GeoIP Reader, if configured, otherwise none"""
|
|
path = self.path()
|
|
if path == "" or not path:
|
|
return
|
|
try:
|
|
self.reader = Reader(path)
|
|
self._last_mtime = Path(path).stat().st_mtime
|
|
self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path)
|
|
except OSError as exc:
|
|
self.logger.warning("Failed to load MMDB database", path=path, exc=exc)
|
|
|
|
def check_expired(self):
|
|
"""Check if the modification date of the MMDB database has
|
|
changed, and reload it if so"""
|
|
path = self.path()
|
|
if path == "" or not path:
|
|
return
|
|
try:
|
|
mtime = Path(path).stat().st_mtime
|
|
diff = self._last_mtime < mtime
|
|
if diff > 0:
|
|
self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path)
|
|
self.open()
|
|
except OSError as exc:
|
|
self.logger.warning("Failed to check MMDB age", exc=exc)
|
|
|
|
def configured(self) -> bool:
|
|
"""Return true if this context processor is configured"""
|
|
return bool(self.reader)
|