【问题标题】:Cognito attribute mapping with CDK / CloudFormation使用 CDK / CloudFormation 进行 Cognito 属性映射
【发布时间】:2022-10-13 13:05:19
【问题描述】:

根据here 和其他页面上的描述,我通过 CDK 创建了一个 Cognito 用户池和一个身份池,并且在手动映射自定义属性后, 根据用户池中的自定义属性授予访问权限。

现在我正在尝试在 CDK 中做所有事情,但我不知道如何进行自定义属性的映射。我发现的唯一知道的 关于属性映射是UserPoolIdentityProvider /CfnUserPoolIdentityProvider, 但那是错误的类型,我不能用它 CfnIdentityPool认知身份提供者.

我看到了一些关于同一问题的未答复帖子 (this, 或this),但是 希望最后死去,所以我想这次也许会有答案。

我的印象是一切都可以通过 CloudFormation 实现,但这似乎是错误的,因为 this post 和其他人 建议。

那么属性映射是否可以使用 CDK 完成,或者如果我想自动执行此操作,我需要使用自定义资源和 Lambdas(或者可能是其他东西)?

【问题讨论】:

  • 我在 CDK 源代码(尽管是 TypeScript 版本)中进行了一些挖掘,发现了这个 PR:github.com/aws/aws-cdk/pull/8445/files。感觉“UserPoolIdentityProviderAmazon”可能就是您正在寻找的东西。我还找到了这个文档,其中 customAttributes 在 UserPool 和 Identity Pool 示例的上下文中被引用:docs.aws.amazon.com/cdk/api/v1/docs/aws-cognito-readme.html,它可能是相关的。您能否分享到目前为止您的 CDK 脚本的认知部分的内容?
  • 谢谢参观。我将 CDK 代码放在github.com/mciobanu/CognitoTest01。关于 UserPoolIdentityProviderAmazon:据我所知,这只是使用 Google / Facebook / 等登录的替代方法,并且不了解 Cognito 用户池。
  • @ciobi您有没有找到解决方案?
  • @floriannorbertbepunkt - 我认为 4 个月前没有解决方案,之后我没有重新讨论这个话题。我最终做的是在第一次部署后手动设置属性。它在新部署后保持不变,所以没什么大不了的。
  • 谢谢,显然 Cloudformation 仍然不支持它(因此不通过 CDK)。

标签: amazon-web-services amazon-cloudformation amazon-cognito aws-cdk


【解决方案1】:

归功于原始创作者。发现这很有用并解决了自定义资源的问题。

https://github.com/aws-samples/amazon-cognito-abac-authorization-with-react-example/blob/main/lib/cognito_identity_pool_sample-stack.ts

 new cognito.CfnIdentityPoolRoleAttachment(this, "defaultRoles", {
  identityPoolId: identityPool.ref,
  roles: {
    'authenticated': authRole.attrArn
  }
})

const createParameters = {
  "IdentityPoolId": identityPool.ref,
  "IdentityProviderName": userPool.userPoolProviderName,
  "PrincipalTags": {
    "department": "department"
  },
  "UseDefaults": false
}

const setPrincipalTagAction = {
  action: "setPrincipalTagAttributeMap",
  service: "CognitoIdentity",
  parameters: createParameters,
  physicalResourceId: customResources.PhysicalResourceId.of(identityPool.ref)
}

const { region, account }  = Stack.of(this)
const identityPoolArn = `arn:aws:cognito-identity:${region}:${account}:identitypool/${identityPool.ref}`

// Creates a Custom resource (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.custom_resources-readme.html)
// This is necessary to attach Principal Tag mappings to the Identity Pool after it has been created.
// This uses the SDK, rather than CDK code, as attaching Principal Tags through CDK is currently not supported yet
new customResources.AwsCustomResource(this, 'CustomResourcePrincipalTags', {
  onCreate: setPrincipalTagAction,
  onUpdate: setPrincipalTagAction,
  policy: customResources.AwsCustomResourcePolicy.fromSdkCalls({
    resources: [identityPoolArn],
  }),
})

【讨论】:

    猜你喜欢
    • 2018-10-30
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2010-11-06
    • 1970-01-01
    • 2018-02-26
    • 2020-10-16
    相关资源
    最近更新 更多