mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(sandbox): add KVM-backed agent sandboxing via microsandbox
This commit is contained in:
1 parent
1d477bdee4
commit
aa3fb48a62
80 files changed
+17304
-204
No files matched your search
@@ -5,8 +5,10 @@ import { createSettingsRoutes } from '../../src/routes/settings'
|
||||
import { encryptSecret } from '../../src/utils/crypto'
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import { opencodeServerManager } from '../../src/services/opencode-single-server'
|
||||
import { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
||||
import type { OpenCodeClient } from '../../src/services/opencode/client'
|
||||
import type { GitAuthService } from '../../src/services/git-auth'
|
||||
import type { SettingsService } from '../../src/services/settings'
|
||||
|
||||
vi.mock('bun:sqlite', () => ({
|
||||
Database: class Database {},
|
||||
@@ -19,6 +21,8 @@ vi.mock('../../src/services/opencode-single-server', () => ({
|
||||
getVersion: vi.fn(),
|
||||
fetchVersion: vi.fn(),
|
||||
clearStartupError: vi.fn(),
|
||||
getLastStartupError: vi.fn(() => null),
|
||||
checkHealth: vi.fn(() => true),
|
||||
reinitializeBinDirectory: vi.fn(),
|
||||
},
|
||||
ConfigReloadError: class ConfigReloadError extends Error {
|
||||
@@ -181,8 +185,81 @@ describe('OpenCode Server Auth Routes', () => {
|
||||
const restored = db.prepare('SELECT value FROM app_secrets WHERE key = ?').get('opencode_server_password') as { value: string } | undefined
|
||||
expect(restored?.value).toBe(previous.value)
|
||||
})
|
||||
|
||||
it('keeps the proxy lifecycle gate closed during the supervised restart and reopens only after a verified healthy restart', async () => {
|
||||
const lifecycle = { initialized: false }
|
||||
const { app: supervisedApp, manager } = createSupervisedApp(db, lifecycle)
|
||||
|
||||
let releaseRestart!: () => void
|
||||
manager.restart.mockImplementationOnce(
|
||||
() => new Promise<void>((resolve) => { releaseRestart = resolve }),
|
||||
)
|
||||
|
||||
const patchPromise = supervisedApp.request('/api/settings/opencode-server-auth', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'testpassword123' }),
|
||||
})
|
||||
await vi.waitFor(() => expect(manager.restart).toHaveBeenCalledTimes(1))
|
||||
expect(lifecycle.initialized).toBe(false)
|
||||
|
||||
releaseRestart()
|
||||
const response = await patchPromise
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(lifecycle.initialized).toBe(true)
|
||||
expect(await response.json()).toEqual({ isSet: true, source: 'db' })
|
||||
expect(db.prepare('SELECT 1 FROM app_secrets WHERE key = ?').get('opencode_server_password')).toBeDefined()
|
||||
})
|
||||
|
||||
it('fails the auth update and restores the prior password when the supervised restart ends unhealthy, keeping the proxy gate closed', async () => {
|
||||
insertPassword('testpassword123')
|
||||
const previous = db.prepare('SELECT value FROM app_secrets WHERE key = ?').get('opencode_server_password') as { value: string }
|
||||
const lifecycle = { initialized: false }
|
||||
const { app: supervisedApp, manager } = createSupervisedApp(db, lifecycle)
|
||||
manager.checkHealth.mockResolvedValue(false)
|
||||
manager.isLastStartupErrorNonRecoverable.mockReturnValue(true)
|
||||
|
||||
const response = await supervisedApp.request('/api/settings/opencode-server-auth', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: null }),
|
||||
})
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(manager.restart).toHaveBeenCalledTimes(2)
|
||||
expect(lifecycle.initialized).toBe(false)
|
||||
|
||||
const restored = db.prepare('SELECT value FROM app_secrets WHERE key = ?').get('opencode_server_password') as { value: string } | undefined
|
||||
expect(restored?.value).toBe(previous.value)
|
||||
})
|
||||
})
|
||||
|
||||
function createSupervisedApp(db: Database, lifecycle: { initialized: boolean }) {
|
||||
const manager = {
|
||||
start: vi.fn().mockResolvedValue(undefined),
|
||||
stop: vi.fn().mockResolvedValue(undefined),
|
||||
isOperationInProgress: vi.fn(() => false),
|
||||
checkHealth: vi.fn().mockResolvedValue(true),
|
||||
restart: vi.fn().mockResolvedValue(undefined),
|
||||
reloadConfig: vi.fn().mockResolvedValue(undefined),
|
||||
clearStartupError: vi.fn(),
|
||||
getLastStartupError: vi.fn(() => null),
|
||||
isLastStartupErrorNonRecoverable: vi.fn(() => false),
|
||||
setLifecycleInitialized: vi.fn((value: boolean) => { lifecycle.initialized = value }),
|
||||
getPort: vi.fn(() => 5551),
|
||||
getVersion: vi.fn(() => '1.0.137'),
|
||||
getMinVersion: vi.fn(() => '1.0.137'),
|
||||
isVersionSupported: vi.fn(() => true),
|
||||
}
|
||||
const supervisor = new OpenCodeSupervisor(manager as unknown as never, {} as SettingsService, {
|
||||
failureThreshold: 1,
|
||||
watchEnabled: false,
|
||||
})
|
||||
const routes = createSettingsRoutes(db, {} as GitAuthService, {} as OpenCodeClient, supervisor)
|
||||
return { app: new Hono().route('/api/settings', routes), manager }
|
||||
}
|
||||
|
||||
function insertPassword(password: string) {
|
||||
const encrypted = encryptSecret(password)
|
||||
const now = Date.now()
|
||||
|
||||
Reference in new issue
Block a user