|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { ensureHostKey, validateHostKeyExists } from '../../src/proxy/ssh/hostKeyManager'; |
| 3 | + |
| 4 | +// Mock modules |
| 5 | +const { fsStub, childProcessStub } = vi.hoisted(() => { |
| 6 | + return { |
| 7 | + fsStub: { |
| 8 | + existsSync: vi.fn(), |
| 9 | + readFileSync: vi.fn(), |
| 10 | + mkdirSync: vi.fn(), |
| 11 | + accessSync: vi.fn(), |
| 12 | + constants: { R_OK: 4 }, |
| 13 | + }, |
| 14 | + childProcessStub: { |
| 15 | + execSync: vi.fn(), |
| 16 | + }, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock('fs', async () => { |
| 21 | + const actual = await vi.importActual<typeof import('fs')>('fs'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + existsSync: fsStub.existsSync, |
| 25 | + readFileSync: fsStub.readFileSync, |
| 26 | + mkdirSync: fsStub.mkdirSync, |
| 27 | + accessSync: fsStub.accessSync, |
| 28 | + constants: fsStub.constants, |
| 29 | + default: { |
| 30 | + ...actual, |
| 31 | + existsSync: fsStub.existsSync, |
| 32 | + readFileSync: fsStub.readFileSync, |
| 33 | + mkdirSync: fsStub.mkdirSync, |
| 34 | + accessSync: fsStub.accessSync, |
| 35 | + constants: fsStub.constants, |
| 36 | + }, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock('child_process', async () => { |
| 41 | + const actual = await vi.importActual<typeof import('child_process')>('child_process'); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + execSync: childProcessStub.execSync, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('hostKeyManager', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + vi.restoreAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('ensureHostKey', () => { |
| 58 | + it('should return existing host key when it exists', () => { |
| 59 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 60 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 61 | + const mockKeyData = Buffer.from( |
| 62 | + '-----BEGIN OPENSSH PRIVATE KEY-----\ntest\n-----END OPENSSH PRIVATE KEY-----', |
| 63 | + ); |
| 64 | + |
| 65 | + fsStub.existsSync.mockReturnValue(true); |
| 66 | + fsStub.readFileSync.mockReturnValue(mockKeyData); |
| 67 | + |
| 68 | + const result = ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 69 | + |
| 70 | + expect(result).toEqual(mockKeyData); |
| 71 | + expect(fsStub.existsSync).toHaveBeenCalledWith(privateKeyPath); |
| 72 | + expect(fsStub.readFileSync).toHaveBeenCalledWith(privateKeyPath); |
| 73 | + expect(childProcessStub.execSync).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should throw error when existing key cannot be read', () => { |
| 77 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 78 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 79 | + |
| 80 | + fsStub.existsSync.mockReturnValue(true); |
| 81 | + fsStub.readFileSync.mockImplementation(() => { |
| 82 | + throw new Error('Permission denied'); |
| 83 | + }); |
| 84 | + |
| 85 | + expect(() => { |
| 86 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 87 | + }).toThrow('Failed to read existing SSH host key'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should throw error for invalid private key path with unsafe characters', () => { |
| 91 | + const privateKeyPath = '/path/to/key;rm -rf /'; |
| 92 | + const publicKeyPath = '/path/to/key.pub'; |
| 93 | + |
| 94 | + expect(() => { |
| 95 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 96 | + }).toThrow('Invalid SSH host key path'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw error for invalid public key path with unsafe characters', () => { |
| 100 | + const privateKeyPath = '/path/to/key'; |
| 101 | + const publicKeyPath = '/path/to/key.pub && echo hacked'; |
| 102 | + |
| 103 | + expect(() => { |
| 104 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 105 | + }).toThrow('Invalid SSH host key path'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should generate new key when it does not exist', () => { |
| 109 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 110 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 111 | + const mockKeyData = Buffer.from( |
| 112 | + '-----BEGIN OPENSSH PRIVATE KEY-----\ngenerated\n-----END OPENSSH PRIVATE KEY-----', |
| 113 | + ); |
| 114 | + |
| 115 | + fsStub.existsSync |
| 116 | + .mockReturnValueOnce(false) // Check if private key exists |
| 117 | + .mockReturnValueOnce(false) // Check if directory exists |
| 118 | + .mockReturnValueOnce(true); // Verify key was created |
| 119 | + |
| 120 | + fsStub.readFileSync.mockReturnValue(mockKeyData); |
| 121 | + childProcessStub.execSync.mockReturnValue(''); |
| 122 | + |
| 123 | + const result = ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 124 | + |
| 125 | + expect(result).toEqual(mockKeyData); |
| 126 | + expect(fsStub.mkdirSync).toHaveBeenCalledWith('/path/to', { recursive: true }); |
| 127 | + expect(childProcessStub.execSync).toHaveBeenCalledWith( |
| 128 | + `ssh-keygen -t ed25519 -f "${privateKeyPath}" -N "" -C "git-proxy-host-key"`, |
| 129 | + { |
| 130 | + stdio: 'pipe', |
| 131 | + timeout: 10000, |
| 132 | + }, |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should not create directory if it already exists when generating key', () => { |
| 137 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 138 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 139 | + const mockKeyData = Buffer.from( |
| 140 | + '-----BEGIN OPENSSH PRIVATE KEY-----\ngenerated\n-----END OPENSSH PRIVATE KEY-----', |
| 141 | + ); |
| 142 | + |
| 143 | + fsStub.existsSync |
| 144 | + .mockReturnValueOnce(false) // Check if private key exists |
| 145 | + .mockReturnValueOnce(true) // Directory already exists |
| 146 | + .mockReturnValueOnce(true); // Verify key was created |
| 147 | + |
| 148 | + fsStub.readFileSync.mockReturnValue(mockKeyData); |
| 149 | + childProcessStub.execSync.mockReturnValue(''); |
| 150 | + |
| 151 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 152 | + |
| 153 | + expect(fsStub.mkdirSync).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should throw error when key generation fails', () => { |
| 157 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 158 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 159 | + |
| 160 | + fsStub.existsSync.mockReturnValueOnce(false).mockReturnValueOnce(false); |
| 161 | + |
| 162 | + childProcessStub.execSync.mockImplementation(() => { |
| 163 | + throw new Error('ssh-keygen not found'); |
| 164 | + }); |
| 165 | + |
| 166 | + expect(() => { |
| 167 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 168 | + }).toThrow('Failed to generate SSH host key: ssh-keygen not found'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should throw error when generated key file is not found after generation', () => { |
| 172 | + const privateKeyPath = '/path/to/ssh_host_key'; |
| 173 | + const publicKeyPath = '/path/to/ssh_host_key.pub'; |
| 174 | + |
| 175 | + fsStub.existsSync |
| 176 | + .mockReturnValueOnce(false) // Check if private key exists |
| 177 | + .mockReturnValueOnce(false) // Check if directory exists |
| 178 | + .mockReturnValueOnce(false); // Verify key was created - FAIL |
| 179 | + |
| 180 | + childProcessStub.execSync.mockReturnValue(''); |
| 181 | + |
| 182 | + expect(() => { |
| 183 | + ensureHostKey({ privateKeyPath, publicKeyPath }); |
| 184 | + }).toThrow('Key generation appeared to succeed but private key file not found'); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('validateHostKeyExists', () => { |
| 189 | + it('should return true when key exists and is readable', () => { |
| 190 | + fsStub.accessSync.mockImplementation(() => { |
| 191 | + // No error thrown means success |
| 192 | + }); |
| 193 | + |
| 194 | + const result = validateHostKeyExists('/path/to/key'); |
| 195 | + |
| 196 | + expect(result).toBe(true); |
| 197 | + expect(fsStub.accessSync).toHaveBeenCalledWith('/path/to/key', 4); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should return false when key does not exist', () => { |
| 201 | + fsStub.accessSync.mockImplementation(() => { |
| 202 | + throw new Error('ENOENT: no such file or directory'); |
| 203 | + }); |
| 204 | + |
| 205 | + const result = validateHostKeyExists('/path/to/key'); |
| 206 | + |
| 207 | + expect(result).toBe(false); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should return false when key is not readable', () => { |
| 211 | + fsStub.accessSync.mockImplementation(() => { |
| 212 | + throw new Error('EACCES: permission denied'); |
| 213 | + }); |
| 214 | + |
| 215 | + const result = validateHostKeyExists('/path/to/key'); |
| 216 | + |
| 217 | + expect(result).toBe(false); |
| 218 | + }); |
| 219 | + }); |
| 220 | +}); |
0 commit comments