-
Notifications
You must be signed in to change notification settings - Fork 40
Add authz & cluster anchor #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| Copyright 2025 The Kube Bind Authors. | ||
|
|
||
| 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" | ||
| "fmt" | ||
| "reflect" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder" | ||
| mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" | ||
| mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile" | ||
|
|
||
| kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" | ||
| ) | ||
|
|
||
| const ( | ||
| controllerName = "kube-bind-backend-cluster" | ||
| ) | ||
|
|
||
| // ClusterReconciler reconciles a Cluster object. | ||
| type ClusterReconciler struct { | ||
| manager mcmanager.Manager | ||
| opts controller.TypedOptions[mcreconcile.Request] | ||
| reconciler reconciler | ||
| } | ||
|
|
||
| // NewClusterReconciler returns a new ClusterReconciler to reconcile Clusters | ||
| // ands its resources. | ||
| func NewClusterReconciler( | ||
| _ context.Context, | ||
| mgr mcmanager.Manager, | ||
| opts controller.TypedOptions[mcreconcile.Request], | ||
| allowedGroups []string, | ||
| allowedUsers []string, | ||
| ) (*ClusterReconciler, error) { | ||
| r := &ClusterReconciler{ | ||
| manager: mgr, | ||
| opts: opts, | ||
| reconciler: reconciler{ | ||
| allowedGroups: allowedGroups, | ||
| allowedUsers: allowedUsers, | ||
| }, | ||
| } | ||
|
|
||
| return r, nil | ||
| } | ||
|
|
||
| //+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterroles,verbs=get;list;watch;create;update;patch;delete | ||
| //+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=get;list;watch;create;update;patch;delete | ||
|
|
||
| // Reconcile is part of the main kubernetes reconciliation loop which aims to | ||
| // move the current state of the cluster closer to the desired state. | ||
| func (r *ClusterReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) { | ||
| logger := log.FromContext(ctx) | ||
| logger.Info("Reconciling Cluster", "request", req) | ||
|
|
||
| cl, err := r.manager.GetCluster(ctx, req.ClusterName) | ||
| if err != nil { | ||
| return ctrl.Result{}, fmt.Errorf("failed to get client for cluster %q: %w", req.ClusterName, err) | ||
| } | ||
|
|
||
| client := cl.GetClient() | ||
| cache := cl.GetCache() | ||
|
|
||
| cluster := &kubebindv1alpha2.Cluster{} | ||
| if err := client.Get(ctx, req.NamespacedName, cluster); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| logger.Info("Cluster not found, skipping reconciliation") | ||
| return ctrl.Result{}, nil | ||
| } | ||
| return ctrl.Result{}, fmt.Errorf("failed to get Cluster: %w", err) | ||
| } | ||
|
|
||
| original := cluster.DeepCopy() | ||
| if err := r.reconciler.reconcile(ctx, client, cache, cluster); err != nil { | ||
| logger.Error(err, "Failed to reconcile Cluster") | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(original, cluster) { | ||
| err := client.Update(ctx, cluster) | ||
| if err != nil { | ||
| logger.Error(err, "Failed to update Cluster status") | ||
| return ctrl.Result{}, fmt.Errorf("failed to update Cluster status: %w", err) | ||
| } | ||
| logger.Info("Cluster status updated") | ||
| } | ||
|
|
||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *ClusterReconciler) SetupWithManager(mgr mcmanager.Manager) error { | ||
| return mcbuilder.ControllerManagedBy(mgr). | ||
| For(&kubebindv1alpha2.Cluster{}). | ||
| Owns(&rbacv1.ClusterRole{}). | ||
| Owns(&rbacv1.ClusterRoleBinding{}). | ||
| Owns(&rbacv1.RoleBinding{}). | ||
| WithOptions(r.opts). | ||
| Named(controllerName). | ||
| Complete(r) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| Copyright 2025 The Kube Bind Authors. | ||
|
|
||
| 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" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
||
| kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" | ||
| ) | ||
|
|
||
| type reconciler struct { | ||
| allowedGroups []string | ||
| allowedUsers []string | ||
| } | ||
|
|
||
| func (r *reconciler) reconcile(ctx context.Context, client client.Client, _ cache.Cache, cluster *kubebindv1alpha2.Cluster) error { | ||
| var errs []error | ||
|
|
||
| if err := r.ensureOIDCRBAC(ctx, client, cluster); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
|
|
||
| return utilerrors.NewAggregate(errs) | ||
| } | ||
|
|
||
| func (r *reconciler) ensureOIDCRBAC(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { | ||
| if err := r.ensureUserClusterRole(ctx, client, cluster); err != nil { | ||
| return err | ||
| } | ||
| return r.ensureUserClusterRoleBinding(ctx, client, cluster) | ||
| } | ||
|
|
||
| func (r *reconciler) ensureUserClusterRole(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { | ||
| clusterRole := &rbacv1.ClusterRole{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "kube-bind-oidc-user", | ||
| }, | ||
| Rules: []rbacv1.PolicyRule{ | ||
| { | ||
| APIGroups: []string{"kube-bind.io"}, | ||
| Resources: []string{"*"}, | ||
| Verbs: []string{"bind"}, | ||
| }, | ||
| { | ||
| NonResourceURLs: []string{"/", "/api", "/api/*", "/apis", "/apis/*"}, | ||
| Verbs: []string{"access"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if err := controllerutil.SetControllerReference(cluster, clusterRole, client.Scheme()); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var existing rbacv1.ClusterRole | ||
| err := client.Get(ctx, types.NamespacedName{Name: "kube-bind-oidc-user"}, &existing) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return client.Create(ctx, clusterRole) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| existing.Rules = clusterRole.Rules | ||
| existing.OwnerReferences = clusterRole.OwnerReferences | ||
| return client.Update(ctx, &existing) | ||
| } | ||
|
|
||
| func (r *reconciler) ensureUserClusterRoleBinding(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error { | ||
| clusterRoleBinding := &rbacv1.ClusterRoleBinding{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "kube-bind-oidc-user", | ||
| }, | ||
| RoleRef: rbacv1.RoleRef{ | ||
| APIGroup: "rbac.authorization.k8s.io", | ||
| Kind: "ClusterRole", | ||
| Name: "kube-bind-oidc-user", | ||
| }, | ||
| } | ||
|
|
||
| for _, group := range r.allowedGroups { | ||
| clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbacv1.Subject{ | ||
| Kind: "Group", | ||
| Name: group, | ||
| }) | ||
| } | ||
|
|
||
| for _, user := range r.allowedUsers { | ||
| clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbacv1.Subject{ | ||
| Kind: "User", | ||
| Name: user, | ||
| }) | ||
| } | ||
|
|
||
| if err := controllerutil.SetControllerReference(cluster, clusterRoleBinding, client.Scheme()); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var existing rbacv1.ClusterRoleBinding | ||
| err := client.Get(ctx, types.NamespacedName{Name: "kube-bind-oidc-user"}, &existing) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return client.Create(ctx, clusterRoleBinding) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| existing.RoleRef = clusterRoleBinding.RoleRef | ||
| existing.Subjects = clusterRoleBinding.Subjects | ||
| existing.OwnerReferences = clusterRoleBinding.OwnerReferences | ||
| return client.Update(ctx, &existing) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.