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    def __init__(self):
15        self.reader: Reader | None = None
16        self._last_mtime: float = 0.0
17        self.logger = get_logger()
18        self.load()
19
20    def path(self) -> str | None:
21        """Get the path to the MMDB file to load"""
22        raise NotImplementedError
23
24    def load(self):
25        """Get GeoIP Reader, if configured, otherwise none"""
26        path = self.path()
27        if path == "" or not path:
28            return
29        try:
30            self.reader = Reader(path)
31            self._last_mtime = Path(path).stat().st_mtime
32            self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path)
33        except OSError as exc:
34            self.logger.warning("Failed to load MMDB database", path=path, exc=exc)
35
36    def check_expired(self):
37        """Check if the modification date of the MMDB database has
38        changed, and reload it if so"""
39        path = self.path()
40        if path == "" or not path:
41            return
42        try:
43            mtime = Path(path).stat().st_mtime
44            diff = self._last_mtime < mtime
45            if diff > 0:
46                self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path)
47                self.load()
48        except OSError as exc:
49            self.logger.warning("Failed to check MMDB age", exc=exc)
50
51    def configured(self) -> bool:
52        """Return true if this context processor is configured"""
53        return bool(self.reader)
class MMDBContextProcessor(authentik.events.context_processors.base.EventContextProcessor):
12class MMDBContextProcessor(EventContextProcessor):
13    """Common logic for reading MaxMind DB files, including re-loading if the file has changed"""
14
15    def __init__(self):
16        self.reader: Reader | None = None
17        self._last_mtime: float = 0.0
18        self.logger = get_logger()
19        self.load()
20
21    def path(self) -> str | None:
22        """Get the path to the MMDB file to load"""
23        raise NotImplementedError
24
25    def load(self):
26        """Get GeoIP Reader, if configured, otherwise none"""
27        path = self.path()
28        if path == "" or not path:
29            return
30        try:
31            self.reader = Reader(path)
32            self._last_mtime = Path(path).stat().st_mtime
33            self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path)
34        except OSError as exc:
35            self.logger.warning("Failed to load MMDB database", path=path, exc=exc)
36
37    def check_expired(self):
38        """Check if the modification date of the MMDB database has
39        changed, and reload it if so"""
40        path = self.path()
41        if path == "" or not path:
42            return
43        try:
44            mtime = Path(path).stat().st_mtime
45            diff = self._last_mtime < mtime
46            if diff > 0:
47                self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path)
48                self.load()
49        except OSError as exc:
50            self.logger.warning("Failed to check MMDB age", exc=exc)
51
52    def configured(self) -> bool:
53        """Return true if this context processor is configured"""
54        return bool(self.reader)

Common logic for reading MaxMind DB files, including re-loading if the file has changed

reader: geoip2.database.Reader | None
logger
def path(self) -> str | None:
21    def path(self) -> str | None:
22        """Get the path to the MMDB file to load"""
23        raise NotImplementedError

Get the path to the MMDB file to load

def load(self):
25    def load(self):
26        """Get GeoIP Reader, if configured, otherwise none"""
27        path = self.path()
28        if path == "" or not path:
29            return
30        try:
31            self.reader = Reader(path)
32            self._last_mtime = Path(path).stat().st_mtime
33            self.logger.info("Loaded MMDB database", last_write=self._last_mtime, file=path)
34        except OSError as exc:
35            self.logger.warning("Failed to load MMDB database", path=path, exc=exc)

Get GeoIP Reader, if configured, otherwise none

def check_expired(self):
37    def check_expired(self):
38        """Check if the modification date of the MMDB database has
39        changed, and reload it if so"""
40        path = self.path()
41        if path == "" or not path:
42            return
43        try:
44            mtime = Path(path).stat().st_mtime
45            diff = self._last_mtime < mtime
46            if diff > 0:
47                self.logger.info("Found new MMDB Database, reopening", diff=diff, path=path)
48                self.load()
49        except OSError as exc:
50            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

def configured(self) -> bool:
52    def configured(self) -> bool:
53        """Return true if this context processor is configured"""
54        return bool(self.reader)

Return true if this context processor is configured