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

chore(amplify): domain name validation #31959

Merged
merged 9 commits into from
Nov 1, 2024

Conversation

badmintoncryer
Copy link
Contributor

Issue # (if applicable)

None

Reason for this change

We can configure amplify domain name but there is no validation for that.

Description of changes

Add validation for an amplify domain name

  • length must be lower than 255 characters
  • match with the regular expression
    • /^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9]).)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(.)?$/

Description of how you validated changes

Add unit test

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 the p2 label Oct 31, 2024
@github-actions github-actions bot added the distinguished-contributor [Pilot] contributed 50+ PRs to the CDK label Oct 31, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team October 31, 2024 12:53
Copy link
Contributor

@mazyu36 mazyu36 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 the contribution.
some minor comments.

@@ -131,6 +131,13 @@ export class Domain extends Resource {
this.subDomains = props.subDomains || [];

const domainName = props.domainName || id;
if (domainName.length > 255) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we might need to check token.

Token.isUnresolved(domainName)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I keep forgetting to check the token no matter how many times I try... Thank you for the reminder!

@@ -131,6 +131,13 @@ export class Domain extends Resource {
this.subDomains = props.subDomains || [];

const domainName = props.domainName || id;
if (domainName.length > 255) {
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length}`);
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`);

I think it would be more helpful to include "characters" in the error message.

},
],
domainName: 'a'.repeat(256),
})).toThrow('Domain name must be 255 characters or less, got: 256');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
})).toThrow('Domain name must be 255 characters or less, got: 256');
})).toThrow('Domain name must be 255 characters or less, got: 256 characters.');

@badmintoncryer
Copy link
Contributor Author

badmintoncryer commented Oct 31, 2024

@mazyu36 Thank you for your really quick review!! I've addressed all of your comments.

Unfortunately, the integ.app-custom-domain.ts you previously created has failed. The following error message appears, but do you know where the example.com shown here is configured?

/codebuild/output/src3849279982/src/github.com/aws/aws-cdk/packages/@aws-cdk/aws-amplify-alpha/lib/domain.js:30
@aws-cdk/aws-amplify-alpha:             throw new Error(`Domain name must be a valid hostname, got: ${domainName}`);
@aws-cdk/aws-amplify-alpha:             ^
@aws-cdk/aws-amplify-alpha: Error: Domain name must be a valid hostname, got: *.example.com
@aws-cdk/aws-amplify-alpha:     at new Domain (/codebuild/output/src3849279982/src/github.com/aws/aws-cdk/packages/@aws-cdk/aws-amplify-alpha/lib/domain.js:30:19)
@aws-cdk/aws-amplify-alpha:     at App.addDomain (/codebuild/output/src3849279982/src/github.com/aws/aws-cdk/packages/@aws-cdk/aws-amplify-alpha/lib/app.js:151:16)
@aws-cdk/aws-amplify-alpha:     at new TestStack (/codebuild/output/src3849279982/src/github.com/aws/aws-cdk/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js:28:28)
@aws-cdk/aws-amplify-alpha:     at Object.<anonymous> (/codebuild/output/src3849279982/src/github.com/aws/aws-cdk/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js:54:15)
@aws-cdk/aws-amplify-alpha:     at Module._compile (node:internal/modules/cjs/loader:1364:14)
@aws-cdk/aws-amplify-alpha:     at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
@aws-cdk/aws-amplify-alpha:     at Module.load (node:internal/modules/cjs/loader:1203:32)
@aws-cdk/aws-amplify-alpha:     at Module._load (node:internal/modules/cjs/loader:1019:12)
@aws-cdk/aws-amplify-alpha:     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
@aws-cdk/aws-amplify-alpha:     at node:internal/main/run_main_module:28:49
@aws-cdk/aws-amplify-alpha: Node.js v18.20.4
@aws-cdk/aws-amplify-alpha:   ERROR      integ.app-custom-domain 0.509s

@mazyu36
Copy link
Contributor

mazyu36 commented Oct 31, 2024

@badmintoncryer
Ah...
That domain name is actually a placeholder value defined below.
https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/integ-runner/lib/runner/runner-base.ts#L430

For integ test runs, the actual domain is set in an environment variable.
However, in the synthesized template, no personal domain names are retained, and only placeholder values remain.
It seems that this placeholder value causes a validation error.

I’m currently considering ways to avoid this, but I haven’t found a good solution yet.
Changing the placeholder value would require rerunning all integration tests that rely on this placeholder.
I also thought about modifying the placeholder just for this specific test, but even then, this integration test would still need to be rerun...

@badmintoncryer
Copy link
Contributor Author

badmintoncryer commented Oct 31, 2024

@mazyu36 Thank you for your detailed information! I understand very well!

If I modify the referenced value to example.com as shown below and then rerun the integ test, would that resolve the issue?

- const domainName = process.env.CDK_INTEG_DOMAIN_NAME ?? process.env.DOMAIN_NAME;
+ const domainName = process.env.CDK_INTEG_HOSTED_ZONE_NAME ?? process.env.DOMAIN_NAME;

This is a good opportunity, so I’ll go ahead and try purchasing a domain too. Haha

@mazyu36
Copy link
Contributor

mazyu36 commented Oct 31, 2024

@badmintoncryer
Yes, with that approach, the placeholder domain name should become example.com, so it should resolve the issue.
As you noted, purchasing the domain and rerunning the test will be necessary...🫣

@badmintoncryer
Copy link
Contributor Author

@mazyu36 I've bought my domain and rerun integ test.

@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 Nov 1, 2024
Copy link
Contributor

@mazyu36 mazyu36 left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

@paulhcsun
Copy link
Contributor

@Mergifyio update

Copy link
Contributor

mergify bot commented Nov 1, 2024

update

✅ Branch has been successfully updated

Copy link
Contributor

mergify bot commented Nov 1, 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 aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Nov 1, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 91efe52
  • 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 5137e38 into aws:main Nov 1, 2024
12 checks passed
Copy link
Contributor

mergify bot commented Nov 1, 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

github-actions bot commented Nov 1, 2024

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 Nov 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants