使用 CloudFront Connection Function 和 KVS 撤銷 - Amazon CloudFront

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 CloudFront Connection Function 和 KVS 撤銷

您可以結合 CloudFront Connection Functions 與 KeyValueStore,實作交互 TLS 身分驗證的憑證撤銷檢查。此方法提供可擴展的即時憑證撤銷機制,可補充 CloudFront 的內建憑證驗證。

Connection Functions 是在 CloudFront 節點建立 TLS 連線期間執行的 JavaScript 函數,可讓您實作自訂憑證驗證邏輯以進行 mTLS 身分驗證。如需連線函數的詳細資訊,請參閱 關聯 CloudFront 連線函數

憑證撤銷如何與 Connection Functions 搭配使用

CloudFront 的標準憑證驗證會驗證憑證鏈、簽章和過期,但不包含內建憑證撤銷檢查。透過使用連線函數,您可以在 TLS 交握期間實作自訂撤銷檢查。

憑證撤銷程序的運作方式如下:

  1. 在 CloudFront KeyValueStore 中存放撤銷的憑證序號。

  2. 當用戶端提供憑證時,就會叫用您的連線函數。

  3. 函數會根據 KeyValueStore 檢查憑證的序號。

  4. 如果在存放區中找到序號,則會撤銷憑證。

  5. 您的函數拒絕已撤銷憑證的連線。

此方法提供跨 CloudFront 全球邊緣網路near-real-time的撤銷檢查。

為已撤銷的憑證設定 KeyValueStore

首先,建立 KeyValueStore 以存放已撤銷憑證的序號:

建立 KeyValueStore (主控台)

  1. 登入 AWS 管理主控台 並開啟位於 的 CloudFront 主控台https://console.aws.amazon.com/cloudfront/v4/home

  2. 在導覽窗格中,選擇鍵值存放區。

  3. 選擇建立索引鍵值存放區

  4. 輸入金鑰值存放區的名稱 (例如,已撤銷憑證)。

  5. (選用) 新增描述。

  6. 選擇建立索引鍵值存放區

建立 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 匯入

  1. 將 JSON 檔案上傳至 S3 儲存貯體

  2. 將檔案匯入至 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:

Connection Function 程式碼範例

下列範例顯示執行憑證撤銷檢查的連線函數:

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 連線函數一節中所述。