-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclients.go
554 lines (501 loc) · 17.7 KB
/
clients.go
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
log "github.com/sirupsen/logrus"
)
type ClientTopicRole struct {
Topic string `yaml:"topic"`
IsLiteral bool `yaml:"isLiteral"`
Strict bool `yaml:"strict"`
Idempotent bool `yaml:"idempotent"` //Only used for roles allowing producer
}
type ClientGroupRole struct {
Name string `yaml:"name"`
Roles []string `yaml:"roles"`
IsLiteral bool `yaml:"isLiteral" default:"true"`
}
type ClientTransactionalIdRole struct {
Name string `yaml:"name"`
Roles []string `yaml:"roles"`
IsLiteral bool `yaml:"isLiteral" default:"true"`
}
type Client struct {
Principal string `yaml:"principal"`
ConsumerFor []ClientTopicRole `yaml:"consumer_for"`
ProducerFor []ClientTopicRole `yaml:"producer_for"`
ResourceownerFor []ClientTopicRole `yaml:"resourceowner_for"`
Groups []ClientGroupRole `yaml:"groups"`
TransactionalIds []ClientTransactionalIdRole `yaml:"transactional_ids"`
}
func mergeClients(target Client, source Client) Client {
target.ConsumerFor = append(target.ConsumerFor, source.ConsumerFor...)
target.ProducerFor = append(target.ProducerFor, source.ProducerFor...)
target.ResourceownerFor = append(target.ResourceownerFor, source.ResourceownerFor...)
target.Groups = append(target.Groups, source.Groups...)
return target
}
type MDSAdmin struct {
User string
Password string
Url string
SchemaRegistryClusterID string
ConnectClusterId string
KafkaClusterID string
KSQLClusterID string
RolebindingsCache map[string]MDSRolebindings // [principal]rolbindings
TlsConfig *tls.Config
}
const (
CTX_KAFKA = iota // 0
CTX_SR = iota // 1
CTX_KSQL = iota // 2
CTX_CONNECT = iota // 3
)
type MDSRolebindings struct {
DeveloperRead []MDSResourcePattern `json:"DeveloperRead"`
DeveloperWrite []MDSResourcePattern `json:"DeveloperWrite"`
ResourceOwner []MDSResourcePattern `json:"ResourceOwner"`
}
// Matches the response/request objects of MDS on resource patterns
type MDSResourcePattern struct {
ResourceType string `json:"resourceType"`
Name string `json:"name"`
PatternType string `json:"patternType"`
}
type MDSContext struct {
Clusters map[string]string `json:"clusters"` // cluster name : cluster ID map
}
func NewMDSAdmin(config MDSConfig) *MDSAdmin {
admin := MDSAdmin{
User: config.User,
Password: config.Password,
}
admin.Url = config.Url
admin.KafkaClusterID = admin.getKafkaClusterID()
admin.SchemaRegistryClusterID = config.SchemaRegistryClusterID
admin.ConnectClusterId = config.ConnectClusterId
admin.KSQLClusterID = config.KSQLClusterID
admin.RolebindingsCache = make(map[string]MDSRolebindings)
if config.CAPath != "" {
admin.TlsConfig = createTlsConfig(config.CAPath, config.SkipVerify)
}
return &admin
}
func (admin *MDSAdmin) getKafkaClusterID() string {
url := fmt.Sprintf("%s/security/1.0/metadataClusterId", admin.Url)
resp, err := admin.doRest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
return string(resp)
}
// Check if a role is present in Cache. Use the ClientResult object to do so
func (admin *MDSAdmin) roleExists(role ClientResult, existingRoles MDSRolebindings) bool {
var found bool = false
switch role.Role {
case "DeveloperRead":
found = compareResultWithResourcePatterns(role, existingRoles.DeveloperRead)
case "DeveloperWrite":
found = compareResultWithResourcePatterns(role, existingRoles.DeveloperWrite)
case "ResourceOwner":
found = compareResultWithResourcePatterns(role, existingRoles.ResourceOwner)
}
return found
}
func compareResultWithResourcePatterns(res ClientResult, patterns []MDSResourcePattern) bool {
for _, pattern := range patterns {
if (res.ResourceName == pattern.Name) && (res.ResourceType == pattern.ResourceType) && (res.PatternType == pattern.PatternType) {
return true
}
}
return false
}
// low level function that sets a role binding.
func (admin *MDSAdmin) SetRoleBinding(context int, res_type string, res_name string, principal string, roles []string, isLiteral bool, dry_run bool) error {
type MDSRequest struct {
Scope MDSContext `json:"scope"`
ResourcePatterns []MDSResourcePattern `json:"resourcePatterns"`
}
ctx := admin.getContext(context)
var reqData MDSRequest
reqData.Scope.Clusters = ctx.Clusters
resPattern := MDSResourcePattern{
ResourceType: res_type,
Name: res_name,
}
resPattern.PatternType = getPrefixStr(isLiteral)
reqData.ResourcePatterns = append(reqData.ResourcePatterns, resPattern)
// Now should be read to do the POST
for _, role := range roles {
// note we queryescape principals as they contain a ':' and fuck-up the endpoint and we get a 404
url := fmt.Sprintf("%s/security/1.0/principals/%s/roles/%s/bindings", admin.Url, url.QueryEscape(principal), role)
payload, err := json.Marshal(reqData)
if err != nil {
return err
}
_, err = admin.doRest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return err
}
}
return nil
}
func (admin *MDSAdmin) doRest(method string, url string, payload io.Reader) ([]byte, error) {
transport := &http.Transport{TLSClientConfig: admin.TlsConfig}
hClient := http.Client{Transport: transport}
req, err := http.NewRequest(method, url, payload)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(admin.User, admin.Password)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := hClient.Do(req)
if err != nil {
log.Println(err)
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
return respBody, nil
}
// context is one of CTX_* constants
func (admin *MDSAdmin) getContext(context int) MDSContext {
var ctx MDSContext
ctx.Clusters = make(map[string]string)
// kafka-cluster is always present.
ctx.Clusters["kafka-cluster"] = admin.KafkaClusterID
switch context {
case CTX_KAFKA:
return ctx
case CTX_SR:
ctx.Clusters["schema-registry-cluster"] = admin.SchemaRegistryClusterID
case CTX_KSQL:
ctx.Clusters["ksql-cluster"] = admin.KSQLClusterID
case CTX_CONNECT:
ctx.Clusters["connect-cluster"] = admin.ConnectClusterId
}
return ctx
}
// For a given principal name, get the rolebindings assigned
func (admin *MDSAdmin) getRoleBindingsForPrincipalContext(principal string, context int) MDSRolebindings {
type MDSRolebindingResponseInner map[string]MDSRolebindings
ctx := admin.getContext(context)
url := fmt.Sprintf("%s/security/1.0/lookup/principal/%s/resources", admin.Url, principal)
payload, err := json.Marshal(ctx)
if err != nil {
log.Fatal(err)
}
resp, err := admin.doRest("POST", url, bytes.NewBuffer(payload))
if err != nil {
log.Fatal(err)
}
var respObj MDSRolebindingResponseInner
err = json.Unmarshal(resp, &respObj)
if err != nil {
log.Fatal(err)
}
return respObj[principal]
}
func getPrefixStr(isLiteral bool) string {
var rval string
if !isLiteral {
rval = "PREFIXED"
} else {
rval = "LITERAL"
}
return rval
}
func (admin *MDSAdmin) getContextValByID(context int) string {
var rval string
switch context {
case CTX_KAFKA:
rval = admin.KafkaClusterID
case CTX_SR:
rval = admin.SchemaRegistryClusterID
case CTX_KSQL:
rval = admin.KSQLClusterID
case CTX_CONNECT:
rval = admin.ConnectClusterId
}
return rval
}
func (admin *MDSAdmin) getRoleBindingsForPrincipal(principal string) MDSRolebindings {
// Check if it is in the Cache and return it, otherwise it can become a very expensive operation
if rolebindings, exists := admin.RolebindingsCache[principal]; exists {
return rolebindings
}
var allRoles MDSRolebindings
var respObj MDSRolebindings
// Get rolebindings for each context and slowly construct the allRoles obj
contexts := []int{CTX_KAFKA, CTX_SR, CTX_CONNECT, CTX_KSQL}
for _, ctx := range contexts {
// If context is empty, skip this
if admin.getContextValByID(ctx) == "" {
continue
}
respObj = admin.getRoleBindingsForPrincipalContext(principal, ctx)
allRoles.DeveloperRead = append(allRoles.DeveloperRead, respObj.DeveloperRead...)
allRoles.DeveloperWrite = append(allRoles.DeveloperWrite, respObj.DeveloperWrite...)
allRoles.ResourceOwner = append(allRoles.ResourceOwner, respObj.ResourceOwner...)
}
// Set the cache
admin.RolebindingsCache[principal] = allRoles
return allRoles
}
func (admin *MDSAdmin) doConsumerFor(topic string, principal string, isLiteral bool, dryRun bool) ([]ClientResult, error) {
var res []ClientResult
var err error
var subjects []string
existingRoles := admin.getRoleBindingsForPrincipal(principal)
if isLiteral {
subjects = []string{fmt.Sprintf("%s-value", topic), fmt.Sprintf("%s-key", topic)}
} else {
subjects = []string{topic}
}
newRole := ClientResult{Principal: principal, ResourceType: "Topic", ResourceName: topic, Role: "DeveloperRead", PatternType: getPrefixStr(isLiteral)}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Topic", topic, principal, []string{"DeveloperRead"}, isLiteral, dryRun)
if err != nil {
return res, err
}
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
// Set DeveloperRead on subject value
for _, subject := range subjects {
newRole = ClientResult{Principal: principal, ResourceType: "Subject", ResourceName: subject, Role: "DeveloperRead", PatternType: getPrefixStr(isLiteral)}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_SR, "Subject", subject, principal, []string{"DeveloperRead"}, isLiteral, dryRun)
if err != nil {
return res, err
}
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
}
return res, nil
}
func (admin *MDSAdmin) doProducerFor(topic string, principal string, isLiteral, strict, idempotent, dryRun bool) ([]ClientResult, error) {
var res []ClientResult
var err error
// Default role for SR is Read but if strict=false then add Write
srRoles := []string{"DeveloperRead"}
var subjects []string
existingRoles := admin.getRoleBindingsForPrincipal(principal)
if isLiteral {
subjects = []string{fmt.Sprintf("%s-value", topic), fmt.Sprintf("%s-key", topic)}
} else {
subjects = []string{topic}
}
if !strict {
srRoles = append(srRoles, "DeveloperWrite")
}
// ADD developerWrite to Topic resource
newRole := ClientResult{Principal: principal, ResourceType: "Topic", ResourceName: topic, Role: "DeveloperWrite", PatternType: getPrefixStr(isLiteral)}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Topic", topic, principal, []string{"DeveloperWrite"}, isLiteral, dryRun)
if err != nil {
return res, err
}
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
// Add idempotent cluster role if requested
if idempotent {
newRole := ClientResult{Principal: principal, ResourceType: "Cluster", ResourceName: "kafka-cluster", Role: "DeveloperWrite", PatternType: "LITERAL"}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Cluster", "kafka-cluster", principal, []string{"DeveloperWrite"}, true, dryRun)
if err != nil {
return res, err
}
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
// Add idempotent cluster role if requested
}
// Add idempotent role if to result
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
for _, subject := range subjects {
// Prepare plan/result
for _, sRole := range srRoles {
newRole := ClientResult{Principal: principal, ResourceType: "Subject", ResourceName: subject, Role: sRole, PatternType: getPrefixStr(isLiteral)}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
}
// Actually change it, if needed
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_SR, "Subject", subject, principal, srRoles, isLiteral, dryRun)
if err != nil {
return res, err
}
}
}
return res, nil
}
func (admin *MDSAdmin) doResourceOwnerFor(topic string, principal string, isLiteral, idempotent, dryRun bool) ([]ClientResult, error) {
var res []ClientResult
var err error
roles := []string{"ResourceOwner"}
var subjects []string
existingRoles := admin.getRoleBindingsForPrincipal(principal)
if isLiteral {
subjects = []string{fmt.Sprintf("%s-value", topic), fmt.Sprintf("%s-key", topic)}
} else {
subjects = []string{topic}
}
// Add ResourceOwner on the topic
newRole := ClientResult{Principal: principal, ResourceType: "Topic", ResourceName: topic, Role: "ResourceOwner", PatternType: getPrefixStr(isLiteral)}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Topic", topic, principal, roles, isLiteral, dryRun)
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
if err != nil {
return res, err
}
// Add IdempotentWrite
if idempotent {
newRole := ClientResult{Principal: principal, ResourceType: "Cluster", ResourceName: "kafka-cluster", Role: "ResourceOwner", PatternType: "LITERAL"}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Cluster", "kafka-cluster", principal, []string{"ResourceOwner"}, true, dryRun)
if err != nil {
return res, err
}
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
// Add idempotent cluster role if requested
}
// Add Schemaregistry rolebindings
for _, subject := range subjects {
newRole = ClientResult{Principal: principal, ResourceType: "Subject", ResourceName: subject, Role: "ResourceOwner", PatternType: getPrefixStr(isLiteral)}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_SR, "Subject", subject, principal, roles, isLiteral, dryRun)
}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
if err != nil {
return res, err
}
}
return res, nil
}
/*
Create any Consumer Group permission
If dry_run, only return a ClientResult so that a Plan can be created.
*/
func (admin *MDSAdmin) doGroupRoleFor(groupName, principal string, roles []string, isLiteral bool, dryRun bool) ([]ClientResult, error) {
var res []ClientResult
var err error
var actualRoles []string
existingRoles := admin.getRoleBindingsForPrincipal(principal)
if len(roles) > 0 {
actualRoles = roles
} else {
actualRoles = []string{"DeveloperRead"}
}
for _, roleName := range actualRoles {
newRole := ClientResult{Principal: principal, ResourceType: "Group", ResourceName: groupName, Role: roleName, PatternType: getPrefixStr(isLiteral)}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "Group", groupName, principal, []string{roleName}, isLiteral, dryRun)
if err != nil {
return res, err
}
}
}
return res, err
}
/*
Create any needed TransactionalId rolebindings
*/
func (admin *MDSAdmin) doTransactionalIdRole(transactionalIdName, principal string, roles []string, isLiteral, dryRun bool) ([]ClientResult, error) {
var res []ClientResult
var err error
var actualRoles []string
existingRoles := admin.getRoleBindingsForPrincipal(principal)
if len(roles) > 0 {
actualRoles = roles
} else {
actualRoles = []string{"DeveloperWrite"}
}
for _, roleName := range actualRoles {
log.Tracef("doTransactionalIdRole for role: %s", roleName)
newRole := ClientResult{Principal: principal, ResourceType: "TransactionalId", ResourceName: transactionalIdName, Role: roleName, PatternType: getPrefixStr(isLiteral)}
if !admin.roleExists(newRole, existingRoles) {
res = append(res, newRole)
}
if !dryRun && !admin.roleExists(newRole, existingRoles) {
err = admin.SetRoleBinding(CTX_KAFKA, "TransactionalId", transactionalIdName, principal, []string{roleName}, isLiteral, dryRun)
if err != nil {
return res, err
}
}
}
return res, err
}
func (admin *MDSAdmin) Reconcile(clients map[string]Client, dryRun bool) []ClientResult {
var clientResults []ClientResult
for _, client := range clients {
for _, consumerRole := range client.ConsumerFor {
clientRes, err := admin.doConsumerFor(consumerRole.Topic, client.Principal, consumerRole.IsLiteral, dryRun)
if err != nil {
log.Fatal(err)
}
clientResults = append(clientResults, clientRes...)
}
for _, producerRole := range client.ProducerFor {
clientRes, err := admin.doProducerFor(producerRole.Topic, client.Principal, producerRole.IsLiteral, producerRole.Strict, producerRole.Idempotent, dryRun)
if err != nil {
log.Fatal(err)
}
clientResults = append(clientResults, clientRes...)
}
for _, resourceOwnerRole := range client.ResourceownerFor {
clientRes, err := admin.doResourceOwnerFor(resourceOwnerRole.Topic, client.Principal, resourceOwnerRole.IsLiteral, resourceOwnerRole.Idempotent, dryRun)
if err != nil {
log.Fatal(err)
}
clientResults = append(clientResults, clientRes...)
}
// Add any Consumer Group permissions defined
for _, groupRole := range client.Groups {
clientRes, err := admin.doGroupRoleFor(groupRole.Name, client.Principal, groupRole.Roles, groupRole.IsLiteral, dryRun)
if err != nil {
log.Fatal(err)
}
clientResults = append(clientResults, clientRes...)
}
// Add Transactional Ids
for _, transactionalIdRole := range client.TransactionalIds {
clientRes, err := admin.doTransactionalIdRole(transactionalIdRole.Name, client.Principal, transactionalIdRole.Roles, transactionalIdRole.IsLiteral, dryRun)
if err != nil {
log.Fatal(err)
}
clientResults = append(clientResults, clientRes...)
}
}
return clientResults
}