Skip to content

Commit

Permalink
Add error
Browse files Browse the repository at this point in the history
Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya committed Mar 14, 2024
1 parent 326bc98 commit bb8b02b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/infra/infra-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', {

Check warning on line 430 in lib/infra/infra-stack.ts

View check run for this annotation

Codecov / codecov/patch

lib/infra/infra-stack.ts#L430

Added line #L430 was not covered by tests
port: this.opensearchPortMapping,
Expand All @@ -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', {
Expand Down
39 changes: 39 additions & 0 deletions test/opensearch-cluster-cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});

0 comments on commit bb8b02b

Please sign in to comment.