ElastiCache Security Options

ElastiCache 提供多種安全性選項來提供使用者去使用,例如IAM來管理操作 ElastiCache 的權限、安全組(Security Group)來限制可以存取的網段或IP位置的設備、傳輸時加密(In-Transit) for Redis 來加密客戶端到服務器端之間的連線、或是靜態加密(At-Rest) for Redis 來加密備份的數據。

Jerry’s Notes
What’s next?
8 min readMar 24, 2022

--

■ IAM — Identity and access management in Amazon ElastiCache

Access to Amazon ElastiCache requires credentials that AWS can use to authenticate your requests. Those credentials must have permissions to access AWS resources, such as an ElastiCache cache cluster or an Amazon Elastic Compute Cloud (Amazon EC2) instance. The following sections provide details on how you can use AWS Identity and Access Management (IAM) and ElastiCache to help secure your resources by controlling who can access them.

ElastiCache 是 AWS 全託管的服務,就如同其他服務一樣,使用者可以使用 IAM/User/Role 來管理特定使用者的操作 AWS ElastiCache 權限的限制。

IAM Policy — 至少要的權限

VPC

The client needs to be in the same VPC or, alternatively, the customer can use a NAT instance to allow connections from outside (not recommended), or VPC peering (EC2 and EC in same region), Transit VPC (EC2 and EC in different region), VPN/Direct Connect (Outside AWS). ElastiCache.

只提供內網存取,而 Endpoint 解析出來的也只有內網IP位置(Private IP Address),所以使用者若要透過外網連接的話,只能透過 VPN/Direct Connect (Outside AWS) 或 NAT instance。但 ”十分不建議” 從外部網路來使用 ElastiCache 的服務,因為 ElastiCache 本身為內存型的服務(資料存在記憶體內存中),使用他的目的為的就是低延遲、快速存取,當透過外網連接的話,這樣的優勢就沒了,還不如直接使用RDS之類的服務,反而適為適合。

Security Group

An ElastiCache security group acts like a firewall, controlling network access to your cluster. By default, network access is turned off to your clusters. If you want your applications to access your cluster, you must explicitly enable access from hosts in specific Amazon EC2 security groups. Once ingress rules are configured, the same rules apply to all clusters associated with that security group.

使用者可以限制可以存取的網段或IP位置的設備,才能存取 ElastiCache port 11211 (Memcached) or 6379 (Redis)。但請注意,當使用 Security Group 去限制連接時,會受到機型大小本身的 ”連接跟蹤(connection track)” 的限制,所以大量的連線會受到影響,所以 ”建議使用長連接(connection pooling)” 來避免這樣的問題。

Network access control list (NACL)

It is an optional layer of security for your VPC that acts as a firewall for controlling traffic in and out of one or more subnets. You might set up network ACLs with rules similar to your security groups in order to add an additional layer of security to your VPC.

At-Rest Encryption for ElastiCache Redis

It is an optional feature to increase data security by encrypting on-disk data. When enabled on a replication group, it encrypts the following aspects. 1)Disk during sync, backup and swap operations. 2) Backups stored in Amazon S3.

Disk during sync, backup and swap operations
Backups stored in Amazon S3

透過 AWS Key Management Service (KMS) 將落盤的數據做加密的動作,而這個功能只支援 ElastiCache Redis,而 “不支援 ElastiCache Memcached” (因為 Memcached 本身沒有Snapshot backup快照備份的功能,也不支援同步Replication),另外此功能並不會造成效能影響

■ In-transit encryption (TLS) for ElastiCache Redis

By providing in-transit encryption capability, ElastiCache gives you a tool you can use to help protect your data when it is moving from one location to another. For example, you might move data from a primary node to a read replica node within a replication group, or between your replication group and your application. It only can enable when creating the replication group, and the redis client must support and be enabled for transport layer security (TLS).

啓用傳輸中加密是從您的客戶端到 Redis 之間數據透過 TLS 的傳輸加密方式,來提高數據的安全性,而啓用傳輸中加密本身並沒有提供身份驗證(Redis AUTH)的功能,因此這一個 Redis 不需要透過客戶端身份驗證,就可以跟您的客戶端連線。

在 Elasticache Redis 版本 3.2.6 或 4.0.10 及之後的版本開始支援,而 Open-Source Redis 在版本 6 之後,才正式支援 transport layer security (TLS) 這個功能,而在服務器端開啟此功能後,”前端應用程序(Redis Client)也必需使用 TLS/SSL協定來連接”。

!!! Memcached 沒有加密功能,只有Redis有!!

■ Redis — AUTH

Authenticating Users with the Redis AUTH Command. Redis authentication tokens enable Redis to require a token (password) before allowing clients to execute commands, thereby improving data security.

Redis AUTH 開啟時,”當 Redis Client 必需使用 AUTH 命令來認証”,Redis檢測給定的密碼和配置文件中的密碼是否相符。而在 ElastiCache Redis 中,若要使用 Redis AUTH,必需先開啟”傳輸中加密In-transit encryption (TLS)”的功能,”ElastiCache Redis AUTH是無法單獨開啟的”,主因是因為 Redis AUTH 傳輸在沒有加密的狀況下,是可以透過 tcpdump 來擷取密碼的,所以原生 Redis 6 也開始支援 transport layer security (TLS) 這個功能,來強化安全性。

!!!不管是單節點或是 Cluster-enable,只要有啟用 ”傳輸中加密” 才能提供 ”AUTH 身份驗證”。
加密連線功能只能在新建時選
現存的要開啟加密連線,只能重建!

!!! 若您的組織重視安全性,請務必同時使用這兩個功能,但您也許會說使用 transport layer security (TLS) 會造成效能影響,而這個是可以透過架構上來改善的,例如使用 “長連接 Connection pooling 來減少連接上的延遲”,若業務上以讀取為主的話,可以 “增加多個副本節點來提升讀取的能力”,若是以寫入為主的話,也可以使用 Cluster mode enabled Redis cluster,並使用 “多個分片組,來提高寫入的能力”

Q: 外網用redis客戶端,怎麼連接到創建的redis實例服務?

Amazon ElastiCache 是提供基於雲的內存中鍵值存儲的 AWS 服務。該服務設計為,”只能從 AWS 中訪問”。但是,如果 ElastiCache 集群託管在 VPC 中,則可以使用網絡地址轉換實例(NAT instance)來提供外部訪問。另外,實現這一需求需要以下前提條件:

1) 集群必須駐留在 VPC 中,並且可以通過網絡地址轉換 (NAT) 實例訪問。此要求不存在例外情況。
2) NAT 實例必須在與集群相同的 VPC 中啟動。
3) NAT 實例必須在與集群分開的公有子網中啟動。
4) 彈性 IP 地址 (EIP) 必須與 NAT 實例關聯。
5) iptables 的端口轉發功能用於將 NAT 實例上的端口轉發到 VPC 中的緩存節點端口。

注意事項:。
1) 客戶端連接到 NAT 實例的 EIP 和緩存端口。NAT 實例上的端口轉發功能會將流量轉發到相應的緩存集群端口。
2) 如果添加或替換集群節點,則需要更新 iptables 規則以反映此更改。
3) 此方法應僅應用於測試和開發用途

其他限制:
1) NAT 實例用作客戶端與多個集群之間的代理。添加代理會影響到緩存集群的性能。這種影響會隨著您通過 NAT 實例訪問的緩存集群數量增加而增大。
2) 從客戶端到 NAT 實例的流量未加密。因此,您應當避免通過 NAT 實例發送敏感數據。
3) NAT 實例增加了維護另一實例的開銷。
4) NAT 實例會成為單點故障。

Q: 如何透過 ssh tunnel 從外網來連接內網的 ElastiCache?

您可以透過使用 ssh tunnel 的方式先連到與 ElastiCache 相同 VPC 的 EC2 實例後,之後再連到 ElastiCache。

1. 先在本地端建立 ssh tunnel:

[+] How to Setup Bastion Server with AWS EC2
https://medium.com/codex/how-to-setup-bastion-server-with-aws-ec2-b1590d2ff815

!!! Windows 也可以使用 putty 連建立:
[+] Configure SSH Tunneling with Windows Using PuTTY:
https://docs.marklogic.com/cloudservices/aws/admin/configure-putty-tunneling.html

2. 本地端建立連線,當您連到 localhost 的 26379 時,便可以連到 ElastiCache 的 6379 port。

!!! 此作法 ”非常不建議” 使用於正式環境。

Q: 為什麼在使用 redis-cli 訪問 Redis 群集時,我收到下面的錯誤消息?

Redis — RBAC: Authenticating Users with Role-Based Access Control — Only support Redis 6.x

user / default user / user group / Access String (ACL)

Access String (ACL): Flag | Commands | Keys (ex : on +@all ~* = on | -@all +get | ~*:keyword )

使用者可以創建 多個不同的帳號 User,然後每一個不同的帳號去指定不同的 Access String (ACL),來去限制這個帳號 User,可以執行那些命令,去操作那些鍵值,接下來使用者還可以創建多個不同的群組 User group,然後將帳戶加入到這個群組 User group,然後將特定群組 User group指定到特定 Redis Cluster,用這個方式來做帳號/User的權限管理。而有一個帳號是比較特別的,有一個預設帳號為 default user,這個帳號預設的權限是可以操作大部份的命令及鍵值,就如同Redis 5以前的版本,直接使用 AUTH 命令認証一樣。

另外,在 ElastiCache Redis 中,若要使用 Redis RBAC (Authenticating Users with Role-Based Access Control),必需先開啟 ”傳輸中加密In-transit encryption (TLS)” 的功能,ElastiCache Redis RBAC是無法單獨開啟的

!!! 注意 !!! 使用RBAC時,由於不同的帳號使用不同的Access String (ACL),所以每一筆命令及鍵值的操作,都必需去判斷是否充許操作,請理解 Redis 是單執行序服務,這代表每一筆鍵值操作前,都需要”先”做這個的判斷,勢必造成效能影響,故使用 RBAC 請務必先做足夠的壓力測試的動作。

常見問題

Q: 開啓At-Rest加密(In-Transit) 會有什麼效能影響?

A: 上面有回覆 。

Q: 我只想開啓身份驗證的功能(AUTH),但不要開啓轉輸加密的功能(In-Transit)?

A: ElastiCache Redis AUTH是無法單獨開啟的,但原生 Redis AUTH 及 transport layer security (TLS) 是分的,所以自建的 Redis 是可以只開啟 AUTH的,但同上面所說 ”若您的組織重視安全性,請務必同時使用這兩個功能”,理由上面已回覆。

Q: 開啓轉輸加密(In-Transit) 會有什麼效能影響?

A: 有一定程度的影響,取決於連接方式、實際操作行為,故請自行做測試。

Q: 開啓身份驗證的功能(AUTH)會有什麼效能影響?

A: 上面有回覆 ,另外開啟 Redis RBAC 的影響,上面也有回覆。

Q: 如何在現存的ElastiCache Redis 打開/關閉傳輸加密(In-Transit)?

A: ElastiCache Redis不支援這個功能,只能透過備份重建的方式,來達到這個目的。 ← 在 2022/12 之後,已支援,不需要再重建了。

A:在運行中的 ElastiCache Redis cluster,支援直接開啟 in-transit 了。
```
[+] Amazon ElastiCache for Redis now supports enabling encryption in transit on existing clusters:
https://aws.amazon.com/tw/about-aws/whats-new/2022/12/amazon-elasticache-redis-enabling-encryption-transit-existing-clusters/

[+] ElastiCache in-transit encryption (TLS) — Enabling in-transit encryption for an existing cluster using the AWS Management Console:
https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/in-transit-encryption.html#in-transit-encryption-enable-existing
```

Q: 如何在現存(運行中)的ElastiCache Redis 開啓/關閉At-Rest的功能?

A: ElastiCache Redis不支援這個功能,只能透過備份重建的方式,來達到這個目的。

Q: 是否可以在線更改身份驗證的密碼(AUTH Password)?

A: 可以,ElastiCache Redis 支援線上直接變更 AUTH Password,變更後,當前的連接是不受影響的,但新建立的連線,就必需使用新的密碼。

Q: Can I use my certificates for encryption?

A: No. Currently, Amazon ElastiCache does not provide the ability for you to use your certificates. Amazon ElastiCache manages certificates transparently for you.

!!! ElastiCache Redis 並不支援客戶自已的憑證,所以當您開啟 In-transit的功能後,是無法在Endpoint前面再加其他的 DNS網域的記錄,來指向 ElastiCache Redis Cluster

Q: Is there any action needed to renew certificates?

A: No. Amazon ElastiCache manages certification expiration and renewal behind the scene. No user action is necessary for ongoing certificate maintenance.

Elasticache manages the lifecycle of your certificates because ElastiCache for Redis automatically manages the issuance, renewal, and expiration of your certificates.
Amazon ElastiCache seamlessly manages certificate expiration and renewal without requiring any additional action from the application.

不需要,ElastiCache 會自動更新每一個 Redis Cluster的憑證 Certification。

Sample Python code on Redis Cluster with In-Transit and AUTH enabled

Jedis sample code → TLS+AUTH Token

其他: 直接使用 openssl 來測試連線的方式!!

其他: 使用stunnel的方式來連接!!

!!! 不建議使用 stunnel,因為當節點角色更換或是損壞,會造成單點故障,只適合用於前期測試使用,請務必使用支援 transport layer security (TLS) 的 Redis Clinet。

ARM機型的效能在開啟”傳輸加密(In-Transit)”的 ElastiCache Redis,在”短連接 (沒有使用connection pool)”的效能上,在 ElastiCache Redis 版本 6.2 之前的效能,是比 Intel的機型差,測試如下。

!!! Update@2022–12 在新版本的 Redis 有改善,建議用新版本來測試。

--

--

Jerry’s Notes
What’s next?

An cloud support engineer focus on troubleshooting with customer reported issue ,and cloud solution architecture.