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(streaming): add bootstrap server for MskServerless #751

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 28 additions & 5 deletions framework/API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 47 additions & 7 deletions framework/src/streaming/lib/msk/msk-serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ISecurityGroup, IVpc, SecurityGroup, SubnetSelection } from 'aws-cdk-li
import { IPrincipal, PolicyDocument } from 'aws-cdk-lib/aws-iam';
import { CfnClusterPolicy, CfnServerlessCluster } from 'aws-cdk-lib/aws-msk';

import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import { KafkaApi } from './kafka-api';
import { addClusterPolicy } from './msk-helpers';
Expand All @@ -23,11 +24,32 @@ import { Context, DataVpc, TrackedConstruct, TrackedConstructProps } from '../..
*/
export class MskServerless extends TrackedConstruct {

/**
* The MSK cluster as a CloudFormation resource
*/
public readonly cluster: CfnServerlessCluster;
/**
* The VPC where the MSK cluster is deployed
*/
public readonly vpc: IVpc;
/**
* The security group used by the cluster
*/
public readonly brokerSecurityGroup?: ISecurityGroup;
/**
* The name of the cluster
*/
public readonly clusterName: string;
/**
* The security group used by the configuration Lambda
*/
public readonly lambdaSecurityGroup: ISecurityGroup;

/**
* The list of bootstrap servers for client to connect
*/
public readonly clusterBoostrapBrokers: string;

/**
* If there is an already existing service token deployed for the custom resource
* you can reuse it to reduce the number of resource created
Expand Down Expand Up @@ -122,6 +144,25 @@ export class MskServerless extends TrackedConstruct {
value: this.serviceToken!,
exportName: `${Aws.STACK_NAME}-ServiceToken`,
});

const clusterBootstrapBrokers = new AwsCustomResource(this, 'BootstrapBrokersIam', {
onUpdate: {
service: 'Kafka',
action: 'getBootstrapBrokers',
parameters: {
ClusterArn: this.cluster.attrArn,
},
physicalResourceId: PhysicalResourceId.of('BootstrapBrokers'),
},
policy: AwsCustomResourcePolicy.fromSdkCalls({
resources: [this.cluster.attrArn],
}),
installLatestAwsSdk: false,
});

clusterBootstrapBrokers.node.addDependency(this.cluster);

this.clusterBoostrapBrokers = clusterBootstrapBrokers.getResponseField('BootstrapBrokerStringSaslIam');
}

/**
Expand Down Expand Up @@ -189,15 +230,14 @@ export class MskServerless extends TrackedConstruct {


/**
* Add a cluster policy
*
* @param {PolicyDocument} policy the IAM principal to grand the consume action.
* @param {string} id the CDK id for the Cluster Policy
* @return {CfnClusterPolicy}
*/
* Add a cluster policy
*
* @param {PolicyDocument} policy the IAM principal to grand the consume action.
* @param {string} id the CDK id for the Cluster Policy
* @return {CfnClusterPolicy}
*/
public addClusterPolicy (policy: PolicyDocument, id: string): CfnClusterPolicy {

return addClusterPolicy(this, policy, id, this.cluster);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ NagSuppressions.addResourceSuppressionsByPath(
'/stack/cluster/KafkaApi/MskIamProvider/CustomResourceProvider/framework-onEvent/ServiceRole/Resource',
'/stack/cluster/KafkaApi/MskIamProvider/CustomResourceProvider/framework-onEvent/ServiceRole/DefaultPolicy/Resource',
'/stack/cluster/KafkaApi/MskIamProvider/CustomResourceProvider/framework-onEvent/Resource',
'/stack/AWS679f53fac002430cb0da5b7982bd2287/Resource',
],
[
{ id: 'AwsSolutions-IAM4', reason: 'Managed by the L2 resource for Custom Resources we cannot modify it' },
Expand Down Expand Up @@ -80,6 +81,13 @@ NagSuppressions.addResourceSuppressionsByPath(
true,
);

NagSuppressions.addResourceSuppressionsByPath(
stack,
'/stack/cluster/LambdaSecurityGroup',
[{ id: 'AwsSolutions-EC23', reason: 'Handled with Egress rules' }],
true,
);

test('No unsuppressed Warnings', () => {
const warnings = Annotations.fromStack(stack).findWarning('*', Match.stringLikeRegexp('AwsSolutions-.*'));
console.log(warnings);
Expand Down
Loading