The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import type { IncomingMessage } from 'node:http'
import type { Duplex } from 'node:stream'
import { CloudAppWebsocket } from './cloud-app-websocket'
import { principalStorage } from './engine/platform-principal'
import { OrganizationResourcePermission as Permission } from '../organization/enums/organization-resource-permission.enum'
describe('App database WebSocket authorization', () => {
const path = '/api/v1/org/apps/demo/envs/main/db/connect'
const token = 'eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJ1c2VyIn0.test'
function setup() {
const user = { userId: 'owner' }
const key = { ...user, apiKey: { organizationId: 'org' } }
const apiKeys = { validate: jest.fn().mockResolvedValue(key) }
const jwt = {
verifyToken: jest.fn().mockResolvedValue({ sub: 'owner' }),
validate: jest.fn().mockResolvedValue(user),
}
const appByName = jest.fn(async () => ({
id: 'app',
name: 'demo',
org_id: principalStorage.getStore()?.organizationId,
}))
const envByName = jest.fn(async () => ({ name: 'main', db_url: null }))
const service = {
context: jest.fn(() => ({ store: { appByName, envByName } })),
authorize: jest.fn((auth, permissions) => ({
organizationId: auth.organizationId,
actor: 'user:owner',
userId: auth.userId,
permissions,
})),
}
const organizations = { findOne: jest.fn().mockResolvedValue({ id: 'org' }) }
const members = { findOne: jest.fn().mockResolvedValue({ role: 'owner' }) }
const ws = new CloudAppWebsocket(
service as never,
apiKeys as never,
jwt as never,
organizations as never,
members as never,
)
const upgrade = async (token?: string, suffix = '') => {
const request = {
url: path + suffix,
headers: token ? { authorization: 'Bearer ' + token } : {},
} as IncomingMessage
const socket = { end: jest.fn() }
await ws.upgrade(request, socket as unknown as Duplex, Buffer.alloc(0))
return { response: socket.end.mock.calls[0][0] as string, url: request.url }
}
return { ws, jwt, apiKeys, service, upgrade, appByName, envByName, members }
}
test('routes read and write upgrades with their platform identity into the database proxy', async () => {
const h = setup()
expect(h.ws.matches(path)).toBe(true)
expect(h.ws.matches(path + '?write=1')).toBe(true)
expect(h.ws.matches(path + '/else')).toBe(false)
expect(h.ws.matches(undefined)).toBe(false)
const read = await h.upgrade('test-key')
const write = await h.upgrade(token, '?write=1')
expect(h.apiKeys.validate.mock.calls).toEqual([['test-key']])
expect(h.jwt.verifyToken).toHaveBeenCalledWith(token)
expect(h.jwt.validate).toHaveBeenCalledWith(expect.anything(), { sub: 'owner' })
expect(h.service.authorize.mock.calls.map(([, permissions]) => permissions)).toEqual([
[Permission.READ_APPS],
[Permission.WRITE_APPS],
])
expect(h.members.findOne).toHaveBeenCalledWith('org', 'owner')
expect(read.url).toBe('/v1/apps/demo/envs/main/db/connect')
expect(write.url).toBe(read.url + '?write=1')
expect(read.response).toContain('has no database')
expect(write.response).toContain('has no database')
expect(h.envByName).toHaveBeenCalledWith('app', 'main')
})
test('declines absent credentials, service identities and keys owned by a different organization', async () => {
const h = setup()
h.apiKeys.validate
.mockResolvedValueOnce({ role: 'runner' })
.mockResolvedValueOnce({ userId: 'owner', apiKey: { organizationId: 'other' } })
for (const key of [undefined, 'service-token', 'other-org']) {
expect((await h.upgrade(key)).response).toContain('403 Forbidden')
}
expect(h.service.authorize).not.toHaveBeenCalled()
expect(h.service.context).not.toHaveBeenCalled()
})
})