Commit 5ed0afd
authored
refactor(ssh): redesign SSH client with improved resource management and security (oomol-lab#75)
This commit completely redesigns the SSH client implementation with better architecture, resource management, and security features.
## Key Changes
### SSH Package Redesign
- Implement layered architecture: Client -> Session -> Executor
- Add proper lifecycle management with sync.Once and RWMutex
- Introduce dedicated config types (ClientConfig, SessionConfig, ExecOptions)
- Fix race conditions in keepalive loop by using local variable copies
- Resolve "already closed" connection errors with isErrorIsConnectionAlreadyClosed()
- Add comprehensive package documentation (doc.go)
### Security Improvements
- Use shellescape.QuoteCommand() to prevent command injection vulnerabilities
- Properly escape shell arguments in command execution
### Resource Management
- Add automatic keepalive with configurable intervals (default: 5s)
- Implement proper defer-based cleanup for all resources
- Fix pipe management: close writers (not readers) to signal EOF correctly
- Add WaitGroup-based I/O synchronization to prevent goroutine leaks
### Terminal Handling (pkg/system/terminal.go)
- Fix signal handler leak by adding defer signal.Stop(ch)
- Improve terminal state management with wrapper types
- Add terminal type and size auto-detection
- Enhance PTY resize handling with proper context cancellation
### API Changes
- Migrate from old monolithic ssh.Cfg to layered Client/Session/Executor
- Remove manual cleanup callbacks in favor of defer-based patterns
- cmd/attach.go: Refactor to use new SSH API with proper PTY/non-PTY handling
- pkg/server/exec.go: Fix resource management by closing client in goroutine
- pkg/vmconfig: Update SSH key generation and connection probing
### Dependencies
- Add al.essio.dev/pkg/shellescape for safe command escaping
- Add github.com/google/shlex for command parsing
- Add github.com/mattn/go-isatty for reliable terminal detection
### Miscellaneous
- Improve server mode logging with String() method
- Update libkrun bindings (libkrun_v2.go replaces libkrun.go)
- Remove unused install_name_tool.sh script line
## Testing
- Verified attach command works in both PTY and non-PTY modes
- Confirmed REST API exec functionality
- Tested SSH connection probing during VM startup
- Validated proper resource cleanup under various scenarios1 parent 754f1cd commit 5ed0afd
File tree
19 files changed
+2627
-748
lines changed- cmd
- pkg
- libkrun
- server
- ssh
- system
- vmconfig
- vm
- scripts
19 files changed
+2627
-748
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
| 34 | + | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
39 | 46 | | |
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
44 | 55 | | |
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
48 | 59 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
58 | 67 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
64 | 72 | | |
| 73 | + | |
65 | 74 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
69 | 79 | | |
| 80 | + | |
70 | 81 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
| 82 | + | |
| 83 | + | |
76 | 84 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
81 | 96 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 97 | + | |
86 | 98 | | |
87 | 99 | | |
88 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
89 | 105 | | |
90 | 106 | | |
91 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
92 | 120 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| 40 | + | |
| 41 | + | |
38 | 42 | | |
39 | 43 | | |
40 | 44 | | |
| |||
49 | 53 | | |
50 | 54 | | |
51 | 55 | | |
| 56 | + | |
| 57 | + | |
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
| |||
119 | 125 | | |
120 | 126 | | |
121 | 127 | | |
| 128 | + | |
122 | 129 | | |
123 | 130 | | |
124 | 131 | | |
| |||
0 commit comments