2024-03-19 02:46:33 +01:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2020-09-05 04:02:13 +02:00
|
|
|
import secrets
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any
|
2014-02-05 00:35:32 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2014-02-05 00:35:32 +01:00
|
|
|
import redis
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2020-01-26 17:01:23 +01:00
|
|
|
# Redis accepts keys up to 512MB in size, but there's no reason for us to use such size,
|
|
|
|
# so we want to stay limited to 1024 characters.
|
|
|
|
MAX_KEY_LENGTH = 1024
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 19:01:56 +01:00
|
|
|
class ZulipRedisError(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 19:01:56 +01:00
|
|
|
class ZulipRedisKeyTooLongError(ZulipRedisError):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 19:01:56 +01:00
|
|
|
class ZulipRedisKeyOfWrongFormatError(ZulipRedisError):
|
2020-01-26 17:01:23 +01:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-01-26 22:14:36 +01:00
|
|
|
def get_redis_client() -> "redis.StrictRedis[bytes]":
|
2021-02-12 08:19:30 +01:00
|
|
|
return redis.StrictRedis(
|
|
|
|
host=settings.REDIS_HOST,
|
|
|
|
port=settings.REDIS_PORT,
|
|
|
|
password=settings.REDIS_PASSWORD,
|
|
|
|
db=0,
|
|
|
|
decode_responses=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def put_dict_in_redis(
|
|
|
|
redis_client: "redis.StrictRedis[bytes]",
|
|
|
|
key_format: str,
|
|
|
|
data_to_store: Mapping[str, Any],
|
|
|
|
expiration_seconds: int,
|
|
|
|
token_length: int = 64,
|
2024-07-12 02:30:23 +02:00
|
|
|
token: str | None = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
key_length = len(key_format) - len("{token}") + token_length
|
2020-01-26 17:01:23 +01:00
|
|
|
if key_length > MAX_KEY_LENGTH:
|
2021-01-26 20:40:34 +01:00
|
|
|
raise ZulipRedisKeyTooLongError(
|
|
|
|
f"Requested key too long in put_dict_in_redis. Key format: {key_format}, token length: {token_length}",
|
|
|
|
)
|
2020-04-26 23:00:54 +02:00
|
|
|
if token is None:
|
2020-09-05 04:02:13 +02:00
|
|
|
token = secrets.token_hex(token_length // 2)
|
2020-01-26 17:01:23 +01:00
|
|
|
key = key_format.format(token=token)
|
2020-01-20 14:17:53 +01:00
|
|
|
with redis_client.pipeline() as pipeline:
|
2020-08-07 01:09:47 +02:00
|
|
|
pipeline.set(key, orjson.dumps(data_to_store))
|
2020-01-20 14:17:53 +01:00
|
|
|
pipeline.expire(key, expiration_seconds)
|
|
|
|
pipeline.execute()
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def get_dict_from_redis(
|
|
|
|
redis_client: "redis.StrictRedis[bytes]",
|
|
|
|
key_format: str,
|
|
|
|
key: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
) -> dict[str, Any] | None:
|
2020-01-26 19:01:56 +01:00
|
|
|
# This function requires inputting the intended key_format to validate
|
|
|
|
# that the key fits it, as an additionally security measure. This protects
|
|
|
|
# against bugs where a caller requests a key based on user input and doesn't
|
2020-10-23 02:43:28 +02:00
|
|
|
# validate it - which could potentially allow users to poke around arbitrary Redis keys.
|
2020-01-26 17:01:23 +01:00
|
|
|
if len(key) > MAX_KEY_LENGTH:
|
2021-01-26 20:40:34 +01:00
|
|
|
raise ZulipRedisKeyTooLongError(
|
|
|
|
f"Requested key too long in get_dict_from_redis: {key}",
|
|
|
|
)
|
2020-01-26 19:01:56 +01:00
|
|
|
validate_key_fits_format(key, key_format)
|
|
|
|
|
2020-01-20 14:17:53 +01:00
|
|
|
data = redis_client.get(key)
|
|
|
|
if data is None:
|
|
|
|
return None
|
2020-08-07 01:09:47 +02:00
|
|
|
return orjson.loads(data)
|
2020-01-26 19:01:56 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 19:01:56 +01:00
|
|
|
def validate_key_fits_format(key: str, key_format: str) -> None:
|
|
|
|
assert "{token}" in key_format
|
2020-04-26 23:00:54 +02:00
|
|
|
regex = key_format.format(token=r"[a-zA-Z0-9]+")
|
2020-01-26 19:01:56 +01:00
|
|
|
|
|
|
|
if not re.fullmatch(regex, key):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise ZulipRedisKeyOfWrongFormatError(f"{key} does not match format {key_format}")
|
2024-03-19 02:46:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
REDIS_KEY_PREFIX = ""
|
|
|
|
|
|
|
|
|
|
|
|
def bounce_redis_key_prefix_for_testing(test_name: str) -> None:
|
|
|
|
global REDIS_KEY_PREFIX
|
|
|
|
REDIS_KEY_PREFIX = test_name + ":" + str(os.getpid()) + ":"
|