使用 CloudFront 连接函数和 KVS 实施吊销
您可以将 CloudFront 连接函数与 KeyValueStore 结合使用,来实施双向 TLS 身份验证的证书吊销检查。此方法提供了一种可扩展的实时证书吊销机制,可作为 CloudFront 的内置证书验证的有力补充。
连接函数是指在 TLS 连接建立过程中,在 CloudFront 边缘站点运行的 JavaScript 函数,此类函数可用于为 mTLS 身份验证实施自定义证书验证逻辑。有关连接函数的详细信息,请参阅关联 CloudFront 连接函数。
证书吊销如何与连接函数结合使用
CloudFront 的标准证书验证功能可验证证书链、签名和到期日期,但不包括内置的证书吊销检查。通过使用连接函数,您可以在 TLS 握手过程中实施自定义吊销检查。
证书吊销流程如下所述:
-
将已吊销证书的序列号存储在 CloudFront KeyValueStore 中。
-
当客户端提供证书时,系统将调用您的连接函数。
-
该函数会根据 KeyValueStore 中的数据核对证书序列号。
-
如果在存储中找到了序列号,则证书将被吊销。
-
您的函数拒绝已吊销证书的连接。
此方法可在 CloudFront 的全球边缘网络中提供近乎实时的吊销检查。
针对已吊销的证书设置 KeyValueStore
首先,创建一个 KeyValueStore 来存储已吊销证书的序列号:
创建 KeyValueStore(控制台)
登录 AWS 管理控制台,并通过以下网址打开 CloudFront 控制台:https://console.aws.amazon.com/cloudfront/v4/home
。 -
在导航窗格中,选择键值存储。
-
选择创建键值存储。
-
输入键值存储的名称(例如,revoked-certificates)。
-
(可选)添加描述。
-
选择创建键值存储。
创建 KeyValueStore(AWS CLI)
以下示例演示如何创建 KeyValueStore:
aws cloudfront create-key-value-store \ --name "revoked-certificates" \ --comment "Store for revoked certificate serial numbers"
导入已吊销证书的序列号
创建 KeyValueStore 之后,您需要导入已吊销证书的序列号:
准备吊销数据
创建包含已吊销证书序列号的 JSON 文件:
{ "data": [ { "key": "ABC123DEF456", "value": "" }, { "key": "789XYZ012GHI", "value": "" } ] }
从 S3 导入
-
将 JSON 文件上传到 S3 存储桶
-
将该文件导入 KeyValueStore:
aws cloudfront create-key-value-store \ --name "revoked-certificates" \ --import-source '{ "SourceType": "S3", "SourceARN": "arn:aws:s3:::amzn-s3-demo-bucket1/revoked-serials.json" }'
创建用于吊销检查的连接函数
创建连接函数,来根据 KeyValueStore 中的数据核对证书序列号:
连接函数代码示例
以下示例演示连接函数如何执行证书吊销检查:
import cf from 'cloudfront'; async function connectionHandler(connection) { const kvsHandle = cf.kvs(); // Get client certificate serial number const clientSerialNumber = connection.clientCertificate.certificates.leaf.serialNumber; // Check if the serial number exists in the KeyValueStore const isRevoked = await kvsHandle.exists(clientSerialNumber.replaceAll(':', '')); if (isRevoked) { console.log(`Certificate ${clientSerialNumber} is revoked. Denying connection.`); connection.logCustomData(`REVOKED:${clientSerialNumber}`); connection.deny(); } else { console.log(`Certificate ${clientSerialNumber} is valid. Allowing connection.`); connection.allow(); } }
创建连接函数(AWS CLI)
以下示例演示如何创建连接函数与 KeyValueStore 关联:
aws cloudfront create-connection-function \ --name "revocation-checker" \ --connection-function-config '{ "Comment": "Certificate revocation checking function", "Runtime": "cloudfront-js-2.0", "KeyValueStoreAssociations": { "Quantity": 1, "Items": [ { "KeyValueStoreARN": "arn:aws:cloudfront::123456789012:key-value-store/revoked-certificates" } ] } }' \ --connection-function-code fileb://revocation-checker.js
将函数与分配关联
创建并发布连接函数后,将其与已启用 mTLS 的 CloudFront 分配相关联,如关联 CloudFront 连接函数部分中所述。