Skip to content

Commit

Permalink
Add authentication for Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Ledez committed Sep 18, 2024
1 parent 2d105c1 commit 12951e0
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
7 changes: 6 additions & 1 deletion zou/app/blueprints/index/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_status(self):
host=config.KEY_VALUE_STORE["host"],
port=config.KEY_VALUE_STORE["port"],
db=config.AUTH_TOKEN_BLACKLIST_KV_INDEX,
password=config.KEY_VALUE_STORE["password"],
decode_responses=True,
)
store.get("test")
Expand All @@ -64,7 +65,11 @@ def get_status(self):
host = config.KEY_VALUE_STORE["host"]
port = config.KEY_VALUE_STORE["port"]
db = config.KV_JOB_DB_INDEX
url = "redis://%s:%s/%s" % (host, port, db)
password = (config.KEY_VALUE_STORE["password"],)
if password:
url = "redis://:%s@%s:%s/%s" % (password, host, port, db)
else:
url = "redis://%s:%s/%s" % (host, port, db)
args = ["rq", "info", "--url", url]
out = shell.run_command(args)
is_jq_up = b"0 workers" not in out
Expand Down
1 change: 1 addition & 0 deletions zou/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
KEY_VALUE_STORE = {
"host": os.getenv("KV_HOST", "localhost"),
"port": os.getenv("KV_PORT", "6379"),
"password": os.getenv("KV_PASSWORD", None),
}
AUTH_TOKEN_BLACKLIST_KV_INDEX = 0
MEMOIZE_DB_INDEX = 1
Expand Down
1 change: 1 addition & 0 deletions zou/app/stores/auth_tokens_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
host=config.KEY_VALUE_STORE["host"],
port=config.KEY_VALUE_STORE["port"],
db=config.AUTH_TOKEN_BLACKLIST_KV_INDEX,
password=config.KEY_VALUE_STORE["password"],
decode_responses=True,
)
revoked_tokens_store.ping()
Expand Down
12 changes: 10 additions & 2 deletions zou/app/stores/publisher_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
host = config.KEY_VALUE_STORE["host"]
port = config.KEY_VALUE_STORE["port"]
redis_db = config.KV_EVENTS_DB_INDEX
redis_url = "redis://%s:%s/%s" % (host, port, redis_db)
password = config.KEY_VALUE_STORE["password"]
if password:
redis_url = "redis://:%s@%s:%s/%s" % (password, host, port, redis_db)
else:
redis_url = "redis://%s:%s/%s" % (host, port, redis_db)

socketio = None

Expand All @@ -27,7 +31,11 @@ def init():

try:
publisher_store = redis.StrictRedis(
host=host, port=port, db=redis_db, decode_responses=True
host=host,
port=port,
db=redis_db,
password=password,
decode_responses=True,
)
publisher_store.get("test")
socketio = SocketIO(
Expand Down
1 change: 1 addition & 0 deletions zou/app/stores/queue_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
host=config.KEY_VALUE_STORE["host"],
port=config.KEY_VALUE_STORE["port"],
db=config.KV_JOB_DB_INDEX,
password=config.KEY_VALUE_STORE["password"],
decode_responses=True,
)
queue_store.get("test")
Expand Down
2 changes: 2 additions & 0 deletions zou/app/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
host=config.KEY_VALUE_STORE["host"],
port=config.KEY_VALUE_STORE["port"],
db=config.MEMOIZE_DB_INDEX,
password=config.KEY_VALUE_STORE["password"],
decode_responses=True,
)
redis_cache.get("test")
Expand All @@ -26,6 +27,7 @@
"CACHE_REDIS_HOST": config.KEY_VALUE_STORE["host"],
"CACHE_REDIS_PORT": config.KEY_VALUE_STORE["port"],
"CACHE_REDIS_DB": config.MEMOIZE_DB_INDEX,
"CACHE_REDIS_PASSWORD": config.KEY_VALUE_STORE["password"],
}
)

Expand Down
11 changes: 10 additions & 1 deletion zou/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ def get_redis_url():
redis_host = config.KEY_VALUE_STORE["host"]
redis_port = config.KEY_VALUE_STORE["port"]
db_index = config.KV_EVENTS_DB_INDEX
return "redis://%s:%s/%s" % (redis_host, redis_port, db_index)
redis_password = config.KEY_VALUE_STORE["password"]
if redis_password:
return "redis://:%s@%s:%s/%s" % (
redis_password,
redis_host,
redis_port,
db_index,
)
else:
return "redis://%s:%s/%s" % (redis_host, redis_port, db_index)


# Routes
Expand Down
1 change: 1 addition & 0 deletions zou/job_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
REDIS_HOST = config.KEY_VALUE_STORE["host"]
REDIS_PORT = config.KEY_VALUE_STORE["port"]
REDIS_DB = config.KV_JOB_DB_INDEX
REDIS_PASSWORD = config.KEY_VALUE_STORE["password"]

0 comments on commit 12951e0

Please sign in to comment.