-
Notifications
You must be signed in to change notification settings - Fork 116
/
JulieOps.java
320 lines (271 loc) · 11.6 KB
/
JulieOps.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
package com.purbon.kafka.topology;
import static com.purbon.kafka.topology.Constants.*;
import com.purbon.kafka.topology.api.adminclient.TopologyBuilderAdminClient;
import com.purbon.kafka.topology.api.adminclient.TopologyBuilderAdminClientBuilder;
import com.purbon.kafka.topology.api.connect.KConnectApiClient;
import com.purbon.kafka.topology.api.ksql.KsqlApiClient;
import com.purbon.kafka.topology.api.mds.MDSApiClientBuilder;
import com.purbon.kafka.topology.backend.*;
import com.purbon.kafka.topology.exceptions.ValidationException;
import com.purbon.kafka.topology.model.Topology;
import com.purbon.kafka.topology.schemas.SchemaRegistryManager;
import com.purbon.kafka.topology.serviceAccounts.VoidPrincipalProvider;
import com.purbon.kafka.topology.utils.Pair;
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.json.JsonSchemaProvider;
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class JulieOps implements AutoCloseable {
private static final Logger LOGGER = LogManager.getLogger(JulieOps.class);
private TopicManager topicManager;
private final PrincipalManager principalManager;
private AccessControlManager accessControlManager;
private KafkaConnectArtefactManager connectorManager;
private KSqlArtefactManager kSqlArtefactManager;
private final Topology topology;
private final Configuration config;
private final PrintStream outputStream;
private JulieOps(
Topology topology,
Configuration config,
TopicManager topicManager,
AccessControlManager accessControlManager,
PrincipalManager principalManager,
KafkaConnectArtefactManager connectorManager,
KSqlArtefactManager kSqlArtefactManager) {
this.topology = topology;
this.config = config;
this.topicManager = topicManager;
this.accessControlManager = accessControlManager;
this.principalManager = principalManager;
this.connectorManager = connectorManager;
this.kSqlArtefactManager = kSqlArtefactManager;
this.outputStream = System.out;
}
public static JulieOps build(String topologyFile, Map<String, String> config) throws Exception {
return build(topologyFile, "default", config);
}
public static JulieOps build(String topologyFile, String plansFile, Map<String, String> config)
throws Exception {
verifyRequiredParameters(topologyFile, config);
Configuration builderConfig = Configuration.build(config);
TopologyBuilderAdminClient adminClient =
new TopologyBuilderAdminClientBuilder(builderConfig).build();
AccessControlProviderFactory factory =
new AccessControlProviderFactory(
builderConfig, adminClient, new MDSApiClientBuilder(builderConfig));
PrincipalProviderFactory principalProviderFactory = new PrincipalProviderFactory(builderConfig);
JulieOps builder =
build(
topologyFile,
plansFile,
builderConfig,
adminClient,
factory.get(),
factory.builder(),
principalProviderFactory.get());
return builder;
}
public static JulieOps build(
String topologyFileOrDir,
Configuration config,
TopologyBuilderAdminClient adminClient,
AccessControlProvider accessControlProvider,
BindingsBuilderProvider bindingsBuilderProvider)
throws Exception {
return build(
topologyFileOrDir,
"default",
config,
adminClient,
accessControlProvider,
bindingsBuilderProvider,
new VoidPrincipalProvider());
}
public static JulieOps build(
String topologyFileOrDir,
String plansFile,
Configuration config,
TopologyBuilderAdminClient adminClient,
AccessControlProvider accessControlProvider,
BindingsBuilderProvider bindingsBuilderProvider,
PrincipalProvider principalProvider)
throws Exception {
Topology topology;
if (plansFile.equals("default")) {
topology = TopologyObjectBuilder.build(topologyFileOrDir, config);
} else {
topology = TopologyObjectBuilder.build(topologyFileOrDir, plansFile, config);
}
TopologyValidator validator = new TopologyValidator(config);
List<String> validationResults = validator.validate(topology);
if (!validationResults.isEmpty()) {
String resultsMessage = String.join("\n", validationResults);
throw new ValidationException(resultsMessage);
}
config.validateWith(topology);
AccessControlManager accessControlManager =
new AccessControlManager(accessControlProvider, bindingsBuilderProvider, config);
RestService restService = new RestService(config.getConfluentSchemaRegistryUrl());
Map<String, ?> schemaRegistryConfig = config.asMap();
List<SchemaProvider> providers =
Arrays.asList(
new AvroSchemaProvider(), new JsonSchemaProvider(), new ProtobufSchemaProvider());
SchemaRegistryClient schemaRegistryClient =
new CachedSchemaRegistryClient(
restService,
10,
providers,
schemaRegistryConfig.isEmpty() ? null : schemaRegistryConfig,
null);
SchemaRegistryManager schemaRegistryManager =
new SchemaRegistryManager(schemaRegistryClient, topologyFileOrDir);
TopicManager topicManager = new TopicManager(adminClient, schemaRegistryManager, config);
PrincipalManager principalManager = new PrincipalManager(principalProvider, config);
KafkaConnectArtefactManager connectorManager =
configureKConnectArtefactManager(config, topologyFileOrDir);
KSqlArtefactManager kSqlArtefactManager =
configureKSqlArtefactManager(config, topologyFileOrDir);
return new JulieOps(
topology,
config,
topicManager,
accessControlManager,
principalManager,
connectorManager,
kSqlArtefactManager);
}
private static KafkaConnectArtefactManager configureKConnectArtefactManager(
Configuration config, String topologyFileOrDir) {
Map<String, KConnectApiClient> clients =
config.getKafkaConnectServers().entrySet().stream()
.map(entry -> new Pair<>(entry.getKey(), new KConnectApiClient(entry.getValue())))
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
if (clients.isEmpty()) {
LOGGER.debug(
"No KafkaConnect clients configured for JulieOps to use, please verify your config file");
}
return new KafkaConnectArtefactManager(clients, config, topologyFileOrDir);
}
private static KSqlArtefactManager configureKSqlArtefactManager(
Configuration config, String topologyFileOrDir) {
Map<String, KsqlApiClient> clients = new HashMap<>();
if (config.hasKSQLServer()) {
String ksqlAddress = config.getKSQLServer();
String server = ksqlAddress.substring(0, ksqlAddress.lastIndexOf(":"));
Integer port = Integer.parseInt(ksqlAddress.substring(ksqlAddress.lastIndexOf(":") + 1));
KsqlApiClient client = new KsqlApiClient(server, port);
clients.put("default", client);
}
if (clients.isEmpty()) {
LOGGER.debug(
"No KSQL clients configured for JulieOps to use, please verify your config file");
}
return new KSqlArtefactManager(clients, config, topologyFileOrDir);
}
static void verifyRequiredParameters(String topologyFile, Map<String, String> config)
throws IOException {
if (!Files.exists(Paths.get(topologyFile))) {
throw new IOException("Topology file does not exist");
}
String configFilePath = config.get(CommandLineInterface.CLIENT_CONFIG_OPTION);
if (!Files.exists(Paths.get(configFilePath))) {
throw new IOException("AdminClient config file does not exist");
}
}
void run(ExecutionPlan plan) throws IOException {
LOGGER.debug(
String.format(
"Running topology builder with TopicManager=[%s], accessControlManager=[%s], dryRun=[%s], isQuite=[%s]",
topicManager, accessControlManager, config.isDryRun(), config.isQuiet()));
// Create users should always be first, so user exists when making acl link
principalManager.applyCreate(topology, plan);
topicManager.apply(topology, plan);
accessControlManager.apply(topology, plan);
connectorManager.apply(topology, plan);
kSqlArtefactManager.apply(topology, plan);
// Delete users should always be last,
// avoids any unlinked acls, e.g. if acl delete or something errors then there is a link still
// from the account, and can be re-run or manually fixed more easily
principalManager.applyDelete(topology, plan);
plan.run(config.isDryRun());
if (!config.isQuiet() && !config.isDryRun()) {
topicManager.printCurrentState(System.out);
accessControlManager.printCurrentState(System.out);
principalManager.printCurrentState(System.out);
connectorManager.printCurrentState(System.out);
kSqlArtefactManager.printCurrentState(System.out);
}
}
public void run() throws IOException {
if (config.doValidate()) {
return;
}
BackendController cs = buildBackendController(config);
ExecutionPlan plan = ExecutionPlan.init(cs, outputStream);
run(plan);
}
public void close() {
topicManager.close();
}
public static String getVersion() {
InputStream resourceAsStream =
JulieOps.class.getResourceAsStream(
"/META-INF/maven/com.purbon.kafka/julie-ops/pom.properties");
Properties prop = new Properties();
try {
prop.load(resourceAsStream);
return prop.getProperty("version");
} catch (IOException e) {
e.printStackTrace();
return "unknown";
}
}
private static BackendController buildBackendController(Configuration config) throws IOException {
String backendClass = config.getStateProcessorImplementationClassName();
Backend backend = null;
try {
if (backendClass.equalsIgnoreCase(STATE_PROCESSOR_DEFAULT_CLASS)) {
backend = new FileBackend();
} else if (backendClass.equalsIgnoreCase(REDIS_STATE_PROCESSOR_CLASS)) {
String host = config.getProperty(REDIS_HOST_CONFIG);
int port = Integer.parseInt(config.getProperty(REDIS_PORT_CONFIG));
backend = new RedisBackend(host, port);
} else if (backendClass.equalsIgnoreCase(S3_STATE_PROCESSOR_CLASS)) {
backend = new S3Backend();
} else if (backendClass.equalsIgnoreCase(GCP_STATE_PROCESSOR_CLASS)) {
backend = new GCPBackend();
} else {
throw new IOException(backendClass + " Unknown state processor provided.");
}
} catch (Exception ex) {
throw new IOException(ex);
}
backend.configure(config);
return new BackendController(backend);
}
void setTopicManager(TopicManager topicManager) {
this.topicManager = topicManager;
}
void setAccessControlManager(AccessControlManager accessControlManager) {
this.accessControlManager = accessControlManager;
}
public void setConnectorManager(KafkaConnectArtefactManager connectorManager) {
this.connectorManager = connectorManager;
}
public void setKsqlArtefactManager(KSqlArtefactManager kSqlArtefactManager) {
this.kSqlArtefactManager = kSqlArtefactManager;
}
}