From 39b4d27afb397696691b2a1cbb828c58934a010a Mon Sep 17 00:00:00 2001 From: James Robinson Date: Wed, 23 Oct 2024 14:56:26 +0100 Subject: [PATCH 1/4] :loud_sound: Log key errors --- apricot/oauth/keycloak_client.py | 12 ++++++++---- apricot/oauth/microsoft_entra_client.py | 5 +++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apricot/oauth/keycloak_client.py b/apricot/oauth/keycloak_client.py index 54ddaaa..bff945a 100644 --- a/apricot/oauth/keycloak_client.py +++ b/apricot/oauth/keycloak_client.py @@ -3,6 +3,8 @@ import operator from typing import Any, Self, cast +from twisted.python import log + from apricot.types import JSONDict from .oauth_client import OAuthClient @@ -99,8 +101,9 @@ def groups(self: Self) -> list[JSONDict]: user["username"] for user in cast(list[JSONDict], members) ] output.append(attributes) - except KeyError: - pass + except KeyError as exc: + msg = f"Failed to process group {group_dict} due to a missing key {exc}." + log.msg(msg) return output def users(self: Self) -> list[JSONDict]: @@ -170,6 +173,7 @@ def users(self: Self) -> list[JSONDict]: attributes["uid"] = username attributes["uidNumber"] = user_dict["attributes"]["uid"][0] output.append(attributes) - except KeyError: - pass + except KeyError as exc: + msg = f"Failed to process user {user_dict} due to a missing key {exc}." + log.msg(msg) return output diff --git a/apricot/oauth/microsoft_entra_client.py b/apricot/oauth/microsoft_entra_client.py index 0970347..6292f32 100644 --- a/apricot/oauth/microsoft_entra_client.py +++ b/apricot/oauth/microsoft_entra_client.py @@ -114,6 +114,7 @@ def users(self: Self) -> list[JSONDict]: attributes["uid"] = uid or None attributes["uidNumber"] = user_uid output.append(attributes) - except KeyError: - pass + except KeyError as exc: + msg = f"Failed to process user {user_dict} due to a missing key {exc}." + log.msg(msg) return output From 1774d7934c05a0543e41b549df9b827f18772d87 Mon Sep 17 00:00:00 2001 From: James Robinson Date: Wed, 23 Oct 2024 14:56:50 +0100 Subject: [PATCH 2/4] :loud_sound: Log authentication errors --- apricot/oauth/oauth_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apricot/oauth/oauth_client.py b/apricot/oauth/oauth_client.py index 34eddba..62effdc 100644 --- a/apricot/oauth/oauth_client.py +++ b/apricot/oauth/oauth_client.py @@ -162,8 +162,8 @@ def query_(*args: Any, **kwargs: Any) -> requests.Response: try: result = query_(*args, **kwargs) result.raise_for_status() - except (TokenExpiredError, requests.exceptions.HTTPError): - log.msg("Authentication token has expired.") + except (TokenExpiredError, requests.exceptions.HTTPError) as exc: + log.msg(f"Authentication token is invalid.\n{exc!s}") self.bearer_token_ = None result = query_(*args, **kwargs) if result.status_code == HTTPStatus.NO_CONTENT: @@ -181,7 +181,7 @@ def verify(self: Self, username: str, password: str) -> bool: client_secret=self.client_secret, ) except InvalidGrantError as exc: - log.msg(f"Authentication failed for user '{username}'.\n{exc}") + log.msg(f"Authentication failed for user '{username}'.\n{exc!s}") return False else: return True From d34a3cad5c820a5661f3f277a3894d95928ba44e Mon Sep 17 00:00:00 2001 From: James Robinson Date: Wed, 23 Oct 2024 19:58:19 +0100 Subject: [PATCH 3/4] :truck: Rename types to typedefs to avoid shadowing --- apricot/models/ldap_attribute_adaptor.py | 2 +- apricot/oauth/__init__.py | 2 +- apricot/oauth/keycloak_client.py | 2 +- apricot/oauth/microsoft_entra_client.py | 2 +- apricot/oauth/oauth_client.py | 2 +- apricot/oauth/oauth_data_adaptor.py | 2 +- apricot/{types.py => typedefs.py} | 0 pyproject.toml | 2 +- 8 files changed, 7 insertions(+), 7 deletions(-) rename apricot/{types.py => typedefs.py} (100%) diff --git a/apricot/models/ldap_attribute_adaptor.py b/apricot/models/ldap_attribute_adaptor.py index a559f9d..059a5d8 100644 --- a/apricot/models/ldap_attribute_adaptor.py +++ b/apricot/models/ldap_attribute_adaptor.py @@ -4,7 +4,7 @@ if TYPE_CHECKING: from apricot.models import LDAPObjectClass - from apricot.types import JSONDict, LDAPAttributeDict + from apricot.typedefs import JSONDict, LDAPAttributeDict class LDAPAttributeAdaptor: diff --git a/apricot/oauth/__init__.py b/apricot/oauth/__init__.py index c5d6268..3591f58 100644 --- a/apricot/oauth/__init__.py +++ b/apricot/oauth/__init__.py @@ -1,4 +1,4 @@ -from apricot.types import LDAPAttributeDict, LDAPControlTuple +from apricot.typedefs import LDAPAttributeDict, LDAPControlTuple from .enums import OAuthBackend from .keycloak_client import KeycloakClient diff --git a/apricot/oauth/keycloak_client.py b/apricot/oauth/keycloak_client.py index bff945a..660553d 100644 --- a/apricot/oauth/keycloak_client.py +++ b/apricot/oauth/keycloak_client.py @@ -5,7 +5,7 @@ from twisted.python import log -from apricot.types import JSONDict +from apricot.typedefs import JSONDict from .oauth_client import OAuthClient diff --git a/apricot/oauth/microsoft_entra_client.py b/apricot/oauth/microsoft_entra_client.py index 6292f32..3444d92 100644 --- a/apricot/oauth/microsoft_entra_client.py +++ b/apricot/oauth/microsoft_entra_client.py @@ -5,7 +5,7 @@ from twisted.python import log -from apricot.types import JSONDict +from apricot.typedefs import JSONDict from .oauth_client import OAuthClient diff --git a/apricot/oauth/oauth_client.py b/apricot/oauth/oauth_client.py index 62effdc..8536f46 100644 --- a/apricot/oauth/oauth_client.py +++ b/apricot/oauth/oauth_client.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: from apricot.cache import UidCache - from apricot.types import JSONDict + from apricot.typedefs import JSONDict class OAuthClient(ABC): diff --git a/apricot/oauth/oauth_data_adaptor.py b/apricot/oauth/oauth_data_adaptor.py index 0545319..274190f 100644 --- a/apricot/oauth/oauth_data_adaptor.py +++ b/apricot/oauth/oauth_data_adaptor.py @@ -18,7 +18,7 @@ if TYPE_CHECKING: - from apricot.types import JSONDict + from apricot.typedefs import JSONDict from .oauth_client import OAuthClient diff --git a/apricot/types.py b/apricot/typedefs.py similarity index 100% rename from apricot/types.py rename to apricot/typedefs.py diff --git a/pyproject.toml b/pyproject.toml index 3fdf026..319d97b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,7 @@ detached = true dependencies = [ "black~=24.2", "mypy~=1.8", - "ruff~=0.3", + "ruff~=0.7", "types-oauthlib~=3.2", "types-redis~=4.6", "types-requests~=2.31", From 17da39d51852a691615862937a2e8379bad67121 Mon Sep 17 00:00:00 2001 From: James Robinson Date: Wed, 23 Oct 2024 19:58:38 +0100 Subject: [PATCH 4/4] :coffin: Drop unused JSONKey --- apricot/typedefs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apricot/typedefs.py b/apricot/typedefs.py index 5cc0617..e93f9ea 100644 --- a/apricot/typedefs.py +++ b/apricot/typedefs.py @@ -1,6 +1,5 @@ from typing import Any JSONDict = dict[str, Any] -JSONKey = list[Any] | dict[str, Any] | Any LDAPAttributeDict = dict[str, list[str]] LDAPControlTuple = tuple[str, bool, Any]