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

Cognito Construct Library #95

Closed
7 of 10 tasks
nija-at opened this issue Jan 27, 2020 · 6 comments
Closed
7 of 10 tasks

Cognito Construct Library #95

nija-at opened this issue Jan 27, 2020 · 6 comments
Assignees
Labels
management/tracking status/done Implementation complete

Comments

@nija-at
Copy link
Contributor

nija-at commented Jan 27, 2020

PR Champion
#91 @eladb

Description

Increase coverage of CDK construct library for Cognito to cover a lot more features than it currently does. Where necessary, re-design the APIs so they are more ergonomic and extendible.

Progress

  • Tracking Issue Created
  • RFC PR Created
  • Core Team Member Assigned
  • Initial Approval / Final Comment Period
  • Ready For Implementation
    • implement User Pool construct
    • implement UserPoolUser and UserPoolGroup constructs
    • implement IdentityPool & IdentityPoolRoleAttachment constructs
    • open issues to track unimplemented features
  • Resolved
@nija-at nija-at self-assigned this Jan 27, 2020
@eladb eladb changed the title cognito construct library Cognito Construct Library Jan 27, 2020
@nija-at nija-at added status/review Proposal pending review/revision and removed status/proposed Newly proposed RFC labels Jan 29, 2020
@nija-at nija-at added status/final-comment-period Pending final approval status/approved Ready for implementation and removed status/review Proposal pending review/revision status/final-comment-period Pending final approval labels Feb 11, 2020
@0xdevalias
Copy link

Curious if there is a current ETA on these features landing/if they are actively being worked on at the moment?

@nija-at
Copy link
Contributor Author

nija-at commented Mar 18, 2020

Some of these have started landing - https://github.com/aws/aws-cdk/commits/master/packages/%40aws-cdk/aws-cognito/lib, however, we don't have an ETA.

Is there a particular feature you're looking for?

@0xdevalias
Copy link

0xdevalias commented Mar 19, 2020

I'm basically looking to setup a new cognito auth stack, custom domain, some federated logins, attach some custom lambda hooks, etc.

I saw the cognito construct features that landed very recently, but haven't incorporated those yet. So far i've been working around with everything else as required at the Cfn* CloudFormation level + using escape hatches.


Another issue I have just run into (though it's a CloudFormation deficiency, so beyond the scope of CDK, unless you were to roll a custom resource to resolve it) is that it's not currently possible to retrieve the CloudFront target when setting a custom domain, as per aws-cloudformation/cloudformation-coverage-roadmap#356 (Edit: added an example custom resource workaround in my comment)


Also, tangentially related, it would be nice if we could construct a CloudFrontTarget from a string of the distribution's domain name:

A very quick and dirty hacky workaround example:

class CloudFrontFromStringTarget {
  constructor(distributionDomainName) {
    this.distributionDomainName = distributionDomainName
  }
  bind(_record) {
    return {
      // CloudFront Zone ID
      hostedZoneId: 'Z2FDTNDATAQYW2',
      dnsName: this.distributionDomainName
    }
  }
}

@0xdevalias
Copy link

0xdevalias commented Mar 19, 2020

For reference, I'll include some code snippets from my project that cover some of the things i needed that aren't yet supported here.

Example: Using a custom domain name with UserPool
const cdk = require('@aws-cdk/core')
const cognito = require('@aws-cdk/aws-cognito')
const cr = require('@aws-cdk/custom-resources')
const route53 = require('@aws-cdk/aws-route53')

/**
 * Configures the UserPool domain used for authentication.
 *
 * @see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cognito.CfnUserPoolDomain.html
 * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpooldomain.html
 * @see https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html
 */
const userPoolDomain = new cognito.CfnUserPoolDomain(
  this,
  'UserPoolDomain',
  {
    userPoolId: userPool.userPoolId,
    domain: authDomain,
    customDomainConfig: {
      certificateArn,
    },
  }
)
userPoolDomain.node.addDependency(userPool)
new cdk.CfnOutput(this, 'UserPoolDomainValue', {
  value: userPoolDomain.domain,
})

// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#describeUserPoolDomain-property
const describeCognitoUserPoolDomain = new cr.AwsCustomResource(
  this,
  'DescribeCognitoUserPoolDomain',
  {
    resourceType: 'Custom::DescribeCognitoUserPoolDomain',
    onCreate: {
      region: 'us-east-1',
      service: 'CognitoIdentityServiceProvider',
      action: 'describeUserPoolDomain',
      parameters: {
        Domain: userPoolDomain.domain,
      },
      physicalResourceId: cr.PhysicalResourceId.of(userPoolDomain.domain),
    },
    // TODO: can we restrict this policy more? Get the ARN for the user pool domain? Or the user pool maybe?
    policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
      resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
    }),
  }
)
describeCognitoUserPoolDomain.node.addDependency(userPoolDomain)

const userPoolDomainDistribution = describeCognitoUserPoolDomain.getResponseField(
  'DomainDescription.CloudFrontDistribution'
)
new cdk.CfnOutput(this, 'UserPoolDomainDistribution', {
  value: userPoolDomainDistribution,
})

// Route53 alias record for the UserPoolDomain CloudFront distribution
new route53.ARecord(this, 'UserPoolDomainAliasRecord', {
  recordName: userPoolDomain.domain,
  target: route53.RecordTarget.fromAlias({
    bind: _record => ({
      hostedZoneId: 'Z2FDTNDATAQYW2', // CloudFront Zone ID
      dnsName: userPoolDomainDistribution,
    }),
  }),
  zone,
})
Example: Getting the UserPoolClient secret

Ref: aws/aws-cdk#3037 (comment)

const describeCognitoUserPoolClient = new cr.AwsCustomResource(
  this,
  'DescribeCognitoUserPoolClient',
  {
    resourceType: 'Custom::DescribeCognitoUserPoolClient',
    onCreate: {
      region: 'us-east-1',
      service: 'CognitoIdentityServiceProvider',
      action: 'describeUserPoolClient',
      parameters: {
        UserPoolId: userPool.userPoolId,
        ClientId: userPoolClient.userPoolClientId,
      },
      physicalResourceId: cr.PhysicalResourceId.of(userPoolClient.userPoolClientId),
    },
    // TODO: can we restrict this policy more?
    policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
      resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
    }),
  }
)

const userPoolClientSecret = describeCognitoUserPoolClient.getResponseField(
  'UserPoolClient.ClientSecret'
)
new cdk.CfnOutput(this, 'UserPoolClientSecret', {
  value: userPoolClientSecret,
})

@0xdevalias
Copy link

0xdevalias commented Mar 19, 2020

Apologies, but I have removed this comment. Please file a separate bug for issues you are facing. (see aws/aws-cdk#6811) Thanks!
-- @nija-at

@nija-at
Copy link
Contributor Author

nija-at commented May 26, 2020

Please continue tracking progress on Cognito here - aws/aws-cdk#6765

Resolving this issue.

@nija-at nija-at closed this as completed May 26, 2020
@eladb eladb added status/implementing RFC is being implemented and removed status/approved Ready for implementation labels Jun 22, 2020
@eladb eladb reopened this Jun 22, 2020
@eladb eladb closed this as completed Jun 22, 2020
@nija-at nija-at added status/done Implementation complete and removed status/implementing RFC is being implemented labels Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
management/tracking status/done Implementation complete
Projects
None yet
Development

No branches or pull requests

3 participants