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

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

自動輪換使用者密碼

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

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

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

ElastiCache 如何使用秘密

Valkey 7.2 及更高版本具有同等功能,設定為 Redis OSS 7.0。在 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 函數,該函數會與 modify-user 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 ,並在 https://https://console.aws.amazon.com/lambda/ 開啟 Lambda 主控台

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

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

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

    5. 選擇儲存

    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. 選擇儲存

    7. 選擇 Permissions (許可)

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

    9. Lambda 函數需要下列許可,才能修改使用者和設定密碼:

      ElastiCache

      JSON
      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "elasticache:DescribeUsers", "elasticache:ModifyUser" ], "Resource": "arn:aws:elasticache:us-east-1:xxxxxxxxxx918:user:user1" } ] }

      Secrets Manager

      JSON
      { "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