-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
KafkaModule.java
380 lines (303 loc) · 15.1 KB
/
KafkaModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package org.akhq.modules;
import com.google.common.collect.ImmutableMap;
import io.confluent.kafka.schemaregistry.SchemaProvider;
import io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider;
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.rest.RestService;
import io.confluent.kafka.schemaregistry.client.security.SslFactory;
import io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProvider;
import io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProviderFactory;
import io.confluent.kafka.schemaregistry.client.security.basicauth.UserInfoCredentialProvider;
import io.confluent.kafka.schemaregistry.json.JsonSchemaProvider;
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider;
import io.confluent.ksql.api.client.Client;
import io.confluent.ksql.api.client.ClientOptions;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.akhq.configs.AbstractProperties;
import org.akhq.configs.Connection;
import org.akhq.configs.Default;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.codehaus.httpcache4j.uri.URIBuilder;
import org.sourcelab.kafka.connect.apiclient.Configuration;
import org.sourcelab.kafka.connect.apiclient.KafkaConnectClient;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
@Singleton
public class KafkaModule {
@Inject
private List<Connection> connections;
@Inject
private List<Default> defaults;
public List<String> getClustersList() {
return this.connections
.stream()
.map(r -> r.getName().split("\\.")[0])
.distinct()
.collect(Collectors.toList());
}
public boolean clusterExists(String cluster){
return this.getClustersList().contains(cluster);
}
public Connection getConnection(String cluster) throws InvalidClusterException {
if (!this.clusterExists(cluster)) {
throw new InvalidClusterException("Invalid cluster '" + cluster + "'");
}
return this.connections
.stream()
.filter(r -> r.getName().equals(cluster))
.findFirst()
.get();
}
private Properties getDefaultsProperties(List<? extends AbstractProperties> current, String type) {
Properties properties = new Properties();
current
.stream()
.filter(r -> r.getName().equals(type))
.forEach(r -> properties.putAll(r.getProperties()));
return properties;
}
private Properties getConsumerProperties(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
Properties props = new Properties();
props.putAll(this.getDefaultsProperties(this.defaults, "consumer"));
props.putAll(this.getDefaultsProperties(this.connections, clusterId));
return props;
}
private Properties getProducerProperties(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
Properties props = new Properties();
props.putAll(this.getDefaultsProperties(this.defaults, "producer"));
props.putAll(this.getDefaultsProperties(this.connections, clusterId));
return props;
}
private Properties getAdminProperties(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
Properties props = new Properties();
props.putAll(this.getDefaultsProperties(this.defaults, "admin"));
props.putAll(this.getDefaultsProperties(this.connections, clusterId));
return props;
}
private final Map<String, AdminClient> adminClient = new HashMap<>();
public AdminClient getAdminClient(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
if (!this.adminClient.containsKey(clusterId)) {
this.adminClient.put(clusterId, AdminClient.create(this.getAdminProperties(clusterId)));
}
return this.adminClient.get(clusterId);
}
public KafkaConsumer<byte[], byte[]> getConsumer(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
return new KafkaConsumer<>(
this.getConsumerProperties(clusterId),
new ByteArrayDeserializer(),
new ByteArrayDeserializer()
);
}
public KafkaConsumer<byte[], byte[]> getConsumer(String clusterId, Properties properties) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
Properties props = this.getConsumerProperties(clusterId);
props.putAll(properties);
return new KafkaConsumer<>(
props,
new ByteArrayDeserializer(),
new ByteArrayDeserializer()
);
}
private final Map<String, KafkaProducer<byte[], byte[]>> producers = new HashMap<>();
public KafkaProducer<byte[], byte[]> getProducer(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
if (!this.producers.containsKey(clusterId)) {
this.producers.put(clusterId, new KafkaProducer<>(
this.getProducerProperties(clusterId),
new ByteArraySerializer(),
new ByteArraySerializer()
));
}
return this.producers.get(clusterId);
}
public AvroSchemaProvider getAvroSchemaProvider(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
AvroSchemaProvider avroSchemaProvider = new AvroSchemaProvider();
avroSchemaProvider.configure(Collections.singletonMap(
"schemaVersionFetcher",
new CachedSchemaRegistryClient(this.getRegistryRestClient(clusterId), 1000)
));
return avroSchemaProvider;
}
public JsonSchemaProvider getJsonSchemaProvider(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
JsonSchemaProvider jsonSchemaProvider = new JsonSchemaProvider();
jsonSchemaProvider.configure(Collections.singletonMap(
"schemaVersionFetcher",
new CachedSchemaRegistryClient(this.getRegistryRestClient(clusterId), 1000)
));
return jsonSchemaProvider;
}
public ProtobufSchemaProvider getProtobufSchemaProvider(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
ProtobufSchemaProvider protobufSchemaProvider = new ProtobufSchemaProvider();
protobufSchemaProvider.configure(Collections.singletonMap(
"schemaVersionFetcher",
new CachedSchemaRegistryClient(this.getRegistryRestClient(clusterId), 1000)
));
return protobufSchemaProvider;
}
public RestService getRegistryRestClient(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
Connection connection = this.getConnection(clusterId);
if (connection.getSchemaRegistry() != null) {
RestService restService = new RestService(
connection.getSchemaRegistry().getUrl()
);
if (connection.getSchemaRegistry().getProperties() != null
&& !connection.getSchemaRegistry().getProperties().isEmpty()) {
Map<String, Object> sslConfigs =
connection
.getSchemaRegistry()
.getProperties()
.entrySet()
.stream()
.filter(e -> e.getKey().startsWith("schema.registry."))
.collect(Collectors.toMap(e -> e.getKey().substring("schema.registry.".length()), Map.Entry::getValue));
SslFactory sslFactory = new SslFactory(sslConfigs);
if (sslFactory != null && sslFactory.sslContext() != null) {
restService.setSslSocketFactory(sslFactory.sslContext().getSocketFactory());
}
}
restService.setHttpHeaders(Collections.singletonMap("Accept", "application/json"));
if (connection.getSchemaRegistry().getBasicAuthUsername() != null) {
BasicAuthCredentialProvider basicAuthCredentialProvider = BasicAuthCredentialProviderFactory
.getBasicAuthCredentialProvider(
new UserInfoCredentialProvider().alias(),
ImmutableMap.of(
"schema.registry.basic.auth.user.info",
connection.getSchemaRegistry().getBasicAuthUsername() + ":" +
connection.getSchemaRegistry().getBasicAuthPassword()
)
);
restService.setBasicAuthCredentialProvider(basicAuthCredentialProvider);
}
if (connection.getSchemaRegistry().getProperties() != null) {
restService.configure(connection.getSchemaRegistry().getProperties());
}
return restService;
}
return null;
}
private final Map<String, SchemaRegistryClient> registryClient = new HashMap<>();
public SchemaRegistryClient getRegistryClient(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
if (!this.registryClient.containsKey(clusterId)) {
Connection connection = this.getConnection(clusterId);
List<SchemaProvider> providers = new ArrayList<>();
providers.add(new AvroSchemaProvider());
providers.add(new JsonSchemaProvider());
providers.add(new ProtobufSchemaProvider());
SchemaRegistryClient client = new CachedSchemaRegistryClient(
this.getRegistryRestClient(clusterId),
1000,
providers,
connection.getSchemaRegistry() != null ? connection.getSchemaRegistry().getProperties() : null,
null
);
this.registryClient.put(clusterId, client);
}
return this.registryClient.get(clusterId);
}
private final Map<String, Map<String, KafkaConnectClient>> connectRestClient = new HashMap<>();
public Map<String, KafkaConnectClient> getConnectRestClient(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
if (!this.connectRestClient.containsKey(clusterId)) {
Connection connection = this.getConnection(clusterId);
if (connection.getConnect() != null && !connection.getConnect().isEmpty()) {
Map<String, KafkaConnectClient> mapConnects = new HashMap<>();
connection.getConnect().forEach(connect -> {
URIBuilder uri = URIBuilder.fromString(connect.getUrl().toString());
Configuration configuration = new Configuration(uri.toNormalizedURI(false).toString());
if (connect.getBasicAuthUsername() != null) {
configuration.useBasicAuth(
connect.getBasicAuthUsername(),
connect.getBasicAuthPassword()
);
}
if (connect.getSslTrustStore() != null) {
configuration.useTrustStore(
new File(connect.getSslTrustStore()),
connect.getSslTrustStorePassword()
);
}
if (connect.getSslKeyStore() != null) {
configuration.useKeyStore(
new File(connect.getSslKeyStore()),
connect.getSslKeyStorePassword()
);
}
mapConnects.put(connect.getName(), new KafkaConnectClient(configuration));
});
this.connectRestClient.put(clusterId, mapConnects);
}
}
return this.connectRestClient.get(clusterId);
}
private final Map<String, Map<String, Client>> ksqlDbClient = new HashMap<>();
public Map<String, Client> getKsqlDbClient(String clusterId) throws InvalidClusterException {
if (!this.clusterExists(clusterId)) {
throw new InvalidClusterException("Invalid cluster '" + clusterId + "'");
}
if (!this.ksqlDbClient.containsKey(clusterId)) {
Connection connection = this.getConnection(clusterId);
if (connection.getKsqldb() != null && !connection.getKsqldb().isEmpty()) {
Map<String, Client> mapKsqlDbs = new HashMap<>();
connection.getKsqldb().forEach(ksqlDb -> {
URIBuilder uri = URIBuilder.fromString(ksqlDb.getUrl().toString());
ClientOptions options = ClientOptions.create()
.setHost(uri.getHost().get())
.setPort(uri.getPort().get())
.setUseTls(ksqlDb.isUseTls())
.setUseAlpn(ksqlDb.isUseAlpn())
.setVerifyHost(ksqlDb.isVerifyHost());
if (ksqlDb.getBasicAuthUsername() != null && ksqlDb.getBasicAuthPassword() != null) {
options.setBasicAuthCredentials(ksqlDb.getBasicAuthUsername(), ksqlDb.getBasicAuthPassword());
}
Client client = Client.create(options);
mapKsqlDbs.put(ksqlDb.getName(), client);
});
this.ksqlDbClient.put(clusterId, mapKsqlDbs);
}
}
return this.ksqlDbClient.get(clusterId);
}
}