mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(servers): harden pairing and run linked-server sessions in the UI
Replace the plaintext pairing exchange with a nonce and HMAC proof plus AES-256-GCM encrypted credentials, bumping links to v2. Track session ownership per linked server so activity, session picker, transcript, MCP and skills views work against remote servers. Share encrypted secret access, event routing and URL helpers across backend, shared and CLI.
This commit is contained in:
1 parent
c591c15743
commit
f566556fd6
77 files changed
+3170
-702
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { OpenCodeServer } from '@opencode-manager/shared/schemas'
|
||||
import { decryptSecret, encryptSecret } from '../utils/crypto'
|
||||
import { logger } from '../utils/logger'
|
||||
import { trimUrlTrailingSlashes } from '@opencode-manager/shared/utils'
|
||||
import { deleteSecret, readEncryptedSecret, writeEncryptedSecret } from './secrets'
|
||||
|
||||
interface OpenCodeServerRow {
|
||||
id: string
|
||||
@@ -33,19 +33,18 @@ export interface OpenCodeServerUpdate {
|
||||
|
||||
const ROW_COLUMNS = 'id, name, base_url, username, enabled, created_at, updated_at'
|
||||
|
||||
const PASSWORD_SECRET_KEY_PREFIX = 'opencode_server:'
|
||||
const PASSWORD_SECRET_KEY_SUFFIX = ':password'
|
||||
|
||||
const SELECT_SERVER = `
|
||||
SELECT s.id, s.name, s.base_url, s.username, s.enabled, s.created_at, s.updated_at,
|
||||
CASE WHEN k.key IS NULL THEN 0 ELSE 1 END AS has_password
|
||||
FROM opencode_servers s
|
||||
LEFT JOIN app_secrets k ON k.key = 'opencode_server:' || s.id || ':password'
|
||||
LEFT JOIN app_secrets k ON k.key = '${PASSWORD_SECRET_KEY_PREFIX}' || s.id || '${PASSWORD_SECRET_KEY_SUFFIX}'
|
||||
`
|
||||
|
||||
export function normalizeOpenCodeServerBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.trim().replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
function passwordSecretKey(serverId: string): string {
|
||||
return `opencode_server:${serverId}:password`
|
||||
return `${PASSWORD_SECRET_KEY_PREFIX}${serverId}${PASSWORD_SECRET_KEY_SUFFIX}`
|
||||
}
|
||||
|
||||
function mapRow(row: OpenCodeServerRow): OpenCodeServer {
|
||||
@@ -76,29 +75,7 @@ export function getOpenCodeServer(db: Database, serverId: string): OpenCodeServe
|
||||
}
|
||||
|
||||
export function readOpenCodeServerPassword(db: Database, serverId: string): string | null {
|
||||
const row = db
|
||||
.prepare('SELECT value FROM app_secrets WHERE key = ?')
|
||||
.get(passwordSecretKey(serverId)) as { value: string } | undefined
|
||||
if (!row) return null
|
||||
|
||||
try {
|
||||
return decryptSecret(row.value)
|
||||
} catch (error) {
|
||||
logger.error(`Failed to decrypt the password for OpenCode server ${serverId}`, error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function setOpenCodeServerPassword(db: Database, serverId: string, password: string): void {
|
||||
const now = Date.now()
|
||||
db.prepare(`
|
||||
INSERT INTO app_secrets (key, value, created_at, updated_at) VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
|
||||
`).run(passwordSecretKey(serverId), encryptSecret(password), now, now)
|
||||
}
|
||||
|
||||
export function clearOpenCodeServerPassword(db: Database, serverId: string): void {
|
||||
db.prepare('DELETE FROM app_secrets WHERE key = ?').run(passwordSecretKey(serverId))
|
||||
return readEncryptedSecret(db, passwordSecretKey(serverId))
|
||||
}
|
||||
|
||||
export function insertOpenCodeServer(db: Database, input: OpenCodeServerInsert): OpenCodeServer {
|
||||
@@ -109,14 +86,14 @@ export function insertOpenCodeServer(db: Database, input: OpenCodeServerInsert):
|
||||
db.prepare(`INSERT INTO opencode_servers (${ROW_COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?)`).run(
|
||||
id,
|
||||
input.name,
|
||||
normalizeOpenCodeServerBaseUrl(input.baseUrl),
|
||||
trimUrlTrailingSlashes(input.baseUrl),
|
||||
input.username,
|
||||
input.enabled ? 1 : 0,
|
||||
now,
|
||||
now,
|
||||
)
|
||||
if (input.password) {
|
||||
setOpenCodeServerPassword(db, id, input.password)
|
||||
writeEncryptedSecret(db, passwordSecretKey(id), input.password)
|
||||
}
|
||||
return getOpenCodeServer(db, id)
|
||||
})
|
||||
@@ -145,7 +122,7 @@ export function updateOpenCodeServer(
|
||||
}
|
||||
if (patch.baseUrl !== undefined) {
|
||||
assignments.push('base_url = ?')
|
||||
values.push(normalizeOpenCodeServerBaseUrl(patch.baseUrl))
|
||||
values.push(trimUrlTrailingSlashes(patch.baseUrl))
|
||||
}
|
||||
if (patch.username !== undefined) {
|
||||
assignments.push('username = ?')
|
||||
@@ -162,9 +139,9 @@ export function updateOpenCodeServer(
|
||||
db.prepare(`UPDATE opencode_servers SET ${assignments.join(', ')} WHERE id = ?`).run(...values, serverId)
|
||||
|
||||
if (patch.password === null) {
|
||||
clearOpenCodeServerPassword(db, serverId)
|
||||
deleteSecret(db, passwordSecretKey(serverId))
|
||||
} else if (patch.password !== undefined) {
|
||||
setOpenCodeServerPassword(db, serverId, patch.password)
|
||||
writeEncryptedSecret(db, passwordSecretKey(serverId), patch.password)
|
||||
}
|
||||
|
||||
return getOpenCodeServer(db, serverId)
|
||||
@@ -176,7 +153,7 @@ export function updateOpenCodeServer(
|
||||
export function deleteOpenCodeServer(db: Database, serverId: string): boolean {
|
||||
const remove = db.transaction(() => {
|
||||
const result = db.prepare('DELETE FROM opencode_servers WHERE id = ?').run(serverId)
|
||||
clearOpenCodeServerPassword(db, serverId)
|
||||
deleteSecret(db, passwordSecretKey(serverId))
|
||||
return result.changes > 0
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user