Files
opencode-manager/ocm-cli/test/link.test.ts

507 lines
17 KiB
TypeScript

import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'
import { tmpdir, type NetworkInterfaceInfo } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, it, expect } from 'vitest'
import { z } from 'zod'
import { parseJsonText, type JsonObject, type JsonValue } from '@opencode-manager/shared/utils'
import {
buildOpenCodeBasicAuth,
createOpenCodePairingNonce,
createOpenCodePairingProof,
decryptOpenCodePairingCredentials,
} from '@opencode-manager/shared/opencode'
import {
discoverOpenCodeService,
generatePairingCode,
probeOpenCodeServer,
resolveDefaultBaseUrl,
resolveLanIPv4,
startPairingServer,
} from '../src/link'
import type { PairingServerOptions } from '../src/link'
import { buildOpenCodeServerLink, parseOpenCodeServerLink } from '@opencode-manager/shared/schemas'
const CODE = 'test-pairing-code-0123456789abcdef'
const PairingPayloadSchema = z.object({ iv: z.string(), ciphertext: z.string() })
const CREDENTIALS = { username: 'opencode', password: 'secret-password' }
const LAN_INTERFACES: NodeJS.Dict<NetworkInterfaceInfo[]> = {
lo: [{ address: '127.0.0.1', family: 'IPv4', internal: true, netmask: '255.0.0.0', mac: '00:00:00:00:00:00', cidr: '127.0.0.1/8' }],
eth0: [{ address: '192.168.1.20', family: 'IPv4', internal: false, netmask: '255.255.255.0', mac: '00:11:22:33:44:55', cidr: '192.168.1.20/24' }],
}
const INTERNAL_ONLY_INTERFACES: NodeJS.Dict<NetworkInterfaceInfo[]> = {
lo: [
{ address: '127.0.0.1', family: 'IPv4', internal: true, netmask: '255.0.0.0', mac: '00:00:00:00:00:00', cidr: '127.0.0.1/8' },
{ address: '::1', family: 'IPv6', internal: true, netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', mac: '00:00:00:00:00:00', scopeid: 0, cidr: '::1/128' },
],
}
const PLATFORM_SERVICE_PORT = process.platform === 'win32' ? 45278 : 49374
interface ServiceHome {
home: string
state: string
config: string
}
const tempDirs: string[] = []
const probeServers: ProbeServer[] = []
afterEach(async () => {
for (const directory of tempDirs.splice(0)) rmSync(directory, { recursive: true, force: true })
await Promise.all(probeServers.splice(0).map((server) => server.close()))
})
function createServiceHome(): ServiceHome {
const home = mkdtempSync(join(tmpdir(), 'ocm-service-'))
tempDirs.push(home)
const state = join(home, 'state')
const config = join(home, 'config')
mkdirSync(join(state, 'opencode'), { recursive: true })
mkdirSync(join(config, 'opencode'), { recursive: true })
return { home, state, config }
}
function discover(paths: ServiceHome, interfaces: NodeJS.Dict<NetworkInterfaceInfo[]> = LAN_INTERFACES) {
return discoverOpenCodeService({
env: { XDG_STATE_HOME: paths.state, XDG_CONFIG_HOME: paths.config },
home: paths.home,
interfaces,
})
}
function discoverOnPlatform(
platform: NodeJS.Platform,
paths: ServiceHome,
interfaces: NodeJS.Dict<NetworkInterfaceInfo[]> = LAN_INTERFACES,
) {
return discoverOpenCodeService({
env: { XDG_STATE_HOME: paths.state, XDG_CONFIG_HOME: paths.config },
home: paths.home,
interfaces,
platform,
})
}
function writeServiceFile(file: string, content: JsonValue): void {
const raw = z.string().safeParse(content)
writeFileSync(file, raw.success ? raw.data : JSON.stringify(content))
}
function writeRegistration(paths: ServiceHome, content: JsonValue): void {
writeServiceFile(join(paths.state, 'opencode', 'service.json'), content)
}
function writeServiceConfig(paths: ServiceHome, content: JsonValue): void {
writeServiceFile(join(paths.config, 'opencode', 'service.json'), content)
}
function startPairing(overrides: Partial<PairingServerOptions> = {}) {
return startPairingServer({
code: CODE,
credentials: CREDENTIALS,
ttlMs: 5000,
maxAttempts: 3,
port: 0,
hostname: '127.0.0.1',
...overrides,
})
}
function redeem(url: string, body: JsonObject, init: RequestInit = {}): Promise<Response> {
return fetch(url, { method: 'POST', body: JSON.stringify(body), ...init })
}
async function createChallenge(code = CODE): Promise<{ nonce: string; proof: string }> {
const nonce = createOpenCodePairingNonce()
return { nonce, proof: await createOpenCodePairingProof(code, nonce) }
}
interface ProbeServer {
baseUrl: string
requests: Array<{ url: string | undefined; authorization: string | undefined }>
close: () => Promise<void>
}
async function startProbeServer(
respond: (request: IncomingMessage, response: ServerResponse) => void,
): Promise<ProbeServer> {
const requests: ProbeServer['requests'] = []
const server = createServer((request, response) => {
requests.push({ url: request.url, authorization: request.headers.authorization })
respond(request, response)
})
await new Promise<void>((resolve) => {
server.listen(0, '127.0.0.1', resolve)
})
const address = server.address()
const bound = z.object({ port: z.number() }).safeParse(address)
const port = bound.success ? bound.data.port : 0
const handle: ProbeServer = {
baseUrl: `http://127.0.0.1:${port}`,
requests,
close: () => new Promise<void>((resolve) => {
server.closeAllConnections()
server.close(() => resolve())
}),
}
probeServers.push(handle)
return handle
}
describe('generatePairingCode', () => {
it('returns 32 random bytes as base64url', () => {
const first = generatePairingCode()
const second = generatePairingCode()
expect(first).toMatch(/^[A-Za-z0-9_-]{43}$/)
expect(second).toMatch(/^[A-Za-z0-9_-]{43}$/)
expect(first).not.toBe(second)
})
})
describe('resolveDefaultBaseUrl', () => {
it('uses the first non-internal IPv4 interface on port 4096', () => {
expect(resolveDefaultBaseUrl({
lo: [{ address: '127.0.0.1', family: 'IPv4', internal: true, netmask: '255.0.0.0', mac: '00:00:00:00:00:00', cidr: '127.0.0.1/8' }],
eth0: [{ address: '192.168.1.20', family: 'IPv4', internal: false, netmask: '255.255.255.0', mac: '00:11:22:33:44:55', cidr: '192.168.1.20/24' }],
})).toBe('http://192.168.1.20:4096')
})
it('falls back to loopback when only internal interfaces exist', () => {
expect(resolveDefaultBaseUrl({})).toBe('http://127.0.0.1:4096')
})
})
describe('resolveLanIPv4', () => {
it('returns null when no external IPv4 interface exists', () => {
expect(resolveLanIPv4(INTERNAL_ONLY_INTERFACES)).toBeNull()
expect(resolveLanIPv4({})).toBeNull()
})
})
describe('discoverOpenCodeService', () => {
it('substitutes the LAN address for a wildcard registration host and keeps the port', () => {
const paths = createServiceHome()
writeRegistration(paths, {
id: 'registration-id',
version: '2.0.32',
url: 'http://0.0.0.0:49374',
pid: 4242,
password: 'service-secret',
})
expect(discover(paths)).toEqual({
baseUrl: 'http://192.168.1.20:49374',
password: 'service-secret',
source: 'registration',
loopbackOnly: false,
})
})
it('falls back to the service config with the default hostname and port', () => {
const paths = createServiceHome()
writeServiceConfig(paths, { password: 'config-secret' })
expect(discover(paths)).toEqual({
baseUrl: `http://127.0.0.1:${PLATFORM_SERVICE_PORT}`,
password: 'config-secret',
source: 'config',
loopbackOnly: true,
})
})
it('uses the configured hostname and port when present', () => {
const paths = createServiceHome()
writeServiceConfig(paths, { hostname: '0.0.0.0', port: 5000 })
expect(discover(paths)).toEqual({
baseUrl: 'http://192.168.1.20:5000',
password: null,
source: 'config',
loopbackOnly: false,
})
})
it('brackets an IPv6 wildcard config hostname and substitutes the LAN address', () => {
const paths = createServiceHome()
writeServiceConfig(paths, { hostname: '::', port: 5000 })
expect(discover(paths)).toEqual({
baseUrl: 'http://192.168.1.20:5000',
password: null,
source: 'config',
loopbackOnly: false,
})
})
it('brackets an IPv6 loopback config hostname', () => {
const paths = createServiceHome()
writeServiceConfig(paths, { hostname: '::1' })
expect(discover(paths)).toEqual({
baseUrl: `http://[::1]:${PLATFORM_SERVICE_PORT}`,
password: null,
source: 'config',
loopbackOnly: true,
})
})
it('marks loopback registrations as loopback only', () => {
const cases = [
{ host: '127.0.0.1', url: 'http://127.0.0.1:49374' },
{ host: 'localhost', url: 'http://localhost:49374' },
{ host: '::1', url: 'http://[::1]:49374' },
]
for (const { host, url } of cases) {
const paths = createServiceHome()
writeRegistration(paths, { url, pid: 4242 })
expect(discover(paths), host).toEqual({
baseUrl: url,
password: null,
source: 'registration',
loopbackOnly: true,
})
}
})
it('returns null when neither the registration nor the config exists', () => {
expect(discover(createServiceHome())).toBeNull()
})
it('ignores a malformed registration and falls back to the config', () => {
const paths = createServiceHome()
writeRegistration(paths, '{ not json')
writeServiceConfig(paths, { hostname: '192.168.1.50', port: 5000, password: 'config-secret' })
expect(discover(paths)).toEqual({
baseUrl: 'http://192.168.1.50:5000',
password: 'config-secret',
source: 'config',
loopbackOnly: false,
})
})
it('marks a wildcard registration as loopback when no LAN address exists', () => {
const paths = createServiceHome()
writeRegistration(paths, { url: 'http://0.0.0.0:49374', password: 'service-secret' })
expect(discover(paths, INTERNAL_ONLY_INTERFACES)).toEqual({
baseUrl: 'http://127.0.0.1:49374',
password: 'service-secret',
source: 'registration',
loopbackOnly: true,
})
})
it('uses the config password when the registration URL is unusable', () => {
const paths = createServiceHome()
writeRegistration(paths, { url: 'not-a-url', password: 'stale-secret' })
writeServiceConfig(paths, { hostname: '192.168.1.50', port: 5000, password: 'config-secret' })
expect(discover(paths)).toEqual({
baseUrl: 'http://192.168.1.50:5000',
password: 'config-secret',
source: 'config',
loopbackOnly: false,
})
})
it.each([
['linux', 49374],
['win32', 45278],
] as const)('uses the %s default service port when the config omits one', (platform, port) => {
const paths = createServiceHome()
writeServiceConfig(paths, { password: 'config-secret' })
expect(discoverOnPlatform(platform, paths)).toEqual({
baseUrl: `http://127.0.0.1:${port}`,
password: 'config-secret',
source: 'config',
loopbackOnly: true,
})
})
})
describe('probeOpenCodeServer', () => {
it('reports ok with the server version from an authenticated info request', async () => {
const server = await startProbeServer((_request, response) => {
response.writeHead(200, { 'content-type': 'application/json' })
response.end(JSON.stringify({ version: '2.0.32' }))
})
await expect(probeOpenCodeServer(`${server.baseUrl}/`, CREDENTIALS, 1000)).resolves.toEqual({
status: 'ok',
version: '2.0.32',
})
expect(server.requests).toEqual([
{
url: '/api/info',
authorization: buildOpenCodeBasicAuth(CREDENTIALS.password, CREDENTIALS.username),
},
])
})
it.each([
['a JSON error body', 'application/json', JSON.stringify({ _tag: 'UnauthorizedError', message: 'unauthorized' })],
['an empty body', 'text/plain', ''],
['a plain text body', 'text/plain', 'unauthorized'],
])('reports unauthorized when the server rejects the credentials with %s', async (_label, contentType, body) => {
const server = await startProbeServer((_request, response) => {
response.writeHead(401, { 'content-type': contentType })
response.end(body)
})
await expect(probeOpenCodeServer(server.baseUrl, CREDENTIALS, 1000)).resolves.toEqual({
status: 'unauthorized',
})
})
it.each([
['a JSON body', 'application/json', JSON.stringify({ error: 'boom' })],
['a plain text body', 'text/plain', 'boom'],
])('reports unreachable with the HTTP status for an unexpected %s', async (_label, contentType, body) => {
const server = await startProbeServer((_request, response) => {
response.writeHead(500, { 'content-type': contentType })
response.end(body)
})
await expect(probeOpenCodeServer(server.baseUrl, CREDENTIALS, 1000)).resolves.toEqual({
status: 'unreachable',
message: 'HTTP 500',
})
})
it('reports unreachable when the server refuses the connection', async () => {
const server = await startProbeServer(() => {})
const baseUrl = server.baseUrl
await server.close()
const probe = await probeOpenCodeServer(baseUrl, CREDENTIALS, 1000)
expect(probe.status).toBe('unreachable')
expect(probe).toMatchObject({ message: expect.stringContaining('Transport') })
})
})
describe('startPairingServer', () => {
it('redeems a valid proof and returns credentials that decrypt with the code and nonce', async () => {
const pairing = await startPairing()
const challenge = await createChallenge()
const response = await redeem(pairing.url, challenge)
const raw = await response.text()
expect(response.status).toBe(200)
expect(raw).not.toContain(CREDENTIALS.password)
expect(raw).not.toContain(CODE)
const payload = PairingPayloadSchema.parse(parseJsonText(raw))
await expect(decryptOpenCodePairingCredentials({
code: CODE,
nonce: challenge.nonce,
iv: payload.iv,
ciphertext: payload.ciphertext,
})).resolves.toEqual(CREDENTIALS)
await expect(pairing.done).resolves.toBe('linked')
await pairing.close()
})
it('rejects a wrong proof with 401 and rejects after three failures', async () => {
const pairing = await startPairing({ maxAttempts: 3 })
for (let attempt = 0; attempt < 3; attempt++) {
const response = await redeem(pairing.url, await createChallenge(`${CODE}-wrong`))
expect(response.status).toBe(401)
await expect(response.json()).resolves.toEqual({ error: 'invalid pairing proof' })
}
await expect(pairing.done).resolves.toBe('rejected')
await pairing.close()
})
it('rejects a malformed body with 400 and counts it as a failure', async () => {
const pairing = await startPairing({ maxAttempts: 1 })
const response = await redeem(pairing.url, { nonce: 'not-a-nonce', proof: 'not-a-proof' })
expect(response.status).toBe(400)
await expect(response.json()).resolves.toEqual({ error: 'invalid pairing request' })
await expect(pairing.done).resolves.toBe('rejected')
await pairing.close()
})
it('treats a missing challenge as a failed attempt', async () => {
const pairing = await startPairing({ maxAttempts: 1 })
const response = await redeem(pairing.url, { notA: 'challenge' })
expect(response.status).toBe(400)
await expect(pairing.done).resolves.toBe('rejected')
await pairing.close()
})
it('resolves expired after the ttl and stops listening', async () => {
const pairing = await startPairing({ ttlMs: 50 })
await expect(pairing.done).resolves.toBe('expired')
await expect(redeem(pairing.url, await createChallenge())).rejects.toThrow()
await pairing.close()
})
it('answers 404 for non-redeem paths and methods', async () => {
const pairing = await startPairing()
const otherPath = await fetch(new URL('/status', pairing.url))
expect(otherPath.status).toBe(404)
await expect(otherPath.json()).resolves.toEqual({ error: 'not found' })
const wrongMethod = await fetch(pairing.url)
expect(wrongMethod.status).toBe(404)
await pairing.close()
})
it('produces a link that parses back with the shared parser', async () => {
const pairing = await startPairing()
const link = buildOpenCodeServerLink({
kind: 'opencode',
name: 'build-host',
baseUrl: 'http://192.168.1.20:4096',
pairing: { pairUrl: pairing.url, code: CODE },
})
expect(link).toContain('v=2')
expect(parseOpenCodeServerLink(link)).toEqual({
kind: 'opencode',
name: 'build-host',
baseUrl: 'http://192.168.1.20:4096',
pairing: { pairUrl: `http://127.0.0.1:${pairing.port}/redeem`, code: CODE },
})
await pairing.close()
})
it('encrypts credentials under a key derived from the pairing code', async () => {
const pairing = await startPairing()
const challenge = await createChallenge()
const payload = PairingPayloadSchema.parse(await redeem(pairing.url, challenge).then((response) => response.json()))
await expect(decryptOpenCodePairingCredentials({
code: `${CODE}-other`,
nonce: challenge.nonce,
iv: payload.iv,
ciphertext: payload.ciphertext,
})).rejects.toThrow()
await pairing.close()
})
})