本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
自動輪換使用者密碼
使用 AWS Secrets Manager,您可以使用對 Secrets Manager 的 API 呼叫來取代程式碼中的硬式編碼認證 (包括密碼),以程式設計方式擷取密碼。這有助於確保不讓某人研究您的程式碼而盜用秘密,因為秘密根本不在程式碼中。此外,您可以設定 Secrets Manager,根據您指定的排程自動輪換秘密。這可讓您以短期秘密取代長期秘密,有助於大幅降低洩漏風險。
使用 Secrets Manager,您可以使用秘密管理員提供的 AWS Lambda 功能,自動輪換 ElastiCache (Redis OSS) 密碼 (也就是秘密)。
如需有關的詳細資訊 AWS Secrets Manager,請參閱什麼是 AWS Secrets Manager?
如何 ElastiCache 使用秘密
隨著 Redis 的 OSS 6, ElastiCache (Redis OSS) 引入了角色型存取控制 (RBAC) 規則數量保護 Redis 的 OSS 叢集。此功能允許以可執行命令和可存取金鑰限制部分連線。客戶使用 RBAC 以密碼建立使用者時,密碼值須以純文字手動輸入,讓運算子可見。
使用 Secret Manager 時,應用程式會從 Secrets Manager 擷取密碼,而非手動輸入,接著再將密碼儲存在應用程式組態。如需如何執行此作業的資訊,請參閱 使 ElastiCache 用者與密碼相關聯的方式。
使用秘密會產生費用。如需定價資訊,請參閱 AWS Secrets Manager 定價
使 ElastiCache 用者與密碼相關聯的方式
Secrets Manager 會在密碼的 SecretString
欄位保留關聯使用者的參考。不會有從 ElastiCache 側面引用的秘密。
{ "password": "strongpassword", "username": "user1", "user_arn": "arn:aws:elasticache:us-east-1:xxxxxxxxxx918:user:user1" //this is the bond between the secret and the user }
Lambda 輪換函數
若要啟用 Secrets Manager 自動密碼輪換,您要建立 Lambda 函數,該函數會與 modify-user API 互動,以更新使用者密碼。
如需其運作方式的詳細資訊,請參閱輪換的運作方式。
注意
對於某些 AWS 服務,若要避免混淆的副案例, AWS 建議您同時使用aws:SourceArn
和aws:SourceAccount
全域條件金鑰。但是,如果在您的輪換函數政策中包含 aws:SourceArn
條件,則輪換函數只能用於輪換該 ARN 指定的秘密。建議您僅包含內容金鑰 aws:SourceAccount
,以便可以將輪換函數用於多個秘密。
如需您可能遇到的任何問題,請參閱疑難排解 AWS Secrets Manager 輪換
如何創建用 ElastiCache 戶並將其與 Secrets Manager 關聯
下列步驟說明如何建立使用者,並將其與 Secrets Manager 建立關聯:
建立非作用中的使用者
若為 Linux、macOS 或 Unix:
aws elasticache create-user \ --user-id user1 \ --user-name user1 \ --engine "REDIS" \ --no-password \ // no authentication is required --access-string "*off* +get ~keys*" // this disables the user
針對 Windows:
aws elasticache create-user ^ --user-id user1 ^ --user-name user1 ^ --engine "REDIS" ^ --no-password ^ // no authentication is required --access-string "*off* +get ~keys*" // this disables the user
您將看到類似以下的回應:
{ "UserId": "user1", "UserName": "user1", "Status": "active", "Engine": "redis", "AccessString": "off ~keys* -@all +get", "UserGroupIds": [], "Authentication": { "Type": "no_password" }, "ARN": "arn:aws:elasticache:us-east-1:xxxxxxxxxx918:user:user1" }
建立秘密
若為 Linux、macOS 或 Unix:
aws secretsmanager create-secret \ --name production/ec/user1 \ --secret-string \ '{ "user_arn": "arn:aws:elasticache:us-east-1:123456xxxx:user:user1", "username":"user1" }'
針對 Windows:
aws secretsmanager create-secret ^ --name production/ec/user1 ^ --secret-string ^ '{ "user_arn": "arn:aws:elasticache:us-east-1:123456xxxx:user:user1", "username":"user1" }'
您將看到類似以下的回應:
{ "ARN": "arn:aws:secretsmanager:us-east-1:123456xxxx:secret:production/ec/user1-eaFois", "Name": "production/ec/user1", "VersionId": "aae5b963-1e6b-4250-91c6-ebd6c47d0d95" }
設定 Lambda 函數以輪換您的密碼
登入 AWS Management Console 並開啟 Lambda 主控台,網址為 https://console.aws.amazon.com/lambda/
在導覽面板上,選擇 Functions (函數),然後選擇您所建立的函數。請選擇函數名稱,而非其左側的核取方塊。
選擇 Configuration (組態) 索引標籤。
在 General configuration (一般組態) 中,選擇 Edit (編輯),然後將 Timeout (逾時) 設定為至少 12 分鐘。
選擇儲存。
選擇 Environment variables (環境變數),然後設定下列項目:
SECRETS_MANAGER_ENDPOINT – https://secretsmanager.
REGION
.amazonaws.com.rproxy.goskope.comSECRET_ARN – 您在步驟 2 中建立的秘密 Amazon Resource Name (ARN)。
使用者名稱 — 使用者的使用者名稱、 ElastiCache
選擇儲存。
選擇 Permissions (許可)
在 Execution role (執行角色) 下,選擇要在 IAM 主控台檢視的 Lambda 函數角色名稱。
Lambda 函數需要下列許可,才能修改使用者和設定密碼:
ElastiCache
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "elasticache:DescribeUsers", "elasticache:ModifyUser" ], "Resource": "arn:aws:elasticache:us-east-1:xxxxxxxxxx918:user:user1" } ] }
Secrets Manager
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret", "secretsmanager:PutSecretValue", "secretsmanager:UpdateSecretVersionStage" ], "Resource": "arn:aws:secretsmanager:us-east-1:xxxxxxxxxxx:secret:XXXX" }, { "Effect": "Allow", "Action": "secretsmanager:GetRandomPassword", "Resource": "*" } ] }
設定 Secrets Manager 秘密輪換
使用 AWS Management Console,請參閱使用主控台設定 AWS Secrets Manager 密碼的自動輪換
如需輪換排程的詳細資訊,請參閱 Secrets Manager 輪換中的排程表達式。
使用 AWS CLI,請參閱設定自動旋轉以 AWS Secrets Manager 使用 AWS Command Line Interface