【问题标题】:Using CloudFormation, how can I create an Identity Pool that authorizes based on User Pool roles?使用 CloudFormation,如何创建基于用户池角色授权的身份池?
【发布时间】:2020-06-21 16:31:03
【问题描述】:

我正在使用无服务器框架来处理我的 CloudFormation 内容。我正在构建一个用户池,其中包含具有自己角色的组。我想构建我的身份池,以便将 Authenticated role selection 的 Cognito 提供程序设置设置为 Choose role from token,其中 Role resolultionDENY

这是我的相关 CloudFormation - 忽略 ${self:custom....} 的东西:

    IdentityPool:
      Type: AWS::Cognito::IdentityPool
      Properties:
        IdentityPoolName: ${self:custom.identityPoolName}
        AllowUnauthenticatedIdentities: false
        CognitoIdentityProviders:
          - ClientId:
              Ref: UserPoolClient
            ProviderName:
              Fn::GetAtt: ["UserPool", "ProviderName"]

    IdentityPoolRoleAttachment:
      Type: AWS::Cognito::IdentityPoolRoleAttachment
      Properties:
        IdentityPoolId:
          Ref: IdentityPool
        RoleMappings:
          CognitoProvider:
            IdentityProvider:
              Fn::Join:
                - ""
                - - "cognito-idp."
                  - Ref: AWS::Region
                  - ".amazonaws.com/"
                  - Ref: UserPool
                  - ":"
                  - Ref: UserPoolClient
            Type: Token
            AmbiguousRoleResolution: Deny

这不起作用,因为IdentityPoolRoleAttachment 需要Roles 部分。但我不想将经过身份验证和未经身份验证的角色与身份池一起使用。我希望 Identity Pool Cognito 提供者只检查传入的令牌。

这是我得到的错误:

 ServerlessError: An error occurred: IdentityPoolRoleAttachment - 1 validation error detected: Value null at 'roles' failed to satisfy constraint: Member must not be null (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: ValidationException; Request ID: 80026230-eaa9-4045-86d8-6fe4c07cce9d).

我该怎么做?我是否需要创建一个空角色并将其分配给IdentityPoolRoleAttachment

我可以在控制台中没有身份池角色的情况下执行此操作。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-cognito serverless-framework


    【解决方案1】:

    我能够在不创建空角色的情况下完成这项工作。 根据docroles 不是必需的,但似乎 CFN 无法很好地处理 null。

    你只需要设置"roles": { }

    cdk 代码

    new CfnIdentityPoolRoleAttachment(
        this,
        'ExampleCognitoIdentityPoolRoleAttachment',
        {
            identityPoolId: identityPool.ref,
            roles: {},
            roleMappings: {
                mapping: {
                    type: 'Token',
                    ambiguousRoleResolution: 'Deny',
                    identityProvider: `cognito-idp.${cdk.Stack.of(this).region}.amazonaws.com/${userPool.userPoolId}:${cognitoAppClient.ref}`,
                },
            },
        },
    );
    

    来自 cdk 的 Cloudformation 模板输出

    "ExampleCognitoIdentityPoolRoleAttachment": {
        "Type": "AWS::Cognito::IdentityPoolRoleAttachment",
            "Properties": {
            "IdentityPoolId": {
                "Ref": "ExampleCognitoIdentityPool"
            },
            "RoleMappings": {
                "mapping": {
                    "AmbiguousRoleResolution": "Deny",
                        "IdentityProvider": {
                        "Fn::Join": [
                            "",
                            [
                                "cognito-idp.eu-west-1.amazonaws.com/",
                                {
                                    "Ref": "<UserPoolRef>"
                                },
                                ":",
                                {
                                    "Ref": "<UserPoolAppClientRef>"
                                }
                            ]
                        ]
                    },
                    "Type": "Token"
                }
            },
            "Roles": { }
        },
        "Metadata": {
            "aws:cdk:path": "example-stack/ExampleCognitoIdentityPoolRoleAttachment"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 2020-05-23
      • 2019-08-15
      • 2015-10-08
      • 2019-06-02
      • 1970-01-01
      • 2018-01-12
      • 2015-04-21
      • 2019-04-01
      相关资源
      最近更新 更多