-
Notifications
You must be signed in to change notification settings - Fork 575
fix: correct kubeconfig ownership when running under sudo #3826
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/user" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // fixFileOwnershipUnderSudo corrects ownership of a file and its parent directory | ||
| // when running under sudo. This handles the corner case where "sudo vcluster create" | ||
| // is run on a machine without an existing ~/.kube/config — the newly created file | ||
| // and directory are root-owned, making them unusable by the actual user. When the | ||
| // file already exists, overwriting preserves the original ownership (POSIX behavior), | ||
| // so this is a no-op in the common case. | ||
| // | ||
| // Only paths under the invoking user's home directory (resolved via os/user from | ||
| // SUDO_USER) are modified. System paths like /etc or /tmp are never touched. | ||
| func fixFileOwnershipUnderSudo(filePath string) { | ||
| filePath, _ = filepath.Abs(filePath) | ||
|
|
||
| sudoUID := os.Getenv("SUDO_UID") | ||
| sudoGID := os.Getenv("SUDO_GID") | ||
| sudoUser := os.Getenv("SUDO_USER") | ||
| if sudoUID == "" || sudoGID == "" || sudoUser == "" { | ||
| return | ||
| } | ||
|
|
||
| uid, err := strconv.Atoi(sudoUID) | ||
| if err != nil { | ||
| return | ||
| } | ||
| gid, err := strconv.Atoi(sudoGID) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| // Resolve the real user's home from the system user database (passwd/LDAP/ | ||
| // directory services). This avoids hardcoding /home or /Users and handles | ||
| // non-standard home layouts. | ||
| u, err := user.Lookup(sudoUser) | ||
| if err != nil || u.HomeDir == "" { | ||
| return | ||
| } | ||
|
|
||
| // Only fix ownership for paths under the user's home directory. | ||
| // Anything outside (e.g. /etc/kubernetes/admin.conf, /tmp) is left untouched. | ||
| if !strings.HasPrefix(filePath, u.HomeDir+string(os.PathSeparator)) { | ||
| return | ||
| } | ||
|
|
||
| _ = os.Chown(filePath, uid, gid) | ||
| _ = os.Chown(filepath.Dir(filePath), uid, gid) | ||
|
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This helper validates Useful? React with 👍 / 👎. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.