diff --git a/lib/infra/infra-stack.ts b/lib/infra/infra-stack.ts index 384644b95c2..8d0b1cd557d 100644 --- a/lib/infra/infra-stack.ts +++ b/lib/infra/infra-stack.ts @@ -403,6 +403,8 @@ export class InfraStack extends Stack { }); const opensearchPortMap = `${props?.mapOpensearchPortTo ?? scope.node.tryGetContext('mapOpensearchPortTo')}`; + const opensearchDashboardsPortMap = `${props?.mapOpensearchDashboardsPortTo ?? scope.node.tryGetContext('mapOpensearchDashboardsPortTo')}`; + if (opensearchPortMap === 'undefined') { if (!this.securityDisabled && !this.minDistribution) { this.opensearchPortMapping = 443; @@ -413,6 +415,17 @@ export class InfraStack extends Stack { this.opensearchPortMapping = parseInt(opensearchPortMap, 10); } + if (opensearchDashboardsPortMap === 'undefined') { + this.opensearchDashboardsPortMapping = 8443; + } else { + this.opensearchDashboardsPortMapping = parseInt(opensearchDashboardsPortMap, 10); + } + + if (this.opensearchPortMapping === this.opensearchDashboardsPortMapping) { + throw new Error('OpenSearch and OpenSearch-Dashboards cannot be mapped to the same port! Please provide different port numbers.' + + ` Current mapping is OpenSearch:${this.opensearchPortMapping} OpenSearch-Dashboards:${this.opensearchDashboardsPortMapping}`); + } + if (!this.securityDisabled && !this.minDistribution && this.opensearchPortMapping === 443 && certificateArn !== 'undefined') { opensearchListener = nlb.addListener('opensearch', { port: this.opensearchPortMapping, @@ -426,13 +439,6 @@ export class InfraStack extends Stack { }); } - const opensearchDashboardsPortMap = `${props?.mapOpensearchDashboardsPortTo ?? scope.node.tryGetContext('mapOpensearchDashboardsPortTo')}`; - if (opensearchDashboardsPortMap === 'undefined') { - this.opensearchDashboardsPortMapping = 8443; - } else { - this.opensearchDashboardsPortMapping = parseInt(opensearchDashboardsPortMap, 10); - } - if (this.dashboardsUrl !== 'undefined') { if (!this.securityDisabled && !this.minDistribution && this.opensearchDashboardsPortMapping === 443 && certificateArn !== 'undefined') { dashboardsListener = nlb.addListener('dashboards', { diff --git a/test/opensearch-cluster-cdk.test.ts b/test/opensearch-cluster-cdk.test.ts index a68fd5f52d5..d38edc4a520 100644 --- a/test/opensearch-cluster-cdk.test.ts +++ b/test/opensearch-cluster-cdk.test.ts @@ -987,3 +987,42 @@ test('Ignore cert and TLS protocol if none of the ports map to 443', () => { Protocol: 'TCP', }); }); + +test('Throw error on duplicate ports', () => { + const app = new App({ + context: { + securityDisabled: false, + minDistribution: false, + distributionUrl: 'www.example.com', + cpuArch: 'x64', + singleNodeCluster: false, + dashboardsUrl: 'www.example.com', + distVersion: '1.0.0', + serverAccessType: 'ipv4', + restrictServerAccessTo: 'all', + certificateArn: 'arn:1234', + mapOpensearchPortTo: '8443', + }, + }); + // WHEN + try { + // WHEN + const networkStack = new NetworkStack(app, 'opensearch-network-stack', { + env: { account: 'test-account', region: 'us-east-1' }, + }); + + // @ts-ignore + const infraStack = new InfraStack(app, 'opensearch-infra-stack', { + vpc: networkStack.vpc, + securityGroup: networkStack.osSecurityGroup, + env: { account: 'test-account', region: 'us-east-1' }, + }); + + // eslint-disable-next-line no-undef + fail('Expected an error to be thrown'); + } catch (error) { + expect(error).toBeInstanceOf(Error); + // @ts-ignore + expect(error.message).toEqual('OpenSearch and OpenSearch-Dashboards cannot be mapped to the same port! Please provide different port numbers. Current mapping is OpenSearch:8443 OpenSearch-Dashboards:8443'); + } +});