ElastiCache 传输中加密(TLS) - Amazon ElastiCache
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

ElastiCache 传输中加密(TLS)

为了帮助确保数据安全,Amazon ElastiCache 和 Amazon EC2 提供了禁止未经授权来访问服务器上数据的机制。通过传输中加密功能,ElastiCache 为您提供了在不同位置之间移动数据时用来保护数据的工具。

所有无服务器缓存均启用了传输中加密。对于自行设计的集群,在使用 CreateCacheCluster(CLI:create-cache-cluster)操作创建缓存群集时,您可以将参数 TransitEncryptionEnabled 设置为 true(CLI:--transit-encryption-enabled),从而在缓存群集上启用传输中加密。

传输中加密概览

Amazon ElastiCache 传输中加密功能可让您在数据最脆弱时候(从一个位置传输到另一个位置时)提高数据的安全性。由于在端点加密和解密数据时需要进行一些处理,因此启用传输中加密会对性能产生一些影响。应对使用和不使用传输中加密的数据进行基准测试,以确定对使用案例的性能影响。

ElastiCache 传输中加密可实现以下功能:

  • 加密客户端连接:客户端与缓存节点的连接采用 TLS 加密。

  • 加密服务器连接:对集群中在节点之间移动的数据进行了加密。

  • 服务器身份验证 – 客户端可通过身份验证确定它们连接到正确的服务器。

传输中加密的条件

在规划您的自行设计集群的实施时,应注意有关 Amazon ElastiCache 传输中加密的以下限制:

  • 在运行 Memcached 1.6.12 及更高版本的集群上支持传输中加密。

  • 传输中加密支持传输层安全性协议(TLS)版本 1.2 和 1.3。

  • 只有在 Amazon VPC 中运行的集群才支持传输中加密。

  • 只有运行以下节点类型的集群才支持传输中加密。

    • R6g、R5、R4

    • M6g、M5、M4

    • T4g、T3

    有关更多信息,请参阅受支持的节点类型

  • 通过显式将参数 TransitEncryptionEnabled 设置为 true 可启用传输中加密。

  • 只有在创建集群时,才能在集群上启用传输中加密。无法通过修改集群来开启和关闭传输中加密。

  • 确保您的缓存客户端支持 TLS 连接,并且您已在客户端配置中启用传输中加密。

传输中加密最佳实践

  • 由于在端点加密和解密数据时需要进行一些处理,因此实现传输中加密会降低性能。使用自己的数据,对传输中加密进行基准测试,然后与不加密情况进行比较,以确定其对实现性能的影响。

  • 由于创建新连接的成本可能非常高,您可以通过保留 TLS 连接来减小传输中加密对性能的影响。

启用传输中加密

要在使用 Amazon 管理控制台创建 Memcached 集群时启用传输中加密,请进行以下选择:

  • 选择 Memcached 作为引擎。

  • 选择 1.6.12 或更高的引擎版本。

  • Encryption in transit(传输中加密)下,选择 Enable(启用)。

有关此分步过程,请参阅创建 Memcached 集群(控制台)

使用 Openssl 连接到启用了传输中加密的节点

要从启用了传输中加密的 ElastiCache for Memcached 节点中访问数据,您需要使用利用安全套接字层(SSL)的客户端。您也可以在 Amazon Linux 和 Amazon Linux 2 上使用 Openssl s_client。

使用 Openssl s_client 连接到 Amazon Linux 2 或 Amazon Linux 上启用了传输中加密的 Memcached 集群:

/usr/bin/openssl s_client -connect memcached-node-endpoint:memcached-port

使用 Java 创建 TLS Memcached 客户端

要在 TLS 模式下创建客户端,请执行以下操作以利用适当的 SSLContext 初始化该客户端:

import java.security.KeyStore; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import net.spy.memcached.AddrUtil; import net.spy.memcached.ConnectionFactoryBuilder; import net.spy.memcached.MemcachedClient; public class TLSDemo { public static void main(String[] args) throws Exception { ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder(); // Build SSLContext TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init((KeyStore) null); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); // Create the client in TLS mode connectionFactoryBuilder.setSSLContext(sslContext); MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses("mycluster.fnjyzo.cfg.use1.cache.amazonaws.com:11211")); // Store a data item for an hour. client.set("theKey", 3600, "This is the data value"); } }

使用 PHP 创建 TLS Memcached 客户端

要在 TLS 模式下创建客户端,请执行以下操作以利用适当的 SSLContext 初始化该客户端:

<?php /** * Sample PHP code to show how to create a TLS Memcached client. In this example we * will use the Amazon ElastiCache Auto Descovery feature, but TLS can also be * used with a Static mode client. * See Using the ElastiCache Cluster Client for PHP (https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.Using.ModifyApp.PHP.html) for more information * about Auto Discovery and persistent-id. */ /* Configuration endpoint to use to initialize memcached client. * this is only an example */ $server_endpoint = "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com"; /* Port for connecting to the cluster. * This is only an example */ $server_port = 11211; /* Initialize a persistent Memcached client and configure it with the Dynamic client mode */ $tls_client = new Memcached('persistent-id'); $tls_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE); /* Add the memcached's cluster server/s */ $tls_client->addServer($server_endpoint, $server_port); /* Configure the client to use TLS */ if(!$tls_client->setOption(Memcached::OPT_USE_TLS, 1)) { echo $tls_client->getLastErrorMessage(), "\n"; exit(1); } /* Set your TLS context configurations values. * See MemcachedTLSContextConfig in memcached-api.php for all configurations */ $tls_config = new MemcachedTLSContextConfig(); $tls_config->hostname = '*.mycluster.fnjyzo.use1.cache.amazonaws.com'; $tls_config->skip_cert_verify = false; $tls_config->skip_hostname_verify = false; /* Use the created TLS context configuration object to create OpenSSL's SSL_CTX and set it to your client. * Note: These TLS context configurations will be applied to all the servers connected to this client. */ $tls_client->createAndSetTLSContext((array)$tls_config); /* test the TLS connection with set-get scenario: */ /* store the data for 60 seconds in the cluster. * The client will decide which cache host will store this item. */ if($tls_client->set('key', 'value', 60)) { print "Successfully stored key\n"; } else { echo "Failed to set key: ", $tls_client->getLastErrorMessage(), "\n"; exit(1); } /* retrieve the key */ if ($tls_client->get('key') === 'value') { print "Successfully retrieved key\n"; } else { echo "Failed to get key: ", $tls_client->getLastErrorMessage(), "\n"; exit(1); }

有关使用 PHP 客户端的更多信息,请参阅 安装适用于 PHP 的 ElastiCache Cluster Client