Files
opencode-manager/backend/src/services/secrets.ts
T
Chris Scott e0331b43bf feat(secrets): repository secrets and variables injected into agent shells and terminals
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.
2026-10-23 22:59:57 -04:00

124 lines
3.8 KiB
TypeScript

import type { Database } from 'bun:sqlite'
import type { SecretEntry, SecretKind } from '@opencode-manager/shared/schemas'
import { decryptSecret, encryptSecret } from '../utils/crypto'
import { logger } from '../utils/logger'
export class SecretsServiceError extends Error {
constructor(message: string, public statusCode: number = 500) {
super(message)
this.name = 'SecretsServiceError'
}
}
interface SecretRow {
kind: SecretKind
repo_id: number | null
environment: string
name: string
value: string
updated_at: number
}
interface SecretValueRow {
kind: SecretKind
repo_id: number | null
environment: string
name: string
value: string
}
export interface ResolvedSecretValues {
secrets: Record<string, string>
vars: Record<string, string>
}
export class SecretsService {
constructor(private readonly db: Database) {}
list(kind: SecretKind, repoId: number | null): SecretEntry[] {
return this.selectRows(kind, repoId).map((row) => this.toEntry(kind, row))
}
set(kind: SecretKind, repoId: number | null, environment: string | null, name: string, value: string): void {
const normalizedEnvironment = environment ?? ''
if (repoId === null && normalizedEnvironment !== '') {
throw new SecretsServiceError('Environments are only supported for repository secrets', 400)
}
const now = Date.now()
const storedValue = kind === 'secret' ? encryptSecret(value) : value
this.db
.prepare(
`INSERT INTO secrets (kind, repo_id, environment, name, value, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
)
.run(kind, repoId, normalizedEnvironment, name, storedValue, now, now)
}
delete(kind: SecretKind, repoId: number | null, environment: string | null, name: string): boolean {
const result = this.db
.prepare('DELETE FROM secrets WHERE kind = ? AND repo_id IS ? AND environment = ? AND name = ?')
.run(kind, repoId, environment ?? '', name)
return result.changes > 0
}
resolveValues(repoId: number, environment: string | null): ResolvedSecretValues {
const rows = this.db
.prepare(
`SELECT kind, repo_id, environment, name, value FROM secrets
WHERE repo_id IS NULL OR repo_id = ?
ORDER BY CASE WHEN repo_id IS NULL THEN 0 WHEN environment = '' THEN 1 ELSE 2 END`,
)
.all(repoId) as SecretValueRow[]
const secrets: Record<string, string> = {}
const vars: Record<string, string> = {}
for (const row of rows) {
if (row.repo_id !== null && row.environment !== '' && row.environment !== environment) {
continue
}
if (row.kind === 'secret') {
try {
secrets[row.name] = decryptSecret(row.value)
} catch {
logger.warn(`Skipping secret "${row.name}" because it could not be decrypted`)
}
} else {
vars[row.name] = row.value
}
}
return { secrets, vars }
}
private selectRows(kind: SecretKind, repoId: number | null): SecretRow[] {
if (repoId === null) {
return this.db
.prepare(
'SELECT kind, repo_id, environment, name, value, updated_at FROM secrets WHERE kind = ? AND repo_id IS NULL ORDER BY name',
)
.all(kind) as SecretRow[]
}
return this.db
.prepare(
'SELECT kind, repo_id, environment, name, value, updated_at FROM secrets WHERE kind = ? AND repo_id = ? ORDER BY name',
)
.all(kind, repoId) as SecretRow[]
}
private toEntry(kind: SecretKind, row: SecretRow): SecretEntry {
return {
name: row.name,
environment: row.environment === '' ? null : row.environment,
updatedAt: row.updated_at,
...(kind === 'variable' ? { value: row.value } : {}),
}
}
}