您可以编写经过身份验证的用户 IAM 策略,只允许他们访问他们创建的资源。例如:
- 使用您从 CognitoIdentity 获得的 ID 令牌实例化 AWS secretsmanager 客户端
- 指定您的身份验证策略,如下所示:
{
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": [
"secretsmanager:UpdateSecret",
"secretsmanager:PutSecretValue",
"secretsmanager:DescribeSecret",
"secretsmanager:DeleteSecret"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": [
"Sub",
"Service"
]
},
"StringEquals": {
"secretsmanager:ResourceTag/Service": "MYSERVICE",
"secretsmanager:ResourceTag/Sub": "${cognito-identity.amazonaws.com:sub}"
}
}
},
{
"Sid": "",
"Effect": "Allow",
"Action": [
"secretsmanager:TagResource",
"secretsmanager:CreateSecret"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": [
"Sub",
"Service"
]
},
"StringEquals": {
"aws:RequestTag/Service": "MYSERVICE",
"aws:RequestTag/Sub": "${cognito-identity.amazonaws.com:sub}"
}
}
}
]
}
}
3) 创建密钥时,请务必在CreateSecret
请求中应用标签,映射到您的用户身份池 ID 和服务名称。(如果不这样做,您的请求将失败。)
您的用户现在只能访问他们创建的机密。这是安全的,因为“${cognito-identity.amazonaws.com:sub}”值将根据 AWS 开发工具包会话凭证进行插值。即您的其他用户的客户端将嵌入不同的“子”值作为其会话凭据的一部分,因此他们将无法访问他们没有创建的秘密。