mirror of
https://github.com/stablyai/orca.git
synced 2026-10-22 00:03:47 +00:00
* feat(ssh): add SSH config host picker for add-host form Users can now click 'Fill from ~/.ssh/config…' to browse available SSH config hosts in a picker, select one, and have the form automatically prefill with resolved connection details (hostname, port, username, auth). Previously, an 'import' button provided bulk sync on this form—confusing and unhelpful when everything was already synced. That action is now available as a secondary 'Add all' option in the picker. * fix(ssh): import filter preservation and label fallback - Reuse search loader on import completion to preserve active filter inside generation guard - Fall back to hostname when manual host has no label, not empty string - Make alias duplicate detection case-insensitive to match config picker behavior - Validate host availability when restoring project group selection - Add aria-selected attribute to picker options for accessibility * fix(ssh): harden config picker import, alias folding, and host targeting Review findings on the ~/.ssh/config picker + bulk add: - Guard config-host resolution with a generation counter so a late resolve cannot overwrite a later pick or a form the user backed out of; freeze the other rows while a pick resolves. - Stop "Add all N" from re-adopting deleted hosts — it now imports without reAdopt, matching the new-host count it advertises. Settings → Import keeps the explicit re-adopt path. - Fold SSH aliases through a shared normalizeSshConfigAlias for import ownership, delete tombstones, reclaim, picker search, and the save-time duplicate check, which now occupies configHost *and* label like the picker. - Persist GSSAPIAuthentication only when a parsed Host entry asks for it, not when `ssh -G` merely echoes the /etc/ssh system default. - Fail closed with unavailable/setup-not-found when an explicit projectHostSetupId names a non-actionable host instead of silently creating the workspace on a sibling host. - Cache the parsed config for the picker session (refresh on open/retry) so filter keystrokes no longer reparse and Include-expand the file, keep the filter usable during loads, add a Retry on load errors, explain an empty Identity file after a config fill, and drop the always-false aria-selected. * refactor(ssh): centralize host result limit and extract folder group val Move SSH_CONFIG_HOST_RESULT_LIMIT to shared types so the renderer's limit message cannot drift from the host's query limit. Extract findActionableFolderProjectGroup to avoid repeating the folder-host-availability check across the composer hook. * fix(ssh): pass -F to ssh -G when HOME differs from passwd home In E2E tests and sandboxes, isolated HOME can differ from the system passwd home. OpenSSH resolves the default config via getpwuid (passwd), while Node's loadUserSshConfig uses os.homedir() (HOME-aware). Pass -F to explicitly specify the config path when they diverge, so ssh -G and the picker resolve the same file. * fix(ssh): verify config host exists before resolving with ssh -G When a user edits ~/.ssh/config and removes a host, the import picker should not fall back to ssh -G's echoed response (which treats any alias as valid). Check the reloaded config file before resolving. - Force reload config on each resolve to catch user edits post-open - Reject aliases not in the current config before calling ssh -G - Add test for deleted alias edge case - Fix workspace-target fallback to honor explicit host selection * fix(ssh): let tombstoned aliases be re-picked in the config picker Allow users to reclaim a deleted SSH host by re-picking it from ~/.ssh/config. Tombstoned aliases now appear in the picker with a "Removed from Orca" badge and remain pickable, but don't count toward "Add all" operations — ensuring passive import never resurrects a deleted alias while still giving the user a recovery path.
204 lines
6.3 KiB
TypeScript
204 lines
6.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
utils,
|
|
type AnyAuthMethod,
|
|
type AuthenticationType,
|
|
type AuthHandlerMiddleware,
|
|
type ConnectConfig,
|
|
type ParsedKey
|
|
} from 'ssh2'
|
|
|
|
vi.mock('os', () => ({
|
|
homedir: () => '/home/testuser',
|
|
tmpdir: () => '/tmp'
|
|
}))
|
|
|
|
const mockExistsSync = vi.fn().mockReturnValue(false)
|
|
const mockReadFileSync = vi.fn()
|
|
|
|
vi.mock('fs', () => ({
|
|
existsSync: (...args: unknown[]) => mockExistsSync(...args),
|
|
readFileSync: (...args: unknown[]) => mockReadFileSync(...args)
|
|
}))
|
|
|
|
import { buildConnectConfig } from './ssh-connection-utils'
|
|
import { getPassphrasePrivateKeyPath } from './ssh-private-key-authentication'
|
|
import { buildSshArgs } from './system-ssh-args'
|
|
import type { SshTarget } from '../../shared/ssh-types'
|
|
import type { SshResolvedConfig } from './ssh-config-parser'
|
|
|
|
function makeTarget(overrides: Partial<SshTarget> = {}): SshTarget {
|
|
return {
|
|
id: 'target-1',
|
|
label: 'workbox',
|
|
source: 'ssh-config',
|
|
configHost: 'workbox',
|
|
host: 'stale.example.com',
|
|
port: 22,
|
|
username: 'stale-user',
|
|
identityFile: '/keys/stale-imported',
|
|
...overrides
|
|
}
|
|
}
|
|
|
|
function makeResolved(overrides: Partial<SshResolvedConfig> = {}): SshResolvedConfig {
|
|
return {
|
|
hostname: 'current.example.com',
|
|
port: 2222,
|
|
user: 'current-user',
|
|
identityFile: ['/keys/unauthorized-first', '/keys/authorized-second'],
|
|
identitiesOnly: true,
|
|
forwardAgent: false,
|
|
proxyUseFdpass: false,
|
|
controlMaster: 'no',
|
|
controlPersist: 'no',
|
|
...overrides
|
|
}
|
|
}
|
|
|
|
function nextAuth(
|
|
config: ConnectConfig,
|
|
firstAttempt: boolean
|
|
): AuthenticationType | AnyAuthMethod | false {
|
|
let result: AuthenticationType | AnyAuthMethod | false | undefined
|
|
const handler = config.authHandler as AuthHandlerMiddleware
|
|
handler(
|
|
(firstAttempt ? null : ['publickey']) as unknown as AuthenticationType[],
|
|
false,
|
|
(attempt) => {
|
|
result = attempt
|
|
}
|
|
)
|
|
return result ?? false
|
|
}
|
|
|
|
describe('ordered SSH private-key authentication', () => {
|
|
beforeEach(() => {
|
|
vi.stubEnv('SSH_AUTH_SOCK', '')
|
|
mockExistsSync.mockReset()
|
|
mockExistsSync.mockReturnValue(false)
|
|
mockReadFileSync.mockReset()
|
|
mockReadFileSync.mockImplementation((path: unknown) => Buffer.from(String(path)))
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
it('offers every fresh ssh -G IdentityFile in order and ignores the imported snapshot', () => {
|
|
const config = buildConnectConfig(makeTarget(), makeResolved(), {
|
|
includeAgent: false,
|
|
includePrivateKey: true
|
|
})
|
|
|
|
expect(nextAuth(config, true)).toMatchObject({ type: 'none' })
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'publickey',
|
|
key: Buffer.from('/keys/unauthorized-first')
|
|
})
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'publickey',
|
|
key: Buffer.from('/keys/authorized-second')
|
|
})
|
|
expect(nextAuth(config, false)).toBe(false)
|
|
expect(mockReadFileSync).not.toHaveBeenCalledWith('/keys/stale-imported')
|
|
})
|
|
|
|
it('keeps explicit manual keys and unresolved imported keys as singular overrides', () => {
|
|
const manual = buildConnectConfig(
|
|
makeTarget({
|
|
source: 'manual',
|
|
configHost: 'manual.example.com',
|
|
host: 'manual.example.com',
|
|
identityFile: '/keys/manual'
|
|
}),
|
|
makeResolved(),
|
|
{ includeAgent: false, includePrivateKey: true }
|
|
)
|
|
const unresolvedImport = buildConnectConfig(makeTarget(), null, {
|
|
includeAgent: false,
|
|
includePrivateKey: true
|
|
})
|
|
|
|
expect(manual.privateKey).toEqual(Buffer.from('/keys/manual'))
|
|
expect(manual.authHandler).toBeUndefined()
|
|
expect(unresolvedImport.privateKey).toEqual(Buffer.from('/keys/stale-imported'))
|
|
expect(unresolvedImport.authHandler).toBeUndefined()
|
|
})
|
|
|
|
it('offers every resolved key for a manually owned config-picker target', () => {
|
|
const config = buildConnectConfig(
|
|
makeTarget({
|
|
source: 'manual',
|
|
configHost: 'prod',
|
|
host: 'prod.internal',
|
|
identityFile: undefined
|
|
}),
|
|
makeResolved(),
|
|
{ includeAgent: false, includePrivateKey: true }
|
|
)
|
|
|
|
expect(nextAuth(config, true)).toMatchObject({ type: 'none' })
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'publickey',
|
|
key: Buffer.from('/keys/unauthorized-first')
|
|
})
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'publickey',
|
|
key: Buffer.from('/keys/authorized-second')
|
|
})
|
|
})
|
|
|
|
it('resets ordered authentication for credential retries without extra key reads', () => {
|
|
const config = buildConnectConfig(makeTarget(), makeResolved(), {
|
|
includeAgent: false,
|
|
includePrivateKey: true
|
|
})
|
|
const readsAfterResolution = mockReadFileSync.mock.calls.length
|
|
|
|
expect(nextAuth(config, true)).toMatchObject({ type: 'none' })
|
|
config.password = 'retry-password'
|
|
expect(nextAuth(config, true)).toMatchObject({ type: 'none' })
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'password',
|
|
password: 'retry-password'
|
|
})
|
|
expect(nextAuth(config, false)).toMatchObject({
|
|
type: 'publickey',
|
|
key: Buffer.from('/keys/unauthorized-first')
|
|
})
|
|
expect(mockReadFileSync).toHaveBeenCalledTimes(readsAfterResolution)
|
|
})
|
|
|
|
it('keeps encrypted-key prompts tied to the matching fresh identity path', () => {
|
|
vi.spyOn(utils, 'parseKey').mockImplementation((key) => {
|
|
if (
|
|
Buffer.from(key as Buffer)
|
|
.toString()
|
|
.includes('encrypted-second')
|
|
) {
|
|
return new Error('Encrypted private OpenSSH key detected, but no passphrase given')
|
|
}
|
|
return { isPrivateKey: () => true } as ParsedKey
|
|
})
|
|
const config = buildConnectConfig(
|
|
makeTarget(),
|
|
makeResolved({ identityFile: ['/keys/first', '/keys/encrypted-second'] }),
|
|
{ includeAgent: false, includePrivateKey: true }
|
|
)
|
|
|
|
expect(getPassphrasePrivateKeyPath(config)).toBe('/keys/encrypted-second')
|
|
})
|
|
|
|
it('leaves config-host key authority to system OpenSSH', () => {
|
|
const args = buildSshArgs(makeTarget(), { resolvedConfig: makeResolved() })
|
|
|
|
expect(args.at(-1)).toBe('workbox')
|
|
expect(args).not.toContain('-i')
|
|
expect(args).not.toContain('/keys/stale-imported')
|
|
expect(args).not.toContain('/keys/unauthorized-first')
|
|
expect(args).not.toContain('/keys/authorized-second')
|
|
})
|
|
})
|