refactor(credentials): introduce /credentials subresource pattern

Replace the legacy "redacted placeholder + Clear* boolean" pattern with
dedicated per-resource credential subresources across MCP services,
Models, Web search providers, and Data sources.

Why
---
The previous design had three problems:

1. Main PUT body carried secret fields. The frontend echoed back a
   redacted "***" placeholder, and a fragile MergeUpdate / IsRedactedOrEmpty
   defense in the service layer tried to detect "user did not change this"
   vs "user wants to clear this". A regression in that defense (or a new
   frontend forgetting it) silently overwrites the stored secret with the
   placeholder.

2. The "remove this credential" UX was a red checkbox under a pre-filled
   password input. Three intents (preserve / replace / clear) collapsed
   onto one field, and credential changes were bundled with unrelated
   config edits in the same submit. Users wiped working keys by mistake.

3. Secret presence was inferred from "did the response come back with a
   '***' placeholder", which couples the contract to a magic string.

Design
------
Each of the four resources now exposes:

  PUT    /{resource}/{id}/credentials          # write one or more fields
  DELETE /{resource}/{id}/credentials/{field}  # clear a single field

"Is this configured?" metadata travels on the main resource response as
a typed map (dto.MCPServiceResponse.Credentials etc.) — no separate GET
endpoint. The frontend reads the configured boolean from the main GET
and never sees secret values at all.

Main PUT endpoints now ignore any secret fields in the request body and
log a deprecation warning if they appear, so legacy clients fail loudly
rather than overwriting silently.

Frontend
--------
- New reusable <CredentialResource> component renders a three-state card
  (unconfigured / configured / editing) and drives the dedicated
  endpoints. Used by MCP, Model, Web search; DataSource has a bespoke
  card with the same behaviour because its credentials are a single
  atomic per-connector map.
- Cancel from edit mode now restores state synchronously from the meta
  prop. The previous async refresh() was a no-op while state was still
  'editing', leaving the input frozen open.
- Remove is single-click + toast. The danger-themed button is the
  deterrent; a modal confirm adds friction without adding safety (the
  plaintext is irrecoverable client-side either way — recovery means
  re-typing).

DTOs (internal/handler/dto/) are deliberately separate from the GORM
models so "no secret in response" is a compile-time invariant: a future
contributor cannot leak a secret without explicitly adding the field to
the DTO, which is review-able in a single diff.

Storage is unchanged — credentials still live in the existing jsonb /
parameters columns. No schema migration.

Cleanup
-------
- types.MCPService / types.Model / types.WebSearchProviderEntity /
  types.DataSourceConfig: drop ClearAPIKey / ClearToken / ClearAppSecret
  / ClearCredentials boolean fields, MergeUpdate(), RedactSensitiveData().
- utils/types/secret.go: drop PreserveIfRedacted / IsRedactedOrEmpty.
  RedactedSecretPlaceholder constant is retained because VectorStore
  still uses the old pattern and is out of scope here.
- Frontend hasExistingApiKey / clearApiKey / convertToLegacyFormat
  redaction handling removed; i18n keys renamed secret -> credential.
This commit is contained in:
wizardchen
2026-05-17 15:09:30 +08:00
committed by lyingbug
parent f30e9ccd0f
commit 7643dd457e
53 changed files with 3271 additions and 2347 deletions
+27
View File
@@ -17,6 +17,9 @@ export interface DataSource {
last_sync_at: string | null
last_sync_result: any
error_message: string
// Single-field "credentials" map from the main response — DataSource
// credentials are a per-connector atomic set.
credentials?: { credentials: { configured: boolean } }
created_at: string
updated_at: string
latest_sync_log?: SyncLog
@@ -111,3 +114,27 @@ export function resumeDataSource(id: string) {
export function getSyncLogs(id: string, limit = 20, offset = 0) {
return get(`/api/v1/datasource/${id}/logs?limit=${limit}&offset=${offset}`)
}
// ----------------------------------------------------------------------------
// Data source credential subresource. Unlike the other three resources,
// DataSource exposes a single logical field "credentials" because connector
// auth is a per-connector atomic map. See internal/handler/dto/datasource.go.
// ----------------------------------------------------------------------------
export interface DataSourceCredentialsResponse {
fields: {
credentials: { configured: boolean }
}
}
export async function putDataSourceCredentials(
id: string,
credentials: Record<string, unknown>,
): Promise<DataSourceCredentialsResponse> {
const response: any = await put(`/api/v1/datasource/${id}/credentials`, { credentials })
return (response.data ?? response) as DataSourceCredentialsResponse
}
export async function deleteDataSourceCredentials(id: string): Promise<void> {
await del(`/api/v1/datasource/${id}/credentials/credentials`)
}
+43 -4
View File
@@ -10,13 +10,13 @@ export interface MCPService {
url?: string // Optional: required for SSE/HTTP Streamable
headers?: Record<string, string>
auth_config?: {
// Secret fields (api_key, token) are NEVER returned by the server in
// this shape — they live behind the /credentials subresource. The
// optional-property typing remains so create-mode payloads can still
// carry them in the initial POST body.
api_key?: string
token?: string
custom_headers?: Record<string, string>
// Write-only flags — set to true in Update requests to explicitly remove
// the corresponding credential. The server never returns these fields.
clear_api_key?: boolean
clear_token?: boolean
}
advanced_config?: {
timeout?: number
@@ -29,6 +29,10 @@ export interface MCPService {
}
env_vars?: Record<string, string> // Environment variables for stdio transport
is_builtin?: boolean // Whether this is a builtin MCP service
// Per-field "configured?" map embedded on the main response (server-side
// dto.MCPServiceResponse.Credentials). Drives the CredentialResource card
// without a follow-up GET. Absent for builtin services.
credentials?: Record<McpCredentialField, CredentialFieldMetadata>
created_at?: string
updated_at?: string
}
@@ -127,6 +131,41 @@ export async function setMCPToolApproval(serviceId: string, toolName: string, re
})
}
// ----------------------------------------------------------------------------
// Credential subresource (issue #988 follow-up).
//
// Secrets travel through a dedicated /credentials endpoint instead of the
// main MCP PUT body. "Is this configured?" metadata is embedded on the main
// MCPService response (MCPService.credentials), so there is no GET on this
// endpoint — only PUT (write) and DELETE (clear). Both trigger an MCP
// client reconnect server-side.
// ----------------------------------------------------------------------------
export type McpCredentialField = 'api_key' | 'token'
export interface CredentialFieldMetadata {
configured: boolean
}
export interface McpCredentialsResponse {
fields: Record<McpCredentialField, CredentialFieldMetadata>
}
export async function putMCPCredentials(
serviceId: string,
body: Partial<Record<McpCredentialField, string>>
): Promise<McpCredentialsResponse> {
const response: any = await put(`/api/v1/mcp-services/${serviceId}/credentials`, body)
return (response.data ?? response) as McpCredentialsResponse
}
export async function deleteMCPCredentialField(
serviceId: string,
field: McpCredentialField
): Promise<void> {
await del(`/api/v1/mcp-services/${serviceId}/credentials/${field}`)
}
export async function resolveToolApproval(
pendingId: string,
body: { decision: 'approve' | 'reject'; modified_args?: Record<string, unknown>; reason?: string }
+33 -4
View File
@@ -27,15 +27,18 @@ export interface ModelConfig {
custom_headers?: Record<string, string>;
supports_vision?: boolean; // Whether the model accepts image/multimodal input
app_id?: string;
// Secret fields (api_key, app_secret) are never returned by the server in
// this shape — they live behind the /credentials subresource. They are
// kept on the type so create-mode payloads can still carry them in the
// initial POST body.
app_secret?: string;
// Write-only flags — set to true in Update requests to explicitly remove
// the corresponding credential. The server never returns these fields.
clear_api_key?: boolean;
clear_app_secret?: boolean;
};
is_default?: boolean;
is_builtin?: boolean;
status?: string;
// Per-field configured? metadata from the main response. Absent for
// builtin models.
credentials?: Record<ModelCredentialField, { configured: boolean }>;
created_at?: string;
updated_at?: string;
deleted_at?: string | null;
@@ -135,6 +138,32 @@ export function deleteModel(id: string): Promise<void> {
});
}
// ----------------------------------------------------------------------------
// Model credential subresource. See mcp-service.ts for the matching MCP API
// shape and the design notes in internal/handler/dto/mcp.go.
// ----------------------------------------------------------------------------
export type ModelCredentialField = 'api_key' | 'app_secret'
export interface ModelCredentialsResponse {
fields: Record<ModelCredentialField, { configured: boolean }>
}
export async function putModelCredentials(
id: string,
body: Partial<Record<ModelCredentialField, string>>,
): Promise<ModelCredentialsResponse> {
const response: any = await put(`/api/v1/models/${id}/credentials`, body)
return (response.data ?? response) as ModelCredentialsResponse
}
export async function deleteModelCredentialField(
id: string,
field: ModelCredentialField,
): Promise<void> {
await del(`/api/v1/models/${id}/credentials/${field}`)
}
export interface InitializeWeKnoraCloudRequest {
app_id: string
app_secret: string
+30 -3
View File
@@ -8,16 +8,18 @@ export interface WebSearchProviderEntity {
provider: 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama' | 'baidu' | 'searxng'
description?: string
parameters: {
// api_key is never returned by the server in this shape; it lives behind
// the /credentials subresource. Kept on the type so the initial create
// POST can still include it.
api_key?: string
engine_id?: string
base_url?: string
proxy_url?: string
extra_config?: Record<string, string>
// Write-only flag — set to true in Update requests to explicitly remove
// the stored API key. The server never returns this field.
clear_api_key?: boolean
}
is_default?: boolean
// Per-field configured? metadata from the main response.
credentials?: Record<WebSearchCredentialField, { configured: boolean }>
created_at?: string
updated_at?: string
}
@@ -69,6 +71,31 @@ export function listWebSearchProviderTypes(): Promise<WebSearchProviderTypeInfo[
})
}
// ----------------------------------------------------------------------------
// Web search provider credential subresource.
// ----------------------------------------------------------------------------
export type WebSearchCredentialField = 'api_key'
export interface WebSearchCredentialsResponse {
fields: Record<WebSearchCredentialField, { configured: boolean }>
}
export async function putWebSearchProviderCredentials(
id: string,
body: Partial<Record<WebSearchCredentialField, string>>,
): Promise<WebSearchCredentialsResponse> {
const response: any = await put(`/api/v1/web-search-providers/${id}/credentials`, body)
return (response.data ?? response) as WebSearchCredentialsResponse
}
export async function deleteWebSearchProviderCredentialField(
id: string,
field: WebSearchCredentialField,
): Promise<void> {
await del(`/api/v1/web-search-providers/${id}/credentials/${field}`)
}
// Test a web search provider connection.
// If id is provided, tests the existing saved provider.
// If data is provided, tests with raw credentials (no persistence).