/
role.go
827 lines (703 loc) · 24.7 KB
/
role.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cluster
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
jsonpatch "github.com/evanphx/json-patch"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
apiv1 "github.com/kubermatic/kubermatic/api/pkg/api/v1"
"github.com/kubermatic/kubermatic/api/pkg/handler/middleware"
"github.com/kubermatic/kubermatic/api/pkg/handler/v1/common"
"github.com/kubermatic/kubermatic/api/pkg/provider"
"github.com/kubermatic/kubermatic/api/pkg/util/errors"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
UserClusterComponentKey = "component"
UserClusterRoleComponentValue = "userClusterRole"
UserClusterRoleLabelSelector = "component=userClusterRole"
)
func CreateClusterRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createClusterRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
if err := req.Validate(); err != nil {
return nil, errors.NewBadRequest("invalid request: %v", err)
}
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
userClusterAPIRole := req.Body
clusterRole, err := generateRBACClusterRole(userClusterAPIRole.Name, userClusterAPIRole.Rules)
if err != nil {
return nil, errors.NewBadRequest("invalid cluster role: %v", err)
}
if err := client.Create(ctx, clusterRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalClusterRoleToExternal(clusterRole), nil
}
}
func CreateRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
if err := req.Validate(); err != nil {
return nil, errors.NewBadRequest("invalid request: %v", err)
}
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
apiRole := req.Body
role, err := generateRBACRole(apiRole.Name, apiRole.Namespace, apiRole.Rules)
if err != nil {
return nil, errors.NewBadRequest("invalid cluster role: %v", err)
}
if err := client.Create(ctx, role); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalRoleToExternal(role), nil
}
}
// createClusterRoleReq defines HTTP request for createClusterRole endpoint
// swagger:parameters createClusterRole
type createClusterRoleReq struct {
common.DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
// in: body
Body apiv1.ClusterRole
}
// createRoleReq defines HTTP request for createRole endpoint
// swagger:parameters createRole
type createRoleReq struct {
common.DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
// in: body
Body apiv1.Role
}
// Validate validates createRoleReq request
func (r createRoleReq) Validate() error {
if len(r.ProjectID) == 0 || len(r.DC) == 0 {
return fmt.Errorf("the project ID and datacenter cannot be empty")
}
if r.Body.Namespace == "" || r.Body.Name == "" {
return fmt.Errorf("the request Body name and namespace cannot be empty")
}
return nil
}
// Validate validates createRoleReq request
func (r createClusterRoleReq) Validate() error {
if len(r.ProjectID) == 0 || len(r.DC) == 0 {
return fmt.Errorf("the project ID and datacenter cannot be empty")
}
if r.Body.Name == "" {
return fmt.Errorf("the request Body name cannot be empty")
}
return nil
}
func DecodeCreateClusterRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req createClusterRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
if err := json.NewDecoder(r.Body).Decode(&req.Body); err != nil {
return nil, err
}
return req, nil
}
func DecodeCreateRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req createRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
if err := json.NewDecoder(r.Body).Decode(&req.Body); err != nil {
return nil, err
}
return req, nil
}
func ListClusterRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
clusterRoleList := &rbacv1.ClusterRoleList{}
if err := client.List(ctx, clusterRoleList, ctrlruntimeclient.MatchingLabels{UserClusterComponentKey: UserClusterRoleComponentValue}); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalClusterRolesToExternal(clusterRoleList), nil
}
}
func ListClusterRoleNamesEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
clusterRoleList := &rbacv1.ClusterRoleList{}
if err := client.List(ctx, clusterRoleList, ctrlruntimeclient.MatchingLabels{UserClusterComponentKey: UserClusterRoleComponentValue}); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
var clusterRoleNames []apiv1.ClusterRoleName
for _, clusterRole := range clusterRoleList.Items {
clusterRoleNames = append(clusterRoleNames, apiv1.ClusterRoleName{
Name: clusterRole.Name,
})
}
return clusterRoleNames, nil
}
}
func ListRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
roleList := &rbacv1.RoleList{}
if err := client.List(ctx, roleList, ctrlruntimeclient.MatchingLabels{UserClusterComponentKey: UserClusterRoleComponentValue}); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalRolesToExternal(roleList), nil
}
}
func ListRoleNamesEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
roleList := &rbacv1.RoleList{}
if err := client.List(ctx, roleList, ctrlruntimeclient.MatchingLabels{UserClusterComponentKey: UserClusterRoleComponentValue}); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertRoleNames(roleList), nil
}
}
// listReq defines HTTP request for listClusterRole and listRole endpoint
// swagger:parameters listClusterRole listRole listRoleNames listClusterRoleNames
type listReq struct {
common.DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
}
func DecodeListClusterRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req listReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
return req, nil
}
// GetClusterRoleEndpoint gets ClusterRole with given name.
func GetClusterRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getClusterRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
clusterRole := &rbacv1.ClusterRole{}
if err := client.Get(ctx, ctrlruntimeclient.ObjectKey{Name: req.RoleID}, clusterRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalClusterRoleToExternal(clusterRole), nil
}
}
func GetRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
role := &rbacv1.Role{}
if err := client.Get(ctx, ctrlruntimeclient.ObjectKey{Name: req.RoleID, Namespace: req.Namespace}, role); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalRoleToExternal(role), nil
}
}
// DeleteClusterRoleEndpoint deletes ClusterRole with given name
func DeleteClusterRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getClusterRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: req.RoleID,
},
}
if err := client.Delete(ctx, clusterRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return nil, nil
}
}
// DeleteRoleEndpoint deletes Role with given name
func DeleteRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: req.RoleID,
Namespace: req.Namespace,
},
}
if err := client.Delete(ctx, role); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return nil, nil
}
}
// getRoleReq defines HTTP request for getRole endpoint
// swagger:parameters getRole deleteRole
type getRoleReq struct {
common.DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
// in: path
// required: true
RoleID string `json:"role_id"`
// in: path
// required: true
Namespace string `json:"namespace"`
}
func DecodeGetRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req getRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
roleID := mux.Vars(r)["role_id"]
if roleID == "" {
return "", fmt.Errorf("'role_id' parameter is required but was not provided")
}
req.RoleID = roleID
namespace := mux.Vars(r)["namespace"]
if namespace == "" {
return "", fmt.Errorf("'namespace' parameter is required but was not provided")
}
req.Namespace = namespace
return req, nil
}
// getClusterRoleReq defines HTTP request for getClusterRole endpoint
// swagger:parameters getClusterRole deleteClusterRole
type getClusterRoleReq struct {
common.DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
// in: path
// required: true
RoleID string `json:"role_id"`
}
func DecodeGetClusterRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req getClusterRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
roleID := mux.Vars(r)["role_id"]
if roleID == "" {
return "", fmt.Errorf("'role_id' parameter is required but was not provided")
}
req.RoleID = roleID
return req, nil
}
// PatchRoleEndpoint patches Role with given name
func PatchRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(patchRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
existingRole := &rbacv1.Role{}
if err := client.Get(ctx, ctrlruntimeclient.ObjectKey{Name: req.RoleID, Namespace: req.Namespace}, existingRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
existingRoleJSON, err := json.Marshal(existingRole)
if err != nil {
return nil, errors.NewBadRequest("cannot decode existing role: %v", err)
}
patchedRoleJSON, err := jsonpatch.MergePatch(existingRoleJSON, req.Patch)
if err != nil {
return nil, errors.NewBadRequest("cannot patch role: %v", err)
}
var patchedRole *rbacv1.Role
err = json.Unmarshal(patchedRoleJSON, &patchedRole)
if err != nil {
return nil, errors.NewBadRequest("cannot decode patched role: %v", err)
}
if err := client.Update(ctx, patchedRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalRoleToExternal(patchedRole), nil
}
}
// patchRoleReq defines HTTP request for patchRole endpoint
// swagger:parameters patchRole
type patchRoleReq struct {
common.GetClusterReq
// in: path
// required: true
RoleID string `json:"role_id"`
// in: path
// required: true
Namespace string `json:"namespace"`
// in: body
Patch json.RawMessage
}
func DecodePatchRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req patchRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
req.ClusterID = clusterID
roleID := mux.Vars(r)["role_id"]
if roleID == "" {
return "", fmt.Errorf("'role_id' parameter is required but was not provided")
}
req.RoleID = roleID
namespace := mux.Vars(r)["namespace"]
if namespace == "" {
return "", fmt.Errorf("'namespace' parameter is required but was not provided")
}
req.Namespace = namespace
if req.Patch, err = ioutil.ReadAll(r.Body); err != nil {
return nil, err
}
return req, nil
}
// PatchRoleEndpoint patches ClusterRole with given name
func PatchClusterRoleEndpoint(userInfoGetter provider.UserInfoGetter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(patchClusterRoleReq)
clusterProvider := ctx.Value(middleware.ClusterProviderContextKey).(provider.ClusterProvider)
userInfo, err := userInfoGetter(ctx, req.ProjectID)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
cluster, err := clusterProvider.Get(userInfo, req.ClusterID, &provider.ClusterGetOptions{CheckInitStatus: true})
if err != nil {
return nil, err
}
client, err := clusterProvider.GetClientForCustomerCluster(userInfo, cluster)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
existingClusterRole := &rbacv1.ClusterRole{}
if err := client.Get(ctx, ctrlruntimeclient.ObjectKey{Name: req.RoleID}, existingClusterRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
existingClusterRoleJSON, err := json.Marshal(existingClusterRole)
if err != nil {
return nil, errors.NewBadRequest("cannot decode existing cluster role: %v", err)
}
patchedClusterRoleJSON, err := jsonpatch.MergePatch(existingClusterRoleJSON, req.Patch)
if err != nil {
return nil, errors.NewBadRequest("cannot patch cluster role: %v", err)
}
var patchedClusterRole *rbacv1.ClusterRole
err = json.Unmarshal(patchedClusterRoleJSON, &patchedClusterRole)
if err != nil {
return nil, errors.NewBadRequest("cannot decode patched role: %v", err)
}
if err := client.Update(ctx, patchedClusterRole); err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertInternalClusterRoleToExternal(patchedClusterRole), nil
}
}
// patchClusterRoleReq defines HTTP request for patchClusterRole endpoint
// swagger:parameters patchClusterRole
type patchClusterRoleReq struct {
common.GetClusterReq
// in: path
// required: true
RoleID string `json:"role_id"`
// in: body
Patch json.RawMessage
}
func DecodePatchClusterRoleReq(c context.Context, r *http.Request) (interface{}, error) {
var req patchClusterRoleReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
dcr, err := common.DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(common.DCReq)
req.ClusterID = clusterID
roleID := mux.Vars(r)["role_id"]
if roleID == "" {
return "", fmt.Errorf("'role_id' parameter is required but was not provided")
}
req.RoleID = roleID
if req.Patch, err = ioutil.ReadAll(r.Body); err != nil {
return nil, err
}
return req, nil
}
func convertInternalClusterRolesToExternal(internalClusterRoles *rbacv1.ClusterRoleList) []*apiv1.ClusterRole {
var apiClusterRole []*apiv1.ClusterRole
if internalClusterRoles != nil {
for _, clusterRole := range internalClusterRoles.Items {
apiClusterRole = append(apiClusterRole, convertInternalClusterRoleToExternal(clusterRole.DeepCopy()))
}
}
return apiClusterRole
}
func convertInternalRolesToExternal(internalRoles *rbacv1.RoleList) []*apiv1.Role {
var apiClusterRole []*apiv1.Role
if internalRoles != nil {
for _, clusterRole := range internalRoles.Items {
apiClusterRole = append(apiClusterRole, convertInternalRoleToExternal(clusterRole.DeepCopy()))
}
}
return apiClusterRole
}
// generateRBACClusterRole creates cluster role
func generateRBACClusterRole(name string, rules []rbacv1.PolicyRule) (*rbacv1.ClusterRole, error) {
if rules == nil {
return nil, fmt.Errorf("the policy rule can not be nil")
}
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{UserClusterComponentKey: UserClusterRoleComponentValue},
},
Rules: rules,
}
return clusterRole, nil
}
// generateRBACRole creates role
func generateRBACRole(name, namespace string, rules []rbacv1.PolicyRule) (*rbacv1.Role, error) {
if rules == nil {
return nil, fmt.Errorf("the policy rule can not be nil")
}
clusterRole := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{UserClusterComponentKey: UserClusterRoleComponentValue},
Namespace: namespace,
},
Rules: rules,
}
return clusterRole, nil
}
func convertInternalClusterRoleToExternal(clusterRole *rbacv1.ClusterRole) *apiv1.ClusterRole {
return &apiv1.ClusterRole{
ObjectMeta: apiv1.ObjectMeta{
ID: clusterRole.Name,
Name: clusterRole.Name,
DeletionTimestamp: nil,
CreationTimestamp: apiv1.NewTime(clusterRole.CreationTimestamp.Time),
},
Rules: clusterRole.Rules,
}
}
func convertInternalRoleToExternal(clusterRole *rbacv1.Role) *apiv1.Role {
return &apiv1.Role{
ObjectMeta: apiv1.ObjectMeta{
ID: clusterRole.Name,
Name: clusterRole.Name,
DeletionTimestamp: nil,
CreationTimestamp: apiv1.NewTime(clusterRole.CreationTimestamp.Time),
},
Namespace: clusterRole.Namespace,
Rules: clusterRole.Rules,
}
}
func convertRoleNames(internalRoles *rbacv1.RoleList) []*apiv1.RoleName {
var apiRoleName []*apiv1.RoleName
roleMap := map[string][]string{}
if internalRoles != nil {
for _, role := range internalRoles.Items {
if roleMap[role.Name] == nil {
roleMap[role.Name] = []string{}
}
roleMap[role.Name] = append(roleMap[role.Name], role.Namespace)
}
}
for name, namespaces := range roleMap {
apiRoleName = append(apiRoleName, &apiv1.RoleName{
Name: name,
Namespace: namespaces,
})
}
return apiRoleName
}