/** * Export Key Panel - Export SSH key to remote host */ import { ChevronRight, Info } from 'lucide-react'; import React, { useState } from 'react'; import { cn } from '../../lib/utils'; import { Host, SSHKey } from '../../types'; import { Button } from '../ui/button'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '../ui/collapsible'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; import { Textarea } from '../ui/textarea'; import { toast } from '../ui/toast'; import { getKeyIcon, getKeyTypeDisplay, isMacOS } from './utils'; interface ExportKeyPanelProps { keyItem: SSHKey; _hosts: Host[]; // Reserved for future inline host list/validation keys: SSHKey[]; exportHost: Host | null; _setExportHost: (host: Host | null) => void; // Host selection handled by onShowHostSelector callback onShowHostSelector: () => void; onSaveHost?: (host: Host) => void; onClose: () => void; } const DEFAULT_EXPORT_SCRIPT = `DIR="$HOME/$1" FILE="$DIR/$2" if [ ! -d "$DIR" ]; then mkdir -p "$DIR" chmod 700 "$DIR" fi if [ ! -f "$FILE" ]; then touch "$FILE" chmod 600 "$FILE" fi echo $3 >> "$FILE"`; export const ExportKeyPanel: React.FC = ({ keyItem, _hosts, // Reserved for future inline host list/validation keys, exportHost, _setExportHost, // Host selection handled by onShowHostSelector callback onShowHostSelector, onSaveHost, onClose, }) => { const [exportLocation, setExportLocation] = useState('.ssh'); const [exportFilename, setExportFilename] = useState('authorized_keys'); const [exportAdvancedOpen, setExportAdvancedOpen] = useState(false); const [exportScript, setExportScript] = useState(DEFAULT_EXPORT_SCRIPT); const [isExporting, setIsExporting] = useState(false); const isMac = isMacOS(); const handleExport = async () => { if (!exportHost || !keyItem.publicKey) return; setIsExporting(true); try { // Check for authentication method if (!exportHost.password && !exportHost.identityFileId) { throw new Error('Host has no saved password or key. Please add password credentials to the host first.'); } // Get private key for authentication if host uses key auth const hostPrivateKey = exportHost.identityFileId ? keys.find(k => k.id === exportHost.identityFileId)?.privateKey : undefined; // Escape the public key for shell const escapedPublicKey = keyItem.publicKey.replace(/'/g, "'\\''"); // Build the command by replacing $1, $2, $3 const scriptWithVars = exportScript .replace(/\$1/g, exportLocation) .replace(/\$2/g, exportFilename) .replace(/\$3/g, `'${escapedPublicKey}'`); const command = scriptWithVars; // Execute via SSH const result = await window.netcatty?.execCommand({ hostname: exportHost.hostname, username: exportHost.username, port: exportHost.port || 22, password: exportHost.password, privateKey: hostPrivateKey, command, timeout: 30000, }); // Check result const exitCode = result?.code; const hasError = result?.stderr?.trim(); if (exitCode === 0 || (exitCode == null && !hasError)) { // Update host to use this key for authentication if (onSaveHost) { const updatedHost: Host = { ...exportHost, identityFileId: keyItem.id, authMethod: 'key', }; onSaveHost(updatedHost); } toast.success(`Public key exported and attached to ${exportHost.label}`, 'Export Successful'); onClose(); } else { const errorMsg = hasError || result?.stdout?.trim() || `Command exited with code ${exitCode}`; toast.error(`Failed to export key: ${errorMsg}`, 'Export Failed'); } } catch (err) { const message = err instanceof Error ? err.message : String(err); toast.error(`Export failed: ${message}`, 'Export Failed'); } finally { setIsExporting(false); } }; return ( <> {/* Key info card */}
{getKeyIcon(keyItem)}

{keyItem.label}

Type {getKeyTypeDisplay(keyItem, isMac)}

{/* Export to field */}
{/* Location field */}
setExportLocation(e.target.value)} placeholder=".ssh" />
{/* Filename field */}
setExportFilename(e.target.value)} placeholder="authorized_keys" />
{/* Info note */}

Key export currently supports only UNIX systems. Use Advanced section to customize the export script.

{/* Advanced collapsible */}