Skip to content

Commit

Permalink
Override flushdb since it does not respect ACLs
Browse files Browse the repository at this point in the history
  • Loading branch information
john-westcott-iv committed Sep 24, 2024
1 parent 9fb3e1f commit 1f80a45
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions ansible_base/lib/redis/client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import copy
import logging
import os
from typing import Dict, Union
from typing import Any, Awaitable, Dict, Union
from urllib.parse import parse_qs, urlparse

from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext as _
from django_redis.client import DefaultClient
from redis import Redis
from redis.cluster import ClusterNode, RedisCluster
from redis.exceptions import RedisClusterException
from redis.exceptions import NoPermissionError, RedisClusterException

from ansible_base.lib.constants import STATUS_DEGRADED, STATUS_FAILED, STATUS_GOOD

Expand All @@ -20,11 +20,32 @@

_INVALID_STANDALONE_OPTIONS = ['cluster_error_retry_attempts']

# This will eventually be in the Redis package under redis.typing later
ResponseT = Union[Awaitable, Any]


class DABRedisRespectACLFlushMixin:
def flushdb(self, asynchronous: bool = None, **kwargs) -> ResponseT:
if asynchronous is not None:
logger.warning("DABRedis clients implement an ACL friendly FLUSHSB which can not be async")

if self.__class__ == DABRedisCluster:
all_keys = self.keys("*", target_nodes=self.ALL_NODES)
elif self.__class__ == DABRedis:
all_keys = self.keys("*")
else:
raise ImproperlyConfigured(f"Got an inappropriate type of client {self.__class__}")

try:
self.delete(*all_keys)
except NoPermissionError:
pass


# We are going to build our own cluster class to override the mget function
# In a redis cluster, keys might not be in the same slot and this will throw off mget.
# Instead, we are going to try and use mget and then, if we get the slot error, we will try the mget_nonatomic to make it work
class DABRedisCluster(RedisCluster):
class DABRedisCluster(DABRedisRespectACLFlushMixin, RedisCluster):
mode = 'cluster'

def mget(self, *args, **kwargs):
Expand All @@ -36,7 +57,7 @@ def mget(self, *args, **kwargs):
raise


class DABRedis(Redis):
class DABRedis(DABRedisRespectACLFlushMixin, Redis):
mode = 'standalone'


Expand Down

0 comments on commit 1f80a45

Please sign in to comment.