authentik.events.context_processors.asn

ASN Enricher

 1"""ASN Enricher"""
 2
 3from typing import TYPE_CHECKING, TypedDict
 4
 5from django.http import HttpRequest
 6from geoip2.errors import GeoIP2Error
 7from geoip2.models import ASN
 8
 9from authentik.events.context_processors.mmdb import MMDBContextProcessor
10from authentik.lib.config import CONFIG
11from authentik.lib.tracing import active_tracer
12from authentik.root.middleware import ClientIPMiddleware
13
14if TYPE_CHECKING:
15    from authentik.api.v3.config import Capabilities
16    from authentik.events.models import Event
17
18
19class ASNDict(TypedDict):
20    """ASN Details"""
21
22    asn: int | None
23    as_org: str | None
24    network: str | None
25
26
27class ASNContextProcessor(MMDBContextProcessor):
28    """ASN Database reader wrapper"""
29
30    default_path = "/geoip/dbip-asn-lite.mmdb"
31    legacy_path = "/geoip/GeoLite2-ASN.mmdb"
32
33    def capability(self) -> Capabilities | None:
34        from authentik.api.v3.config import Capabilities
35
36        return Capabilities.CAN_ASN
37
38    def path(self) -> str | None:
39        return CONFIG.get("events.context_processors.asn")
40
41    def enrich_event(self, event: Event):
42        asn = self.asn_dict(event.client_ip)
43        if not asn:
44            return
45        event.context["asn"] = asn
46
47    def enrich_context(self, request: HttpRequest) -> dict:
48        return {
49            "asn": self.asn_dict(ClientIPMiddleware.get_client_ip(request)),
50        }
51
52    def asn(self, ip_address: str) -> ASN | None:
53        """Wrapper for Reader.asn"""
54        with active_tracer().start_span(
55            op="authentik.events.asn.asn",
56            name=ip_address,
57        ):
58            if not self.configured():
59                return None
60            self.check_expired()
61            try:
62                return self.reader.asn(ip_address)
63            except GeoIP2Error, ValueError:
64                return None
65
66    def asn_to_dict(self, asn: ASN | None) -> ASNDict | dict:
67        """Convert ASN to dict"""
68        if not asn:
69            return {}
70        asn_dict: ASNDict = {
71            "asn": asn.autonomous_system_number,
72            "as_org": asn.autonomous_system_organization,
73            "network": str(asn.network) if asn.network else None,
74        }
75        return asn_dict
76
77    def asn_dict(self, ip_address: str) -> ASNDict | None:
78        """Wrapper for self.asn that returns a dict"""
79        asn = self.asn(ip_address)
80        if not asn:
81            return None
82        return self.asn_to_dict(asn)
83
84
85ASN_CONTEXT_PROCESSOR = ASNContextProcessor()
class ASNDict(typing.TypedDict):
20class ASNDict(TypedDict):
21    """ASN Details"""
22
23    asn: int | None
24    as_org: str | None
25    network: str | None

ASN Details

asn: int | None
as_org: str | None
network: str | None
class ASNContextProcessor(authentik.events.context_processors.mmdb.MMDBContextProcessor):
28class ASNContextProcessor(MMDBContextProcessor):
29    """ASN Database reader wrapper"""
30
31    default_path = "/geoip/dbip-asn-lite.mmdb"
32    legacy_path = "/geoip/GeoLite2-ASN.mmdb"
33
34    def capability(self) -> Capabilities | None:
35        from authentik.api.v3.config import Capabilities
36
37        return Capabilities.CAN_ASN
38
39    def path(self) -> str | None:
40        return CONFIG.get("events.context_processors.asn")
41
42    def enrich_event(self, event: Event):
43        asn = self.asn_dict(event.client_ip)
44        if not asn:
45            return
46        event.context["asn"] = asn
47
48    def enrich_context(self, request: HttpRequest) -> dict:
49        return {
50            "asn": self.asn_dict(ClientIPMiddleware.get_client_ip(request)),
51        }
52
53    def asn(self, ip_address: str) -> ASN | None:
54        """Wrapper for Reader.asn"""
55        with active_tracer().start_span(
56            op="authentik.events.asn.asn",
57            name=ip_address,
58        ):
59            if not self.configured():
60                return None
61            self.check_expired()
62            try:
63                return self.reader.asn(ip_address)
64            except GeoIP2Error, ValueError:
65                return None
66
67    def asn_to_dict(self, asn: ASN | None) -> ASNDict | dict:
68        """Convert ASN to dict"""
69        if not asn:
70            return {}
71        asn_dict: ASNDict = {
72            "asn": asn.autonomous_system_number,
73            "as_org": asn.autonomous_system_organization,
74            "network": str(asn.network) if asn.network else None,
75        }
76        return asn_dict
77
78    def asn_dict(self, ip_address: str) -> ASNDict | None:
79        """Wrapper for self.asn that returns a dict"""
80        asn = self.asn(ip_address)
81        if not asn:
82            return None
83        return self.asn_to_dict(asn)

ASN Database reader wrapper

default_path = '/geoip/dbip-asn-lite.mmdb'
legacy_path = '/geoip/GeoLite2-ASN.mmdb'
def capability(unknown):
34    def capability(self) -> Capabilities | None:
35        from authentik.api.v3.config import Capabilities
36
37        return Capabilities.CAN_ASN

Return the capability this context processor provides

def path(self) -> str | None:
39    def path(self) -> str | None:
40        return CONFIG.get("events.context_processors.asn")

Get the path to the MMDB file to load

def enrich_event(unknown):
42    def enrich_event(self, event: Event):
43        asn = self.asn_dict(event.client_ip)
44        if not asn:
45            return
46        event.context["asn"] = asn

Modify event

def enrich_context(self, request: django.http.request.HttpRequest) -> dict:
48    def enrich_context(self, request: HttpRequest) -> dict:
49        return {
50            "asn": self.asn_dict(ClientIPMiddleware.get_client_ip(request)),
51        }

Modify context

def asn(self, ip_address: str) -> geoip2.models.ASN | None:
53    def asn(self, ip_address: str) -> ASN | None:
54        """Wrapper for Reader.asn"""
55        with active_tracer().start_span(
56            op="authentik.events.asn.asn",
57            name=ip_address,
58        ):
59            if not self.configured():
60                return None
61            self.check_expired()
62            try:
63                return self.reader.asn(ip_address)
64            except GeoIP2Error, ValueError:
65                return None

Wrapper for Reader.asn

def asn_to_dict( self, asn: geoip2.models.ASN | None) -> ASNDict | dict:
67    def asn_to_dict(self, asn: ASN | None) -> ASNDict | dict:
68        """Convert ASN to dict"""
69        if not asn:
70            return {}
71        asn_dict: ASNDict = {
72            "asn": asn.autonomous_system_number,
73            "as_org": asn.autonomous_system_organization,
74            "network": str(asn.network) if asn.network else None,
75        }
76        return asn_dict

Convert ASN to dict

def asn_dict( self, ip_address: str) -> ASNDict | None:
78    def asn_dict(self, ip_address: str) -> ASNDict | None:
79        """Wrapper for self.asn that returns a dict"""
80        asn = self.asn(ip_address)
81        if not asn:
82            return None
83        return self.asn_to_dict(asn)

Wrapper for self.asn that returns a dict

ASN_CONTEXT_PROCESSOR = <ASNContextProcessor object>