mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +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
@@ -1,12 +1,138 @@
|
||||
import type { IPCServer } from '../ipc/ipcServer'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { AskpassHandler } from '../ipc/askpassHandler'
|
||||
import { SSHHostKeyHandler } from '../ipc/sshHostKeyHandler'
|
||||
import { writeTemporarySSHKey, buildSSHCommand, buildSSHCommandWithKnownHosts, cleanupSSHKey, parseSSHHost } from '../utils/ssh-key-manager'
|
||||
import { decryptSecret } from '../utils/crypto'
|
||||
import { isSSHUrl, extractHostFromSSHUrl, getSSHCredentialsForHost, type GitCredential } from '../utils/git-auth'
|
||||
import { logger } from '../utils/logger'
|
||||
import { SettingsService } from './settings'
|
||||
|
||||
export class GitAuthService {
|
||||
private askpassHandler: AskpassHandler | null = null
|
||||
public sshHostKeyHandler: SSHHostKeyHandler | null = null
|
||||
private sshKeyPath: string | null = null
|
||||
private sshPassphrase: string | null = null
|
||||
private sshPort: string | null = null
|
||||
|
||||
initialize(ipcServer: IPCServer | undefined, database: Database): void {
|
||||
this.askpassHandler = new AskpassHandler(ipcServer, database)
|
||||
this.sshHostKeyHandler = new SSHHostKeyHandler(database, 120_000)
|
||||
this.sshHostKeyHandler.initialize()
|
||||
|
||||
if (ipcServer) {
|
||||
const handlerPath = 'ssh-host-key'
|
||||
ipcServer.registerHandler(handlerPath, this.sshHostKeyHandler)
|
||||
logger.info(`SSH host key handler registered with IPC server at /${handlerPath}`)
|
||||
}
|
||||
}
|
||||
|
||||
async setupSSHKey(credential: GitCredential): Promise<void> {
|
||||
if (credential.type !== 'ssh' || !credential.sshPrivateKeyEncrypted) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const privateKey = decryptSecret(credential.sshPrivateKeyEncrypted)
|
||||
this.sshKeyPath = await writeTemporarySSHKey(privateKey, credential.name)
|
||||
|
||||
if (credential.passphrase) {
|
||||
this.sshPassphrase = decryptSecret(credential.passphrase)
|
||||
logger.info(`SSH key with passphrase created for ${credential.name}: ${this.sshKeyPath}`)
|
||||
} else {
|
||||
this.sshPassphrase = null
|
||||
logger.info(`SSH key created for ${credential.name}: ${this.sshKeyPath}`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Failed to setup SSH key for ${credential.name}:`, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
setSSHPort(port: string | null): void {
|
||||
this.sshPort = port
|
||||
}
|
||||
|
||||
getSSHEnvironment(): Record<string, string> {
|
||||
const knownHostsPath = this.sshHostKeyHandler?.getKnownHostsPath()
|
||||
const port = this.sshPort || undefined
|
||||
|
||||
if (!this.sshKeyPath) {
|
||||
if (knownHostsPath) {
|
||||
const sshCommand = buildSSHCommandWithKnownHosts(knownHostsPath, port)
|
||||
logger.info(`SSH environment: Using known_hosts=${knownHostsPath}, SSH_COMMAND=${sshCommand}`)
|
||||
return {
|
||||
GIT_SSH_COMMAND: sshCommand,
|
||||
...(this.sshHostKeyHandler?.getEnv() || {}),
|
||||
}
|
||||
}
|
||||
logger.info(`SSH environment: No SSH key and no known_hosts path, returning host key handler env only`)
|
||||
return {
|
||||
...(this.sshHostKeyHandler?.getEnv() || {}),
|
||||
}
|
||||
}
|
||||
|
||||
const sshResult = buildSSHCommand(this.sshKeyPath, this.sshPassphrase || undefined, knownHostsPath, port)
|
||||
logger.info(`SSH environment: Using SSH key=${this.sshKeyPath}, known_hosts=${knownHostsPath}, SSH_COMMAND=${sshResult.command}`)
|
||||
return {
|
||||
GIT_SSH_COMMAND: sshResult.command,
|
||||
...(sshResult.env || {}),
|
||||
...(this.sshHostKeyHandler?.getEnv() || {}),
|
||||
}
|
||||
}
|
||||
|
||||
async setupSSHForRepoUrl(repoUrl: string | undefined, database: Database): Promise<boolean> {
|
||||
if (!repoUrl || !isSSHUrl(repoUrl)) {
|
||||
return false
|
||||
}
|
||||
|
||||
const sshHost = extractHostFromSSHUrl(repoUrl)
|
||||
if (!sshHost) {
|
||||
logger.warn(`Could not extract SSH host from URL: ${repoUrl}`)
|
||||
return false
|
||||
}
|
||||
|
||||
const { port } = parseSSHHost(repoUrl)
|
||||
this.setSSHPort(port && port !== '22' ? port : null)
|
||||
|
||||
const settingsService = new SettingsService(database)
|
||||
const settings = settingsService.getSettings('default')
|
||||
const gitCredentials = (settings.preferences.gitCredentials || []) as GitCredential[]
|
||||
const sshCredentials = getSSHCredentialsForHost(gitCredentials, sshHost)
|
||||
|
||||
if (sshCredentials.length > 0 && sshCredentials[0]) {
|
||||
try {
|
||||
await this.setupSSHKey(sshCredentials[0])
|
||||
} catch (error) {
|
||||
logger.error(`Failed to setup SSH key for ${sshHost}:`, error)
|
||||
throw new Error(`Failed to setup SSH authentication: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
const verified = await this.verifyHostKeyBeforeOperation(repoUrl)
|
||||
if (!verified) {
|
||||
await this.cleanupSSHKey()
|
||||
throw new Error('SSH host key verification failed or was rejected by user')
|
||||
}
|
||||
|
||||
return sshCredentials.length > 0
|
||||
}
|
||||
|
||||
async verifyHostKeyBeforeOperation(repoUrl: string): Promise<boolean> {
|
||||
if (!this.sshHostKeyHandler) {
|
||||
return true
|
||||
}
|
||||
return this.sshHostKeyHandler.verifyHostKeyBeforeOperation(repoUrl)
|
||||
}
|
||||
|
||||
async cleanupSSHKey(): Promise<void> {
|
||||
if (this.sshKeyPath) {
|
||||
await cleanupSSHKey(this.sshKeyPath)
|
||||
this.sshKeyPath = null
|
||||
this.sshPassphrase = null
|
||||
this.sshPort = null
|
||||
logger.info('SSH key cleaned up')
|
||||
}
|
||||
}
|
||||
|
||||
getGitEnvironment(silent: boolean = false): Record<string, string> {
|
||||
|
||||
Reference in new issue
Block a user