Skip to content

Commit

Permalink
fix(lambda): allow tokens in kafka consumer group id (#22993)
Browse files Browse the repository at this point in the history
- fix(lambda): allow token kafka consumer group id
- fix(lambda): typo in kafka consumer group id validation

Fixes: #22932

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing 
guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
nikovirtala authored Nov 30, 2022
1 parent d308bc5 commit 320cc25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,18 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
}

private validateKafkaConsumerGroupIdOrThrow(kafkaConsumerGroupId: string) {
if (cdk.Token.isUnresolved(kafkaConsumerGroupId)) {
return;
}

if (kafkaConsumerGroupId.length > 200 ||kafkaConsumerGroupId.length < 1) {
throw new Error('kafkaConsumerGroupId must be a valid string between 1 and 200 characters');
}

const regex = new RegExp(/[a-zA-Z0-9-\/*:_+=.@-]*/);
const patternMatch = regex.exec(kafkaConsumerGroupId);
if (patternMatch === null || patternMatch[0] !== kafkaConsumerGroupId) {
throw new Error('kafkaConsumerGroupId contain ivalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
throw new Error('kafkaConsumerGroupId contains invalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('event source mapping', () => {
eventSourceArn: 'arn:aws:kafka:us-east-1:123456789012:cluster/vpc-2priv-2pub/751d2973-a626-431c-9d4e-d7975eb44dd7-2',
kafkaConsumerGroupId: 'some invalid',
target: fn,
})).toThrow('kafkaConsumerGroupId contain ivalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
})).toThrow('kafkaConsumerGroupId contains invalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
});

test('throws if kafkaConsumerGroupId is too long', () => {
Expand All @@ -178,6 +178,14 @@ describe('event source mapping', () => {
})).not.toThrow();
});

test('not throws if kafkaConsumerGroupId is token', () => {
expect(() => new EventSourceMapping(stack, 'test', {
eventSourceArn: 'arn:aws:kafka:us-east-1:123456789012:cluster/vpc-2priv-2pub/751d2973-a626-431c-9d4e-d7975eb44dd7-2',
kafkaConsumerGroupId: cdk.Lazy.string({ produce: () => 'test' }),
target: fn,
})).not.toThrow();
});

test('not throws if kafkaConsumerGroupId is valid for amazon managed kafka', () => {
expect(() => new EventSourceMapping(stack, 'test', {
eventSourceArn: 'arn:aws:kafka:us-east-1:123456789012:cluster/vpc-2priv-2pub/751d2973-a626-431c-9d4e-d7975eb44dd7-2',
Expand Down

0 comments on commit 320cc25

Please sign in to comment.