|
| 1 | +from datetime import datetime |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from descope._auth_base import AuthBase |
| 5 | +from descope.management.common import MgmtV1 |
| 6 | + |
| 7 | + |
| 8 | +class Audit(AuthBase): |
| 9 | + def search( |
| 10 | + self, |
| 11 | + user_ids: List[str] = None, |
| 12 | + actions: List[str] = None, |
| 13 | + excluded_actions: List[str] = None, |
| 14 | + devices: List[str] = None, |
| 15 | + methods: List[str] = None, |
| 16 | + geos: List[str] = None, |
| 17 | + remote_addresses: List[str] = None, |
| 18 | + login_ids: List[str] = None, |
| 19 | + tenants: List[str] = None, |
| 20 | + no_tenants: bool = False, |
| 21 | + text: str = None, |
| 22 | + from_ts: datetime = None, |
| 23 | + to_ts: datetime = None, |
| 24 | + ) -> dict: |
| 25 | + """ |
| 26 | + Search the audit trail up to last 30 days based on given parameters |
| 27 | +
|
| 28 | + Args: |
| 29 | + user_ids (List[str]): Optional list of user IDs to filter by |
| 30 | + actions (List[str]): Optional list of actions to filter by |
| 31 | + excluded_actions (List[str]): Optional list of actions to exclude |
| 32 | + devices (List[str]): Optional list of devices to filter by. Current devices supported are "Bot"/"Mobile"/"Desktop"/"Tablet"/"Unknown" |
| 33 | + methods (List[str]): Optional list of methods to filter by. Current auth methods are "otp"/"totp"/"magiclink"/"oauth"/"saml"/"password" |
| 34 | + geos (List[str]): Optional list of geos to filter by. Geo is currently country code like "US", "IL", etc. |
| 35 | + remote_addresses (List[str]): Optional list of remote addresses to filter by |
| 36 | + login_ids (List[str]): Optional list of login IDs to filter by |
| 37 | + tenants (List[str]): Optional list of tenants to filter by |
| 38 | + no_tenants (bool): Should audits without any tenants always be included |
| 39 | + text (str): Free text search across all fields |
| 40 | + from_ts (datetime): Retrieve records newer than given time but not older than 30 days |
| 41 | + to_ts (datetime): Retrieve records older than given time |
| 42 | +
|
| 43 | + Return value (dict): |
| 44 | + Return dict in the format |
| 45 | + { |
| 46 | + "audits": [ |
| 47 | + { |
| 48 | + "projectId":"", |
| 49 | + "userId": "", |
| 50 | + "action": "", |
| 51 | + "occurred": 0 (unix-time-milli), |
| 52 | + "device": "", |
| 53 | + "method": "", |
| 54 | + "geo": "", |
| 55 | + "remoteAddress": "", |
| 56 | + "externalIds": [""], |
| 57 | + "tenants": [""], |
| 58 | + "data": { |
| 59 | + "field1": "field1-value", |
| 60 | + "more-details": "in-console-examples" |
| 61 | + } |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + Raise: |
| 66 | + AuthException: raised if search operation fails |
| 67 | + """ |
| 68 | + body = {"noTenants": no_tenants} |
| 69 | + if user_ids is not None: |
| 70 | + body["userIds"] = user_ids |
| 71 | + if actions is not None: |
| 72 | + body["actions"] = actions |
| 73 | + if excluded_actions is not None: |
| 74 | + body["excludedActions"] = excluded_actions |
| 75 | + if devices is not None: |
| 76 | + body["devices"] = devices |
| 77 | + if methods is not None: |
| 78 | + body["methods"] = methods |
| 79 | + if geos is not None: |
| 80 | + body["geos"] = geos |
| 81 | + if remote_addresses is not None: |
| 82 | + body["remoteAddresses"] = remote_addresses |
| 83 | + if login_ids is not None: |
| 84 | + body["externalIds"] = login_ids |
| 85 | + if tenants is not None: |
| 86 | + body["tenants"] = tenants |
| 87 | + if text is not None: |
| 88 | + body["text"] = text |
| 89 | + if from_ts is not None: |
| 90 | + body["from"] = from_ts.timestamp * 1000 |
| 91 | + if to_ts is not None: |
| 92 | + body["to"] = to_ts.timestamp * 1000 |
| 93 | + |
| 94 | + response = self._auth.do_post( |
| 95 | + MgmtV1.audit_search, |
| 96 | + body=body, |
| 97 | + pswd=self._auth.management_key, |
| 98 | + ) |
| 99 | + return { |
| 100 | + "audits": list(map(Audit._convert_audit_record, response.json()["audits"])) |
| 101 | + } |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def _convert_audit_record(a: dict) -> dict: |
| 105 | + return { |
| 106 | + "projectId": a.get("projectId", ""), |
| 107 | + "userId": a.get("userId", ""), |
| 108 | + "action": a.get("action", ""), |
| 109 | + "occurred": datetime.utcfromtimestamp(float(a.get("occurred", "0")) / 1000), |
| 110 | + "device": a.get("device", ""), |
| 111 | + "method": a.get("method", ""), |
| 112 | + "geo": a.get("geo", ""), |
| 113 | + "remoteAddress": a.get("remoteAddress", ""), |
| 114 | + "loginIds": a.get("externalIds", []), |
| 115 | + "tenants": a.get("tenants", []), |
| 116 | + "data": a.get("data", {}), |
| 117 | + } |
0 commit comments