2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2020-01-20 14:17:53 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2014-02-05 00:35:32 +01:00
|
|
|
|
|
|
|
import redis
|
2020-01-20 14:17:53 +01:00
|
|
|
import ujson
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from zerver.lib.utils import generate_random_token
|
2014-02-05 00:35:32 +01:00
|
|
|
|
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
|
|
|
|
|
2020-01-26 19:01:56 +01:00
|
|
|
class ZulipRedisError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ZulipRedisKeyTooLongError(ZulipRedisError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ZulipRedisKeyOfWrongFormatError(ZulipRedisError):
|
2020-01-26 17:01:23 +01:00
|
|
|
pass
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_redis_client() -> redis.StrictRedis:
|
2016-08-01 04:51:00 +02:00
|
|
|
return redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,
|
|
|
|
password=settings.REDIS_PASSWORD, db=0)
|
2020-01-20 14:17:53 +01:00
|
|
|
|
|
|
|
def put_dict_in_redis(redis_client: redis.StrictRedis, key_format: str,
|
|
|
|
data_to_store: Dict[str, Any],
|
2020-01-23 12:21:55 +01:00
|
|
|
expiration_seconds: int,
|
2020-04-26 23:00:54 +02:00
|
|
|
token_length: int=64,
|
|
|
|
token: Optional[str]=None) -> str:
|
2020-01-26 17:01:23 +01:00
|
|
|
key_length = len(key_format) - len('{token}') + token_length
|
|
|
|
if key_length > MAX_KEY_LENGTH:
|
|
|
|
error_msg = "Requested key too long in put_dict_in_redis. Key format: %s, token length: %s"
|
|
|
|
raise ZulipRedisKeyTooLongError(error_msg % (key_format, token_length))
|
2020-04-26 23:00:54 +02:00
|
|
|
if token is None:
|
|
|
|
token = generate_random_token(token_length)
|
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:
|
|
|
|
pipeline.set(key, ujson.dumps(data_to_store))
|
|
|
|
pipeline.expire(key, expiration_seconds)
|
|
|
|
pipeline.execute()
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
def get_dict_from_redis(redis_client: redis.StrictRedis, key_format: str, key: str,
|
2020-01-26 19:01:56 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
# 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
|
|
|
|
# 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:
|
|
|
|
error_msg = "Requested key too long in get_dict_from_redis: %s"
|
|
|
|
raise ZulipRedisKeyTooLongError(error_msg % (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
|
|
|
|
return ujson.loads(data)
|
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}")
|