-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathRBACBindingsBuilder.java
475 lines (412 loc) · 17 KB
/
RBACBindingsBuilder.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.purbon.kafka.topology.roles.rbac;
import static com.purbon.kafka.topology.api.mds.ClusterIDs.KSQL_CLUSTER_ID_LABEL;
import static com.purbon.kafka.topology.roles.rbac.RBACPredefinedRoles.DEVELOPER_READ;
import static com.purbon.kafka.topology.roles.rbac.RBACPredefinedRoles.DEVELOPER_WRITE;
import static com.purbon.kafka.topology.roles.rbac.RBACPredefinedRoles.RESOURCE_OWNER;
import static com.purbon.kafka.topology.roles.rbac.RBACPredefinedRoles.SECURITY_ADMIN;
import static com.purbon.kafka.topology.roles.rbac.RBACPredefinedRoles.SYSTEM_ADMIN;
import com.purbon.kafka.topology.BindingsBuilderProvider;
import com.purbon.kafka.topology.api.mds.MDSApiClient;
import com.purbon.kafka.topology.model.Component;
import com.purbon.kafka.topology.model.JulieRoleAcl;
import com.purbon.kafka.topology.model.users.Connector;
import com.purbon.kafka.topology.model.users.Consumer;
import com.purbon.kafka.topology.model.users.KSqlApp;
import com.purbon.kafka.topology.model.users.Other;
import com.purbon.kafka.topology.model.users.Producer;
import com.purbon.kafka.topology.model.users.platform.KsqlServerInstance;
import com.purbon.kafka.topology.model.users.platform.SchemaRegistryInstance;
import com.purbon.kafka.topology.roles.TopologyAclBinding;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.kafka.common.resource.PatternType;
public class RBACBindingsBuilder implements BindingsBuilderProvider {
public static final String LITERAL = "LITERAL";
public static final String PREFIX = "PREFIXED";
private final MDSApiClient apiClient;
public RBACBindingsBuilder(MDSApiClient apiClient) {
this.apiClient = apiClient;
}
@Override
public List<TopologyAclBinding> buildBindingsForConnect(Connector connector, String topicPrefix) {
String principal = connector.getPrincipal();
List<String> readTopics = connector.getTopics().get("read");
List<String> writeTopics = connector.getTopics().get("write");
List<TopologyAclBinding> bindings = new ArrayList<>();
TopologyAclBinding secAdminBinding =
apiClient.bind(principal, SECURITY_ADMIN).forKafkaConnect(connector).apply();
bindings.add(secAdminBinding);
TopologyAclBinding readBinding = apiClient.bind(principal, DEVELOPER_READ, topicPrefix, PREFIX);
bindings.add(readBinding);
if (readTopics != null && !readTopics.isEmpty()) {
readTopics.forEach(
topic -> {
TopologyAclBinding binding = apiClient.bind(principal, DEVELOPER_READ, topic, LITERAL);
bindings.add(binding);
});
}
if (writeTopics != null && !writeTopics.isEmpty()) {
writeTopics.forEach(
topic -> {
TopologyAclBinding binding = apiClient.bind(principal, DEVELOPER_WRITE, topic, LITERAL);
bindings.add(binding);
});
}
String[] resources =
new String[] {
"Topic:" + connector.configsTopicString(),
"Topic:" + connector.offsetTopicString(),
"Topic:" + connector.statusTopicString(),
"Group:" + connector.groupString(),
"Group:secret-registry",
"Topic:_confluent-secrets"
};
Arrays.asList(resources)
.forEach(
resourceObject -> {
String[] elements = resourceObject.split(":");
String resource = elements[1];
String resourceType = elements[0];
TopologyAclBinding binding =
apiClient.bind(principal, RESOURCE_OWNER, resource, resourceType, LITERAL);
bindings.add(binding);
});
return bindings;
}
@Override
public List<TopologyAclBinding> buildBindingsForStreamsApp(
String principal,
String topicPrefix,
List<String> readTopics,
List<String> writeTopics,
boolean eos) {
List<TopologyAclBinding> bindings = new ArrayList<>();
TopologyAclBinding binding = apiClient.bind(principal, DEVELOPER_READ, topicPrefix, PREFIX);
bindings.add(binding);
readTopics.forEach(
topic -> {
TopologyAclBinding readBinding =
apiClient.bind(principal, DEVELOPER_READ, topic, LITERAL);
bindings.add(readBinding);
});
writeTopics.forEach(
topic -> {
TopologyAclBinding writeBinding =
apiClient.bind(principal, DEVELOPER_WRITE, topic, LITERAL);
bindings.add(writeBinding);
});
if (eos) {
bindings.add(
apiClient.bind(principal, DEVELOPER_WRITE, topicPrefix, "TransactionalId", PREFIX));
}
binding = apiClient.bind(principal, RESOURCE_OWNER, topicPrefix, PREFIX);
bindings.add(binding);
binding = apiClient.bind(principal, RESOURCE_OWNER, topicPrefix, "Group", PREFIX);
bindings.add(binding);
return bindings;
}
@Override
public List<TopologyAclBinding> buildBindingsForConsumers(
Collection<Consumer> consumers, String resource, boolean prefixed) {
String patternType = prefixed ? PREFIX : LITERAL;
List<TopologyAclBinding> bindings = new ArrayList<>();
consumers.forEach(
consumer -> {
TopologyAclBinding binding =
apiClient.bind(consumer.getPrincipal(), DEVELOPER_READ, resource, patternType);
bindings.add(binding);
binding =
apiClient.bind(
consumer.getPrincipal(),
consumer.getGroupRole(),
evaluateResourcePattern(consumer.groupString()),
"Group",
evaluateResourcePatternType(consumer.groupString()));
bindings.add(binding);
});
return bindings;
}
private boolean isResourcePrefixed(String res) {
return res.length() > 1 && res.endsWith("*");
}
private String evaluateResourcePattern(String res) {
return isResourcePrefixed(res) ? res.replaceFirst(".$", "") : res;
}
private String evaluateResourcePatternType(String res) {
return isResourcePrefixed(res) ? PREFIX : LITERAL;
}
@Override
public List<TopologyAclBinding> buildBindingsForProducers(
Collection<Producer> producers, String resource, boolean prefixed) {
String patternType = prefixed ? PREFIX : LITERAL;
List<TopologyAclBinding> bindings = new ArrayList<>();
producers.forEach(
producer -> {
TopologyAclBinding binding =
apiClient.bind(producer.getPrincipal(), DEVELOPER_WRITE, resource, patternType);
bindings.add(binding);
if (producer.isIdempotent()) {
binding =
apiClient.bind(
producer.getPrincipal(), DEVELOPER_WRITE, "kafka-cluster", "Cluster", LITERAL);
bindings.add(binding);
}
if (producer.hasTransactionId()) {
binding =
apiClient.bind(
producer.getPrincipal(),
DEVELOPER_WRITE,
producer.getTransactionId().get(),
"TransactionalId",
LITERAL);
bindings.add(binding);
}
});
return bindings;
}
@Override
public TopologyAclBinding setPredefinedRole(
String principal, String predefinedRole, String topicPrefix) {
return apiClient.bind(principal, predefinedRole, topicPrefix, PREFIX);
}
@Override
public String toString() {
return super.toString();
}
@Override
public List<TopologyAclBinding> buildBindingsForSchemaRegistry(
SchemaRegistryInstance schemaRegistry) {
String principal = schemaRegistry.getPrincipal();
List<TopologyAclBinding> bindings = new ArrayList<>();
TopologyAclBinding binding =
apiClient.bind(principal, RESOURCE_OWNER, schemaRegistry.topicString(), LITERAL);
bindings.add(binding);
binding =
apiClient.bind(principal, RESOURCE_OWNER, schemaRegistry.groupString(), "Group", LITERAL);
bindings.add(binding);
binding = apiClient.bind(principal, SECURITY_ADMIN).forSchemaRegistry().apply();
bindings.add(binding);
return bindings;
}
@Override
public List<TopologyAclBinding> buildBindingsForControlCenter(String principal, String appId) {
TopologyAclBinding binding = apiClient.bind(principal, SYSTEM_ADMIN).forControlCenter().apply();
return Collections.singletonList(binding);
}
@Override
public Collection<TopologyAclBinding> buildBindingsForKSqlServer(KsqlServerInstance ksqlServer) {
List<TopologyAclBinding> bindings = new ArrayList<>();
// Ksql cluster scope
String clusterId = ksqlServer.getKsqlDbId();
TopologyAclBinding ownerBinding =
apiClient.bind(ksqlServer.getOwner(), RESOURCE_OWNER).forKSqlServer(clusterId).apply();
bindings.add(ownerBinding);
ownerBinding =
apiClient.bind(ksqlServer.getOwner(), SECURITY_ADMIN).forKSqlServer(clusterId).apply();
bindings.add(ownerBinding);
ownerBinding =
apiClient.bind(ksqlServer.getPrincipal(), RESOURCE_OWNER).forKSqlServer(clusterId).apply();
bindings.add(ownerBinding);
// Kafka Cluster scope
List<String> topics =
Arrays.asList(
ksqlServer.commandTopic(),
ksqlServer.processingLogTopic(),
ksqlServer.consumerGroupPrefix());
for (String topic : topics) {
TopologyAclBinding binding =
apiClient.bind(ksqlServer.getPrincipal(), RESOURCE_OWNER, topic, LITERAL);
bindings.add(binding);
}
String resource = String.format("_confluent-ksql-%stransient", clusterId);
TopologyAclBinding binding =
apiClient.bind(ksqlServer.getPrincipal(), RESOURCE_OWNER, resource, "Topic", PREFIX);
bindings.add(binding);
binding =
apiClient.bind(
ksqlServer.getPrincipal(), DEVELOPER_WRITE, ksqlServer.TransactionId(), LITERAL);
bindings.add(binding);
binding =
apiClient.bind(
ksqlServer.getPrincipal(), DEVELOPER_WRITE, "Cluster:kafka-cluster", LITERAL);
bindings.add(binding);
// For tables that use Avro, Protobuf, or JSON_SR:
// Grant full access for the ksql service principal to all internal ksql subjects.
String subject = String.format("_confluent-ksql-%s", clusterId);
apiClient
.bind(ksqlServer.getPrincipal(), RESOURCE_OWNER)
.forSchemaSubject(subject, PREFIX)
.apply();
return bindings;
}
@Override
public Collection<TopologyAclBinding> buildBindingsForKSqlApp(KSqlApp app, String prefix) {
List<TopologyAclBinding> bindings = new ArrayList<>();
// Ksql cluster scope
String clusterId = app.getKsqlDbId();
TopologyAclBinding binding =
apiClient
.bind(app.getPrincipal(), DEVELOPER_WRITE)
.forKSqlServer(clusterId)
.apply("KsqlCluster", "ksql-cluster");
bindings.add(binding);
// Kafka Cluster scope
String resource = String.format("_confluent-ksql-%s", clusterId);
binding = apiClient.bind(app.getPrincipal(), DEVELOPER_READ, resource, "Group", PREFIX);
bindings.add(binding);
// Assigned to allow access to the processing log
resource = String.format("%sksql_processing_log", clusterId);
binding = apiClient.bind(app.getPrincipal(), DEVELOPER_READ, resource, "Topic", LITERAL);
bindings.add(binding);
// Topic access
Optional<List<String>> readTopics = Optional.ofNullable(app.getTopics().get("read"));
readTopics.ifPresent(
topics -> {
for (String topic : topics) {
TopologyAclBinding topicBinding =
apiClient.bind(app.getPrincipal(), DEVELOPER_READ, topic, LITERAL);
bindings.add(topicBinding);
}
});
Optional<List<String>> writeTopics = Optional.ofNullable(app.getTopics().get("write"));
writeTopics.ifPresent(
topics -> {
for (String topic : topics) {
TopologyAclBinding topicBinding =
apiClient.bind(app.getPrincipal(), DEVELOPER_WRITE, topic, LITERAL);
bindings.add(topicBinding);
}
});
// schema access
List<String> subjects =
readTopics.stream()
.flatMap((Function<List<String>, Stream<String>>) Collection::stream)
.map(topicName -> String.format("%s-value", topicName))
.collect(Collectors.toList());
subjects.stream()
.map(
subject ->
apiClient
.bind(app.getPrincipal(), DEVELOPER_READ)
.forSchemaSubject(subject)
.apply("Subject", subject))
.filter(Objects::nonNull)
.collect(Collectors.toList());
subjects =
writeTopics.stream()
.flatMap((Function<List<String>, Stream<String>>) topics -> topics.stream())
.map(topicName -> String.format("%s-value", topicName))
.collect(Collectors.toList());
subjects.stream()
.map(
subject ->
apiClient
.bind(app.getPrincipal(), RESOURCE_OWNER)
.forSchemaSubject(subject)
.apply("Subject", subject))
.filter(Objects::nonNull)
.forEach(bindings::add);
// Access to transient query topics
resource = String.format("_confluent-ksql-%stransient", clusterId);
binding = apiClient.bind(app.getPrincipal(), RESOURCE_OWNER, resource, "Topic", PREFIX);
bindings.add(binding);
return bindings;
}
@Override
public Collection<TopologyAclBinding> buildBindingsForJulieRole(
Other other, String name, List<JulieRoleAcl> acls) {
var stream = acls.stream().map(acl -> julieRoleToBinding(other, acl));
return stream.collect(Collectors.toList());
}
private TopologyAclBinding julieRoleToBinding(Other other, JulieRoleAcl acl) {
String resourceType = acl.getResourceType();
if (resourceType.equalsIgnoreCase("Subject")) {
String subjectName = acl.getResourceName().replaceFirst("Subject:", "").trim();
return apiClient
.bind(other.getPrincipal(), acl.getRole())
.forSchemaSubject(subjectName, acl.getPatternType())
.apply("Subject", subjectName, acl.getPatternType());
} else if (resourceType.equalsIgnoreCase("Connector")) {
String connectorName = acl.getResourceName().replaceFirst("Connector:", "").trim();
return apiClient
.bind(other.getPrincipal(), acl.getRole())
.forAKafkaConnector(connectorName, acl.getPatternType())
.apply(acl.getResourceType(), connectorName, acl.getPatternType());
} else if (resourceType.equalsIgnoreCase("KsqlCluster")) {
var clusterIds = apiClient.withClusterIDs().forKsql().asMap();
var clusterId = clusterIds.get("clusters").get(KSQL_CLUSTER_ID_LABEL);
String resourceName = acl.getResourceName().replaceFirst("KsqlCluster:", "").trim();
return apiClient
.bind(other.getPrincipal(), acl.getRole())
.forKSqlServer(clusterId)
.apply(acl.getResourceType(), resourceName);
}
String resourceName = acl.getResourceName();
if (resourceName.contains(":")) {
var pos = resourceName.indexOf(":");
resourceName = resourceName.substring(pos + 1);
}
return apiClient.bind(
other.getPrincipal(),
acl.getRole(),
resourceName,
acl.getResourceType(),
acl.getPatternType());
}
@Override
public List<TopologyAclBinding> setClusterLevelRole(
String role, String principal, Component component) throws IOException {
ClusterLevelRoleBuilder clusterLevelRoleBuilder = apiClient.bind(principal, role);
TopologyAclBinding binding;
switch (component) {
case KAFKA:
binding = clusterLevelRoleBuilder.forKafka().apply();
break;
case SCHEMA_REGISTRY:
binding = clusterLevelRoleBuilder.forSchemaRegistry().apply();
break;
case KAFKA_CONNECT:
binding = clusterLevelRoleBuilder.forKafkaConnect().apply();
break;
default:
throw new IOException("Non valid component selected");
}
return Collections.singletonList(binding);
}
@Override
public List<TopologyAclBinding> setSchemaAuthorization(
String principal, List<String> subjects, String role, boolean prefixed) {
String patternType = prefixed ? PatternType.PREFIXED.name() : PatternType.LITERAL.name();
return subjects.stream()
.map(
subject ->
apiClient
.bind(principal, role)
.forSchemaSubject(subject, patternType)
.apply("Subject", subject))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
@Override
public List<TopologyAclBinding> setConnectorAuthorization(
String principal, List<String> connectors) {
return connectors.stream()
.map(
connector ->
apiClient
.bind(principal, RESOURCE_OWNER)
.forAKafkaConnector(connector)
.apply("Connector", connector))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}