mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Give each session VM its hidden secrets, attest them, and
This commit is contained in:
1 parent
a58dcc96d4
commit
4ea3ecca9e
3 files changed
+567
-42
No files matched your search
@@ -7,6 +7,7 @@ import { ENV, getOpenCodeAgentTmpPath, getOpenCodeGlobalSkillsPath, getOpenCodeT
|
||||
import { SettingsService } from '../../../src/services/settings'
|
||||
import {
|
||||
SANDBOX_DOCKER_DATA_GUEST_PATH,
|
||||
buildSandboxSecretConf,
|
||||
buildSandboxStopArgs,
|
||||
buildSandboxStopManagedArgs,
|
||||
parseMemoryMib,
|
||||
@@ -16,6 +17,8 @@ import {
|
||||
sandboxDockerDataVolumeName,
|
||||
sandboxExecutablePath,
|
||||
sandboxProjectRoots,
|
||||
sandboxSecretEnvName,
|
||||
sandboxSecretPlaceholder,
|
||||
sessionSandboxName,
|
||||
} from '../../../src/services/sandbox/command'
|
||||
import {
|
||||
@@ -28,7 +31,7 @@ import {
|
||||
} from '../../../src/services/sandbox/runtime'
|
||||
import type { JsonValue } from '@opencode-manager/shared/utils'
|
||||
import { sandboxCapabilityProbes, resetSandboxCapabilityCache } from '../../../src/services/sandbox/capability'
|
||||
import type { RepoEnvService } from '../../../src/services/repo-env'
|
||||
import type { RepoEnvService, HiddenSecretPolicy } from '../../../src/services/repo-env'
|
||||
import { overrideSandboxExecutableTrustValidator } from '../../../src/services/sandbox/command'
|
||||
import { forceProcessAttestation } from '../../../src/services/opencode/process-identity'
|
||||
import { z } from 'zod'
|
||||
@@ -79,7 +82,12 @@ const FakeVmStateSchema = z.object({
|
||||
|
||||
type FakeVmState = z.infer<typeof FakeVmStateSchema>
|
||||
|
||||
const FakeCallSchema = z.object({ argv: z.array(z.string()) })
|
||||
const FakeCallSchema = z.object({
|
||||
argv: z.array(z.string()),
|
||||
env: z.record(z.string(), z.string()).optional(),
|
||||
})
|
||||
|
||||
type FakeCall = z.infer<typeof FakeCallSchema>
|
||||
|
||||
interface FakeMountOptions {
|
||||
readonly: boolean
|
||||
@@ -193,13 +201,17 @@ function fakeMsbStatePath(): string {
|
||||
return path.join(fakeMsbStateDir, 'state.json')
|
||||
}
|
||||
|
||||
function fakeMsbInvocations(): string[][] {
|
||||
function fakeMsbCallRecords(): FakeCall[] {
|
||||
const logPath = path.join(fakeMsbStateDir, 'calls.jsonl')
|
||||
if (!existsSync(logPath)) return []
|
||||
return readFileSync(logPath, 'utf8')
|
||||
.split('\n')
|
||||
.filter((line) => line.length > 0)
|
||||
.map((line) => FakeCallSchema.parse(JSON.parse(line)).argv)
|
||||
.map((line) => FakeCallSchema.parse(JSON.parse(line)))
|
||||
}
|
||||
|
||||
function fakeMsbInvocations(): string[][] {
|
||||
return fakeMsbCallRecords().map((call) => call.argv)
|
||||
}
|
||||
|
||||
function fakeVms(): Record<string, FakeVmState> {
|
||||
@@ -329,6 +341,54 @@ function shellTarget(locationDirectory: string, resources = defaultResources()):
|
||||
return { rootSessionId: ROOT_SESSION, locationDirectory, resources }
|
||||
}
|
||||
|
||||
type TestHiddenSecret = { name: string; value: string; allowedHosts: string[] }
|
||||
|
||||
function hiddenRepoEnv(secrets: TestHiddenSecret[]) {
|
||||
return {
|
||||
resolveForDirectory: vi.fn(async (_directory: string, policy: HiddenSecretPolicy = { kind: 'withhold' }) => {
|
||||
const hiddenSecrets = secrets.map((secret) => ({ ...secret }))
|
||||
const env: Record<string, string> = {}
|
||||
for (const secret of hiddenSecrets) {
|
||||
env[secret.name] =
|
||||
policy.kind === 'reveal'
|
||||
? secret.value
|
||||
: policy.kind === 'placeholder'
|
||||
? policy.placeholderFor(secret.name)
|
||||
: ''
|
||||
}
|
||||
return { env, maskValues: [], hiddenSecrets }
|
||||
}),
|
||||
} satisfies Pick<RepoEnvService, 'resolveForDirectory'>
|
||||
}
|
||||
|
||||
function installFakeVmSecrets(name: string, secrets: TestHiddenSecret[]): void {
|
||||
const spec = {
|
||||
name,
|
||||
rootSessionId: ROOT_SESSION,
|
||||
projectMounts: [repoADir],
|
||||
cpus: defaultResources().cpus,
|
||||
memoryMib: defaultResources().memoryMib,
|
||||
secrets: secrets.map((secret) => ({
|
||||
name: secret.name,
|
||||
envVar: sandboxSecretEnvName(secret.name),
|
||||
placeholder: sandboxSecretPlaceholder(name, secret.name),
|
||||
allowedHosts: secret.allowedHosts,
|
||||
value: secret.value,
|
||||
})),
|
||||
}
|
||||
const secretPath = path.join(fakeMsbStateDir, 'vms', name, 'secrets.json')
|
||||
mkdirSync(path.dirname(secretPath), { recursive: true })
|
||||
writeFileSync(secretPath, buildSandboxSecretConf(spec))
|
||||
}
|
||||
|
||||
function runCalls(): FakeCall[] {
|
||||
return fakeMsbCallRecords().filter((call) => call.argv[1] === 'run')
|
||||
}
|
||||
|
||||
function modifyCalls(): FakeCall[] {
|
||||
return fakeMsbCallRecords().filter((call) => call.argv[1] === 'modify')
|
||||
}
|
||||
|
||||
function bindMount(host: string): FakeBindMount {
|
||||
return {
|
||||
type: 'Bind',
|
||||
@@ -678,6 +738,284 @@ describe('SandboxRuntimeService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('creates the VM with a secret conf and returns only the placeholder', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const withSecrets = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
|
||||
const plan = await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
const placeholder = sandboxSecretPlaceholder(VM_NAME, 'TOKEN')
|
||||
expect(plan).toEqual({ mode: 'sandbox', name: VM_NAME, workdir: repoADir, env: { TOKEN: placeholder } })
|
||||
const runs = runCalls()
|
||||
expect(runs).toHaveLength(1)
|
||||
expect(runs[0]?.argv).toContain('--secret-conf')
|
||||
expect(runs[0]?.env).toEqual({ OCM_SECRET_TOKEN: 'hunter2' })
|
||||
expect(fakeMsbInvocations().flat()).not.toContain('hunter2')
|
||||
expect(readFileSync(path.join(fakeMsbStateDir, 'vms', VM_NAME, 'secrets.json'), 'utf8')).toBe(
|
||||
JSON.stringify({
|
||||
OCM_SECRET_TOKEN: {
|
||||
value: { $msb_env: 'OCM_SECRET_TOKEN' },
|
||||
placeholder,
|
||||
allow: ['api.example.com'],
|
||||
substitution: { headers: true, query: false, body: false },
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('attests a running VM that carries the expected network secrets without recreating it', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const withSecrets = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
|
||||
await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
const second = await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(second).toEqual({
|
||||
mode: 'sandbox',
|
||||
name: VM_NAME,
|
||||
workdir: repoADir,
|
||||
env: { TOKEN: sandboxSecretPlaceholder(VM_NAME, 'TOKEN') },
|
||||
})
|
||||
expect(runCalls()).toHaveLength(1)
|
||||
expect(msbCalls('rm')).toHaveLength(0)
|
||||
expect(modifyCalls()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('recreates the VM when a hidden secret is added', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const first = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
const added = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([
|
||||
{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] },
|
||||
{ name: 'OTHER', value: 'other-value', allowedHosts: ['api.example.com'] },
|
||||
]),
|
||||
)
|
||||
|
||||
await first.planShell(repoADir, shellTarget(repoADir))
|
||||
await added.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(msbCalls('rm')).toHaveLength(1)
|
||||
expect(runCalls()).toHaveLength(2)
|
||||
expect(modifyCalls()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('recreates the VM when the allowed hosts of a hidden secret change', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const first = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
const rehosted = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api2.example.com'] }]),
|
||||
)
|
||||
|
||||
await first.planShell(repoADir, shellTarget(repoADir))
|
||||
await rehosted.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(msbCalls('rm')).toHaveLength(1)
|
||||
expect(runCalls()).toHaveLength(2)
|
||||
expect(modifyCalls()).toHaveLength(0)
|
||||
expect(readFileSync(path.join(fakeMsbStateDir, 'vms', VM_NAME, 'secrets.json'), 'utf8')).toContain(
|
||||
'"allow":["api2.example.com"]',
|
||||
)
|
||||
})
|
||||
|
||||
it('rotates a changed value with one modify and no recreate', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const first = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'value-one', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
const rotated = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'value-two', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
|
||||
await first.planShell(repoADir, shellTarget(repoADir))
|
||||
const plan = await rotated.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(plan).toEqual({
|
||||
mode: 'sandbox',
|
||||
name: VM_NAME,
|
||||
workdir: repoADir,
|
||||
env: { TOKEN: sandboxSecretPlaceholder(VM_NAME, 'TOKEN') },
|
||||
})
|
||||
const modifies = modifyCalls()
|
||||
expect(modifies).toHaveLength(1)
|
||||
expect(modifies[0]?.argv).toEqual([
|
||||
sandboxExecutablePath(),
|
||||
'modify',
|
||||
VM_NAME,
|
||||
'--secret',
|
||||
'OCM_SECRET_TOKEN@api.example.com',
|
||||
])
|
||||
expect(modifies[0]?.env).toEqual({ OCM_SECRET_TOKEN: 'value-two' })
|
||||
expect(fakeMsbInvocations().flat()).not.toContain('value-two')
|
||||
expect(runCalls()).toHaveLength(1)
|
||||
expect(msbCalls('rm')).toHaveLength(0)
|
||||
|
||||
await rotated.planShell(repoADir, shellTarget(repoADir))
|
||||
expect(modifyCalls()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('recreates the VM when a live modify is refused', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const first = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'value-one', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
const rotated = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'TOKEN', value: 'value-two', allowedHosts: ['api.example.com'] }]),
|
||||
)
|
||||
|
||||
await first.planShell(repoADir, shellTarget(repoADir))
|
||||
writeFailureFile('modify', 'secret update refused')
|
||||
const plan = await rotated.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(plan).toEqual({
|
||||
mode: 'sandbox',
|
||||
name: VM_NAME,
|
||||
workdir: repoADir,
|
||||
env: { TOKEN: sandboxSecretPlaceholder(VM_NAME, 'TOKEN') },
|
||||
})
|
||||
expect(modifyCalls()).toHaveLength(1)
|
||||
expect(msbCalls('rm')).toHaveLength(1)
|
||||
expect(runCalls()).toHaveLength(2)
|
||||
expect(runCalls().at(-1)?.env).toEqual({ OCM_SECRET_TOKEN: 'value-two' })
|
||||
expectLoggedMessage('was refused with code 1', 'warn')
|
||||
})
|
||||
|
||||
it('rotates each secret once after a Manager restart and then stops modifying', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const withSecrets = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([
|
||||
{ name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] },
|
||||
{ name: 'OTHER', value: 'other-value', allowedHosts: ['api2.example.com'] },
|
||||
]),
|
||||
)
|
||||
|
||||
await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
expect(modifyCalls()).toHaveLength(0)
|
||||
|
||||
resetSandboxRuntimeState()
|
||||
await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(modifyCalls()).toHaveLength(2)
|
||||
expect(runCalls()).toHaveLength(1)
|
||||
expect(msbCalls('rm')).toHaveLength(0)
|
||||
|
||||
await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
expect(modifyCalls()).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('passes the secret env to msb start and trusts the CA for a stopped VM with secrets', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map([[VM_NAME, vm({ running: false })]]))
|
||||
const secret = { name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }
|
||||
installFakeVmSecrets(VM_NAME, [secret])
|
||||
const withSecrets = new SandboxRuntimeService(db, hiddenRepoEnv([secret]))
|
||||
|
||||
const plan = await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(plan).toEqual({
|
||||
mode: 'sandbox',
|
||||
name: VM_NAME,
|
||||
workdir: repoADir,
|
||||
env: { TOKEN: sandboxSecretPlaceholder(VM_NAME, 'TOKEN') },
|
||||
})
|
||||
const start = fakeMsbCallRecords().find((call) => call.argv[1] === 'start')
|
||||
expect(start?.env).toEqual({ OCM_SECRET_TOKEN: 'hunter2' })
|
||||
expect(fakeMsbInvocations().filter((args) => args[1] === 'exec').some((args) => args.some((arg) => arg.includes('certutil')))).toBe(true)
|
||||
expect(runCalls()).toHaveLength(0)
|
||||
expect(msbCalls('rm')).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('runs no secret conf or CA trust when the VM has no hidden secrets', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
|
||||
await service.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
const runs = runCalls()
|
||||
expect(runs).toHaveLength(1)
|
||||
expect(runs[0]?.argv).not.toContain('--secret-conf')
|
||||
expect(fakeMsbInvocations().filter((args) => args[1] === 'exec').some((args) => args.some((arg) => arg.includes('certutil')))).toBe(false)
|
||||
expect(modifyCalls()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('withholds a working directory hidden secret outside the session VM set', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const nested = path.join(repoADir, 'nested')
|
||||
mkdirSync(nested, { recursive: true })
|
||||
const sessionSecret: TestHiddenSecret = { name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] }
|
||||
const workingSecret: TestHiddenSecret = { name: 'OTHER', value: 'other-secret', allowedHosts: ['api.example.com'] }
|
||||
const repoEnv = {
|
||||
resolveForDirectory: vi.fn(async (dir: string, policy: HiddenSecretPolicy = { kind: 'withhold' }) => {
|
||||
const bindings = dir === repoADir ? [sessionSecret] : [sessionSecret, workingSecret]
|
||||
const env: Record<string, string> = {}
|
||||
for (const binding of bindings) {
|
||||
env[binding.name] =
|
||||
policy.kind === 'reveal'
|
||||
? binding.value
|
||||
: policy.kind === 'placeholder'
|
||||
? policy.placeholderFor(binding.name)
|
||||
: ''
|
||||
}
|
||||
return { env, maskValues: [], hiddenSecrets: bindings.map((binding) => ({ ...binding })) }
|
||||
}),
|
||||
} satisfies Pick<RepoEnvService, 'resolveForDirectory'>
|
||||
const withSecrets = new SandboxRuntimeService(db, repoEnv)
|
||||
|
||||
const plan = await withSecrets.planShell(nested, shellTarget(repoADir))
|
||||
|
||||
expect(plan).toEqual({
|
||||
mode: 'sandbox',
|
||||
name: VM_NAME,
|
||||
workdir: nested,
|
||||
env: { TOKEN: sandboxSecretPlaceholder(VM_NAME, 'TOKEN'), OTHER: '' },
|
||||
})
|
||||
expect(readFileSync(path.join(fakeMsbStateDir, 'vms', VM_NAME, 'secrets.json'), 'utf8')).not.toContain(
|
||||
'OCM_SECRET_OTHER',
|
||||
)
|
||||
})
|
||||
|
||||
it('leaves a hidden secret with no allowed hosts out of the VM and withholds it', async () => {
|
||||
enableEnforcement()
|
||||
installFakeMsb(new Map())
|
||||
const withSecrets = new SandboxRuntimeService(
|
||||
db,
|
||||
hiddenRepoEnv([{ name: 'ORPHAN', value: 'orphan-value', allowedHosts: [] }]),
|
||||
)
|
||||
|
||||
const plan = await withSecrets.planShell(repoADir, shellTarget(repoADir))
|
||||
|
||||
expect(plan).toEqual({ mode: 'sandbox', name: VM_NAME, workdir: repoADir, env: { ORPHAN: '' } })
|
||||
expect(runCalls()[0]?.argv).not.toContain('--secret-conf')
|
||||
expect(existsSync(path.join(fakeMsbStateDir, 'vms', VM_NAME, 'secrets.json'))).toBe(false)
|
||||
expect(fakeMsbInvocations().flat()).not.toContain('orphan-value')
|
||||
})
|
||||
|
||||
it('starts an existing stopped sandbox instead of recreating it', async () => {
|
||||
enableEnforcement()
|
||||
const vms = new Map<string, FakeVm>([[VM_NAME, vm({ running: false })]])
|
||||
|
||||
Reference in new issue
Block a user