【问题标题】:How can I retrieve correct credentials to access AWS SecretsManger on a boto3 client using AWS Cognito - Indentity Pool如何使用 AWS Cognito 检索正确的凭证以访问 boto3 客户端上的 AWS SecretsManger - 身份池
【发布时间】:2019-09-16 18:13:35
【问题描述】:

我正在尝试使用运行 python3 (kivy) 的独立应用程序检索 AWS Secret(这是 boto3 配置)。

客户端使用 boto3 并且使用硬编码的凭据可以正常工作。

我正在尝试在 boto3 中使用 get_credentials_for_identity 获得信誉。

我创建了一个联合身份池并为其分配了一个访问 Secrets Manager 的策略:

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "secretsmanager:GetSecretValue",
        "Resource": "<arn-of-the-secret-the-app-needs-to-access>"
    }
}

我也使用通用 Cognito 角色做了同样的事情。 (知道这不是问题,但我敢肯定我们都在尝试解决问题时做傻事)。

client = boto3.client('cognito-identity', region_name='eu-west-1')
response = client.get_credentials_for_identity(
    #identity taken from fedenration/identity pools;app with cognito access
    IdentityId='eu-west-1:6###...',
)
creds_dic = response['Credentials']
awsaccesskey = creds_dic['AccessKeyId']
awsseckey = creds_dic['SecretKey']
print(response)
print(awsseckey)
print(awsaccesskey)

给我错误的AccessKeyIdSecretKey,我什至找不到它提供的那些。我想我要的是持有此identity 的 IAM 的访问密钥/秘密,但我需要访问 secrets manager 的 IAM 角色的访问密钥/秘密,因此我可以将它们输入 boto3 和检索信用/秘密。

user pool 我也需要同样的设置,但一旦我意识到我在做的愚蠢,我就可以配置它。

【问题讨论】:

  • 您获得的凭证很可能是使用 IAM 角色为会话生成的临时凭证。问题是,您是否尝试过在 boto3 中使用检索到的凭据来获取机密?。
  • 是的!这就是我获取invalid token error 的地方 - 我使用具有secretsmanager 权限的身份池获取_credentials_for_identity 我将这些通过我的SecretsManager 并获得无效令牌

标签: python amazon-web-services boto3 amazon-cognito aws-secrets-manager


【解决方案1】:

Cognito 生成由 AWS STS 提供的新凭证。您将无法将这些凭证与您拥有的现有 IAM 生成凭证相匹配。要访问 AWS Secrets Manager API,请确保您的 Cognito 身份池的 Authenticated Role 具有足够的权限。

【讨论】:

  • 我的Authenticated role 和我的unAuthenticated 角色具有正确的权限。我正在使用以下策略SecretsManagerReadWrite。我仍然收到无效令牌错误。 response_getcreds = client.get_credentials_for_identity(IdentityId='eu-west-1:fbfda46a-0e03-4aa4-bf54-049467ccbfb3',)
  • 不会无效。尝试使用通过 AWS CLI 生成的凭据作为测试。
  • 当我使用 identity pool 的池 id 时,我的 IdentityId 是从 GetId 生成的......感觉好像我的权限在某处是错误的。我什至将 AdminAccess 分配给了我的unathen 角色,这两个角色都在使用。我现在试试 CLI 方法!
  • 使用aws cliget_credentials_for_identity 生成的凭据给了我相同的令牌错误
  • 我需要指定Trusted entities 吗?我目前只有cognito-identity.amazonaws.com
猜你喜欢
  • 2020-04-12
  • 2018-06-03
  • 2017-03-24
  • 1970-01-01
  • 2023-03-09
  • 2021-01-05
  • 1970-01-01
  • 2017-07-01
  • 2021-05-05
相关资源
最近更新 更多