authentik.enterprise.endpoints.connectors.microsoft_intune.controller
1from asyncio import run 2from typing import Any 3 4from msgraph.generated.models.managed_device import ManagedDevice 5from msgraph.graph_service_client import GraphServiceClient 6 7from authentik.endpoints.controller import BaseController, Capabilities 8from authentik.endpoints.facts import OSFamily 9from authentik.endpoints.models import Device, DeviceConnection 10from authentik.enterprise.endpoints.connectors.microsoft_intune.models import ( 11 MicrosoftIntuneConnector, 12) 13from authentik.enterprise.providers.microsoft_entra.clients.base import entity_as_dict 14from authentik.policies.utils import delete_none_values 15 16_OS_FAMILY_MAP: dict[str, OSFamily] = { 17 "windows": OSFamily.windows, 18 "ios": OSFamily.iOS, 19 "macos": OSFamily.macOS, 20 "mac os x": OSFamily.macOS, 21 "android": OSFamily.android, 22 "linux": OSFamily.linux, 23 "chromeos": OSFamily.linux, 24} 25 26 27class MicrosoftIntuneController(BaseController): 28 29 def __init__(self, connector: MicrosoftIntuneConnector): 30 super().__init__(connector) 31 self.credentials = connector.microsoft_credentials() 32 33 @staticmethod 34 def vendor_identifier() -> str: 35 return "intune.microsoft.com" 36 37 def capabilities(self) -> list[Capabilities]: 38 return [Capabilities.ENROLL_AUTOMATIC_API, Capabilities.STAGE_ENDPOINTS] 39 40 def client(self): 41 return GraphServiceClient(**self.credentials) 42 43 def sync_endpoints(self): 44 client = self.client() 45 devices = run(client.device_management.managed_devices.get()) 46 next_link = True 47 while next_link: 48 for dev in devices.value: 49 serial = dev.serial_number or dev.id 50 device = Device.get_or_create( 51 identifier=serial, name=dev.device_name, defaults={"expiring": False} 52 ) 53 connection, _ = DeviceConnection.objects.update_or_create( 54 device=device, 55 connector=self.connector, 56 ) 57 connection.create_snapshot(self.map_device_data(dev)) 58 next_link = devices.odata_next_link 59 if not next_link: 60 break 61 devices = run(client.device_management.managed_devices.with_url(next_link).get()) 62 63 @staticmethod 64 def _os_family(operating_system: str | None) -> OSFamily: 65 if not operating_system: 66 return OSFamily.other 67 return _OS_FAMILY_MAP.get(operating_system.lower(), OSFamily.other) 68 69 def map_device_data(self, device: ManagedDevice) -> dict[str, Any]: 70 os_family = self._os_family(device.operating_system) 71 return { 72 "os": delete_none_values( 73 { 74 "version": device.os_version, 75 "name": device.operating_system, 76 "family": os_family, 77 } 78 ), 79 "disks": [ 80 # 'virtual' disk, intune doesn't give us per disk info, just a general 81 # `is_encrypted` field 82 { 83 "name": f"virtual@{self.vendor_identifier()}", 84 "mountpoint": "/", 85 "encryption_enabled": device.is_encrypted, 86 }, 87 ], 88 "network": delete_none_values({"hostname": device.device_name, "interfaces": []}), 89 "hardware": delete_none_values( 90 { 91 "model": device.model, 92 "manufacturer": device.manufacturer, 93 "serial": device.serial_number, 94 "memory_bytes": device.physical_memory_in_bytes, 95 } 96 ), 97 "software": [], 98 "vendor": { 99 self.vendor_identifier(): entity_as_dict(device), 100 }, 101 }
class
MicrosoftIntuneController(typing.Generic[T]):
28class MicrosoftIntuneController(BaseController): 29 30 def __init__(self, connector: MicrosoftIntuneConnector): 31 super().__init__(connector) 32 self.credentials = connector.microsoft_credentials() 33 34 @staticmethod 35 def vendor_identifier() -> str: 36 return "intune.microsoft.com" 37 38 def capabilities(self) -> list[Capabilities]: 39 return [Capabilities.ENROLL_AUTOMATIC_API, Capabilities.STAGE_ENDPOINTS] 40 41 def client(self): 42 return GraphServiceClient(**self.credentials) 43 44 def sync_endpoints(self): 45 client = self.client() 46 devices = run(client.device_management.managed_devices.get()) 47 next_link = True 48 while next_link: 49 for dev in devices.value: 50 serial = dev.serial_number or dev.id 51 device = Device.get_or_create( 52 identifier=serial, name=dev.device_name, defaults={"expiring": False} 53 ) 54 connection, _ = DeviceConnection.objects.update_or_create( 55 device=device, 56 connector=self.connector, 57 ) 58 connection.create_snapshot(self.map_device_data(dev)) 59 next_link = devices.odata_next_link 60 if not next_link: 61 break 62 devices = run(client.device_management.managed_devices.with_url(next_link).get()) 63 64 @staticmethod 65 def _os_family(operating_system: str | None) -> OSFamily: 66 if not operating_system: 67 return OSFamily.other 68 return _OS_FAMILY_MAP.get(operating_system.lower(), OSFamily.other) 69 70 def map_device_data(self, device: ManagedDevice) -> dict[str, Any]: 71 os_family = self._os_family(device.operating_system) 72 return { 73 "os": delete_none_values( 74 { 75 "version": device.os_version, 76 "name": device.operating_system, 77 "family": os_family, 78 } 79 ), 80 "disks": [ 81 # 'virtual' disk, intune doesn't give us per disk info, just a general 82 # `is_encrypted` field 83 { 84 "name": f"virtual@{self.vendor_identifier()}", 85 "mountpoint": "/", 86 "encryption_enabled": device.is_encrypted, 87 }, 88 ], 89 "network": delete_none_values({"hostname": device.device_name, "interfaces": []}), 90 "hardware": delete_none_values( 91 { 92 "model": device.model, 93 "manufacturer": device.manufacturer, 94 "serial": device.serial_number, 95 "memory_bytes": device.physical_memory_in_bytes, 96 } 97 ), 98 "software": [], 99 "vendor": { 100 self.vendor_identifier(): entity_as_dict(device), 101 }, 102 }
Abstract base class for generic types.
On Python 3.12 and newer, generic classes implicitly inherit from Generic when they declare a parameter list after the class's name::
class Mapping[KT, VT]:
def __getitem__(self, key: KT) -> VT:
...
# Etc.
On older versions of Python, however, generic classes have to explicitly inherit from Generic.
After a class has been declared to be generic, it can then be used as follows::
def lookup_name[KT, VT](mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
try:
return mapping[key]
except KeyError:
return default
MicrosoftIntuneController( connector: authentik.enterprise.endpoints.connectors.microsoft_intune.models.MicrosoftIntuneConnector)
def
sync_endpoints(self):
44 def sync_endpoints(self): 45 client = self.client() 46 devices = run(client.device_management.managed_devices.get()) 47 next_link = True 48 while next_link: 49 for dev in devices.value: 50 serial = dev.serial_number or dev.id 51 device = Device.get_or_create( 52 identifier=serial, name=dev.device_name, defaults={"expiring": False} 53 ) 54 connection, _ = DeviceConnection.objects.update_or_create( 55 device=device, 56 connector=self.connector, 57 ) 58 connection.create_snapshot(self.map_device_data(dev)) 59 next_link = devices.odata_next_link 60 if not next_link: 61 break 62 devices = run(client.device_management.managed_devices.with_url(next_link).get())
def
map_device_data( self, device: msgraph.generated.models.managed_device.ManagedDevice) -> dict[str, typing.Any]:
70 def map_device_data(self, device: ManagedDevice) -> dict[str, Any]: 71 os_family = self._os_family(device.operating_system) 72 return { 73 "os": delete_none_values( 74 { 75 "version": device.os_version, 76 "name": device.operating_system, 77 "family": os_family, 78 } 79 ), 80 "disks": [ 81 # 'virtual' disk, intune doesn't give us per disk info, just a general 82 # `is_encrypted` field 83 { 84 "name": f"virtual@{self.vendor_identifier()}", 85 "mountpoint": "/", 86 "encryption_enabled": device.is_encrypted, 87 }, 88 ], 89 "network": delete_none_values({"hostname": device.device_name, "interfaces": []}), 90 "hardware": delete_none_values( 91 { 92 "model": device.model, 93 "manufacturer": device.manufacturer, 94 "serial": device.serial_number, 95 "memory_bytes": device.physical_memory_in_bytes, 96 } 97 ), 98 "software": [], 99 "vendor": { 100 self.vendor_identifier(): entity_as_dict(device), 101 }, 102 }