-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from alan-turing-institute/16-redis
Switch to Redis for ID caching
- Loading branch information
Showing
14 changed files
with
221 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .uid_cache import UidCache | ||
|
||
__all__ = [ | ||
"UidCache", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import cast | ||
|
||
import redis | ||
|
||
|
||
class UidCache: | ||
def __init__(self, redis_host: str, redis_port: str) -> None: | ||
self.redis_host = redis_host | ||
self.redis_port = redis_port | ||
self.cache_ = None | ||
|
||
@property | ||
def cache(self) -> redis.Redis: # type: ignore[type-arg] | ||
""" | ||
Lazy-load the cache on request | ||
""" | ||
if not self.cache_: | ||
self.cache_ = redis.Redis( # type: ignore[call-overload] | ||
host=self.redis_host, port=self.redis_port, decode_responses=True | ||
) | ||
return self.cache_ # type: ignore[return-value] | ||
|
||
@property | ||
def keys(self) -> list[str]: | ||
""" | ||
Get list of keys from the cache | ||
""" | ||
return [str(k) for k in self.cache.keys()] | ||
|
||
def get_group_uid(self, identifier: str) -> int: | ||
""" | ||
Get UID for a group, constructing one if necessary | ||
@param identifier: Identifier for group needing a UID | ||
""" | ||
return self.get_uid(identifier, category="group", min_value=3000) | ||
|
||
def get_user_uid(self, identifier: str) -> int: | ||
""" | ||
Get UID for a user, constructing one if necessary | ||
@param identifier: Identifier for user needing a UID | ||
""" | ||
return self.get_uid(identifier, category="user", min_value=2000) | ||
|
||
def get_uid( | ||
self, identifier: str, category: str, min_value: int | None = None | ||
) -> int: | ||
""" | ||
Get UID, constructing one if necessary. | ||
@param identifier: Identifier for object needing a UID | ||
@param category: Category the object belongs to | ||
@param min_value: Minimum allowed value for the UID | ||
""" | ||
identifier_ = f"{category}-{identifier}" | ||
uid = self.cache.get(identifier_) | ||
if not uid: | ||
min_value = min_value if min_value else 0 | ||
next_uid = max(self._get_max_uid(category) + 1, min_value) | ||
self.cache.set(identifier_, next_uid) | ||
return cast(int, self.cache.get(identifier_)) | ||
|
||
def _get_max_uid(self, category: str | None) -> int: | ||
""" | ||
Get maximum UID for a given category | ||
@param category: Category to check UIDs for | ||
""" | ||
if category: | ||
keys = [k for k in self.keys if k.startswith(category)] | ||
else: | ||
keys = self.keys | ||
values = [int(cast(str, v)) for v in self.cache.mget(keys)] + [-999] | ||
return max(values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .ldap_string import LDAPString # type: ignore[attr-defined] | ||
|
||
__all__ = [ | ||
"LDAPString", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Patch LDAPString to avoid TypeError when parsing LDAP filter strings""" | ||
|
||
from typing import Any | ||
|
||
from ldaptor.protocols.pureldap import LDAPString | ||
|
||
old_init = LDAPString.__init__ | ||
|
||
|
||
def patched_init(self, *args: Any, **kwargs: Any) -> None: # type: ignore[no-untyped-def] | ||
"""Patch LDAPString init to store its value as 'str' not 'bytes'""" | ||
old_init(self, *args, **kwargs) | ||
if isinstance(self.value, bytes): | ||
self.value = self.value.decode() | ||
|
||
|
||
LDAPString.__init__ = patched_init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.