aws-cdk: (cognito-idp): cannot use Cognito identity pool for role mappings
Description
The following role mapping will fail:
const identityPool = new IdentityPool(this, "IdentityPool", {
roleMappings: [
{
providerUrl: IdentityPoolProviderUrl.custom(userPool.userPoolProviderUrl),
resolveAmbiguousRoles: false,
useToken: true
}
]
})
The reason it will fail is because the internal logic is to map the provided URL as the corresponding key value, which is performed here.
The same function is achieved in Cloudformation by specifying the key separately from the provider url. See notes specified under the IdentityProvider field description.
Use Case
Referencing user pool from the same stack.
Proposed Solution
Allow the ability to optionally specify static key when creating a role mapping.
Other information
Possible (untested) workaround is to create role attachment with Cfn resource and manually assign an arbitrary key.
const identityPool = new IdentityPool(this, "IdentityPool", {
allowUnauthenticatedIdentities: false
})
new CfnIdentityPoolRoleAttachment(this, "RoleAttachment2", {
identityPoolId: identityPool.identityPoolId,
roleMappings: {
cognito: { // 👈 manually specified key of "cognito"
type: "Token",
ambiguousRoleResolution: "Deny",
identityProvider: userPool.userPoolProviderUrl
}
}
}).node.addDependency(identityPool)
Acknowledge
- I may be able to implement this feature request
- This feature might incur a breaking change
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 16 (14 by maintainers)
Commits related to this issue
- fix(cognito-identitypool): providerUrl causes error when mappingKey is not provided and it is a token (#21191) This property is for use when the identityProvider is a Token. By default identityProvi... — committed to aws/aws-cdk by SamStephens 2 years ago
- fix(cognito-identitypool): providerUrl causes error when mappingKey is not provided and it is a token (#21191) This property is for use when the identityProvider is a Token. By default identityProvi... — committed to josephedward/aws-cdk by SamStephens 2 years ago
@rix0rrr said:
I don’t think it’s as simple as that.
IdentityPoolProviderType provides the USER_POOL constant, which I believe is for Cognito User Pools. IdentityPoolProviderUrl provides a userPool method that uses this constant. The documentation includes an example of IdentityPoolProviderUrl.userPool being used with a Cognito user pool provider URL.
I’m using the IdentityPoolProviderUrl.userPool method, and getting the same results.
(or more accurately, the Python equivalent, as I use Python). userPool is of course an earlier instantated aws_cognito.UserPool construct.
The specific error message we’re getting is similar to
The problem being that as @michaeljfazio points out, the provider URL is used as the key of a map. However map keys do not allow for Tokens, only for string constants.
As @michaeljfazio also points out, the solution is to stop providing the identity provider using the role mapping keys, and instead use the IdentityProvider attribute of the role mapping object. The existing code already sets the attribute. So all that is needed is to stop using the provider URL as the map key.
What the map key should be instead of the provider URL is an open question. Maybe it could be an optional parameter to IdentityPoolRoleMapping that you are required to provide if the providerUrl is a Token.
Thanks @SamStephens, you’re spot on, the following did the trick:
To smooth over this process, I’m proposing https://github.com/aws/aws-cdk/pull/21585
@SamStephens thanks a lot for fixing this! I ran into this issue tonight, saw this thread and when updated the CDK everything worked perfectly.
@alukach this would be a CDK change.
If you have a look at the documentation for a RoleMapping, it says:
I read this as saying that if the IdentityProvider is provided, then the key of the Role Mapping can be any arbitrary string, it does not also have to be the identifier for the identity provider. I have not verified this experimentally, however.
You can see this from the template my CDK currently generates. I’ve hardcoded the identity provider temporarily to work around this issue. The role mapping generated for my attachment is:
See the identity provider is there as the key in RoleMappings, but also present as IdentityProvider.
I’m suggesting we provide a way for the CDK to generate this as:
Instead.