mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
Add a GitHub Actions-style store for secrets and variables so tests can read
credentials from the environment without .env files in the repo.
- secrets table keyed by kind, repo (NULL for global) and environment;
secrets are encrypted with encryptSecret and write-only, variables are plain
text. Names follow GitHub's rules and reject Manager-reserved names.
- Global (/api/secrets, /api/variables) and repo
(/api/repos/:id/secrets|variables) routes.
- The trusted .ocm/project.json gains environment and env fields that map
names to ${{ secrets.X }} / ${{ vars.X }} expressions. Both are part of the
trust hash; files without them hash as before, so existing trust survives.
- RepoEnvService.resolveForDirectory resolves the repo and approved mapping
for any directory, including worktrees outside the repo folder, with
environment > repo > global precedence. It never throws.
- /api/internal/shell-env replaces /git-credentials/gh-env and feeds the
ocm-gh-env plugin, the sandbox shell plan and TerminalService.create.
- The plugin masks injected secret values as *** in completed tool results.
- The sandbox shell plan is now the only source of the guest environment: the
plugin pins OCM_SANDBOX_FORWARD_ENV and the shim forwards only those names.
198 lines
6.9 KiB
TypeScript
198 lines
6.9 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { Hono } from 'hono'
|
|
import { Database } from 'bun:sqlite'
|
|
import { migrate } from '../../src/db/migration-runner'
|
|
import { allMigrations } from '../../src/db/migrations'
|
|
import { createRepo } from '../../src/db/queries'
|
|
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 type { GitAuthService } from '../../src/services/git-auth'
|
|
import type { SecretEntry } from '@opencode-manager/shared/schemas'
|
|
|
|
const gitAuthService = { getGitEnvironment: () => ({}) } as unknown as GitAuthService
|
|
const jsonHeaders = { 'Content-Type': 'application/json' }
|
|
|
|
function createTestDb(): Database {
|
|
const db = new Database(':memory:')
|
|
migrate(db, allMigrations)
|
|
return db
|
|
}
|
|
|
|
function createApp(db: Database): Hono {
|
|
const secretsService = new SecretsService(db)
|
|
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
|
|
}
|
|
|
|
function seedRepo(db: Database): number {
|
|
return createRepo(db, {
|
|
localPath: 'repo-a',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}).id
|
|
}
|
|
|
|
async function readItems(res: Response): Promise<SecretEntry[]> {
|
|
const body = await res.json() as { items: SecretEntry[] }
|
|
return body.items
|
|
}
|
|
|
|
describe('secret routes', () => {
|
|
let db: Database
|
|
let app: Hono
|
|
|
|
beforeEach(() => {
|
|
db = createTestDb()
|
|
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 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 list = await app.request('/secrets')
|
|
const items = await readItems(list)
|
|
expect(items).toHaveLength(1)
|
|
expect(items[0]).toMatchObject({ name: 'TOKEN', environment: null })
|
|
expect(items[0]).not.toHaveProperty('value')
|
|
|
|
const stored = db.prepare('SELECT value FROM secrets WHERE name = ?').get('TOKEN') as { value: string }
|
|
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('normalizes names to upper case', async () => {
|
|
await app.request('/secrets/my_token', {
|
|
method: 'PUT',
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ value: 'v' }),
|
|
})
|
|
|
|
const items = await readItems(await app.request('/secrets'))
|
|
expect(items[0]?.name).toBe('MY_TOKEN')
|
|
})
|
|
|
|
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('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 repository secrets against the repo and never returns values', async () => {
|
|
const repoId = seedRepo(db)
|
|
|
|
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)
|
|
|
|
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)
|
|
})
|
|
})
|