mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat: SSH Authentication Keys v2 (#113)
* feat: add SSH authentication keys v2 for Git operations Add comprehensive SSH key authentication support for Git operations including: - SSH host key verification with user approval flow - Encrypted SSH key storage with passphrase support - SSH connection testing endpoint - Real-time host key change warnings Backend changes: - New SSHHostKeyHandler for managing host key verification via IPC - Add passphrase handler for locked SSH keys - Add SSH key manager utilities (key writing, validation, cleanup) - Add crypto utilities for secure key/secret encryption - Add SSH validation utilities for key format checking - Add SSH routes for host key response handling - Enhance GitAuthService with SSH setup and environment methods - Update GitService to handle SSH authentication for push/fetch/pull - Update OpenCode server to support SSH keys with SSH config generation - Add database migration for trusted_ssh_hosts table - Add settings endpoint for SSH connection testing Frontend changes: - Add SSHHostKeyDialog for host key verification UI - Add PassphraseModal for SSH key passphrase input - Update GitCredentialDialog with SSH key type and testing - Update GitSettings to manage SSH keys - Add SSH API client and hooks (useSSH, usePassphraseHandler) Shared changes: - Add SSH schemas and types Test coverage: - Integration tests for SSH flow - IPC handler tests for host key and passphrase - SSH route tests - SSH utility validation tests - Updated existing tests for SSH-aware changes
This commit is contained in:
1 parent
3d5ee463d7
commit
414f39b809
54 files changed
+4051
-214
No files matched your search
@@ -209,9 +209,9 @@ describe('fetchGitHubUserInfo', () => {
|
||||
describe('findGitHubCredential', () => {
|
||||
it('should find GitHub credential in credentials array', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'gitlab', host: 'https://gitlab.com/', token: 'gitlab-token' },
|
||||
{ name: 'github', host: 'https://github.com/', token: 'github-token' },
|
||||
{ name: 'private', host: 'https://git.example.com/', token: 'private-token' },
|
||||
{ name: 'gitlab', host: 'https://gitlab.com/', type: 'pat', token: 'gitlab-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'github-token' },
|
||||
{ name: 'private', host: 'https://git.example.com/', type: 'pat', token: 'private-token' },
|
||||
]
|
||||
|
||||
const result = findGitHubCredential(credentials)
|
||||
@@ -219,13 +219,14 @@ describe('findGitHubCredential', () => {
|
||||
expect(result).toEqual({
|
||||
name: 'github',
|
||||
host: 'https://github.com/',
|
||||
type: 'pat',
|
||||
token: 'github-token'
|
||||
})
|
||||
})
|
||||
|
||||
it('should not find GitHub credential with www subdomain', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://www.github.com/', token: 'github-token' },
|
||||
{ name: 'github', host: 'https://www.github.com/', type: 'pat', token: 'github-token' },
|
||||
]
|
||||
|
||||
const result = findGitHubCredential(credentials)
|
||||
@@ -241,8 +242,8 @@ describe('findGitHubCredential', () => {
|
||||
|
||||
it('should return null when no GitHub credential exists', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'gitlab', host: 'https://gitlab.com/', token: 'gitlab-token' },
|
||||
{ name: 'private', host: 'https://git.example.com/', token: 'private-token' },
|
||||
{ name: 'gitlab', host: 'https://gitlab.com/', type: 'pat', token: 'gitlab-token' },
|
||||
{ name: 'private', host: 'https://git.example.com/', type: 'pat', token: 'private-token' },
|
||||
]
|
||||
|
||||
const result = findGitHubCredential(credentials)
|
||||
@@ -273,7 +274,7 @@ describe('resolveGitIdentity', () => {
|
||||
it('should prioritize manual name but use GitHub email', async () => {
|
||||
const manual = { name: 'Custom Name', email: undefined }
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://github.com/', token: 'gh-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'gh-token' },
|
||||
]
|
||||
|
||||
const userResponse = {
|
||||
@@ -306,7 +307,7 @@ describe('resolveGitIdentity', () => {
|
||||
it('should use GitHub identity when no manual identity', async () => {
|
||||
const manual = undefined
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://github.com/', token: 'gh-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'gh-token' },
|
||||
]
|
||||
|
||||
const userResponse = {
|
||||
@@ -339,7 +340,7 @@ describe('resolveGitIdentity', () => {
|
||||
it('should use login as name when GitHub name is null', async () => {
|
||||
const manual = undefined
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://github.com/', token: 'gh-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'gh-token' },
|
||||
]
|
||||
|
||||
const userResponse = {
|
||||
@@ -372,7 +373,7 @@ describe('resolveGitIdentity', () => {
|
||||
it('should return partial identity when GitHub fails but manual has one field', async () => {
|
||||
const manual = { name: 'Only Name', email: undefined }
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://github.com/', token: 'invalid-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'invalid-token' },
|
||||
]
|
||||
|
||||
global.fetch = vi.fn() as any;
|
||||
@@ -399,7 +400,7 @@ describe('resolveGitIdentity', () => {
|
||||
it('should return null when both manual fields are empty and GitHub fails', async () => {
|
||||
const manual = { name: undefined, email: undefined }
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'https://github.com/', token: 'invalid-token' },
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'invalid-token' },
|
||||
]
|
||||
|
||||
global.fetch = vi.fn() as any;
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import * as fs from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import { parseSSHHost, normalizeHostPort, parseHostPort, writeTemporarySSHKey, cleanupSSHKey, cleanupAllSSHKeys } from '../../src/utils/ssh-key-manager'
|
||||
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
|
||||
|
||||
describe('SSH Host Parsing', () => {
|
||||
it('should parse git@host:path format', () => {
|
||||
const result = parseSSHHost('git@github.com:user/repo.git')
|
||||
expect(result.user).toBe('git')
|
||||
expect(result.host).toBe('github.com')
|
||||
expect(result.port).toBe('22')
|
||||
})
|
||||
|
||||
it('should parse ssh:// format', () => {
|
||||
const result = parseSSHHost('ssh://git@gitlab.com/user/repo.git')
|
||||
expect(result.user).toBe('git')
|
||||
expect(result.host).toBe('gitlab.com')
|
||||
expect(result.port).toBe('22')
|
||||
})
|
||||
|
||||
it('should parse ssh:// with custom port', () => {
|
||||
const result = parseSSHHost('ssh://git@git.example.com:2222/user/repo.git')
|
||||
expect(result.user).toBe('git')
|
||||
expect(result.host).toBe('git.example.com')
|
||||
expect(result.port).toBe('2222')
|
||||
})
|
||||
|
||||
it('should parse git@host:port:path format', () => {
|
||||
const result = parseSSHHost('git@github.com:22:user/repo.git')
|
||||
expect(result.user).toBe('git')
|
||||
expect(result.host).toBe('github.com')
|
||||
expect(result.port).toBe('22')
|
||||
})
|
||||
|
||||
it('should normalize host:port for non-standard ports', () => {
|
||||
expect(normalizeHostPort('github.com', '2222')).toBe('github.com:2222')
|
||||
expect(normalizeHostPort('github.com', '22')).toBe('github.com')
|
||||
expect(normalizeHostPort('github.com', undefined)).toBe('github.com')
|
||||
})
|
||||
|
||||
it('should parse host:port strings', () => {
|
||||
expect(parseHostPort('github.com:2222')).toEqual({ host: 'github.com', port: '2222' })
|
||||
expect(parseHostPort('github.com')).toEqual({ host: 'github.com', port: '22' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('SSH Key Validation', () => {
|
||||
describe('RSA Keys', () => {
|
||||
const rsaKey = `-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEAzR8u8I5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5
|
||||
-----END RSA PRIVATE KEY-----`
|
||||
|
||||
it('should accept RSA private key', async () => {
|
||||
const keyPath = await writeTemporarySSHKey(rsaKey, 'test-rsa')
|
||||
expect(keyPath).toBeTruthy()
|
||||
|
||||
const stats = await fs.stat(keyPath)
|
||||
const mode = stats.mode & 0o777
|
||||
expect(mode).toBe(0o600)
|
||||
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
|
||||
it('should accept RSA public key', async () => {
|
||||
const rsaPublicKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRHw== test@host'
|
||||
const keyPath = await writeTemporarySSHKey(rsaPublicKey, 'test-rsa-pub')
|
||||
expect(keyPath).toBeTruthy()
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ED25519 Keys', () => {
|
||||
const ed25519Key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
it('should accept ED25519 private key', async () => {
|
||||
const keyPath = await writeTemporarySSHKey(ed25519Key, 'test-ed25519')
|
||||
expect(keyPath).toBeTruthy()
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
|
||||
it('should accept ED25519 public key', async () => {
|
||||
const ed25519PublicKey = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIQQ/test@test.com'
|
||||
const keyPath = await writeTemporarySSHKey(ed25519PublicKey, 'test-ed25519-pub')
|
||||
expect(keyPath).toBeTruthy()
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ECDSA Keys', () => {
|
||||
const ecdsaKey = `-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIP5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5Z5
|
||||
-----END EC PRIVATE KEY-----`
|
||||
|
||||
it('should accept ECDSA private key', async () => {
|
||||
const keyPath = await writeTemporarySSHKey(ecdsaKey, 'test-ecdsa')
|
||||
expect(keyPath).toBeTruthy()
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
|
||||
it('should accept ECDSA public key', async () => {
|
||||
const ecdsaPublicKey = 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEM/test@test.com'
|
||||
const keyPath = await writeTemporarySSHKey(ecdsaPublicKey, 'test-ecdsa-pub')
|
||||
expect(keyPath).toBeTruthy()
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
})
|
||||
|
||||
it('should reject invalid key format', async () => {
|
||||
const invalidKey = 'not a valid ssh key'
|
||||
await expect(writeTemporarySSHKey(invalidKey, 'test-invalid')).rejects.toThrow('Invalid SSH key format')
|
||||
})
|
||||
|
||||
it('should reject empty key', async () => {
|
||||
const emptyKey = ''
|
||||
await expect(writeTemporarySSHKey(emptyKey, 'test-empty')).rejects.toThrow('Invalid SSH key format')
|
||||
})
|
||||
|
||||
it('should set correct permissions on key file', async () => {
|
||||
const validKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRHw== test@host'
|
||||
const keyPath = await writeTemporarySSHKey(validKey, 'test-perms')
|
||||
|
||||
const stats = await fs.stat(keyPath)
|
||||
const mode = stats.mode & 0o777
|
||||
expect(mode).toBe(0o600)
|
||||
|
||||
await cleanupSSHKey(keyPath)
|
||||
})
|
||||
})
|
||||
|
||||
describe('SSH Key Cleanup', () => {
|
||||
it('should clean up individual key', async () => {
|
||||
const key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRHw== test@host'
|
||||
const keyPath = await writeTemporarySSHKey(key, 'test-cleanup')
|
||||
|
||||
await cleanupSSHKey(keyPath)
|
||||
|
||||
await expect(fs.access(keyPath)).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('should clean up all keys', async () => {
|
||||
const key1 = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRHw== test1@host'
|
||||
const key2 = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIQQ/test2@test.com'
|
||||
|
||||
await writeTemporarySSHKey(key1, 'test-cleanup-1')
|
||||
await writeTemporarySSHKey(key2, 'test-cleanup-2')
|
||||
|
||||
await cleanupAllSSHKeys()
|
||||
|
||||
const keysDir = join(getWorkspacePath(), '.ssh-keys')
|
||||
await expect(fs.access(keysDir)).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('should handle cleanup of non-existent file gracefully', async () => {
|
||||
const nonExistentPath = '/tmp/non-existent-ssh-key-12345'
|
||||
|
||||
await expect(cleanupSSHKey(nonExistentPath)).resolves.not.toThrow()
|
||||
})
|
||||
|
||||
it('should handle cleanup of already cleaned up file gracefully', async () => {
|
||||
const key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRHw== test@host'
|
||||
const keyPath = await writeTemporarySSHKey(key, 'test-double-cleanup')
|
||||
|
||||
await cleanupSSHKey(keyPath)
|
||||
await expect(cleanupSSHKey(keyPath)).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,260 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
const mockExecuteCommand = vi.fn()
|
||||
|
||||
vi.mock('@opencode-manager/shared/config/env', () => ({
|
||||
ENV: {
|
||||
AUTH: {
|
||||
SECRET: 'test-secret-for-encryption'
|
||||
},
|
||||
OPENCODE: {
|
||||
PORT: 5551
|
||||
},
|
||||
SERVER: {
|
||||
PORT: 5003
|
||||
}
|
||||
},
|
||||
getReposPath: vi.fn(() => '/tmp/test-repos'),
|
||||
getWorkspacePath: vi.fn(() => '/tmp/test-workspace')
|
||||
}))
|
||||
|
||||
vi.mock('../../src/utils/process', () => ({
|
||||
executeCommand: mockExecuteCommand
|
||||
}))
|
||||
|
||||
vi.mock('fs', () => ({
|
||||
promises: {
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
unlink: vi.fn().mockResolvedValue(undefined)
|
||||
}
|
||||
}))
|
||||
|
||||
import { validateSSHPrivateKey } from '../../src/utils/ssh-validation'
|
||||
|
||||
describe('validateSSHPrivateKey', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockExecuteCommand.mockResolvedValue('ssh-ed25519 AAAAC...')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('valid key formats', () => {
|
||||
const validHeaders = [
|
||||
{ name: 'OPENSSH', header: '-----BEGIN OPENSSH PRIVATE KEY-----' },
|
||||
{ name: 'RSA', header: '-----BEGIN RSA PRIVATE KEY-----' },
|
||||
{ name: 'EC', header: '-----BEGIN EC PRIVATE KEY-----' },
|
||||
{ name: 'DSA', header: '-----BEGIN DSA PRIVATE KEY-----' },
|
||||
{ name: 'ED25519', header: '-----BEGIN ED25519 PRIVATE KEY-----' },
|
||||
{ name: 'PGP', header: '-----BEGIN PGP PRIVATE KEY BLOCK-----' }
|
||||
]
|
||||
|
||||
validHeaders.forEach(({ name, header }) => {
|
||||
it(`should accept valid ${name} key format`, async () => {
|
||||
const key = `${header}\n${'A'.repeat(100)}\n-----END ${name} PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
it('should accept properly formatted OPENSSH key', async () => {
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBmNkCQ0YDZiVJmHqMvK1PqjWxV4aRqK2Pm8RlV4aRqK2Pm8RlV4aRqK2Pm8RlV4
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('passphrase detection', () => {
|
||||
it('should detect key without passphrase', async () => {
|
||||
mockExecuteCommand.mockResolvedValue('ssh-ed25519 AAAAC3...')
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.hasPassphrase).toBe(false)
|
||||
})
|
||||
|
||||
it('should detect key with passphrase via incorrect passphrase error', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('incorrect passphrase supplied to decrypt private key'))
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.hasPassphrase).toBe(true)
|
||||
})
|
||||
|
||||
it('should detect key with passphrase via failed error', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('failed: incorrect passphrase supplied to decrypt private key'))
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.hasPassphrase).toBe(true)
|
||||
})
|
||||
|
||||
it('should detect key with passphrase via Load key error', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('Load key "/tmp/temp-ssh-key-123": bad passphrase'))
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.hasPassphrase).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('invalid key rejection', () => {
|
||||
it('should reject key with invalid format', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('invalid format'))
|
||||
|
||||
const key = `-----BEGIN INVALID KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END INVALID KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toContain('Invalid SSH key')
|
||||
})
|
||||
|
||||
it('should reject corrupted key', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('load key: invalid format'))
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toContain('Invalid SSH key')
|
||||
})
|
||||
})
|
||||
|
||||
describe('empty/null/undefined key handling', () => {
|
||||
it('should reject null key', async () => {
|
||||
const result = await validateSSHPrivateKey(null as unknown as string)
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toBe('SSH key is required')
|
||||
})
|
||||
|
||||
it('should reject undefined key', async () => {
|
||||
const result = await validateSSHPrivateKey(undefined as unknown as string)
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toBe('SSH key is required')
|
||||
})
|
||||
|
||||
it('should reject empty string', async () => {
|
||||
const result = await validateSSHPrivateKey('')
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toBe('SSH key is required')
|
||||
})
|
||||
|
||||
it('should reject whitespace-only string', async () => {
|
||||
const result = await validateSSHPrivateKey(' \n\t ')
|
||||
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.error).toBe('SSH key cannot be empty')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
||||
describe('ssh-keygen command execution', () => {
|
||||
it('should call ssh-keygen with correct arguments', async () => {
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
await validateSSHPrivateKey(key)
|
||||
|
||||
expect(mockExecuteCommand).toHaveBeenCalled()
|
||||
const call = mockExecuteCommand.mock.calls[0]
|
||||
expect(call).toBeDefined()
|
||||
const args = call?.[0] as string[] | undefined
|
||||
expect(args?.[0]).toBe('ssh-keygen')
|
||||
expect(args?.[1]).toBe('-y')
|
||||
expect(args?.[2]).toBe('-P')
|
||||
expect(args?.[3]).toBe('')
|
||||
expect(args?.[4]).toBe('-f')
|
||||
})
|
||||
|
||||
it('should pass silent option to command execution', async () => {
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
await validateSSHPrivateKey(key)
|
||||
|
||||
expect(mockExecuteCommand).toHaveBeenCalled()
|
||||
const call = mockExecuteCommand.mock.calls[0]
|
||||
expect(call).toBeDefined()
|
||||
const options = call?.[1] as { silent?: boolean } | undefined
|
||||
expect(options?.silent).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('file cleanup after validation', () => {
|
||||
it('should complete validation successfully with cleanup', async () => {
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
})
|
||||
|
||||
it('should complete validation with passphrase detection with cleanup', async () => {
|
||||
mockExecuteCommand.mockRejectedValue(new Error('incorrect passphrase'))
|
||||
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.hasPassphrase).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('temp file permission handling', () => {
|
||||
it('should complete validation which includes writing temp file', async () => {
|
||||
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
${'A'.repeat(120)}
|
||||
-----END OPENSSH PRIVATE KEY-----`
|
||||
|
||||
const result = await validateSSHPrivateKey(key)
|
||||
|
||||
expect(result.valid).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user