Added support for SSL connection to the Kafka cluster

This commit is contained in:
Nir Gazit
2021-01-17 14:45:29 +02:00
parent 895eb46961
commit 66025c7d59
5 changed files with 33 additions and 11 deletions
+4
View File
@@ -14,6 +14,10 @@ The name of the client to use when connecting to the cluster.
A list of the brokers' host names and ports to connect to.
### ssl (optional)
Configure TLS connection to the Kafka cluster. The options are passed directly to [tls.connect] and used to create the TLS secure context. Normally these would include `key` and `cert`.
Example:
```yaml
+5
View File
@@ -17,5 +17,10 @@ export interface Config {
kafka?: {
clientId: string;
brokers: string[];
ssl?: {
ca: string[];
key: string;
cert: string;
};
};
}
+13 -5
View File
@@ -16,6 +16,7 @@
import { Kafka, SeekEntry } from 'kafkajs';
import { Logger } from 'winston';
import { ConnectionOptions } from 'tls';
export type PartitionOffset = {
id: number;
@@ -27,6 +28,13 @@ export type TopicOffset = {
partitions: PartitionOffset[];
};
export type Options = {
clientId: string;
brokers: string[];
ssl?: ConnectionOptions;
logger: Logger;
};
export interface KafkaApi {
fetchTopicOffsets(topic: string): Promise<Array<PartitionOffset>>;
fetchGroupOffsets(groupId: string): Promise<Array<TopicOffset>>;
@@ -36,13 +44,13 @@ export class KafkaJsApiImpl implements KafkaApi {
private readonly kafka: Kafka;
private readonly logger: Logger;
constructor(clientId: string, brokers: string[], logger: Logger) {
logger.debug(
`creating kafka client with clientId=${clientId} and brokers=${brokers}`,
constructor(options: Options) {
options.logger.debug(
`creating kafka client with clientId=${options.clientId} and brokers=${options.brokers}`,
);
this.kafka = new Kafka({ clientId, brokers });
this.logger = logger;
this.kafka = new Kafka(options);
this.logger = options.logger;
}
async fetchTopicOffsets(topic: string): Promise<Array<PartitionOffset>> {
+5 -1
View File
@@ -20,6 +20,7 @@ import { Logger } from 'winston';
import { Config } from '@backstage/config';
import { KafkaApi, KafkaJsApiImpl } from './KafkaApi';
import _ from 'lodash';
import { ConnectionOptions } from 'tls';
export interface RouterOptions {
logger: Logger;
@@ -71,7 +72,10 @@ export async function createRouter(
const clientId = options.config.getString('kafka.clientId');
const brokers = options.config.getStringArray('kafka.brokers');
const kafkaApi = new KafkaJsApiImpl(clientId, brokers, logger);
const sslConfig = options.config.getOptional('kafka.ssl');
const ssl = sslConfig ? (sslConfig as ConnectionOptions) : undefined;
const kafkaApi = new KafkaJsApiImpl({ clientId, brokers, logger, ssl });
return makeRouter(logger, kafkaApi);
}
+6 -5
View File
@@ -2437,7 +2437,7 @@
"@backstage/catalog-model@^0.2.0":
version "0.6.1"
dependencies:
"@backstage/config" "^0.1.1"
"@backstage/config" "^0.1.2"
"@types/json-schema" "^7.0.5"
"@types/yup" "^0.29.8"
json-schema "^0.2.5"
@@ -2448,7 +2448,7 @@
"@backstage/catalog-model@^0.3.0":
version "0.6.1"
dependencies:
"@backstage/config" "^0.1.1"
"@backstage/config" "^0.1.2"
"@types/json-schema" "^7.0.5"
"@types/yup" "^0.29.8"
json-schema "^0.2.5"
@@ -2459,13 +2459,14 @@
"@backstage/core@^0.3.0":
version "0.4.4"
dependencies:
"@backstage/config" "^0.1.1"
"@backstage/core-api" "^0.2.1"
"@backstage/theme" "^0.2.1"
"@backstage/config" "^0.1.2"
"@backstage/core-api" "^0.2.8"
"@backstage/theme" "^0.2.2"
"@material-ui/core" "^4.11.0"
"@material-ui/icons" "^4.9.1"
"@material-ui/lab" "4.0.0-alpha.45"
"@types/dagre" "^0.7.44"
"@types/prop-types" "^15.7.3"
"@types/react" "^16.9"
"@types/react-sparklines" "^1.7.0"
classnames "^2.2.6"