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

fix(opensearch): add validation to domainName property #17017

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/@aws-cdk/aws-opensearchservice/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,9 +1472,9 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {

if (props.logging?.auditLogEnabled) {
this.auditLogGroup = props.logging.auditLogGroup ??
new logs.LogGroup(this, 'AuditLogs', {
retention: logs.RetentionDays.ONE_MONTH,
});
new logs.LogGroup(this, 'AuditLogs', {
retention: logs.RetentionDays.ONE_MONTH,
});

logGroups.push(this.auditLogGroup);
};
Expand Down Expand Up @@ -1624,7 +1624,17 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {

if (logGroupResourcePolicy) { this.domain.node.addDependency(logGroupResourcePolicy); }

if (props.domainName) { this.node.addMetadata('aws:cdk:hasPhysicalName', props.domainName); }
if (props.domainName) {
if (!cdk.Token.isUnresolved(props.domainName) && !props.domainName.match(/^[a-z][a-z0-9\-]{2,26}$/)) {
nom3ad marked this conversation as resolved.
Show resolved Hide resolved
// https://docs.aws.amazon.com/opensearch-service/latest/developerguide/configuration-api.html#configuration-api-datatypes-domainname
throw new Error(
`domainName '${props.domainName}' failed to satisfy constraint. ` +
'It must start with a lowercase letter and must be between 3 and 28 characters. ' +
'Valid characters are a-z (lowercase only), 0-9, and – (hyphen).',
);
}
this.node.addMetadata('aws:cdk:hasPhysicalName', props.domainName);
}

this.domainName = this.getResourceNameAttribute(this.domain.ref);

Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-opensearchservice/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,18 @@ describe('custom error responses', () => {
})).toThrow('Unknown Elasticsearch version: 5.4');
});


test('error when invalid domain name is given', () => {
expect(() => new Domain(stack, 'Domain1', {
version: EngineVersion.OPENSEARCH_1_0,
domainName: 'InvalidName',
})).toThrow(/domainName 'InvalidName' failed to satisfy constraint/);
expect(() => new Domain(stack, 'Domain1', {
nom3ad marked this conversation as resolved.
Show resolved Hide resolved
version: EngineVersion.OPENSEARCH_1_0,
domainName: 'a'.repeat(29),
})).toThrow(/domainName 'a{29}' failed to satisfy constraint/);
});

test('error when error log publishing is enabled for Elasticsearch version < 5.1', () => {
const error = /Error logs publishing requires Elasticsearch version 5.1 or later or OpenSearch version 1.0 or later/;
expect(() => new Domain(stack, 'Domain1', {
Expand Down