为用户自动轮换密码
借助 Amazon Secrets Manager,您可以将代码中的硬编码凭证(包括密码)替换为对 Secrets Manager 的 API 调用,从而以编程方式检索密钥。这有助于确保检查您的代码的人不会泄露密钥,因为其中根本不包含密钥。此外,您还可以配置 Secrets Manager 以根据指定的计划自动轮换密钥。这使您能够将长期密钥替换为短期密钥,这有助于显著减少泄露风险。
借助 Secrets Manager,您可以使用 Secrets Manager 提供的 Amazon Lambda 函数自动轮换 ElastiCache for Redis OSS 的密码(即密钥)。
有关 Amazon Secrets Manager 的更多信息,请参阅什么是 Amazon Secrets Manager?
ElastiCache 如何使用密钥
Valkey 7.2 及更高版本具有与 Redis OSS 7.0 相同的功能集。在 Redis OSS 6 中,ElastiCache 引入了基于角色的访问控制(RBAC) 来确保 Valkey 或 Redis OSS 集群的安全。此功能允许在可以执行的命令和可以访问的密钥方面限制某些连接。使用 RBAC,当客户使用密码创建用户时,密码值需要以明文形式手动输入,且对操作员可见。
使用 Secrets Manager,应用程序从 Secrets Manager 获取密码,而不是手动输入密码并将其存储在应用程序的配置中。有关如何执行此操作的信息,请参阅 ElastiCache 是如何与密钥关联的。
使用密钥会产生费用。有关定价信息,请参阅 Amazon 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 交互以更新用户的密码。
有关其工作原理的信息,请参阅轮换的工作原理。
注意
对于某些 Amazon 服务,为了避免混淆代理情况,Amazon 建议您同时使用 aws:SourceArn 和 aws:SourceAccount 全局条件密钥。但如果轮换函数策略中包括 aws:SourceArn 条件,则轮换函数只能用于轮换该 ARN 指定的密钥。我们建议您仅在其中包括上下文键 aws:SourceAccount,以便对多个密钥使用轮换函数。
有关您可能遇到的任何问题,请参阅 Amazon 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 函数来轮换您的密码
登录到 Amazon Web Services 管理控制台,然后通过以下网址打开 Lambda 控制台:https://console.aws.amazon.com/lambda/
在导航面板上,选择 Functions(函数),然后选择您创建的函数。选择函数名称,而不是其左边的复选框。
选择配置选项卡。
在 General configuration(常规配置)中,选择 Edit(编辑),然后将 Timeout(超时)设置为至少 12 分钟。
选择保存。
选择 Environment variables(环境变量),然后设置以下内容:
SECRETS_MANAGER_ENDPOINT – https://secretsmanager.
REGION.amazonaws.comSECRET_ARN – 您在第 2 步中创建的密钥的 Amazon 资源名称(ARN)。
USER_NAME – ElastiCache 用户的用户名,
选择保存。
选择 Permissions(权限)。
在 Execution role(执行角色)下,选择要在 IAM 控制台上查看的 Lambda 函数角色的名称。
Lambda 函数需要以下权限才能修改用户和设置密码:
ElastiCache
Secrets Manager
设置 Secrets Manager 密钥轮换
使用 Amazon Web Services 管理控制台,请参阅使用控制台设置 Amazon Secrets Manager 密钥的自动轮换
有关设置轮换计划的更多信息,请参阅 Secrets Manager 轮换中的计划表达式。
使用 Amazon CLI,请参阅使用Amazon Command Line Interface 为 Amazon Secrets Manager 设置自动轮换