diff --git a/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts b/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts index c90d2ed78a46b..762566ea97e63 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts @@ -1,6 +1,6 @@ import * as acm from 'aws-cdk-lib/aws-certificatemanager'; import * as iam from 'aws-cdk-lib/aws-iam'; -import { Lazy, Resource, IResolvable } from 'aws-cdk-lib/core'; +import { Lazy, Resource, IResolvable, Token } from 'aws-cdk-lib/core'; import { Construct } from 'constructs'; import { CfnDomain } from 'aws-cdk-lib/aws-amplify'; import { IApp } from './app'; @@ -131,6 +131,13 @@ export class Domain extends Resource { this.subDomains = props.subDomains || []; const domainName = props.domainName || id; + if (!Token.isUnresolved(domainName) && domainName.length > 255) { + throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`); + } + if (!Token.isUnresolved(domainName) && !/^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9])\.)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(\.)?$/.test(domainName)) { + throw new Error(`Domain name must be a valid hostname, got: ${domainName}.`); + } + const domain = new CfnDomain(this, 'Resource', { appId: props.app.appId, domainName, diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts b/packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts index cbf8d9d9a42d2..40e5d9c43bffd 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts @@ -176,6 +176,55 @@ test('map a branch to the domain root', () => { }); }); +test('throw error for invalid domain name length', () => { + const stack = new Stack(); + const app = new amplify.App(stack, 'App', { + sourceCodeProvider: new amplify.GitHubSourceCodeProvider({ + owner: 'aws', + repository: 'aws-cdk', + oauthToken: SecretValue.unsafePlainText('secret'), + }), + }); + const prodBranch = app.addBranch('main'); + + expect(() => app.addDomain('Domain', { + subDomains: [ + { + branch: prodBranch, + prefix: 'prod', + }, + ], + domainName: 'a'.repeat(256), + })).toThrow('Domain name must be 255 characters or less, got: 256 characters.'); +}); + +test.each([ + '-example.com', + 'example..com', + 'example.com-', + 'ex@mple.com', +])('throw error for invalid domain name', (domainName) => { + const stack = new Stack(); + const app = new amplify.App(stack, 'App', { + sourceCodeProvider: new amplify.GitHubSourceCodeProvider({ + owner: 'aws', + repository: 'aws-cdk', + oauthToken: SecretValue.unsafePlainText('secret'), + }), + }); + const prodBranch = app.addBranch('main'); + + expect(() => app.addDomain('Domain', { + subDomains: [ + { + branch: prodBranch, + prefix: 'prod', + }, + ], + domainName, + })).toThrow(`Domain name must be a valid hostname, got: ${domainName}.`); +}); + test('throws at synthesis without subdomains', () => { // GIVEN const app = new App(); diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json index f02a474074771..3bc04549756e1 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json @@ -1,5 +1,5 @@ { - "version": "36.0.0", + "version": "38.0.1", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json index f1c3c1e152115..fe18053d305be 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json @@ -1,7 +1,7 @@ { - "version": "36.0.0", + "version": "38.0.1", "files": { - "5314d52dac3409b06902f1cf40df3d58dceb16146ad8cf66843edee0096d1d87": { + "52c01435146f94b28f058464ccdc77ecea779ba1f2809be438facf8863e8b544": { "source": { "path": "cdk-amplify-app-custom-domain.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "5314d52dac3409b06902f1cf40df3d58dceb16146ad8cf66843edee0096d1d87.json", + "objectKey": "52c01435146f94b28f058464ccdc77ecea779ba1f2809be438facf8863e8b544.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.template.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.template.json index 5909af114f54a..8ceb21aa67975 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.template.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.template.json @@ -99,7 +99,7 @@ "EnablePullRequestPreview": true } }, - "Appexamplecom6AF1A3AD": { + "Appexamplecom93D8EC68": { "Type": "AWS::Amplify::Domain", "Properties": { "AppId": { @@ -124,7 +124,7 @@ "Ref": "Certificate4E7ABB08" } }, - "DomainName": "*.example.com", + "DomainName": "example.com", "EnableAutoSubDomain": false, "SubDomainSettings": [ { @@ -156,10 +156,10 @@ "Certificate4E7ABB08": { "Type": "AWS::CertificateManager::Certificate", "Properties": { - "DomainName": "*.*.example.com", + "DomainName": "*.example.com", "DomainValidationOptions": [ { - "DomainName": "*.*.example.com", + "DomainName": "*.example.com", "HostedZoneId": "Z23ABC4XYZL05B" } ], diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk.out b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk.out index 1f0068d32659a..c6e612584e352 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk.out +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"36.0.0"} \ No newline at end of file +{"version":"38.0.1"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/integ.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/integ.json index b8c953f413ca9..76fede549a071 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/integ.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/integ.json @@ -1,6 +1,6 @@ { "enableLookups": true, - "version": "36.0.0", + "version": "38.0.1", "testCases": { "amplify-app-custom-domain-integ/DefaultTest": { "stacks": [ diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/manifest.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/manifest.json index 1586bbb9fed72..e124d3c961c65 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/manifest.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "36.0.0", + "version": "38.0.1", "artifacts": { "cdk-amplify-app-custom-domain.assets": { "type": "cdk:asset-manifest", @@ -16,9 +16,10 @@ "templateFile": "cdk-amplify-app-custom-domain.template.json", "terminationProtection": false, "validateOnSynth": false, + "notificationArns": [], "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/5314d52dac3409b06902f1cf40df3d58dceb16146ad8cf66843edee0096d1d87.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/52c01435146f94b28f058464ccdc77ecea779ba1f2809be438facf8863e8b544.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -70,19 +71,16 @@ "data": "AppdevB328DAFC" } ], - "/cdk-amplify-app-custom-domain/App/*.example.com/Resource": [ + "/cdk-amplify-app-custom-domain/App/example.com/Resource": [ { "type": "aws:cdk:logicalId", - "data": "Appexamplecom6AF1A3AD" + "data": "Appexamplecom93D8EC68" } ], "/cdk-amplify-app-custom-domain/Certificate/Resource": [ { "type": "aws:cdk:logicalId", - "data": "Certificate4E7ABB08", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_REPLACE" - ] + "data": "Certificate4E7ABB08" } ], "/cdk-amplify-app-custom-domain/BootstrapVersion": [ @@ -115,6 +113,7 @@ "templateFile": "amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.template.json", "terminationProtection": false, "validateOnSynth": false, + "notificationArns": [], "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/tree.json b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/tree.json index d91f03344d460..4e519a4374bde 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/tree.json +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/tree.json @@ -22,14 +22,14 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_codecommit.CfnRepository", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_codecommit.Repository", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "App": { @@ -44,8 +44,8 @@ "id": "ImportRole", "path": "cdk-amplify-app-custom-domain/App/Role/ImportRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "Resource": { @@ -69,8 +69,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "DefaultPolicy": { @@ -107,20 +107,20 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Policy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "Resource": { @@ -149,8 +149,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_amplify.CfnApp", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "main": { @@ -175,13 +175,13 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_amplify.CfnBranch", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-amplify-alpha.Branch", "version": "0.0.0" } }, @@ -207,23 +207,23 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_amplify.CfnBranch", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-amplify-alpha.Branch", "version": "0.0.0" } }, - "*.example.com": { - "id": "*.example.com", - "path": "cdk-amplify-app-custom-domain/App/*.example.com", + "example.com": { + "id": "example.com", + "path": "cdk-amplify-app-custom-domain/App/example.com", "children": { "Resource": { "id": "Resource", - "path": "cdk-amplify-app-custom-domain/App/*.example.com/Resource", + "path": "cdk-amplify-app-custom-domain/App/example.com/Resource", "attributes": { "aws:cdk:cloudformation:type": "AWS::Amplify::Domain", "aws:cdk:cloudformation:props": { @@ -249,7 +249,7 @@ "Ref": "Certificate4E7ABB08" } }, - "domainName": "*.example.com", + "domainName": "example.com", "enableAutoSubDomain": false, "subDomainSettings": [ { @@ -279,19 +279,19 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_amplify.CfnDomain", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-amplify-alpha.Domain", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-amplify-alpha.App", "version": "0.0.0" } }, @@ -299,8 +299,8 @@ "id": "HostedZone", "path": "cdk-amplify-app-custom-domain/HostedZone", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "Certificate": { @@ -313,10 +313,10 @@ "attributes": { "aws:cdk:cloudformation:type": "AWS::CertificateManager::Certificate", "aws:cdk:cloudformation:props": { - "domainName": "*.*.example.com", + "domainName": "*.example.com", "domainValidationOptions": [ { - "domainName": "*.*.example.com", + "domainName": "*.example.com", "hostedZoneId": "Z23ABC4XYZL05B" } ], @@ -330,36 +330,36 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_certificatemanager.CfnCertificate", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_certificatemanager.Certificate", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "BootstrapVersion": { "id": "BootstrapVersion", "path": "cdk-amplify-app-custom-domain/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "cdk-amplify-app-custom-domain/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "amplify-app-custom-domain-integ": { @@ -375,7 +375,7 @@ "path": "amplify-app-custom-domain-integ/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.3.0" + "version": "10.4.2" } }, "DeployAssert": { @@ -386,22 +386,22 @@ "id": "BootstrapVersion", "path": "amplify-app-custom-domain-integ/DefaultTest/DeployAssert/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "amplify-app-custom-domain-integ/DefaultTest/DeployAssert/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } }, @@ -421,13 +421,13 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.3.0" + "version": "10.4.2" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.App", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.4.2" } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts index 2d38147cb0475..3a6338676a6de 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts @@ -58,7 +58,7 @@ const hostedZoneId = process.env.CDK_INTEG_HOSTED_ZONE_ID ?? process.env.HOSTED_ if (!hostedZoneId) throw new Error('For this test you must provide your own HostedZoneId as an env var "HOSTED_ZONE_ID". See framework-integ/README.md for details.'); const hostedZoneName = process.env.CDK_INTEG_HOSTED_ZONE_NAME ?? process.env.HOSTED_ZONE_NAME; if (!hostedZoneName) throw new Error('For this test you must provide your own HostedZoneName as an env var "HOSTED_ZONE_NAME". See framework-integ/README.md for details.'); -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; if (!domainName) throw new Error('For this test you must provide your own DomainName as an env var "DOMAIN_NAME". See framework-integ/README.md for details.'); const app = new App();