自動輪換使用者密碼 - Amazon ElastiCache

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

自動輪換使用者密碼

使用 AWS Secrets Manager時,您可以將程式碼中的硬式編碼憑證 (包括密碼) 取代為API呼叫 Secrets Manager,以程式設計方式擷取秘密。這有助於確保不讓某人研究您的程式碼而盜用秘密,因為秘密根本不在程式碼中。此外,您可以設定 Secrets Manager,根據您指定的排程自動輪換秘密。這可讓您以短期秘密取代長期秘密,有助於大幅降低洩漏風險。

使用 Secrets Manager,您可以使用 Secrets Manager 提供的 AWS Lambda 函數自動輪換 ElastiCache (RedisOSS) 密碼 (即秘密)。

如需 的詳細資訊 AWS Secrets Manager,請參閱什麼是 AWS Secrets Manager?

ElastiCache 如何使用秘密

Valkey 7.2 具有與 Redis 7.0 OSS 相同的功能集。在 Redis OSS 6 中, ElastiCache 介紹角色型存取控制 (RBAC)如何保護 Valkey 或 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 函數,該函數將與修改使用者互動,API以更新使用者的密碼。

如需其運作方式的詳細資訊,請參閱輪換的運作方式

注意

對於某些 AWS 服務,為避免混淆代理案例, AWS 建議您同時使用 aws:SourceArnaws:SourceAccount全域條件金鑰。不過,如果您在輪換函數政策中包含 aws:SourceArn條件,則輪換函數只能用來輪換該 指定的秘密ARN。建議您僅包含內容金鑰 aws:SourceAccount,以便可以將輪換函數用於多個秘密。

對於您可能遇到的任何問題,請參閱對 AWS Secrets Manager 輪換進行故障診斷

如何建立 ElastiCache 使用者並將其與 Secrets Manager 建立關聯

下列步驟說明如何建立使用者,並將其與 Secrets Manager 建立關聯:

  1. 建立非作用中的使用者

    若為 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" }
  2. 建立秘密

    若為 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" }
  3. 設定 Lambda 函數以輪換您的密碼

    1. 登入 AWS Management Console ,並在 開啟 Lambda 主控台 https://console.aws.amazon.com/lambda/

    2. 在導覽面板上,選擇 Functions (函數),然後選擇您所建立的函數。請選擇函數名稱,而非其左側的核取方塊。

    3. 選擇 Configuration (組態) 索引標籤。

    4. General configuration (一般組態) 中,選擇 Edit (編輯),然後將 Timeout (逾時) 設定為至少 12 分鐘。

    5. 選擇 Save (儲存)。

    6. 選擇 Environment variables (環境變數),然後設定下列項目:

      1. SECRETS_MANAGER_ENDPOINT – https://secretsmanager.REGION.amazonaws.com

      2. SECRET_ARN – 您在步驟 2 中建立的秘密的 Amazon Resource Name (ARN)。

      3. USER_NAME – ElastiCache 使用者的使用者名稱,

      4. 選擇 Save (儲存)。

    7. 選擇 Permissions (許可)

    8. 執行角色 下,選擇要在IAM主控台上檢視的 Lambda 函數角色名稱。

    9. 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": "*" } ] }
  4. 設定 Secrets Manager 秘密輪換

    1. 使用 AWS Management Console,請參閱使用主控台設定 AWS Secrets Manager 秘密的自動輪換

      如需輪換排程的詳細資訊,請參閱 Secrets Manager 輪換中的排程表達式

    2. 使用 AWS CLI,請參閱設定 AWS Secrets Manager 使用 的自動輪換 AWS Command Line Interface