Skip to content

Enhance Kubernetes configuration by setting kubeconfig permissions#3048

Merged
ks-ci-bot merged 1 commit intokubesphere:mainfrom
redscholar:sudo_user
Mar 24, 2026
Merged

Enhance Kubernetes configuration by setting kubeconfig permissions#3048
ks-ci-bot merged 1 commit intokubesphere:mainfrom
redscholar:sudo_user

Conversation

@redscholar
Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

  • Added tasks to set permissions for the kubeconfig file in both the renewal and join processes, ensuring proper access control.
  • Introduced a function to retrieve the current local user, improving user context handling in the local connector.
  • Updated the SSH connector to pass the SUDO_USER environment variable, enhancing command execution context.

These changes improve security and user management in Kubernetes operations.

Which issue(s) this PR fixes:

Fixes #

Special notes for reviewers:

Does this PR introduced a user-facing change?

none

Additional documentation, usage docs, etc.:


- Added tasks to set permissions for the kubeconfig file in both the renewal and join processes, ensuring proper access control.
- Introduced a function to retrieve the current local user, improving user context handling in the local connector.
- Updated the SSH connector to pass the SUDO_USER environment variable, enhancing command execution context.

These changes improve security and user management in Kubernetes operations.

Signed-off-by: redscholar <blacktiledhouse@gmail.com>
@kubesphere-prow kubesphere-prow Bot added release-note-none kind/bug Categorizes issue or PR as related to a bug. labels Mar 24, 2026
@kubesphere-prow
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: redscholar

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubesphere-prow kubesphere-prow Bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 24, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 security and user management aspects of Kubernetes operations. It introduces robust mechanisms for setting appropriate permissions on kubeconfig files during cluster setup and renewal, preventing unauthorized access. Furthermore, it refines how user context is handled within the system's connectors, ensuring that commands executed with sudo correctly reflect the initiating user.

Highlights

  • Kubeconfig Permissions: Implemented tasks to correctly set file permissions for kubeconfig during Kubernetes certificate renewal and cluster joining processes, ensuring proper access control.
  • Local User Retrieval: Introduced a new function getLocalUser in the local connector to dynamically determine the current local user, improving user context handling.
  • SUDO_USER Environment Variable: Ensured the SUDO_USER environment variable is properly passed in both local and SSH connector command executions for enhanced command context.
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.

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.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

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

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 changes to explicitly set the SUDO_USER environment variable for sudo commands executed via local and SSH connectors, and adds chown tasks in Kubernetes Ansible playbooks to manage kubeconfig file permissions. However, the local_connector.go implementation for setting SUDO_USER is problematic as command.SetEnv replaces the entire environment, potentially breaking sudo by removing essential variables like PATH. Furthermore, the chown commands in the Ansible playbooks contain a logical flaw where ~ expands to /root when run with sudo, causing both chown operations to target /root/.kube/config and thus making the intended user-specific permission change ineffective.

klog.V(5).InfoS("exec local command", "cmd", cmd)
// in
command := c.Cmd.CommandContext(ctx, "sudo", "-SE", c.shell, "-c", cmd)
command.SetEnv([]string{"SUDO_USER=" + c.User})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

command.SetEnv replaces the entire environment for the child process. This will likely cause the sudo command to fail because essential environment variables like PATH will be missing. You should append to the current environment instead of replacing it to ensure the command executes in a valid context.

Suggested change
command.SetEnv([]string{"SUDO_USER=" + c.User})
command.SetEnv(append(os.Environ(), "SUDO_USER="+c.User))

Comment on lines +59 to +60
chown "$SUDO_USER":root ~/.kube/config
chown root:root /root/.kube/config No newline at end of file
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

The chown command for the user's kubeconfig is flawed. When the command is executed with sudo, the shell runs as root, causing ~ to expand to /root. As a result, both chown commands in this task operate on the same file, /root/.kube/config. The second command immediately overwrites the first, making the permission change for $SUDO_USER ineffective. You should use a more reliable method to get the user's home directory.

    if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
      USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
      if [ -n "$USER_HOME" ]; then
        chown "$SUDO_USER":root "$USER_HOME"/.kube/config
      fi
    fi
    chown root:root /root/.kube/config

Comment on lines +32 to +33
chown "$SUDO_USER":root ~/.kube/config
chown root:root /root/.kube/config
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 command has a logical flaw. When executed with sudo, the shell runs as root, so ~ expands to /root. This means both chown commands target /root/.kube/config. The second command (chown root:root ...) nullifies the effect of the first one. To correctly set permissions for the user who invoked sudo, you need to determine their home directory explicitly rather than using ~.

    if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
      USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
      if [ -n "$USER_HOME" ]; then
        chown "$SUDO_USER":root "$USER_HOME"/.kube/config
      fi
    fi
    chown root:root /root/.kube/config

@redscholar redscholar added the lgtm Indicates that a PR is ready to be merged. label Mar 24, 2026
@ks-ci-bot ks-ci-bot merged commit 2add32d into kubesphere:main Mar 24, 2026
7 checks passed
@redscholar redscholar deleted the sudo_user branch March 24, 2026 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note-none size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants