mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
277 lines
10 KiB
TypeScript
277 lines
10 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { Hono } from 'hono'
|
|
import { z } from 'zod'
|
|
import type { Database } from 'bun:sqlite'
|
|
import { SecretListResponseSchema, type SecretEntry } from '@opencode-manager/shared/schemas'
|
|
import { SecretsService } from '../../src/services/secrets'
|
|
import { ProjectConfigService } from '../../src/services/project-config'
|
|
import { createGitService } from '../../src/services/git/GitService'
|
|
import { createGlobalSecretRoutes, createRepoSecretRoutes } from '../../src/routes/secrets'
|
|
import { selectRow } from '../../src/db/rows'
|
|
import { createGitAuthService } from '../helpers/git-fixtures'
|
|
import { readJson } from '../helpers/read-json'
|
|
import { createTestDatabase, seedRepo } from '../helpers/test-database'
|
|
|
|
const jsonHeaders = { 'Content-Type': 'application/json' }
|
|
const StoredSecretSchema = z.object({ value: z.string() })
|
|
|
|
function createApp(db: Database): Hono {
|
|
const secretsService = new SecretsService(db)
|
|
const gitAuthService = createGitAuthService()
|
|
const projectConfigService = new ProjectConfigService(db, createGitService(gitAuthService), gitAuthService)
|
|
const app = new Hono()
|
|
app.route('/secrets', createGlobalSecretRoutes(secretsService, 'secret'))
|
|
app.route('/variables', createGlobalSecretRoutes(secretsService, 'variable'))
|
|
app.route('/repos', createRepoSecretRoutes({ database: db, projectConfigService, secretsService }, 'secret'))
|
|
app.route('/repos', createRepoSecretRoutes({ database: db, projectConfigService, secretsService }, 'variable'))
|
|
return app
|
|
}
|
|
|
|
async function readItems(res: Response): Promise<SecretEntry[]> {
|
|
return (await readJson(res, SecretListResponseSchema)).items
|
|
}
|
|
|
|
describe('secret routes', () => {
|
|
let db: Database
|
|
let app: Hono
|
|
|
|
beforeEach(() => {
|
|
db = createTestDatabase()
|
|
app = createApp(db)
|
|
})
|
|
|
|
afterEach(() => {
|
|
db.close()
|
|
})
|
|
|
|
it('lists global secrets and variables as empty initially', async () => {
|
|
const secrets = await app.request('/secrets')
|
|
const variables = await app.request('/variables')
|
|
expect(secrets.status).toBe(200)
|
|
expect(variables.status).toBe(200)
|
|
expect(await readItems(secrets)).toEqual([])
|
|
expect(await readItems(variables)).toEqual([])
|
|
})
|
|
|
|
it('stores a secret under its upper-cased name without ever returning its value', async () => {
|
|
const put = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'super-secret' }),
|
|
})
|
|
expect(put.status).toBe(204)
|
|
|
|
const items = await readItems(await app.request('/secrets'))
|
|
expect(items).toHaveLength(1)
|
|
expect(items[0]).toMatchObject({ name: 'TOKEN', environment: null })
|
|
expect(items[0]).not.toHaveProperty('value')
|
|
|
|
const stored = selectRow(db, StoredSecretSchema, 'SELECT value FROM secrets WHERE name = ?', 'TOKEN')
|
|
expect(stored?.value).not.toBe('super-secret')
|
|
})
|
|
|
|
it('stores a variable and returns its value', async () => {
|
|
const put = await app.request('/variables/log_level', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'debug' }),
|
|
})
|
|
expect(put.status).toBe(204)
|
|
|
|
const items = await readItems(await app.request('/variables'))
|
|
expect(items).toEqual([expect.objectContaining({ name: 'LOG_LEVEL', environment: null, value: 'debug' })])
|
|
})
|
|
|
|
it('rejects invalid, GITHUB_, and reserved names with 400', async () => {
|
|
for (const name of ['1BAD', 'GITHUB_TOKEN', 'PATH', 'OCM_SOMETHING']) {
|
|
const res = await app.request(`/secrets/${name}`, {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v' }),
|
|
})
|
|
expect(res.status, name).toBe(400)
|
|
}
|
|
})
|
|
|
|
it('enforces the 48 KiB value limit', async () => {
|
|
const atLimit = await app.request('/variables/big', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'a'.repeat(48 * 1024) }),
|
|
})
|
|
expect(atLimit.status).toBe(204)
|
|
|
|
const overLimit = await app.request('/variables/too_big', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'a'.repeat(48 * 1024 + 1) }),
|
|
})
|
|
expect(overLimit.status).toBe(400)
|
|
})
|
|
|
|
it('rejects an environment on a global secret', async () => {
|
|
const res = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v', environment: 'prod' }),
|
|
})
|
|
expect(res.status).toBe(400)
|
|
})
|
|
|
|
it('rejects an invalid JSON body', async () => {
|
|
const global = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: '{not json',
|
|
})
|
|
expect(global.status).toBe(400)
|
|
expect(await global.json()).toEqual({ error: 'Invalid JSON' })
|
|
|
|
const repoId = seedRepo(db).id
|
|
const repo = await app.request(`/repos/${repoId}/secrets/token`, {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: '{not json',
|
|
})
|
|
expect(repo.status).toBe(400)
|
|
expect(await repo.json()).toEqual({ error: 'Invalid JSON' })
|
|
})
|
|
|
|
it('rejects a body that fails validation', async () => {
|
|
const res = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: '' }),
|
|
})
|
|
expect(res.status).toBe(400)
|
|
expect(await res.json()).toMatchObject({ error: expect.any(String) })
|
|
})
|
|
|
|
it('deletes a global secret and returns 404 when missing', async () => {
|
|
await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v' }),
|
|
})
|
|
|
|
expect((await app.request('/secrets/token', { method: 'DELETE' })).status).toBe(204)
|
|
expect((await app.request('/secrets/token', { method: 'DELETE' })).status).toBe(404)
|
|
})
|
|
|
|
it('stores an agent hidden secret with normalized allowed hosts and never returns its value', async () => {
|
|
const put = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({
|
|
value: 'super-secret',
|
|
agentHidden: true,
|
|
allowedHosts: [' API.Example.com ', '*.example.org', 'api.example.com'],
|
|
}),
|
|
})
|
|
expect(put.status).toBe(204)
|
|
|
|
const items = await readItems(await app.request('/secrets'))
|
|
expect(items).toEqual([
|
|
expect.objectContaining({
|
|
name: 'TOKEN',
|
|
environment: null,
|
|
agentHidden: true,
|
|
allowedHosts: ['api.example.com', '*.example.org'],
|
|
}),
|
|
])
|
|
expect(items[0]).not.toHaveProperty('value')
|
|
})
|
|
|
|
it('rejects invalid hidden secret host configuration', async () => {
|
|
const bodies = [
|
|
{ value: 'v', agentHidden: true },
|
|
{ value: 'v', agentHidden: true, allowedHosts: ['*'] },
|
|
{ value: 'v', agentHidden: true, allowedHosts: ['https://example.com'] },
|
|
{ value: 'v', agentHidden: true, allowedHosts: ['example.com:443'] },
|
|
{ value: 'v', agentHidden: true, allowedHosts: ['example.com/path'] },
|
|
{ value: 'v', agentHidden: true, allowedHosts: ['10.0.0.1'] },
|
|
{
|
|
value: 'v',
|
|
agentHidden: true,
|
|
allowedHosts: Array.from({ length: 21 }, (_, index) => `host${index}.example.com`),
|
|
},
|
|
{ value: 'v', allowedHosts: ['example.com'] },
|
|
]
|
|
|
|
for (const body of bodies) {
|
|
const res = await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify(body),
|
|
})
|
|
expect(res.status, JSON.stringify(body)).toBe(400)
|
|
expect(await res.json()).toMatchObject({ error: expect.any(String) })
|
|
}
|
|
})
|
|
|
|
it('rejects hiding a variable', async () => {
|
|
const res = await app.request('/variables/log_level', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'debug', agentHidden: true, allowedHosts: ['example.com'] }),
|
|
})
|
|
expect(res.status).toBe(400)
|
|
expect(await res.json()).toMatchObject({ error: 'Only secrets can be hidden from agents' })
|
|
})
|
|
|
|
it('clears hidden settings when a secret is saved again without them', async () => {
|
|
await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v', agentHidden: true, allowedHosts: ['example.com'] }),
|
|
})
|
|
await app.request('/secrets/token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v2' }),
|
|
})
|
|
|
|
const items = await readItems(await app.request('/secrets'))
|
|
expect(items).toEqual([expect.objectContaining({ name: 'TOKEN', agentHidden: false, allowedHosts: [] })])
|
|
})
|
|
|
|
it('stores repository secrets against the repo and never returns values', async () => {
|
|
const repoId = seedRepo(db).id
|
|
|
|
const put = await app.request(`/repos/${repoId}/secrets/token`, {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'repo-secret' }),
|
|
})
|
|
expect(put.status).toBe(204)
|
|
|
|
const items = await readItems(await app.request(`/repos/${repoId}/secrets`))
|
|
expect(items).toEqual([expect.objectContaining({ name: 'TOKEN', environment: null })])
|
|
expect(items[0]).not.toHaveProperty('value')
|
|
})
|
|
|
|
it('supports repository environments and scoped deletes', async () => {
|
|
const repoId = seedRepo(db).id
|
|
|
|
await app.request(`/repos/${repoId}/variables/deploy_url`, {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'https://example.test', environment: 'Production' }),
|
|
})
|
|
|
|
const items = await readItems(await app.request(`/repos/${repoId}/variables`))
|
|
expect(items).toEqual([expect.objectContaining({ name: 'DEPLOY_URL', environment: 'production', value: 'https://example.test' })])
|
|
|
|
const deleted = await app.request(`/repos/${repoId}/variables/deploy_url?environment=production`, { method: 'DELETE' })
|
|
expect(deleted.status).toBe(204)
|
|
|
|
const missing = await app.request(`/repos/${repoId}/variables/deploy_url?environment=production`, { method: 'DELETE' })
|
|
expect(missing.status).toBe(404)
|
|
})
|
|
|
|
it('returns 404 for an unknown repository', async () => {
|
|
const res = await app.request('/repos/9999/secrets')
|
|
expect(res.status).toBe(404)
|
|
})
|
|
})
|