testcontainers 8.7.2 → 8.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,17 +6,42 @@ import { AbstractStartedContainer } from "../abstract-started-container";
6
6
  import { Host } from "../../docker/types";
7
7
  import { InspectResult } from "../../docker/functions/container/inspect-container";
8
8
  export declare const KAFKA_IMAGE = "confluentinc/cp-kafka:5.5.4";
9
+ interface SaslSslListenerOptions {
10
+ sasl: SaslOptions;
11
+ port: number;
12
+ keystore: PKCS12CertificateStore;
13
+ truststore?: PKCS12CertificateStore;
14
+ }
15
+ interface SaslOptions {
16
+ mechanism: "SCRAM-SHA-256" | "SCRAM-SHA-512";
17
+ user: User;
18
+ }
19
+ interface User {
20
+ name: string;
21
+ password: string;
22
+ }
23
+ interface PKCS12CertificateStore {
24
+ content: Parameters<GenericContainer["withCopyContentToContainer"]>[0];
25
+ passphrase: string;
26
+ }
9
27
  export declare class KafkaContainer extends GenericContainer {
10
28
  private readonly uuid;
11
29
  private isZooKeeperProvided;
12
30
  private zooKeeperHost?;
13
31
  private zooKeeperPort?;
32
+ private saslSslConfig?;
14
33
  constructor(image?: string);
15
34
  withZooKeeper(host: Host, port: Port): this;
35
+ withSaslSslListener(options: SaslSslListenerOptions): this;
16
36
  protected preStart(): Promise<void>;
17
37
  start(): Promise<StartedKafkaContainer>;
18
38
  protected postStart(container: StartedTestContainer, inspectResult: InspectResult, boundPorts: BoundPorts): Promise<void>;
39
+ private addPlaintextAndSecureListener;
40
+ private addPlaintextListener;
41
+ private updateAdvertisedListeners;
42
+ private createUser;
19
43
  }
20
44
  export declare class StartedKafkaContainer extends AbstractStartedContainer {
21
45
  constructor(startedTestContainer: StartedTestContainer);
22
46
  }
47
+ export {};
@@ -13,7 +13,6 @@ exports.StartedKafkaContainer = exports.KafkaContainer = exports.KAFKA_IMAGE = v
13
13
  const generic_container_1 = require("../../generic-container/generic-container");
14
14
  const uuid_1 = require("../../uuid");
15
15
  const abstract_started_container_1 = require("../abstract-started-container");
16
- const docker_client_1 = require("../../docker/docker-client");
17
16
  const KAFKA_PORT = 9093;
18
17
  const KAFKA_BROKER_PORT = 9092;
19
18
  const DEFAULT_ZOOKEEPER_PORT = 2181;
@@ -25,7 +24,6 @@ class KafkaContainer extends generic_container_1.GenericContainer {
25
24
  this.isZooKeeperProvided = false;
26
25
  this.withExposedPorts(KAFKA_PORT)
27
26
  .withStartupTimeout(180000)
28
- .withEnv("KAFKA_LISTENERS", `PLAINTEXT://0.0.0.0:${KAFKA_PORT},BROKER://0.0.0.0:${KAFKA_BROKER_PORT}`)
29
27
  .withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT")
30
28
  .withEnv("KAFKA_INTER_BROKER_LISTENER_NAME", "BROKER")
31
29
  .withEnv("KAFKA_BROKER_ID", "1")
@@ -42,10 +40,20 @@ class KafkaContainer extends generic_container_1.GenericContainer {
42
40
  this.zooKeeperPort = port;
43
41
  return this;
44
42
  }
43
+ withSaslSslListener(options) {
44
+ this.saslSslConfig = options;
45
+ return this;
46
+ }
45
47
  preStart() {
46
48
  return __awaiter(this, void 0, void 0, function* () {
47
49
  const network = this.networkMode && this.networkAliases.length > 0 ? this.networkAliases[0] : "localhost";
48
50
  this.withEnv("KAFKA_ADVERTISED_LISTENERS", `BROKER://${network}:${KAFKA_BROKER_PORT}`);
51
+ if (this.saslSslConfig) {
52
+ this.addPlaintextAndSecureListener(this.saslSslConfig);
53
+ }
54
+ else {
55
+ this.addPlaintextListener();
56
+ }
49
57
  let command = "#!/bin/bash\n";
50
58
  if (this.isZooKeeperProvided) {
51
59
  this.withEnv("KAFKA_ZOOKEEPER_CONNECT", `${this.zooKeeperHost}:${this.zooKeeperPort}`);
@@ -74,9 +82,41 @@ class KafkaContainer extends generic_container_1.GenericContainer {
74
82
  });
75
83
  }
76
84
  postStart(container, inspectResult, boundPorts) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ yield this.updateAdvertisedListeners(container, inspectResult, boundPorts);
87
+ if (this.saslSslConfig) {
88
+ yield this.createUser(container, this.saslSslConfig.sasl);
89
+ }
90
+ });
91
+ }
92
+ addPlaintextAndSecureListener({ port, sasl, keystore, truststore }) {
93
+ this.withEnv("KAFKA_LISTENERS", `SECURE://0.0.0.0:${port},PLAINTEXT://0.0.0.0:${KAFKA_PORT},BROKER://0.0.0.0:${KAFKA_BROKER_PORT}`)
94
+ .withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SECURE:SASL_SSL")
95
+ .withEnv("KAFKA_SSL_PROTOCOL", "TLSv1.2")
96
+ .withCopyContentToContainer(keystore.content, "/etc/kafka/secrets/server.keystore.pfx")
97
+ .withEnv("KAFKA_SSL_KEYSTORE_LOCATION", "/etc/kafka/secrets/server.keystore.pfx")
98
+ .withEnv("KAFKA_SSL_KEYSTORE_PASSWORD", keystore.passphrase)
99
+ .withEnv("KAFKA_SSL_KEYSTORE_TYPE", "PKCS12")
100
+ .withEnv("KAFKA_SASL_ENABLED_MECHANISMS", sasl.mechanism)
101
+ .withEnv(`KAFKA_LISTENER_NAME_SECURE_${sasl.mechanism}_SASL_JAAS_CONFIG`, "org.apache.kafka.common.security.scram.ScramLoginModule required;")
102
+ .addExposedPorts(KAFKA_PORT, port);
103
+ if (truststore) {
104
+ this.withCopyContentToContainer(truststore.content, "/etc/kafka/secrets/server.truststore.pfx")
105
+ .withEnv("KAFKA_SSL_TRUSTSTORE_LOCATION", "/etc/kafka/secrets/server.truststore.pfx")
106
+ .withEnv("KAFKA_SSL_TRUSTSTORE_PASSWORD", truststore.passphrase)
107
+ .withEnv("KAFKA_SSL_TRUSTSTORE_TYPE", "PKCS12");
108
+ }
109
+ }
110
+ addPlaintextListener() {
111
+ this.withEnv("KAFKA_LISTENERS", `PLAINTEXT://0.0.0.0:${KAFKA_PORT},BROKER://0.0.0.0:${KAFKA_BROKER_PORT}`).addExposedPorts(KAFKA_PORT);
112
+ }
113
+ updateAdvertisedListeners(container, inspectResult, boundPorts) {
77
114
  return __awaiter(this, void 0, void 0, function* () {
78
115
  const brokerAdvertisedListener = `BROKER://${inspectResult.hostname}:${KAFKA_BROKER_PORT}`;
79
- const bootstrapServers = `PLAINTEXT://${(yield docker_client_1.dockerClient).host}:${boundPorts.getBinding(KAFKA_PORT)}`;
116
+ let bootstrapServers = `PLAINTEXT://${container.getHost()}:${boundPorts.getBinding(KAFKA_PORT)}`;
117
+ if (this.saslSslConfig) {
118
+ bootstrapServers = `${bootstrapServers},SECURE://${container.getHost()}:${boundPorts.getBinding(this.saslSslConfig.port)}`;
119
+ }
80
120
  const { output, exitCode } = yield container.exec([
81
121
  "kafka-configs",
82
122
  "--alter",
@@ -94,6 +134,27 @@ class KafkaContainer extends generic_container_1.GenericContainer {
94
134
  }
95
135
  });
96
136
  }
137
+ createUser(container, { user: { name, password }, mechanism }) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ const { output, exitCode } = yield container.exec([
140
+ "kafka-configs",
141
+ "--alter",
142
+ // At the time of writing kafka-configs displays a warning stating that the 'zookeeper' flag is deprecated in favor of 'bootstrap-server'.
143
+ // Unfortunately, 'bootstrap-server' can only be used to set quotas and not to create a user.
144
+ "--zookeeper",
145
+ this.env["KAFKA_ZOOKEEPER_CONNECT"],
146
+ "--entity-type",
147
+ "users",
148
+ "--entity-name",
149
+ `${name}`,
150
+ "--add-config",
151
+ `${mechanism}=[password=${password}]`,
152
+ ]);
153
+ if (exitCode !== 0) {
154
+ throw new Error(`Kafka container configuration failed with exit code ${exitCode}: ${output}`);
155
+ }
156
+ });
157
+ }
97
158
  }
98
159
  exports.KafkaContainer = KafkaContainer;
99
160
  class StartedKafkaContainer extends abstract_started_container_1.AbstractStartedContainer {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "testcontainers",
3
3
  "author": "Cristian Greco",
4
- "version": "8.7.2",
4
+ "version": "8.8.0",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
7
7
  "keywords": [