authentik.lib.sync.outgoing.exceptions

  1from json import JSONDecodeError
  2
  3from authentik.lib.tracing.exceptions import TracingIgnoredException
  4
  5
  6class BaseSyncException(TracingIgnoredException):
  7    """Base class for all sync exceptions"""
  8
  9    error_prefix = "Sync error"
 10    error_default = "Error communicating with remote system"
 11
 12    def __init__(self, response=None):
 13        super().__init__()
 14        self.response = response
 15
 16    def __str__(self):
 17        if self.response is not None:
 18            if hasattr(self.response, "json"):
 19                try:
 20                    return f"{self.error_prefix}: {self.response.json()}"
 21                except JSONDecodeError:
 22                    pass
 23            if hasattr(self.response, "text"):
 24                return f"{self.error_prefix}: {self.response.text}"
 25            return f"{self.error_prefix}: {self.response}"
 26        return self.error_default
 27
 28    def __repr__(self):
 29        return self.__str__()
 30
 31    def detail(self) -> str:
 32        """Get human readable details of this error"""
 33        return str(self)
 34
 35
 36class TransientSyncException(BaseSyncException):
 37    """Transient sync exception which may be caused by network blips, etc"""
 38
 39    error_prefix = "Network error"
 40    error_default = "Network error communicating with remote system"
 41
 42
 43class ObjectLockTimeout(TransientSyncException):
 44    """Timed out waiting to synchronize another change to the same object."""
 45
 46    error_prefix = "Object lock timeout"
 47    error_default = "Timed out waiting to synchronize object"
 48
 49
 50class NotFoundSyncException(BaseSyncException):
 51    """Exception when an object was not found in the remote system"""
 52
 53    error_prefix = "Object not found"
 54    error_default = "Object not found in remote system"
 55
 56
 57class ObjectExistsSyncException(BaseSyncException):
 58    """Exception when an object already exists in the remote system"""
 59
 60    error_prefix = "Object exists"
 61    error_default = "Object exists in remote system"
 62
 63    def detail(self) -> str:
 64        """Get human readable details of this error, including how to resolve it"""
 65        return (
 66            f"{self}. The remote system already has an object that conflicts with this one. "
 67            "Delete or rename the conflicting object in the remote system, or change the "
 68            "property mappings so the values that the remote system requires to be unique "
 69            "are different."
 70        )
 71
 72
 73class BadRequestSyncException(BaseSyncException):
 74    """Exception when invalid data was sent to the remote system"""
 75
 76    error_prefix = "Bad request"
 77    error_default = "Bad request to remote system"
 78
 79
 80class DryRunRejected(BaseSyncException):
 81    """When dry_run is enabled and a provider dropped a mutating request"""
 82
 83    def __init__(self, url: str, method: str, body: dict):
 84        super().__init__()
 85        self.url = url
 86        self.method = method
 87        self.body = body
 88
 89    def __repr__(self):
 90        return self.__str__()
 91
 92    def __str__(self):
 93        return f"Dry-run rejected request: {self.method} {self.url}"
 94
 95
 96class StopSync(BaseSyncException):
 97    """Exception raised when a configuration error should stop the sync process"""
 98
 99    def __init__(
100        self, exc: Exception, obj: object | None = None, mapping: object | None = None
101    ) -> None:
102        self.exc = exc
103        self.obj = obj
104        self.mapping = mapping
105
106    def detail(self) -> str:
107        """Get human readable details of this error"""
108        msg = f"Error {str(self.exc)}"
109        if self.obj:
110            msg += f", caused by {self.obj}"
111        if self.mapping:
112            msg += f" (mapping {self.mapping})"
113        return msg
114
115    def __str__(self):
116        return self.detail()
class BaseSyncException(authentik.lib.tracing.exceptions.TracingIgnoredException):
 7class BaseSyncException(TracingIgnoredException):
 8    """Base class for all sync exceptions"""
 9
10    error_prefix = "Sync error"
11    error_default = "Error communicating with remote system"
12
13    def __init__(self, response=None):
14        super().__init__()
15        self.response = response
16
17    def __str__(self):
18        if self.response is not None:
19            if hasattr(self.response, "json"):
20                try:
21                    return f"{self.error_prefix}: {self.response.json()}"
22                except JSONDecodeError:
23                    pass
24            if hasattr(self.response, "text"):
25                return f"{self.error_prefix}: {self.response.text}"
26            return f"{self.error_prefix}: {self.response}"
27        return self.error_default
28
29    def __repr__(self):
30        return self.__str__()
31
32    def detail(self) -> str:
33        """Get human readable details of this error"""
34        return str(self)

Base class for all sync exceptions

BaseSyncException(response=None)
13    def __init__(self, response=None):
14        super().__init__()
15        self.response = response
error_prefix = 'Sync error'
error_default = 'Error communicating with remote system'
response
def detail(self) -> str:
32    def detail(self) -> str:
33        """Get human readable details of this error"""
34        return str(self)

Get human readable details of this error

class TransientSyncException(BaseSyncException):
37class TransientSyncException(BaseSyncException):
38    """Transient sync exception which may be caused by network blips, etc"""
39
40    error_prefix = "Network error"
41    error_default = "Network error communicating with remote system"

Transient sync exception which may be caused by network blips, etc

error_prefix = 'Network error'
error_default = 'Network error communicating with remote system'
class ObjectLockTimeout(TransientSyncException):
44class ObjectLockTimeout(TransientSyncException):
45    """Timed out waiting to synchronize another change to the same object."""
46
47    error_prefix = "Object lock timeout"
48    error_default = "Timed out waiting to synchronize object"

Timed out waiting to synchronize another change to the same object.

error_prefix = 'Object lock timeout'
error_default = 'Timed out waiting to synchronize object'
class NotFoundSyncException(BaseSyncException):
51class NotFoundSyncException(BaseSyncException):
52    """Exception when an object was not found in the remote system"""
53
54    error_prefix = "Object not found"
55    error_default = "Object not found in remote system"

Exception when an object was not found in the remote system

error_prefix = 'Object not found'
error_default = 'Object not found in remote system'
class ObjectExistsSyncException(BaseSyncException):
58class ObjectExistsSyncException(BaseSyncException):
59    """Exception when an object already exists in the remote system"""
60
61    error_prefix = "Object exists"
62    error_default = "Object exists in remote system"
63
64    def detail(self) -> str:
65        """Get human readable details of this error, including how to resolve it"""
66        return (
67            f"{self}. The remote system already has an object that conflicts with this one. "
68            "Delete or rename the conflicting object in the remote system, or change the "
69            "property mappings so the values that the remote system requires to be unique "
70            "are different."
71        )

Exception when an object already exists in the remote system

error_prefix = 'Object exists'
error_default = 'Object exists in remote system'
def detail(self) -> str:
64    def detail(self) -> str:
65        """Get human readable details of this error, including how to resolve it"""
66        return (
67            f"{self}. The remote system already has an object that conflicts with this one. "
68            "Delete or rename the conflicting object in the remote system, or change the "
69            "property mappings so the values that the remote system requires to be unique "
70            "are different."
71        )

Get human readable details of this error, including how to resolve it

class BadRequestSyncException(BaseSyncException):
74class BadRequestSyncException(BaseSyncException):
75    """Exception when invalid data was sent to the remote system"""
76
77    error_prefix = "Bad request"
78    error_default = "Bad request to remote system"

Exception when invalid data was sent to the remote system

error_prefix = 'Bad request'
error_default = 'Bad request to remote system'
class DryRunRejected(BaseSyncException):
81class DryRunRejected(BaseSyncException):
82    """When dry_run is enabled and a provider dropped a mutating request"""
83
84    def __init__(self, url: str, method: str, body: dict):
85        super().__init__()
86        self.url = url
87        self.method = method
88        self.body = body
89
90    def __repr__(self):
91        return self.__str__()
92
93    def __str__(self):
94        return f"Dry-run rejected request: {self.method} {self.url}"

When dry_run is enabled and a provider dropped a mutating request

DryRunRejected(url: str, method: str, body: dict)
84    def __init__(self, url: str, method: str, body: dict):
85        super().__init__()
86        self.url = url
87        self.method = method
88        self.body = body
url
method
body
class StopSync(BaseSyncException):
 97class StopSync(BaseSyncException):
 98    """Exception raised when a configuration error should stop the sync process"""
 99
100    def __init__(
101        self, exc: Exception, obj: object | None = None, mapping: object | None = None
102    ) -> None:
103        self.exc = exc
104        self.obj = obj
105        self.mapping = mapping
106
107    def detail(self) -> str:
108        """Get human readable details of this error"""
109        msg = f"Error {str(self.exc)}"
110        if self.obj:
111            msg += f", caused by {self.obj}"
112        if self.mapping:
113            msg += f" (mapping {self.mapping})"
114        return msg
115
116    def __str__(self):
117        return self.detail()

Exception raised when a configuration error should stop the sync process

StopSync( exc: Exception, obj: object | None = None, mapping: object | None = None)
100    def __init__(
101        self, exc: Exception, obj: object | None = None, mapping: object | None = None
102    ) -> None:
103        self.exc = exc
104        self.obj = obj
105        self.mapping = mapping
exc
obj
mapping
def detail(self) -> str:
107    def detail(self) -> str:
108        """Get human readable details of this error"""
109        msg = f"Error {str(self.exc)}"
110        if self.obj:
111            msg += f", caused by {self.obj}"
112        if self.mapping:
113            msg += f" (mapping {self.mapping})"
114        return msg

Get human readable details of this error