|
| 1 | +import { afterEach, expect, test, vi } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import net from 'node:net'; |
| 4 | +import { readProcessStartTime } from '@agent-device/host-kit/process'; |
| 5 | +import { readVersion } from '@agent-device/host-kit/version'; |
| 6 | +import { sendToDaemon } from '../daemon-client.ts'; |
| 7 | +import { resolveDaemonPaths } from '../../daemon-resolution.ts'; |
| 8 | +import { |
| 9 | + currentDaemonCodeSignature, |
| 10 | + startHttpDaemonFixture, |
| 11 | +} from '../../__tests__/test-utils/daemon-http-fixture.ts'; |
| 12 | +import { |
| 13 | + closeLoopbackServer, |
| 14 | + listenOnLoopback, |
| 15 | + supportsLoopbackBind, |
| 16 | +} from '../../__tests__/test-utils/loopback.ts'; |
| 17 | +import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; |
| 18 | +import { providerCredentialFingerprint } from '../../provider-credential-fingerprint.ts'; |
| 19 | +import { LIMRUN_CREDENTIAL_VARIABLES } from '../../provider-limrun-credentials.ts'; |
| 20 | + |
| 21 | +const API_KEY = 'lim-secret-key'; |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + vi.unstubAllEnvs(); |
| 25 | +}); |
| 26 | + |
| 27 | +type CapturedDaemon = { bodies: string[]; close: () => Promise<void> }; |
| 28 | + |
| 29 | +test.sequential.for(['socket', 'http'] as const)( |
| 30 | + 'a local %s daemon gets a fingerprint, never the key, only on lease_allocate with credentials', |
| 31 | + async (transport, t) => { |
| 32 | + if (!(await supportsLoopbackBind())) { |
| 33 | + t.skip('loopback listeners are not permitted in this environment'); |
| 34 | + return; |
| 35 | + } |
| 36 | + clearCredentialEnv(); |
| 37 | + vi.stubEnv('AGENT_DEVICE_DAEMON_BASE_URL', undefined); |
| 38 | + vi.stubEnv('AGENT_DEVICE_DAEMON_AUTH_TOKEN', undefined); |
| 39 | + const stateDir = mkdtempForTestSync('agent-device-provider-credentials-daemon-'); |
| 40 | + const daemon = await startLocalDaemon(stateDir, transport); |
| 41 | + const send = (command: string) => |
| 42 | + sendToDaemon({ |
| 43 | + session: 'default', |
| 44 | + command, |
| 45 | + positionals: [], |
| 46 | + flags: { stateDir, daemonTransport: transport }, |
| 47 | + meta: { requestId: `req-${command}`, leaseProvider: 'limrun', tenantId: 'tenant-a' }, |
| 48 | + }); |
| 49 | + |
| 50 | + try { |
| 51 | + vi.stubEnv('LIMRUN_API_KEY', API_KEY); |
| 52 | + await send('lease_allocate'); |
| 53 | + await send('devices'); |
| 54 | + vi.stubEnv('LIMRUN_API_KEY', undefined); |
| 55 | + await send('lease_allocate'); |
| 56 | + } finally { |
| 57 | + await daemon.close(); |
| 58 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 59 | + } |
| 60 | + |
| 61 | + expect(daemon.bodies.map(readFingerprint)).toEqual([ |
| 62 | + providerCredentialFingerprint('limrun', { LIMRUN_API_KEY: API_KEY }), |
| 63 | + undefined, |
| 64 | + undefined, |
| 65 | + ]); |
| 66 | + for (const body of daemon.bodies) expect(body).not.toContain(API_KEY); |
| 67 | + }, |
| 68 | +); |
| 69 | + |
| 70 | +test.sequential('a remote daemon gets no provider credential fingerprint', async (t) => { |
| 71 | + if (!(await supportsLoopbackBind())) { |
| 72 | + t.skip('loopback listeners are not permitted in this environment'); |
| 73 | + return; |
| 74 | + } |
| 75 | + clearCredentialEnv(); |
| 76 | + vi.stubEnv('LIMRUN_API_KEY', API_KEY); |
| 77 | + const remote = await startHttpDaemonFixture({}); |
| 78 | + vi.stubEnv('AGENT_DEVICE_DAEMON_BASE_URL', `http://127.0.0.1:${remote.port}`); |
| 79 | + vi.stubEnv('AGENT_DEVICE_DAEMON_AUTH_TOKEN', 'remote-secret'); |
| 80 | + |
| 81 | + try { |
| 82 | + await sendToDaemon({ |
| 83 | + session: 'default', |
| 84 | + command: 'lease_allocate', |
| 85 | + positionals: [], |
| 86 | + meta: { requestId: 'req-remote-lease', leaseProvider: 'limrun', tenantId: 'tenant-a' }, |
| 87 | + }); |
| 88 | + } finally { |
| 89 | + await closeLoopbackServer(remote.server); |
| 90 | + } |
| 91 | + |
| 92 | + expect(remote.rpcRequests.map((rpc) => rpc.method)).toEqual(['agent_device.lease.allocate']); |
| 93 | + const body = JSON.stringify(remote.rpcRequests[0]); |
| 94 | + expect(readFingerprint(body)).toBe(undefined); |
| 95 | + expect(body).not.toContain(API_KEY); |
| 96 | +}); |
| 97 | + |
| 98 | +function readFingerprint(body: string): unknown { |
| 99 | + const parsed = JSON.parse(body) as { |
| 100 | + meta?: { providerCredentialFingerprint?: unknown }; |
| 101 | + params?: { providerCredentialFingerprint?: unknown; meta?: Record<string, unknown> }; |
| 102 | + }; |
| 103 | + return ( |
| 104 | + parsed.meta?.providerCredentialFingerprint ?? |
| 105 | + parsed.params?.providerCredentialFingerprint ?? |
| 106 | + parsed.params?.meta?.providerCredentialFingerprint |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function clearCredentialEnv(): void { |
| 111 | + for (const name of LIMRUN_CREDENTIAL_VARIABLES) vi.stubEnv(name, undefined); |
| 112 | +} |
| 113 | + |
| 114 | +async function startLocalDaemon( |
| 115 | + stateDir: string, |
| 116 | + transport: 'socket' | 'http', |
| 117 | +): Promise<CapturedDaemon> { |
| 118 | + if (transport === 'http') { |
| 119 | + const daemon = await startHttpDaemonFixture({}); |
| 120 | + writeDaemonInfo(stateDir, { httpPort: daemon.port, transport }); |
| 121 | + return { |
| 122 | + get bodies() { |
| 123 | + return daemon.rpcRequests.map((rpc) => JSON.stringify(rpc)); |
| 124 | + }, |
| 125 | + close: () => closeLoopbackServer(daemon.server), |
| 126 | + }; |
| 127 | + } |
| 128 | + const bodies: string[] = []; |
| 129 | + const server = net.createServer((socket) => { |
| 130 | + socket.setEncoding('utf8'); |
| 131 | + let body = ''; |
| 132 | + socket.on('data', (chunk) => { |
| 133 | + body += chunk; |
| 134 | + if (!body.includes('\n')) return; |
| 135 | + bodies.push(body.trim()); |
| 136 | + socket.end(`${JSON.stringify({ ok: true, data: {} })}\n`); |
| 137 | + }); |
| 138 | + }); |
| 139 | + writeDaemonInfo(stateDir, { port: await listenOnLoopback(server), transport }); |
| 140 | + return { bodies, close: () => closeLoopbackServer(server) }; |
| 141 | +} |
| 142 | + |
| 143 | +function writeDaemonInfo(stateDir: string, endpoint: Record<string, unknown>): void { |
| 144 | + const paths = resolveDaemonPaths(stateDir); |
| 145 | + fs.mkdirSync(paths.baseDir, { recursive: true }); |
| 146 | + fs.writeFileSync( |
| 147 | + paths.infoPath, |
| 148 | + `${JSON.stringify({ |
| 149 | + ...endpoint, |
| 150 | + token: 'local-secret', |
| 151 | + pid: process.pid, |
| 152 | + version: readVersion(), |
| 153 | + codeSignature: currentDaemonCodeSignature(), |
| 154 | + processStartTime: readProcessStartTime(process.pid) ?? undefined, |
| 155 | + })}\n`, |
| 156 | + 'utf8', |
| 157 | + ); |
| 158 | +} |
0 commit comments