|
| 1 | +/* |
| 2 | +Copyright 2025 The Kube Bind Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + rbacv1 "k8s.io/api/rbac/v1" |
| 23 | + "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 30 | + |
| 31 | + kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" |
| 32 | +) |
| 33 | + |
| 34 | +type reconciler struct { |
| 35 | + allowedGroups []string |
| 36 | + allowedUsers []string |
| 37 | +} |
| 38 | + |
| 39 | +func (r *reconciler) reconcile(ctx context.Context, client client.Client, _ cache.Cache, cluster *kubebindv1alpha2.Cluster) error { |
| 40 | + var errs []error |
| 41 | + |
| 42 | + if err := r.ensureOIDCRBAC(ctx, client, cluster); err != nil { |
| 43 | + errs = append(errs, err) |
| 44 | + } |
| 45 | + |
| 46 | + return utilerrors.NewAggregate(errs) |
| 47 | +} |
| 48 | + |
| 49 | +func (r *reconciler) ensureOIDCRBAC(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { |
| 50 | + if err := r.ensureUserClusterRole(ctx, client, cluster); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + return r.ensureUserClusterRoleBinding(ctx, client, cluster) |
| 54 | +} |
| 55 | + |
| 56 | +func (r *reconciler) ensureUserClusterRole(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { |
| 57 | + clusterRole := &rbacv1.ClusterRole{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: "kube-bind-oidc-user", |
| 60 | + }, |
| 61 | + Rules: []rbacv1.PolicyRule{ |
| 62 | + { |
| 63 | + APIGroups: []string{"kube-bind.io"}, |
| 64 | + Resources: []string{"*"}, |
| 65 | + Verbs: []string{"bind"}, |
| 66 | + }, |
| 67 | + { |
| 68 | + NonResourceURLs: []string{"/", "/api", "/api/*", "/apis", "/apis/*"}, |
| 69 | + Verbs: []string{"access"}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + if err := controllerutil.SetControllerReference(cluster, clusterRole, client.Scheme()); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + var existing rbacv1.ClusterRole |
| 79 | + err := client.Get(ctx, types.NamespacedName{Name: "kube-bind-oidc-user"}, &existing) |
| 80 | + if err != nil { |
| 81 | + if errors.IsNotFound(err) { |
| 82 | + return client.Create(ctx, clusterRole) |
| 83 | + } |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + existing.Rules = clusterRole.Rules |
| 88 | + existing.OwnerReferences = clusterRole.OwnerReferences |
| 89 | + return client.Update(ctx, &existing) |
| 90 | +} |
| 91 | + |
| 92 | +func (r *reconciler) ensureUserClusterRoleBinding(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { |
| 93 | + clusterRoleBinding := &rbacv1.ClusterRoleBinding{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: "kube-bind-oidc-user", |
| 96 | + }, |
| 97 | + RoleRef: rbacv1.RoleRef{ |
| 98 | + APIGroup: "rbac.authorization.k8s.io", |
| 99 | + Kind: "ClusterRole", |
| 100 | + Name: "kube-bind-oidc-user", |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, group := range r.allowedGroups { |
| 105 | + clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbacv1.Subject{ |
| 106 | + Kind: "Group", |
| 107 | + Name: group, |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + for _, user := range r.allowedUsers { |
| 112 | + clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbacv1.Subject{ |
| 113 | + Kind: "User", |
| 114 | + Name: user, |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + if err := controllerutil.SetControllerReference(cluster, clusterRoleBinding, client.Scheme()); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + var existing rbacv1.ClusterRoleBinding |
| 123 | + err := client.Get(ctx, types.NamespacedName{Name: "kube-bind-oidc-user"}, &existing) |
| 124 | + if err != nil { |
| 125 | + if errors.IsNotFound(err) { |
| 126 | + return client.Create(ctx, clusterRoleBinding) |
| 127 | + } |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + existing.RoleRef = clusterRoleBinding.RoleRef |
| 132 | + existing.Subjects = clusterRoleBinding.Subjects |
| 133 | + existing.OwnerReferences = clusterRoleBinding.OwnerReferences |
| 134 | + return client.Update(ctx, &existing) |
| 135 | +} |
0 commit comments