Redis user permissions (ACL) and python connection
Recently, I was working on setting up a redis cluster along with separate users for readwrite and readonly. I setup the readonly user using the below privileges (ACLs):
user redisreadonly on >mySuperSecretPassword ~* resetchannels -@all +@read +ping +asking
Once this is setup, I wrote a simple program in python to connect to the redis cluster using the readonly credentials and print the number of keys.
#!/usr/bin/env python3
"""
pip3 install redis
"""
from redis.cluster import RedisCluster, ClusterNode
# ─────── Cluster connection ───────
startup_nodes = [
ClusterNode("redis_host_1", 6379),
ClusterNode("redis_host_2", 6379),
ClusterNode("redis_host_3", 6379)
]
rc = RedisCluster(startup_nodes=startup_nodes,decode_responses=True,username='redisreadonly',password='mySuperSecretPassword')
if rc.ping():
print("Total keys BEFORE write: ", rc.dbsize(), "\n")
else:
print("Unable to connect to redis cluster. Do investigate why.\n")
However, when executed, the program was erroring out with the below error. Upon investigation, it was failing at the client definition statement (rc = ).
redis.exceptions.NoPermissionError: this user has no permissions to run the 'command' command
A google search / ChatGPT query didn't return useful results. In fact, ChatGPT pointed me in a wrong direction and I was unable to resolve this error. That's when I tried to take the "bull by the horns", so to speak, and tackle the error head-on, by assigning the permission on "command" to the readonly user. So, this is what I did to resolve the issue:
ACL SETUSER redisreadonly on >mySuperSecretPassword ~* resetchannels -@all +@read +ping +asking +cluster|slots +command
A simple solution indeed but it took me a few tries and a couple of hours to figure out. So, putting it out here so that it helps someone.
Comments