iTranslated by AI
How to connect to security-enabled Memorystore for Redis from Ruby
Introduction
This article introduces how to connect to a Memorystore for Redis instance with security options enabled from Ruby.
There are three types of security options for Memorystore for Redis:
Of these, I will briefly introduce Redis AUTH and In-transit encryption, which are related to client connections, and show how to connect from Ruby when each is enabled.
We will use the redis gem as the Ruby Redis client.
Sample
I have prepared a sample to try out the contents of this article.
This sample involves accessing Memorystore for Redis instances configured with four different settings from Cloud Run.
The included Terraform code also contains network settings required for connecting from Cloud Run to Redis instances (Private Service Access and Serverless VPC Access), as well as code to set the AUTH string and CA in Secret Manager.
Redis AUTH
This is authentication using the Redis AUTH command. It is essentially password authentication.
You can create an instance with AUTH enabled as follows:
gcloud redis instances create auth-instance \
--region asia-northeast1 \
--enable-auth
In Memorystore for Redis, when AUTH is enabled on an instance, a UUID-formatted AUTH string is generated. The client authenticates using that AUTH string.
You can obtain the AUTH string with the following command. It can also be checked in the Web Console.
gcloud redis instances get-auth-string auth-instance \
--region asia-northeast1
Here is the Ruby code to connect to this instance:
require "redis"
redis = Redis.new(
host: "10.232.151.35", # Instance endpoint
password: "8af4db94-887a-438f-bc9f-97877848e4ea", # AUTH string
)
Alternatively, you can do it like this:
redis = Redis.new(host: "10.232.151.35")
redis.call("AUTH", "8af4db94-887a-438f-bc9f-97877848e4ea")
In-transit encryption
Enabling in-transit encryption encrypts all traffic between Redis and the client using TLS.
You can create an instance with in-transit encryption enabled using the following command:
gcloud redis instances create tls-instance \
--region asia-northeast1 \
--transit-encryption-mode server-authentication
Redis instances with in-transit encryption enabled have a Certificate Authority (CA) used for server verification. The client must use this CA to verify the server certificate.
Note that when in-transit encryption is enabled, the CA certificate's expiration is set to 10 years, and there are connection limits.[1]
You can check the CA certificate with the following command:
gcloud redis instances describe tls-instance \
--region asia-northeast1 \
--format json \
| jq -r ".serverCaCerts[].cert"
Example:
❯ gcloud redis instances describe tls-instance \
--region asia-northeast1 \
--format json \
| jq -r ".serverCaCerts[].cert"
-----BEGIN CERTIFICATE-----
MI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ycxFNH7NDOzqzrNjQId++jRK0Xr/U7u
Ah/HK8JHs/pUPtPOgluMbduv8kUQqscD7SF7wkpl7ggNB5VUL0vdG4giv00aY4y0
Y/xoUK0KEqrtW+AohnmPXSf61APU3jJwC8EFRlqZ9FYIF+Tgapbjc3maYDmZdTEX
FFkNYATunv34hWpawTtsKvw=
-----END CERTIFICATE-----
By saving this as a file, for example at /path/to/redis-ca.pem, you can connect to the Redis instance using the following Ruby code:
redis = Redis.new(
host: "10.226.48.11",
port: 6378,
ssl: true,
ssl_params: { ca_file: "/path/to/redis-ca.pem" },
)
Note that SSL parameters are required and the port number changes.
When both AUTH and In-transit Encryption are configured
It will look like this:
redis = Redis.new(
host: "10.226.48.11",
port: 6378,
password: "8af4db94-887a-438f-bc9f-97877848e4ea",
ssl: true,
ssl_params: { ca_file: "/path/to/redis-ca.pem" },
)
Conclusion
In this article, I have only excerpted the necessary parts, but I think it will be easier to understand the overall picture intended for actual use by looking at the sample.
Discussion