Browse Source

Replace StrictRedis usages with Redis

As of version 3.0, the redis-py package no longer has a distinction
between its Redis and StrictRedis classes, and both behave the same
(StrictRedis is just an alias for Redis).

This would have continued working as-is, but we might as well switch it
back to the normal name now that StrictRedis doesn't have any benefit.
merge-requests/53/head
Deimos 6 years ago
parent
commit
03f32d44e5
  1. 4
      tildes/scripts/breached_passwords.py
  2. 4
      tildes/tests/conftest.py
  3. 8
      tildes/tildes/__init__.py
  4. 4
      tildes/tildes/lib/password.py
  5. 8
      tildes/tildes/lib/ratelimit.py

4
tildes/scripts/breached_passwords.py

@ -30,7 +30,7 @@ import subprocess
from typing import Any from typing import Any
import click import click
from redis import ResponseError, StrictRedis
from redis import Redis, ResponseError
from tildes.lib.password import ( from tildes.lib.password import (
BREACHED_PASSWORDS_BF_KEY, BREACHED_PASSWORDS_BF_KEY,
@ -38,7 +38,7 @@ from tildes.lib.password import (
) )
REDIS = StrictRedis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET)
REDIS = Redis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET)
def generate_redis_protocol(*elements: Any) -> str: def generate_redis_protocol(*elements: Any) -> str:

4
tildes/tests/conftest.py

@ -7,7 +7,7 @@ import os
from pyramid import testing from pyramid import testing
from pyramid.paster import get_app, get_appsettings from pyramid.paster import get_app, get_appsettings
from pytest import fixture from pytest import fixture
from redis import StrictRedis
from redis import Redis
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.engine.url import make_url from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@ -118,7 +118,7 @@ def overall_redis_session():
redis_modules = ["/opt/redis-cell/libredis_cell.so"] redis_modules = ["/opt/redis-cell/libredis_cell.so"]
with RedisServer() as temp_redis_server: with RedisServer() as temp_redis_server:
redis = StrictRedis(**temp_redis_server.dsn())
redis = Redis(**temp_redis_server.dsn())
for module in redis_modules: for module in redis_modules:
redis.execute_command("MODULE LOAD", module) redis.execute_command("MODULE LOAD", module)

8
tildes/tildes/__init__.py

@ -10,7 +10,7 @@ from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPTooManyRequests from pyramid.httpexceptions import HTTPTooManyRequests
from pyramid.registry import Registry from pyramid.registry import Registry
from pyramid.request import Request from pyramid.request import Request
from redis import StrictRedis
from redis import Redis
from webassets import Bundle from webassets import Bundle
from tildes.lib.ratelimit import RATE_LIMITED_ACTIONS, RateLimitResult from tildes.lib.ratelimit import RATE_LIMITED_ACTIONS, RateLimitResult
@ -90,10 +90,10 @@ def http_method_tween_factory(handler: Callable, registry: Registry) -> Callable
return method_override_tween return method_override_tween
def get_redis_connection(request: Request) -> StrictRedis:
"""Return a StrictRedis connection to the Redis server."""
def get_redis_connection(request: Request) -> Redis:
"""Return a connection to the Redis server."""
socket = request.registry.settings["redis.unix_socket_path"] socket = request.registry.settings["redis.unix_socket_path"]
return StrictRedis(unix_socket_path=socket)
return Redis(unix_socket_path=socket)
def is_safe_request_method(request: Request) -> bool: def is_safe_request_method(request: Request) -> bool:

4
tildes/tildes/lib/password.py

@ -5,7 +5,7 @@
from hashlib import sha1 from hashlib import sha1
from redis import ConnectionError, ResponseError, StrictRedis # noqa
from redis import ConnectionError, Redis, ResponseError # noqa
# unix socket path for redis server with the breached passwords bloom filter # unix socket path for redis server with the breached passwords bloom filter
BREACHED_PASSWORDS_REDIS_SOCKET = "/run/redis_breached_passwords/socket" BREACHED_PASSWORDS_REDIS_SOCKET = "/run/redis_breached_passwords/socket"
@ -16,7 +16,7 @@ BREACHED_PASSWORDS_BF_KEY = "breached_passwords_bloom"
def is_breached_password(password: str) -> bool: def is_breached_password(password: str) -> bool:
"""Return whether the password is in the breached-passwords list.""" """Return whether the password is in the breached-passwords list."""
redis = StrictRedis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET)
redis = Redis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET)
hashed = sha1(password.encode("utf-8")).hexdigest() hashed = sha1(password.encode("utf-8")).hexdigest()

8
tildes/tildes/lib/ratelimit.py

@ -8,7 +8,7 @@ from ipaddress import ip_address
from typing import Any, List, Optional, Sequence from typing import Any, List, Optional, Sequence
from pyramid.response import Response from pyramid.response import Response
from redis import StrictRedis
from redis import Redis
from tildes.lib.datetime import utc_now from tildes.lib.datetime import utc_now
@ -170,7 +170,7 @@ class RateLimitedAction:
max_burst: Optional[int] = None, max_burst: Optional[int] = None,
by_user: bool = True, by_user: bool = True,
by_ip: bool = True, by_ip: bool = True,
redis: Optional[StrictRedis] = None,
redis: Optional[Redis] = None,
): ):
"""Initialize the limits on a particular action. """Initialize the limits on a particular action.
@ -207,7 +207,7 @@ class RateLimitedAction:
self._redis = redis self._redis = redis
@property @property
def redis(self) -> StrictRedis:
def redis(self) -> Redis:
"""Return the redis connection.""" """Return the redis connection."""
if not self._redis: if not self._redis:
raise RateLimitError("No redis connection set") raise RateLimitError("No redis connection set")
@ -215,7 +215,7 @@ class RateLimitedAction:
return self._redis return self._redis
@redis.setter @redis.setter
def redis(self, redis_connection: StrictRedis) -> None:
def redis(self, redis_connection: Redis) -> None:
"""Set the redis connection.""" """Set the redis connection."""
self._redis = redis_connection self._redis = redis_connection

Loading…
Cancel
Save