-
Notifications
You must be signed in to change notification settings - Fork 12
fix(provisioner): split SSH sessions in createKindConfig #657
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?
fix(provisioner): split SSH sessions in createKindConfig #657
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes critical SSH session management bugs in the createKindConfig function by addressing two audit findings: SSH session reuse after Run() (HIGH severity) and a local file resource leak (MEDIUM severity). The fix splits a single SSH session into two separate sessions and adds proper cleanup with defer statements.
Changes:
- Split SSH session usage: mkdir operation now uses dedicated session1, file write uses dedicated session2
- Added defer statement to close local file handle, fixing resource leak
- Refactored to use local
kindConfigPathvariable instead of mutating the environment struct
| if err := session2.Start("cat > " + remoteFilePath); err != nil { | ||
| return fmt.Errorf("failed to start session: %w", err) | ||
| } | ||
|
|
||
| // open local file for reading | ||
| // first check if file path is relative or absolute | ||
| // if relative, then prepend the current working directory | ||
| if !filepath.IsAbs(env.Spec.Kubernetes.KindConfig) { | ||
| // Resolve local file path | ||
| kindConfigPath := env.Spec.Kubernetes.KindConfig | ||
| if !filepath.IsAbs(kindConfigPath) { | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get current working directory: %w", err) | ||
| } | ||
|
|
||
| env.Spec.Kubernetes.KindConfig = filepath.Join(cwd, strings.TrimPrefix(env.Spec.Kubernetes.KindConfig, "./")) | ||
| kindConfigPath = filepath.Join(cwd, strings.TrimPrefix(kindConfigPath, "./")) | ||
| } | ||
|
|
||
| localFile, err := os.Open(env.Spec.Kubernetes.KindConfig) | ||
| localFile, err := os.Open(kindConfigPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open local file %s: %w", env.Spec.Kubernetes.KindConfig, err) | ||
| return fmt.Errorf("failed to open local file %s: %w", kindConfigPath, err) | ||
| } | ||
| defer func() { _ = localFile.Close() }() | ||
|
|
||
| // copy local file to remote file | ||
| if _, err := io.Copy(remoteFile, localFile); err != nil { | ||
| return fmt.Errorf("failed to copy local file %s to remote file %s: %w", env.Spec.Kubernetes.KindConfig, remoteFilePath, err) | ||
| return fmt.Errorf("failed to copy local file %s to remote file %s: %w", kindConfigPath, remoteFilePath, err) | ||
| } |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an error occurs after session2.Start() is called (lines 304-319), the function returns without calling session2.Wait() or closing remoteFile properly. This could leave the SSH session in an incomplete state. Consider closing remoteFile and calling session2.Wait() in error paths, similar to how the provision() function handles this (lines 249-258).
Pull Request Test Coverage Report for Build 21979597278Details
💛 - Coveralls |
SSH sessions in x/crypto/ssh are single-use. The old code called session.Run() then tried session.StdinPipe() on the same session, which always failed. Split into two sessions (mkdir + file write), following the pattern used in createKubeAdmConfig. Also close the local file that was previously leaked. Audit findings NVIDIA#2 (HIGH), NVIDIA#12 (MEDIUM). Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
ed1d8b9 to
f362ca8
Compare
Summary
Audit Findings
Changes
pkg/provisioner/provisioner.go: Rewrite createKindConfig with two SSH sessions and proper resource cleanupTest plan
gofmt— no formatting issuesgo build— compilesgo test ./pkg/...— all tests pass