Skip to content

fix: correct kubeconfig ownership when running under sudo#3826

Open
saiyam1814 wants to merge 1 commit into
loft-sh:mainfrom
saiyam1814:fix/sudo-kubeconfig-ownership
Open

fix: correct kubeconfig ownership when running under sudo#3826
saiyam1814 wants to merge 1 commit into
loft-sh:mainfrom
saiyam1814:fix/sudo-kubeconfig-ownership

Conversation

@saiyam1814
Copy link
Copy Markdown
Contributor

Summary

  • When running sudo vcluster create on a machine without an existing ~/.kube/config, the newly created kubeconfig is owned by root with 0600 permissions, making it unreadable by the actual user
  • After writing the kubeconfig, check SUDO_UID/SUDO_GID env vars and os.Chown the file + parent directory back to the real user
  • No-op when not running under sudo, or when the file already existed (POSIX preserves ownership on overwrite)

Changes

  • pkg/cli/sudo.go (new): fixFileOwnershipUnderSudo() — reads SUDO_UID/SUDO_GID, chowns file + parent dir
  • pkg/cli/connect_helm.go (+2 lines): calls the fix after both kubeconfig write paths in writeKubeConfig()

Test plan

  • go build ./pkg/cli/ passes
  • go vet ./pkg/cli/ passes
  • go test ./pkg/cli/ passes
  • sudo vcluster create test --driver docker on a machine with existing ~/.kube/config — works as before
  • sudo vcluster create test --driver docker after rm ~/.kube/config — kubeconfig should be user-owned

@saiyam1814 saiyam1814 requested a review from a team as a code owner April 10, 2026 08:53
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from f0dde8f to 0facfbf Compare April 10, 2026 08:56
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0facfbfe1f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from 0facfbf to a39df25 Compare April 10, 2026 09:03
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a39df25107

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from a39df25 to df0d4ed Compare April 10, 2026 09:14
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: df0d4ed11c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go Outdated
Comment thread pkg/cli/sudo.go Outdated
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from df0d4ed to df211b0 Compare April 10, 2026 09:33
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: df211b09ad

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go Outdated
Comment thread pkg/cli/sudo.go Outdated
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from df211b0 to 70d2176 Compare April 10, 2026 09:43
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 70d2176ce2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go
When running "sudo vcluster create" on a machine without an existing
~/.kube/config, the newly created kubeconfig is owned by root with 0600
permissions, making it unreadable by the actual user.

Fix by checking SUDO_UID/SUDO_GID after writing and chowning the file
and parent directory back to the real user. This is a no-op when not
running under sudo or when the file already existed (POSIX preserves
ownership on overwrite).
@saiyam1814 saiyam1814 force-pushed the fix/sudo-kubeconfig-ownership branch from 70d2176 to edc4f2f Compare April 10, 2026 09:54
@saiyam1814
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: edc4f2fc5b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/cli/sudo.go
Comment on lines +53 to +54
_ = os.Chown(filePath, uid, gid)
_ = os.Chown(filepath.Dir(filePath), uid, gid)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Resolve symlinks before chowning kubeconfig paths

This helper validates filePath with a string prefix check, then calls os.Chown on both the file and its parent. Because os.Chown follows symlinks, a path like ~/.kube -> /etc passes the home-directory guard but causes the ownership change to apply to the symlink targets outside the user home (including the parent directory target). Under sudo, this can unexpectedly re-own system paths and break the host. Resolve and validate real paths (or reject symlink components) before performing ownership changes.

Useful? React with 👍 / 👎.

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.

1 participant