Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cognito-identitypool-alpha): cannot configure roleMappings with imported userPool and client #30421

Merged
merged 17 commits into from
Sep 18, 2024

Conversation

sakurai-ryo
Copy link
Contributor

@sakurai-ryo sakurai-ryo commented Jun 1, 2024

Issue # (if applicable)

Closes #30304

Reason for this change

Currently, we cannot use imported user pools and clients for role mapping in an identity pool.
This is because the IdentityPoolProviderUrl.userPool method takes an L2 construct as its argument type instead of Interface (IUserPool, IUserPoolClient).

    const userPool = cognito.UserPool.fromUserPoolArn(this, 'CognitoUserPool', 'arn');
    const userPoolClient = cognito.UserPoolClient.fromUserPoolClientId(this, 'UserPoolClientId', 'client-id');
    const identityPool = new cognitoidp.IdentityPool(this, 'IdentityPool', {
      // ~
      roleMappings: [
        {
          mappingKey: 'cognito', 
          providerUrl: cognitoidp.IdentityPoolProviderUrl.userPool(userPool, userPoolClient), // ! type error here !
          useToken: true
        }
      ],
      allowUnauthenticatedIdentities: false
    });

Description of changes

The argument types of the IdentityPoolProviderUrl.userPool method are changed to IUserPool and IUserPoolClient.
This method requires the userPoolProviderName of the userPool, but since it does not exist for IUserPool, a property was added.
Since this property is required in the UserPool construct, it is also required in IUserPool.

public readonly userPoolProviderName: string;

I add a required attribute to the Interface of the aws-cognito module(stable), but I do not think this to be a breaking change.
Please let me know if it is not.

Description of how you validated changes

Unit tests and integ tests are added to verify that the imported userPool and clinet can be used.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added star-contributor [Pilot] contributed between 25-49 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Jun 1, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team June 1, 2024 13:44
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 1, 2024
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. Please see my comments inline.

@@ -353,13 +353,18 @@ cannot be references. For example:
import { UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';

declare const userPool: UserPool;
declare const userPoolClient: UserPoolClient;
// If you use a previously defined Cognito User Pool, use the `fromUserPoolAttributes` method instead of `fromUserPoolId` or `fromUserPoolArn`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify why this is the case. This comment alone gives me pause about these changes.

@@ -874,6 +906,48 @@ export class UserPool extends UserPoolBase {
class ImportedUserPool extends UserPoolBase {
public readonly userPoolArn = userPoolArn;
public readonly userPoolId = userPoolId;

// In the UserPool construct, the userPoolProviderName is a required property but it is constructed from the arn and id of the user pool.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm getting this. If the name is constructed from the ARN and ID of the userpool, why can't we construct this on import functions so that it's accessible to users?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, wouldn't the ID be a subset of the ARN ?

* Import an existing user pool into the stack.
*/
public static fromUserPoolAttributes(scope: Construct, id: string, attrs: UserPoolAttributes): IUserPool {
if (!attrs.userPoolArn && !attrs.userPoolId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this function is really needed here. The fromId and fromArn functions should be able to assemble and return all of the missing attributes.

Correct me if I'm wrong but the attributes for a UserPool are:

Arn: arn:<partition>:cognito-idp:<region>:<account>:userpool/<userpoolId>
ProviderName: cognito-idp.<region>.<url-suffix>/<userpoolId>
ProviderURL: https://cognito-idp.<region>.<url-suffix>/userpoolId/.well-known/jwks.json (this is listed as the token signing key url, which I think is what's being referred to here?)
UserPoolId: userpoolId

So, given that, you could add each of the missing fields as attributes in the IUserPool and then construct each of these (assuming same region as in the id for fromId) in the existing fromXxx functions so that you don't even need to create a new function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing so would mitigate every one of my comments above.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 11, 2024
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review June 12, 2024 13:51

Pull request has been modified.

@github-actions github-actions bot added p2 and removed p1 labels Jun 12, 2024
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 12, 2024
@sakurai-ryo
Copy link
Contributor Author

Hi @TheRealAmazonKendra
Thanks for the review.
Really sorry. I got mixed up with other PRs and misunderstood.
I fixed it by deleting the fromUserPoolAttributes method and constructing the ProviderName within the fromUserPoolArn method.
Also, I updated the PR description.

Copy link
Member

@Leo10Gama Leo10Gama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! Left a few more comments looking for a bit more clarification on these changes.

Comment on lines 296 to 298
const stack = new Stack();
const stack = new Stack(undefined, undefined, {
env: { region: 'us-east-1', account: '0123456789012' },
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the stack identical for this test, or does it need these additional properties to work? If so, that would probably make these breaking changes. If not, can we update the logic that checks for the new userPoolProviderName so that it works without additional props?

@@ -767,6 +767,12 @@ export interface IUserPool extends IResource {
*/
readonly userPoolArn: string;

/**
* User pool provider name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the docstring to be more similar to some of the other attributes? Feels redundant to have an attribute userPoolProviderName with the description User pool provider name.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 29, 2024
@@ -870,10 +877,14 @@ export class UserPool extends UserPoolBase {
}

const userPoolId = arnParts.resourceName;
// ex) cognito-idp.us-east-1.amazonaws.com/us-east-1_abcdefghi
const providerName = `cognito-idp.${Stack.of(scope).region}.amazonaws.com/${userPoolId}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formation logic of this string should be changed, since amazonaws.com is a partition that could be different for different users. Can you use the arnParts attributes for this instead? Something along the lines of:

`cognito-idp.${arnParts.region}.${arnParts.partition}/${userPoolId}`;

@mergify mergify bot dismissed Leo10Gama’s stale review September 15, 2024 19:51

Pull request has been modified.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation dismissed their stale review September 17, 2024 18:23

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

paulhcsun
paulhcsun previously approved these changes Sep 18, 2024
Copy link
Contributor

@paulhcsun paulhcsun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, thanks @sakurai-ryo and @Leo10Gama both!

@paulhcsun
Copy link
Contributor

@Mergifyio update

Copy link
Contributor

mergify bot commented Sep 18, 2024

update

❌ Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/github-merit-badger.yml without workflows permission

@paulhcsun
Copy link
Contributor

@Mergifyio update

Copy link
Contributor

mergify bot commented Sep 18, 2024

update

☑️ Nothing to do

  • #commits-behind > 0 [📌 update requirement]
  • -closed [📌 update requirement]
  • -conflict [📌 update requirement]
  • queue-position = -1 [📌 update requirement]

@mergify mergify bot dismissed paulhcsun’s stale review September 18, 2024 21:15

Pull request has been modified.

Copy link
Contributor

mergify bot commented Sep 18, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: c627419
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 0fdd6a9 into aws:main Sep 18, 2024
9 checks passed
Copy link
Contributor

mergify bot commented Sep 18, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-cdk-aws-cognito-identitypool-alpha: IdentityPoolProviderUrl.user_pool cant handle imported userpools
5 participants