authentik.events.context_processors.mmdb
Common logic for reading MMDB files
1"""Common logic for reading MMDB files""" 2 3from pathlib import Path 4 5from geoip2.database import Reader 6from structlog.stdlib import get_logger 7 8from authentik.events.context_processors.base import EventContextProcessor 9 10 11class MMDBContextProcessor(EventContextProcessor): 12 """Common logic for reading MaxMind DB files, including re-loading if the file has changed""" 13 14 # Default path from default.yml, and the pre-DB-IP filename to fall back to 15 default_path: str | None = None 16 legacy_path: str | None = None 17 18 def __init__(self): 19 self.reader: Reader | None = None 20 self._last_mtime: float = 0.0 21 self.logger = get_logger() 22 self.load() 23 24 def path(self) -> str | None: 25 """Get the path to the MMDB file to load""" 26 raise NotImplementedError 27 28 def resolved_path(self) -> str | None: 29 """Get the path to load, falling back to the legacy filename 30 when the default file is missing""" 31 path = self.path() 32 if ( 33 path 34 and self.legacy_path 35 and path == self.default_path 36 and not Path(path).exists() 37 and Path(self.legacy_path).exists() 38 ): 39 return self.legacy_path 40 return path 41 42 def load(self): 43 """Get GeoIP Reader, if configured, otherwise none""" 44 path = self.resolved_path() 45 if path == "" or not path: 46 return 47 if path != self.path(): 48 self.logger.warning( 49 "Default MMDB database not found, falling back to legacy path. " 50 "Set the path in the config to keep using this file.", 51 default=self.default_path, 52 path=path, 53 ) 54 try: 55 self.reader = Reader(path) 56 self._last_mtime = Path(path).stat().st_mtime 57 self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path) 58 except OSError as exc: 59 self.logger.warning("Failed to load MMDB database", path=path, exc=exc) 60 61 def check_expired(self): 62 """Check if the modification date of the MMDB database has 63 changed, and reload it if so""" 64 path = self.resolved_path() 65 if path == "" or not path: 66 return 67 try: 68 mtime = Path(path).stat().st_mtime 69 diff = self._last_mtime < mtime 70 if diff > 0: 71 self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path) 72 self.load() 73 except OSError as exc: 74 self.logger.warning("Failed to check MMDB age", exc=exc) 75 76 def configured(self) -> bool: 77 """Return true if this context processor is configured""" 78 return bool(self.reader)
12class MMDBContextProcessor(EventContextProcessor): 13 """Common logic for reading MaxMind DB files, including re-loading if the file has changed""" 14 15 # Default path from default.yml, and the pre-DB-IP filename to fall back to 16 default_path: str | None = None 17 legacy_path: str | None = None 18 19 def __init__(self): 20 self.reader: Reader | None = None 21 self._last_mtime: float = 0.0 22 self.logger = get_logger() 23 self.load() 24 25 def path(self) -> str | None: 26 """Get the path to the MMDB file to load""" 27 raise NotImplementedError 28 29 def resolved_path(self) -> str | None: 30 """Get the path to load, falling back to the legacy filename 31 when the default file is missing""" 32 path = self.path() 33 if ( 34 path 35 and self.legacy_path 36 and path == self.default_path 37 and not Path(path).exists() 38 and Path(self.legacy_path).exists() 39 ): 40 return self.legacy_path 41 return path 42 43 def load(self): 44 """Get GeoIP Reader, if configured, otherwise none""" 45 path = self.resolved_path() 46 if path == "" or not path: 47 return 48 if path != self.path(): 49 self.logger.warning( 50 "Default MMDB database not found, falling back to legacy path. " 51 "Set the path in the config to keep using this file.", 52 default=self.default_path, 53 path=path, 54 ) 55 try: 56 self.reader = Reader(path) 57 self._last_mtime = Path(path).stat().st_mtime 58 self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path) 59 except OSError as exc: 60 self.logger.warning("Failed to load MMDB database", path=path, exc=exc) 61 62 def check_expired(self): 63 """Check if the modification date of the MMDB database has 64 changed, and reload it if so""" 65 path = self.resolved_path() 66 if path == "" or not path: 67 return 68 try: 69 mtime = Path(path).stat().st_mtime 70 diff = self._last_mtime < mtime 71 if diff > 0: 72 self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path) 73 self.load() 74 except OSError as exc: 75 self.logger.warning("Failed to check MMDB age", exc=exc) 76 77 def configured(self) -> bool: 78 """Return true if this context processor is configured""" 79 return bool(self.reader)
Common logic for reading MaxMind DB files, including re-loading if the file has changed
def
path(self) -> str | None:
25 def path(self) -> str | None: 26 """Get the path to the MMDB file to load""" 27 raise NotImplementedError
Get the path to the MMDB file to load
def
resolved_path(self) -> str | None:
29 def resolved_path(self) -> str | None: 30 """Get the path to load, falling back to the legacy filename 31 when the default file is missing""" 32 path = self.path() 33 if ( 34 path 35 and self.legacy_path 36 and path == self.default_path 37 and not Path(path).exists() 38 and Path(self.legacy_path).exists() 39 ): 40 return self.legacy_path 41 return path
Get the path to load, falling back to the legacy filename when the default file is missing
def
load(self):
43 def load(self): 44 """Get GeoIP Reader, if configured, otherwise none""" 45 path = self.resolved_path() 46 if path == "" or not path: 47 return 48 if path != self.path(): 49 self.logger.warning( 50 "Default MMDB database not found, falling back to legacy path. " 51 "Set the path in the config to keep using this file.", 52 default=self.default_path, 53 path=path, 54 ) 55 try: 56 self.reader = Reader(path) 57 self._last_mtime = Path(path).stat().st_mtime 58 self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path) 59 except OSError as exc: 60 self.logger.warning("Failed to load MMDB database", path=path, exc=exc)
Get GeoIP Reader, if configured, otherwise none
def
check_expired(self):
62 def check_expired(self): 63 """Check if the modification date of the MMDB database has 64 changed, and reload it if so""" 65 path = self.resolved_path() 66 if path == "" or not path: 67 return 68 try: 69 mtime = Path(path).stat().st_mtime 70 diff = self._last_mtime < mtime 71 if diff > 0: 72 self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path) 73 self.load() 74 except OSError as exc: 75 self.logger.warning("Failed to check MMDB age", exc=exc)
Check if the modification date of the MMDB database has changed, and reload it if so