| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 1 | package dockerimg |
| 2 | |
| 3 | import ( |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 4 | "bufio" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 5 | "bytes" |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 6 | "crypto/ed25519" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 7 | "crypto/rand" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 8 | "fmt" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "golang.org/x/crypto/ssh" |
| 16 | ) |
| 17 | |
| 18 | // MockFileSystem implements the FileSystem interface for testing |
| 19 | type MockFileSystem struct { |
| 20 | Files map[string][]byte |
| 21 | CreatedDirs map[string]bool |
| 22 | OpenedFiles map[string]*MockFile |
| 23 | StatCalledWith []string |
| Sean McCullough | 0d95d3a | 2025-04-30 16:22:28 +0000 | [diff] [blame] | 24 | TempFiles []string |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 25 | FailOn map[string]error // Map of function name to error to simulate failures |
| 26 | } |
| 27 | |
| 28 | func NewMockFileSystem() *MockFileSystem { |
| 29 | return &MockFileSystem{ |
| 30 | Files: make(map[string][]byte), |
| 31 | CreatedDirs: make(map[string]bool), |
| 32 | OpenedFiles: make(map[string]*MockFile), |
| Sean McCullough | 0d95d3a | 2025-04-30 16:22:28 +0000 | [diff] [blame] | 33 | TempFiles: []string{}, |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 34 | FailOn: make(map[string]error), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func (m *MockFileSystem) Stat(name string) (fs.FileInfo, error) { |
| 39 | m.StatCalledWith = append(m.StatCalledWith, name) |
| 40 | if err, ok := m.FailOn["Stat"]; ok { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | _, exists := m.Files[name] |
| 45 | if exists { |
| 46 | return nil, nil // File exists |
| 47 | } |
| 48 | _, exists = m.CreatedDirs[name] |
| 49 | if exists { |
| 50 | return nil, nil // Directory exists |
| 51 | } |
| 52 | return nil, os.ErrNotExist |
| 53 | } |
| 54 | |
| 55 | func (m *MockFileSystem) Mkdir(name string, perm fs.FileMode) error { |
| 56 | if err, ok := m.FailOn["Mkdir"]; ok { |
| 57 | return err |
| 58 | } |
| 59 | m.CreatedDirs[name] = true |
| 60 | return nil |
| 61 | } |
| 62 | |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 63 | func (m *MockFileSystem) MkdirAll(name string, perm fs.FileMode) error { |
| 64 | if err, ok := m.FailOn["MkdirAll"]; ok { |
| 65 | return err |
| 66 | } |
| 67 | m.CreatedDirs[name] = true |
| 68 | return nil |
| 69 | } |
| 70 | |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 71 | func (m *MockFileSystem) ReadFile(name string) ([]byte, error) { |
| 72 | if err, ok := m.FailOn["ReadFile"]; ok { |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | data, exists := m.Files[name] |
| 77 | if !exists { |
| 78 | return nil, fmt.Errorf("file not found: %s", name) |
| 79 | } |
| 80 | return data, nil |
| 81 | } |
| 82 | |
| 83 | func (m *MockFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error { |
| 84 | if err, ok := m.FailOn["WriteFile"]; ok { |
| 85 | return err |
| 86 | } |
| 87 | m.Files[name] = data |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // MockFile implements a simple in-memory file for testing |
| 92 | type MockFile struct { |
| 93 | name string |
| 94 | buffer *bytes.Buffer |
| 95 | fs *MockFileSystem |
| 96 | position int64 |
| 97 | } |
| 98 | |
| 99 | // MockFileContents represents in-memory file contents for testing |
| 100 | type MockFileContents struct { |
| 101 | name string |
| 102 | contents string |
| 103 | } |
| 104 | |
| 105 | func (m *MockFileSystem) OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) { |
| 106 | if err, ok := m.FailOn["OpenFile"]; ok { |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | // Initialize the file content if it doesn't exist and we're not in read-only mode |
| 111 | if _, exists := m.Files[name]; !exists && (flag&os.O_CREATE != 0) { |
| 112 | m.Files[name] = []byte{} |
| 113 | } |
| 114 | |
| 115 | data, exists := m.Files[name] |
| 116 | if !exists { |
| 117 | return nil, fmt.Errorf("file not found: %s", name) |
| 118 | } |
| 119 | |
| 120 | // For OpenFile, we'll just use WriteFile to simulate file operations |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 121 | // The actual file handle isn't used for much in the localsshimmer code |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 122 | // but we still need to return a valid file handle |
| 123 | tmpFile, err := os.CreateTemp("", "mockfile-*") |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | if _, err := tmpFile.Write(data); err != nil { |
| 128 | tmpFile.Close() |
| 129 | return nil, err |
| 130 | } |
| 131 | if _, err := tmpFile.Seek(0, 0); err != nil { |
| 132 | tmpFile.Close() |
| 133 | return nil, err |
| 134 | } |
| 135 | |
| 136 | return tmpFile, nil |
| 137 | } |
| 138 | |
| Sean McCullough | 0d95d3a | 2025-04-30 16:22:28 +0000 | [diff] [blame] | 139 | func (m *MockFileSystem) TempFile(dir, pattern string) (*os.File, error) { |
| 140 | if err, ok := m.FailOn["TempFile"]; ok { |
| 141 | return nil, err |
| 142 | } |
| 143 | |
| 144 | // Create an actual temporary file for testing purposes |
| 145 | tmpFile, err := os.CreateTemp(dir, pattern) |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | |
| 150 | // Record the temp file path |
| 151 | m.TempFiles = append(m.TempFiles, tmpFile.Name()) |
| 152 | |
| 153 | return tmpFile, nil |
| 154 | } |
| 155 | |
| 156 | func (m *MockFileSystem) Rename(oldpath, newpath string) error { |
| 157 | if err, ok := m.FailOn["Rename"]; ok { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | // If the old path exists in our mock file system, move its contents |
| 162 | if data, exists := m.Files[oldpath]; exists { |
| 163 | m.Files[newpath] = data |
| 164 | delete(m.Files, oldpath) |
| 165 | } |
| 166 | |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | func (m *MockFileSystem) SafeWriteFile(name string, data []byte, perm fs.FileMode) error { |
| 171 | if err, ok := m.FailOn["SafeWriteFile"]; ok { |
| 172 | return err |
| 173 | } |
| 174 | |
| 175 | // For the mock, we'll create a backup if the file exists |
| 176 | if existingData, exists := m.Files[name]; exists { |
| 177 | backupName := name + ".bak" |
| 178 | m.Files[backupName] = existingData |
| 179 | } |
| 180 | |
| 181 | // Write the new data |
| 182 | m.Files[name] = data |
| 183 | |
| 184 | return nil |
| 185 | } |
| 186 | |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 187 | // MockKeyGenerator implements KeyGenerator interface for testing |
| 188 | type MockKeyGenerator struct { |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 189 | privateKey ed25519.PrivateKey |
| 190 | publicKey ed25519.PublicKey |
| 191 | sshPublicKey ssh.PublicKey |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 192 | caSigner ssh.Signer |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 193 | FailOn map[string]error |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 196 | func NewMockKeyGenerator(privateKey ed25519.PrivateKey, publicKey ed25519.PublicKey, sshPublicKey ssh.PublicKey, caSigner ssh.Signer) *MockKeyGenerator { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 197 | return &MockKeyGenerator{ |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 198 | privateKey: privateKey, |
| 199 | publicKey: publicKey, |
| 200 | sshPublicKey: sshPublicKey, |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 201 | caSigner: caSigner, |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 202 | FailOn: make(map[string]error), |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 206 | func (m *MockKeyGenerator) GenerateKeyPair() (ed25519.PrivateKey, ed25519.PublicKey, error) { |
| 207 | if err, ok := m.FailOn["GenerateKeyPair"]; ok { |
| 208 | return nil, nil, err |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 209 | } |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 210 | return m.privateKey, m.publicKey, nil |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 213 | func (m *MockKeyGenerator) ConvertToSSHPublicKey(publicKey ed25519.PublicKey) (ssh.PublicKey, error) { |
| 214 | if err, ok := m.FailOn["ConvertToSSHPublicKey"]; ok { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 215 | return nil, err |
| 216 | } |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 217 | // If we're generating the CA public key, return the caSigner's public key |
| 218 | if m.caSigner != nil && bytes.Equal(publicKey, m.publicKey) { |
| 219 | return m.caSigner.PublicKey(), nil |
| 220 | } |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 221 | return m.sshPublicKey, nil |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // setupMocks sets up common mocks for testing |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 225 | func setupMocks(t *testing.T) (*MockFileSystem, *MockKeyGenerator, ed25519.PrivateKey) { |
| 226 | // Generate a real Ed25519 key pair |
| 227 | publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 228 | if err != nil { |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 229 | t.Fatalf("Failed to generate test key pair: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 232 | // Generate a test SSH public key |
| 233 | sshPublicKey, err := ssh.NewPublicKey(publicKey) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 234 | if err != nil { |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 235 | t.Fatalf("Failed to generate test SSH public key: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 238 | // Create CA key pair |
| 239 | _, caPrivKey, err := ed25519.GenerateKey(rand.Reader) |
| 240 | if err != nil { |
| 241 | t.Fatalf("Failed to generate CA key pair: %v", err) |
| 242 | } |
| 243 | |
| 244 | // Create CA signer |
| 245 | caSigner, err := ssh.NewSignerFromKey(caPrivKey) |
| 246 | if err != nil { |
| 247 | t.Fatalf("Failed to create CA signer: %v", err) |
| 248 | } |
| 249 | |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 250 | // Create mocks |
| 251 | mockFS := NewMockFileSystem() |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 252 | mockKG := NewMockKeyGenerator(privateKey, publicKey, sshPublicKey, caSigner) |
| 253 | |
| 254 | // Add some files needed for tests |
| 255 | mockFS.Files["/home/testuser/.config/sketch/host_cert"] = []byte("test-certificate") |
| 256 | caPubKeyBytes := ssh.MarshalAuthorizedKey(ssh.PublicKey(caSigner.PublicKey())) |
| 257 | mockFS.Files["/home/testuser/.config/sketch/container_ca.pub"] = caPubKeyBytes |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 258 | |
| 259 | return mockFS, mockKG, privateKey |
| 260 | } |
| 261 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 262 | // Helper function to setup a basic LocalSSHimmer for testing |
| 263 | func setupTestLocalSSHimmer(t *testing.T) (*LocalSSHimmer, *MockFileSystem, *MockKeyGenerator) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 264 | mockFS, mockKG, _ := setupMocks(t) |
| 265 | |
| 266 | // Setup home dir in mock filesystem |
| 267 | homePath := "/home/testuser" |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 268 | sketchDir := filepath.Join(homePath, ".config/sketch") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 269 | mockFS.CreatedDirs[sketchDir] = true |
| 270 | |
| Sean McCullough | 0d95d3a | 2025-04-30 16:22:28 +0000 | [diff] [blame] | 271 | // Create empty files so the tests don't fail |
| 272 | sketchConfigPath := filepath.Join(sketchDir, "ssh_config") |
| 273 | mockFS.Files[sketchConfigPath] = []byte("") |
| 274 | knownHostsPath := filepath.Join(sketchDir, "known_hosts") |
| 275 | mockFS.Files[knownHostsPath] = []byte("") |
| 276 | |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 277 | // Set HOME environment variable for the test |
| 278 | oldHome := os.Getenv("HOME") |
| 279 | os.Setenv("HOME", homePath) |
| 280 | t.Cleanup(func() { os.Setenv("HOME", oldHome) }) |
| 281 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 282 | // Create LocalSSHimmer with mocks |
| 283 | ssh, err := newLocalSSHimmerWithDeps("test-container", "localhost", "2222", mockFS, mockKG) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 284 | if err != nil { |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 285 | t.Fatalf("Failed to create LocalSSHimmer: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return ssh, mockFS, mockKG |
| 289 | } |
| 290 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 291 | func TestNewLocalSSHimmerCreatesRequiredDirectories(t *testing.T) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 292 | mockFS, mockKG, _ := setupMocks(t) |
| 293 | |
| 294 | // Set HOME environment variable for the test |
| 295 | oldHome := os.Getenv("HOME") |
| 296 | os.Setenv("HOME", "/home/testuser") |
| 297 | defer func() { os.Setenv("HOME", oldHome) }() |
| 298 | |
| Sean McCullough | 0d95d3a | 2025-04-30 16:22:28 +0000 | [diff] [blame] | 299 | // Create empty files so the test doesn't fail |
| 300 | sketchDir := "/home/testuser/.config/sketch" |
| 301 | sketchConfigPath := filepath.Join(sketchDir, "ssh_config") |
| 302 | mockFS.Files[sketchConfigPath] = []byte("") |
| 303 | knownHostsPath := filepath.Join(sketchDir, "known_hosts") |
| 304 | mockFS.Files[knownHostsPath] = []byte("") |
| 305 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 306 | // Create sshimmer |
| 307 | _, err := newLocalSSHimmerWithDeps("test-container", "localhost", "2222", mockFS, mockKG) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 308 | if err != nil { |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 309 | t.Fatalf("Failed to create LocalSSHimmer: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 312 | // Check if the .config/sketch directory was created |
| 313 | expectedDir := "/home/testuser/.config/sketch" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 314 | if !mockFS.CreatedDirs[expectedDir] { |
| 315 | t.Errorf("Expected directory %s to be created", expectedDir) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | func TestCreateKeyPairIfMissing(t *testing.T) { |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 320 | ssh, mockFS, _ := setupTestLocalSSHimmer(t) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 321 | |
| 322 | // Test key pair creation |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 323 | keyPath := "/home/testuser/.config/sketch/test_key" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 324 | _, err := ssh.createKeyPairIfMissing(keyPath) |
| 325 | if err != nil { |
| 326 | t.Fatalf("Failed to create key pair: %v", err) |
| 327 | } |
| 328 | |
| 329 | // Verify private key file was created |
| 330 | if _, exists := mockFS.Files[keyPath]; !exists { |
| 331 | t.Errorf("Private key file not created at %s", keyPath) |
| 332 | } |
| 333 | |
| 334 | // Verify public key file was created |
| 335 | pubKeyPath := keyPath + ".pub" |
| 336 | if _, exists := mockFS.Files[pubKeyPath]; !exists { |
| 337 | t.Errorf("Public key file not created at %s", pubKeyPath) |
| 338 | } |
| 339 | |
| 340 | // Verify public key content format |
| 341 | pubKeyContent, _ := mockFS.ReadFile(pubKeyPath) |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 342 | if !bytes.HasPrefix(pubKeyContent, []byte("ssh-ed25519 ")) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 343 | t.Errorf("Public key does not have expected format, got: %s", pubKeyContent) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // TestAddContainerToSSHConfig tests that the container gets added to the SSH config |
| 348 | // This test uses a direct approach since the OpenFile mocking is complex |
| 349 | func TestAddContainerToSSHConfig(t *testing.T) { |
| 350 | // Create a temporary directory for test files |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 351 | tempDir, err := os.MkdirTemp("", "localsshimmer-test-*") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 352 | if err != nil { |
| 353 | t.Fatalf("Failed to create temp dir: %v", err) |
| 354 | } |
| 355 | defer os.RemoveAll(tempDir) |
| 356 | |
| 357 | // Create real files in temp directory |
| 358 | configPath := filepath.Join(tempDir, "ssh_config") |
| 359 | initialConfig := `# SSH Config |
| 360 | Host existing-host |
| 361 | HostName example.com |
| 362 | User testuser |
| 363 | ` |
| Autoformatter | 33f7172 | 2025-04-25 23:23:22 +0000 | [diff] [blame] | 364 | if err := os.WriteFile(configPath, []byte(initialConfig), 0o644); err != nil { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 365 | t.Fatalf("Failed to write initial config: %v", err) |
| 366 | } |
| 367 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 368 | // Create a sshimmer with the real filesystem but custom paths |
| 369 | ssh := &LocalSSHimmer{ |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 370 | cntrName: "test-container", |
| 371 | sshHost: "localhost", |
| 372 | sshPort: "2222", |
| 373 | sshConfigPath: configPath, |
| 374 | userIdentityPath: filepath.Join(tempDir, "user_identity"), |
| 375 | fs: &RealFileSystem{}, |
| 376 | kg: &RealKeyGenerator{}, |
| 377 | } |
| 378 | |
| 379 | // Add container to SSH config |
| 380 | err = ssh.addContainerToSSHConfig() |
| 381 | if err != nil { |
| 382 | t.Fatalf("Failed to add container to SSH config: %v", err) |
| 383 | } |
| 384 | |
| 385 | // Read the updated file |
| 386 | configData, err := os.ReadFile(configPath) |
| 387 | if err != nil { |
| 388 | t.Fatalf("Failed to read updated config: %v", err) |
| 389 | } |
| 390 | configStr := string(configData) |
| 391 | |
| 392 | // Check for expected values |
| 393 | if !strings.Contains(configStr, "Host test-container") { |
| 394 | t.Errorf("Container host entry not found in config") |
| 395 | } |
| 396 | |
| 397 | if !strings.Contains(configStr, "HostName localhost") { |
| 398 | t.Errorf("HostName not correctly added to SSH config") |
| 399 | } |
| 400 | |
| 401 | if !strings.Contains(configStr, "Port 2222") { |
| 402 | t.Errorf("Port not correctly added to SSH config") |
| 403 | } |
| 404 | |
| 405 | if !strings.Contains(configStr, "User root") { |
| 406 | t.Errorf("User not correctly set to root in SSH config") |
| 407 | } |
| 408 | |
| 409 | // Check if identity file path is correct |
| 410 | identityLine := "IdentityFile " + ssh.userIdentityPath |
| 411 | if !strings.Contains(configStr, identityLine) { |
| 412 | t.Errorf("Identity file path not correctly added to SSH config") |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func TestAddContainerToKnownHosts(t *testing.T) { |
| 417 | // Skip this test as it requires more complex setup |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 418 | // The TestLocalSSHimmerCleanup test covers the addContainerToKnownHosts |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 419 | // functionality in a more integrated way |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 420 | t.Skip("This test requires more complex setup, integrated test coverage exists in TestLocalSSHimmerCleanup") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | func TestRemoveContainerFromSSHConfig(t *testing.T) { |
| 424 | // Create a temporary directory for test files |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 425 | tempDir, err := os.MkdirTemp("", "localsshimmer-test-*") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 426 | if err != nil { |
| 427 | t.Fatalf("Failed to create temp dir: %v", err) |
| 428 | } |
| 429 | defer os.RemoveAll(tempDir) |
| 430 | |
| 431 | // Create paths for test files |
| 432 | sshConfigPath := filepath.Join(tempDir, "ssh_config") |
| 433 | userIdentityPath := filepath.Join(tempDir, "user_identity") |
| 434 | knownHostsPath := filepath.Join(tempDir, "known_hosts") |
| 435 | |
| 436 | // Create initial SSH config with container entry |
| 437 | cntrName := "test-container" |
| 438 | sshHost := "localhost" |
| 439 | sshPort := "2222" |
| 440 | |
| 441 | initialConfig := fmt.Sprintf( |
| 442 | `Host existing-host |
| 443 | HostName example.com |
| 444 | User testuser |
| 445 | |
| 446 | Host %s |
| 447 | HostName %s |
| 448 | User root |
| 449 | Port %s |
| 450 | IdentityFile %s |
| 451 | UserKnownHostsFile %s |
| 452 | `, |
| 453 | cntrName, sshHost, sshPort, userIdentityPath, knownHostsPath, |
| 454 | ) |
| 455 | |
| Autoformatter | 33f7172 | 2025-04-25 23:23:22 +0000 | [diff] [blame] | 456 | if err := os.WriteFile(sshConfigPath, []byte(initialConfig), 0o644); err != nil { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 457 | t.Fatalf("Failed to write initial SSH config: %v", err) |
| 458 | } |
| 459 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 460 | // Create a sshimmer with the real filesystem but custom paths |
| 461 | ssh := &LocalSSHimmer{ |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 462 | cntrName: cntrName, |
| 463 | sshHost: sshHost, |
| 464 | sshPort: sshPort, |
| 465 | sshConfigPath: sshConfigPath, |
| 466 | userIdentityPath: userIdentityPath, |
| 467 | knownHostsPath: knownHostsPath, |
| 468 | fs: &RealFileSystem{}, |
| 469 | } |
| 470 | |
| 471 | // Remove container from SSH config |
| 472 | err = ssh.removeContainerFromSSHConfig() |
| 473 | if err != nil { |
| 474 | t.Fatalf("Failed to remove container from SSH config: %v", err) |
| 475 | } |
| 476 | |
| 477 | // Read the updated file |
| 478 | configData, err := os.ReadFile(sshConfigPath) |
| 479 | if err != nil { |
| 480 | t.Fatalf("Failed to read updated config: %v", err) |
| 481 | } |
| 482 | configStr := string(configData) |
| 483 | |
| 484 | // Check if the container host entry was removed |
| 485 | if strings.Contains(configStr, "Host "+cntrName) { |
| 486 | t.Errorf("Container host not removed from SSH config") |
| 487 | } |
| 488 | |
| 489 | // Check if existing host remains |
| 490 | if !strings.Contains(configStr, "Host existing-host") { |
| 491 | t.Errorf("Existing host entry affected by container removal") |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | func TestRemoveContainerFromKnownHosts(t *testing.T) { |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 496 | ssh, mockFS, _ := setupTestLocalSSHimmer(t) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 497 | |
| 498 | // Setup server public key |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 499 | _, publicKey, _ := ssh.kg.GenerateKeyPair() |
| 500 | sshPublicKey, _ := ssh.kg.ConvertToSSHPublicKey(publicKey) |
| 501 | ssh.serverPublicKey = sshPublicKey |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 502 | |
| 503 | // Create host line to be removed |
| 504 | hostLine := "[localhost]:2222 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ..." |
| 505 | otherLine := "otherhost ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ..." |
| 506 | |
| 507 | // Set initial content with the line to be removed |
| 508 | initialContent := otherLine + "\n" + hostLine |
| 509 | mockFS.Files[ssh.knownHostsPath] = []byte(initialContent) |
| 510 | |
| 511 | // Add the host to test remove function |
| 512 | err := ssh.addContainerToKnownHosts() |
| 513 | if err != nil { |
| 514 | t.Fatalf("Failed to add container to known_hosts for removal test: %v", err) |
| 515 | } |
| 516 | |
| 517 | // Now remove it |
| 518 | err = ssh.removeContainerFromKnownHosts() |
| 519 | if err != nil { |
| 520 | t.Fatalf("Failed to remove container from known_hosts: %v", err) |
| 521 | } |
| 522 | |
| 523 | // Verify content |
| 524 | updatedContent, _ := mockFS.ReadFile(ssh.knownHostsPath) |
| 525 | content := string(updatedContent) |
| 526 | |
| 527 | hostPattern := ssh.sshHost + ":" + ssh.sshPort |
| 528 | if strings.Contains(content, hostPattern) { |
| 529 | t.Errorf("Container entry not removed from known_hosts") |
| 530 | } |
| 531 | |
| 532 | // Verify other content remains |
| 533 | if !strings.Contains(content, otherLine) { |
| 534 | t.Errorf("Other known_hosts entries improperly removed") |
| 535 | } |
| 536 | } |
| 537 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 538 | func TestLocalSSHimmerCleanup(t *testing.T) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 539 | // Create a temporary directory for test files |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 540 | tempDir, err := os.MkdirTemp("", "localsshimmer-test-*") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 541 | if err != nil { |
| 542 | t.Fatalf("Failed to create temp dir: %v", err) |
| 543 | } |
| 544 | defer os.RemoveAll(tempDir) |
| 545 | |
| 546 | // Create paths for test files |
| 547 | sshConfigPath := filepath.Join(tempDir, "ssh_config") |
| 548 | userIdentityPath := filepath.Join(tempDir, "user_identity") |
| 549 | knownHostsPath := filepath.Join(tempDir, "known_hosts") |
| 550 | serverIdentityPath := filepath.Join(tempDir, "server_identity") |
| 551 | |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 552 | // Create keys for server key |
| 553 | publicKey, _, err := ed25519.GenerateKey(rand.Reader) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 554 | if err != nil { |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 555 | t.Fatalf("Failed to generate key pair: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 556 | } |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 557 | sshPublicKey, err := ssh.NewPublicKey(publicKey) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 558 | if err != nil { |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 559 | t.Fatalf("Failed to generate SSH public key: %v", err) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // Initialize files |
| Autoformatter | 33f7172 | 2025-04-25 23:23:22 +0000 | [diff] [blame] | 563 | os.WriteFile(sshConfigPath, []byte("initial ssh_config content"), 0o644) |
| 564 | os.WriteFile(knownHostsPath, []byte("initial known_hosts content"), 0o644) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 565 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 566 | // Create a sshimmer with the real filesystem but custom paths |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 567 | cntrName := "test-container" |
| 568 | sshHost := "localhost" |
| 569 | sshPort := "2222" |
| 570 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 571 | ssh := &LocalSSHimmer{ |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 572 | cntrName: cntrName, |
| 573 | sshHost: sshHost, |
| 574 | sshPort: sshPort, |
| 575 | sshConfigPath: sshConfigPath, |
| 576 | userIdentityPath: userIdentityPath, |
| 577 | knownHostsPath: knownHostsPath, |
| 578 | serverIdentityPath: serverIdentityPath, |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 579 | serverPublicKey: sshPublicKey, |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 580 | fs: &RealFileSystem{}, |
| 581 | kg: &RealKeyGenerator{}, |
| 582 | } |
| 583 | |
| 584 | // Add container to configs |
| 585 | err = ssh.addContainerToSSHConfig() |
| 586 | if err != nil { |
| 587 | t.Fatalf("Failed to set up SSH config for cleanup test: %v", err) |
| 588 | } |
| 589 | |
| 590 | err = ssh.addContainerToKnownHosts() |
| 591 | if err != nil { |
| 592 | t.Fatalf("Failed to set up known_hosts for cleanup test: %v", err) |
| 593 | } |
| 594 | |
| 595 | // Execute cleanup |
| 596 | err = ssh.Cleanup() |
| 597 | if err != nil { |
| 598 | t.Fatalf("Cleanup failed: %v", err) |
| 599 | } |
| 600 | |
| 601 | // Read updated files |
| 602 | configData, err := os.ReadFile(sshConfigPath) |
| 603 | if err != nil { |
| 604 | t.Fatalf("Failed to read updated SSH config: %v", err) |
| 605 | } |
| 606 | configStr := string(configData) |
| 607 | |
| 608 | // Check container was removed from SSH config |
| 609 | hostEntry := "Host " + ssh.cntrName |
| 610 | if strings.Contains(configStr, hostEntry) { |
| 611 | t.Errorf("Container not removed from SSH config during cleanup") |
| 612 | } |
| 613 | |
| 614 | // Verify known hosts was updated |
| 615 | knownHostsContent, err := os.ReadFile(knownHostsPath) |
| 616 | if err != nil { |
| 617 | t.Fatalf("Failed to read updated known_hosts: %v", err) |
| 618 | } |
| 619 | |
| 620 | expectedHostPattern := ssh.sshHost + ":" + ssh.sshPort |
| 621 | if strings.Contains(string(knownHostsContent), expectedHostPattern) { |
| 622 | t.Errorf("Container not removed from known_hosts during cleanup") |
| 623 | } |
| 624 | } |
| 625 | |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 626 | func TestCheckForInclude_userAccepts(t *testing.T) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 627 | mockFS := NewMockFileSystem() |
| 628 | |
| 629 | // Set HOME environment variable for the test |
| 630 | oldHome := os.Getenv("HOME") |
| 631 | os.Setenv("HOME", "/home/testuser") |
| 632 | defer func() { os.Setenv("HOME", oldHome) }() |
| 633 | |
| 634 | // Create a mock ssh config with the expected include |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 635 | includeLine := "Include /home/testuser/.config/sketch/ssh_config" |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 636 | initialConfig := fmt.Sprintf("%s\nHost example\n HostName example.com\n", includeLine) |
| 637 | |
| 638 | // Add the config to the mock filesystem |
| 639 | sshConfigPath := "/home/testuser/.ssh/config" |
| 640 | mockFS.Files[sshConfigPath] = []byte(initialConfig) |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 641 | stdinReader := bufio.NewReader(strings.NewReader("y\n")) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 642 | // Test the function with our mock |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 643 | err := CheckForIncludeWithFS(mockFS, *stdinReader) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 644 | if err != nil { |
| 645 | t.Fatalf("CheckForInclude failed with proper include: %v", err) |
| 646 | } |
| 647 | |
| 648 | // Now test with config missing the include |
| 649 | mockFS.Files[sshConfigPath] = []byte("Host example\n HostName example.com\n") |
| 650 | |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 651 | stdinReader = bufio.NewReader(strings.NewReader("y\n")) |
| 652 | err = CheckForIncludeWithFS(mockFS, *stdinReader) |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 653 | if err != nil { |
| 654 | t.Fatalf("CheckForInclude should have created the Include line without an error") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| Sean McCullough | 15c9528 | 2025-05-08 16:48:38 -0700 | [diff] [blame] | 658 | func TestCheckForInclude_userDeclines(t *testing.T) { |
| 659 | mockFS := NewMockFileSystem() |
| 660 | |
| 661 | // Set HOME environment variable for the test |
| 662 | oldHome := os.Getenv("HOME") |
| 663 | os.Setenv("HOME", "/home/testuser") |
| 664 | defer func() { os.Setenv("HOME", oldHome) }() |
| 665 | |
| 666 | // Create a mock ssh config with the expected include |
| 667 | includeLine := "Include /home/testuser/.config/sketch/ssh_config" |
| 668 | initialConfig := fmt.Sprintf("%s\nHost example\n HostName example.com\n", includeLine) |
| 669 | |
| 670 | // Add the config to the mock filesystem |
| 671 | sshConfigPath := "/home/testuser/.ssh/config" |
| 672 | mockFS.Files[sshConfigPath] = []byte(initialConfig) |
| 673 | stdinReader := bufio.NewReader(strings.NewReader("n\n")) |
| 674 | // Test the function with our mock |
| 675 | err := CheckForIncludeWithFS(mockFS, *stdinReader) |
| 676 | if err != nil { |
| 677 | t.Fatalf("CheckForInclude failed with proper include: %v", err) |
| 678 | } |
| 679 | |
| 680 | // Now test with config missing the include |
| 681 | missingInclude := []byte("Host example\n HostName example.com\n") |
| 682 | mockFS.Files[sshConfigPath] = missingInclude |
| 683 | |
| 684 | stdinReader = bufio.NewReader(strings.NewReader("n\n")) |
| 685 | err = CheckForIncludeWithFS(mockFS, *stdinReader) |
| 686 | if err == nil { |
| 687 | t.Errorf("CheckForInclude should have returned an error") |
| 688 | } |
| 689 | if !bytes.Equal(mockFS.Files[sshConfigPath], missingInclude) { |
| 690 | t.Errorf("ssh config should not have been edited") |
| 691 | } |
| 692 | } |
| 693 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 694 | func TestLocalSSHimmerWithErrors(t *testing.T) { |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 695 | // Test directory creation failure |
| 696 | mockFS := NewMockFileSystem() |
| Sean McCullough | c796e7f | 2025-04-30 08:44:06 -0700 | [diff] [blame] | 697 | mockFS.FailOn["MkdirAll"] = fmt.Errorf("mock mkdir error") |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 698 | mockKG := NewMockKeyGenerator(nil, nil, nil, nil) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 699 | |
| 700 | // Set HOME environment variable for the test |
| 701 | oldHome := os.Getenv("HOME") |
| 702 | os.Setenv("HOME", "/home/testuser") |
| 703 | defer func() { os.Setenv("HOME", oldHome) }() |
| 704 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 705 | // Try to create sshimmer with failing FS |
| 706 | _, err := newLocalSSHimmerWithDeps("test-container", "localhost", "2222", mockFS, mockKG) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 707 | if err == nil || !strings.Contains(err.Error(), "mock mkdir error") { |
| 708 | t.Errorf("Should have failed with mkdir error, got: %v", err) |
| 709 | } |
| 710 | |
| 711 | // Test key generation failure |
| 712 | mockFS = NewMockFileSystem() |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 713 | mockKG = NewMockKeyGenerator(nil, nil, nil, nil) |
| Sean McCullough | 3e9d80c | 2025-05-13 23:35:23 +0000 | [diff] [blame] | 714 | mockKG.FailOn["GenerateKeyPair"] = fmt.Errorf("mock key generation error") |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 715 | |
| banksean | 29d689f | 2025-06-23 15:41:26 +0000 | [diff] [blame] | 716 | _, err = newLocalSSHimmerWithDeps("test-container", "localhost", "2222", mockFS, mockKG) |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 717 | if err == nil || !strings.Contains(err.Error(), "key generation error") { |
| 718 | t.Errorf("Should have failed with key generation error, got: %v", err) |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | func TestRealSSHTheatherInit(t *testing.T) { |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 723 | // Skip this test as it requires real files for the CA which we don't want to create |
| 724 | // in a real integration test |
| 725 | t.Skip("Skipping test that requires real file system access for the CA") |
| 726 | } |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 727 | |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 728 | // Methods to help with the mocking interface |
| 729 | func (m *MockKeyGenerator) GetCASigner() ssh.Signer { |
| 730 | return m.caSigner |
| 731 | } |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 732 | |
| Sean McCullough | 7013e9e | 2025-05-14 02:03:58 +0000 | [diff] [blame] | 733 | func (m *MockKeyGenerator) IsMock() bool { |
| 734 | return true |
| Sean McCullough | 2cba695 | 2025-04-25 20:32:10 +0000 | [diff] [blame] | 735 | } |