Connecting to GCP Redis instance with AUTH & TLS enabled using cloud functions (python)

311 Views Asked by At

I want to connect to the redis instance from a flask app which is using cloud functions.

I'm using StrictRedis, and I have stored the redis_host, redis_port, redis_password and redis_cert as runtime variables of the cloud functions. I have tried the following solutions:

Approach 1:

redis_client = redis.StrictRedis(
                host=os.environ.get("redis_host"),
                port=os.environ.get("redis_port"),
                password=os.environ.get("redis_password"),
                ssl=True,
                ssl_cert_reqs=ssl.CERT_REQUIRED,
                ssl_ca_certs=os.environ.get("redis_cert"))

Approach 2:

ssl_context = ssl.SSLContext()
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context = ssl.load_verify_locations(cadata=os.environ.get("redis_cert").encode())
redis_client = redis.StrictRedis(
                host=os.environ.get("redis_host"),
                port=os.environ.get("redis_port"),
                password=os.environ.get("redis_password"),
                ssl=True,
                ssl_cert_reqs=ssl.CERT_REQUIRED,
                ssl_context=ssl_context)

Approach 3:

ssl_context = ssl.create_default_context(cadata=os.environ.get("redis_cert").encode())
redis_client = redis.StrictRedis(
                host=os.environ.get("redis_host"),
                port=os.environ.get("redis_port"),
                password=os.environ.get("redis_password"),
                ssl=True,
                ssl_cert_reqs=ssl.CERT_REQUIRED,
                ssl_context=ssl_context)

Approach 4:

Also tried downloading server-ca.pem file and passing it in cafile.

Error:
context.load_verify_locations(cafile, capath, cadata)
FileNotFoundError: [Errno 2] No such file or directory
0

There are 0 best solutions below