Skip to content

Feat/single cluster auth - #6602

Draft
ljluestc wants to merge 2 commits into
kubesphere:masterfrom
ljluestc:feat/single-cluster-auth
Draft

ljluestc wants to merge 2 commits into
kubesphere:masterfrom
ljluestc:feat/single-cluster-auth

Conversation

@ljluestc

@ljluestc ljluestc commented Jan 4, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR implements single-cluster authorization, allowing granular access control for users on specific member clusters.

Previously, the checkClusterPermission logic contained a hardcoded check that effectively restricted cluster access to users with the cluster-admin role (or platform admins). This prevented scenarios where a user (e.g., a VM owner) needed read-only or limited management access (cluster-viewer) to a specific connected cluster without having elevated platform privileges.

Changes:

  • Updated pkg/models/tenant/tenant.go: The checkClusterPermission function now grants access if any ClusterRoleBinding exists for the user in the target cluster.
  • This delegates the specific permission level (Read vs. Write) to the actual roles bound to the user, rather than blocking the request at the tenant check level.

Which issue(s) this PR fixes:

Fixes #6271

Special notes for reviewers:

The logic change is in pkg/models/tenant/tenant.go. I replaced the temporary FIXME/TODO block that checked specifically for iamv1beta1.ClusterAdmin with a check that validates if the list of ClusterRoleBindings for the user is non-empty.

Does this PR introduced a user-facing change?

Support single-cluster authorization: Users assigned granular roles (e.g., `cluster-viewer`) on specific member clusters can now access those clusters without requiring platform-level administrator privileges.

Additional documentation, usage docs, etc.:


@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @ljluestc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the authorization model by introducing single-cluster authorization, allowing users to be granted specific roles on individual member clusters without needing platform-wide administrative privileges. This change refines the 'checkClusterPermission' logic to be more flexible, enabling scenarios where users require limited access to particular clusters. Alongside this core authorization update, new API endpoints have been added to facilitate the management of cluster members, providing a comprehensive solution for granular access control.

Highlights

  • Single-Cluster Authorization: Implements granular access control for users on specific member clusters, moving beyond the previous restriction to only 'cluster-admin' or platform administrators.
  • Permission Logic Update: The 'checkClusterPermission' function has been modified to grant access if any 'ClusterRoleBinding' exists for the user in the target cluster, rather than requiring a specific 'cluster-admin' role.
  • Delegated Permissions: Permission levels (Read vs. Write) are now delegated to the actual roles bound to the user, allowing for more flexible access control without blocking requests at a higher tenant check level.
  • New API Endpoints for Cluster Member Management: Introduces new API endpoints for listing, creating, removing, and updating cluster members under '/clusters/{cluster}/members'.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new API endpoints and backend logic for managing cluster members, enabling the listing, creation, removal, and updating of user roles within a specified cluster by manipulating ClusterRoleBinding resources. It also includes a minor fix for a JSON tag typo. Review comments highlighted that the RemoveClusterMember function has potential issues with loop variable addressing and error handling, as it currently returns on the first error, which could lead to an inconsistent state. Furthermore, the UpdateClusterMember implementation, which relies on a non-atomic remove-then-create operation, could result in a temporary loss of user access if the creation step fails.

Comment on lines +822 to +827
for _, crb := range crbList.Items {
if err := rtClient.Delete(context.Background(), &crb); err != nil {
return err
}
}
return nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This loop for deleting ClusterRoleBindings has a couple of issues:

  1. Loop variable address: You are taking the address of the loop variable crb (&crb). In Go, this variable is reused in each iteration. While Delete is synchronous and this might work, it's a common pitfall that can lead to bugs and is best avoided. Iterating by index is safer.
  2. Error handling: If deleting one of the bindings fails, the function returns immediately. This can leave the user's permissions in an inconsistent state. It's more robust to attempt to delete all bindings and then report any accumulated errors.

Here is a suggestion to address both points.

Suggested change
for _, crb := range crbList.Items {
if err := rtClient.Delete(context.Background(), &crb); err != nil {
return err
}
}
return nil
var allErrors []string
for i := range crbList.Items {
if err := rtClient.Delete(context.Background(), &crbList.Items[i]); err != nil {
allErrors = append(allErrors, err.Error())
}
}
if len(allErrors) > 0 {
return fmt.Errorf("failed to remove all cluster member roles: %s", strings.Join(allErrors, "; "))
}
return nil

Comment on lines +831 to +835
// Simple implementation: remove all existing bindings for user and add new one
if err := t.RemoveClusterMember(cluster, username); err != nil {
return err
}
return t.CreateClusterMember(cluster, username, role)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment acknowledges this is a "Simple implementation", but it's worth noting that this approach is not atomic. If RemoveClusterMember succeeds but CreateClusterMember fails, the user will be left without any roles in the cluster. This could lead to a temporary or permanent loss of access until manually fixed.

A more robust approach would be to ensure the operation is as atomic as possible. For example, you could list the existing bindings, create the new binding, and only then delete the old ones. If creating the new binding fails, the user still has their old role.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grant the user permission to manage a single cluster

1 participant