refactor(agent): move the connector credential helpers out of VaultStore (P1.2)
getConnectorCredential, setConnectorCredential and setConnectorCredentialBundle are now plain functions over a SecretStore in packages/agent/src/connectors/connector-credentials.ts. The secret names, metadata layout and the rule that tokens never go into plaintext metadata are unchanged, so existing vault files keep working. A bundle still lands as one update: SecretStore gains writeBatch, which VaultStore applies with a single vault-file replacement. Connector tests seed a Map-backed SecretStore instead of mocking VaultStore methods; the helper tests from core and server moved to the agent package.
This commit is contained in:
@@ -70,7 +70,7 @@ export {
|
||||
type PendingActionRow, type PendingActionStatus, type SavePendingActionInput,
|
||||
} from './cron-store.js';
|
||||
export { VaultStore, type VaultEntry } from './vault.js';
|
||||
export type { SecretStore, SecretEntry, SecretSummary } from './secret-store.js';
|
||||
export type { SecretStore, SecretEntry, SecretSummary, SecretBatch, SecretWrite } from './secret-store.js';
|
||||
export { TelemetryStore, TelemetryCollector, TELEMETRY_EVENTS, type TelemetryEvent, type TelemetrySummary } from './telemetry.js';
|
||||
export {
|
||||
SkillHashStore, computeSkillHash, SKILL_HASHES_TABLE_SQL,
|
||||
|
||||
@@ -18,11 +18,29 @@ export interface SecretSummary {
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface SecretWrite {
|
||||
name: string;
|
||||
value: string;
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/** Writes and deletions that a store applies as one update. */
|
||||
export interface SecretBatch {
|
||||
set?: readonly SecretWrite[];
|
||||
delete?: readonly string[];
|
||||
}
|
||||
|
||||
export interface SecretStore {
|
||||
/** The decrypted secret, or null when it is missing or cannot be read. */
|
||||
get(name: string): SecretEntry | null;
|
||||
set(name: string, value: string, metadata?: Record<string, unknown>): void;
|
||||
setAsync(name: string, value: string, metadata?: Record<string, unknown>): Promise<void>;
|
||||
/**
|
||||
* Applies several writes and deletions as one update, so secrets that belong
|
||||
* together (a connector token and its companion values) are never left
|
||||
* half-written. Deletions apply before writes.
|
||||
*/
|
||||
writeBatch(batch: SecretBatch): void;
|
||||
/** Returns true when the secret existed. */
|
||||
delete(name: string): boolean;
|
||||
deleteAsync(name: string): Promise<boolean>;
|
||||
|
||||
@@ -12,7 +12,7 @@ import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { createCoreLogger } from './logger.js';
|
||||
import type { SecretEntry, SecretStore } from './secret-store.js';
|
||||
import type { SecretBatch, SecretEntry, SecretStore } from './secret-store.js';
|
||||
|
||||
const log = createCoreLogger('vault');
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
@@ -34,14 +34,6 @@ interface VaultRecord {
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
interface ConnectorCredentialInput {
|
||||
type: 'api_key' | 'oauth2' | 'bearer' | 'basic';
|
||||
value: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: string;
|
||||
scopes?: string[];
|
||||
}
|
||||
|
||||
/** The file-backed SecretStore. */
|
||||
export class VaultStore implements SecretStore {
|
||||
private dataDir: string;
|
||||
@@ -270,6 +262,19 @@ export class VaultStore implements SecretStore {
|
||||
this.set(name, value, metadata);
|
||||
}
|
||||
|
||||
/** Apply several writes and deletions with one vault-file replacement. */
|
||||
writeBatch(batch: SecretBatch): void {
|
||||
const vault = this.readVault();
|
||||
for (const name of batch.delete ?? []) {
|
||||
delete vault[name];
|
||||
}
|
||||
const updatedAt = new Date().toISOString();
|
||||
for (const { name, value, metadata } of batch.set ?? []) {
|
||||
vault[name] = { encrypted: this.encrypt(value), metadata, updatedAt };
|
||||
}
|
||||
this.writeVault(vault);
|
||||
}
|
||||
|
||||
/** Get a decrypted secret by name. Returns null if not found or decryption fails. */
|
||||
get(name: string): VaultEntry | null {
|
||||
const vault = this.readVault();
|
||||
@@ -323,83 +328,6 @@ export class VaultStore implements SecretStore {
|
||||
return name in vault;
|
||||
}
|
||||
|
||||
/** Set a connector credential with typed metadata. */
|
||||
setConnectorCredential(connectorId: string, credential: ConnectorCredentialInput): void {
|
||||
this.setConnectorCredentialBundle(connectorId, credential);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist a connector credential and its encrypted companion values with one
|
||||
* vault-file replacement. This prevents a multi-field credential (for
|
||||
* example Jira token + email + site origin) from being partially updated.
|
||||
*/
|
||||
setConnectorCredentialBundle(
|
||||
connectorId: string,
|
||||
credential: ConnectorCredentialInput,
|
||||
relatedSecrets: Readonly<Record<string, string>> = {},
|
||||
): void {
|
||||
const secretEntries = Object.entries(relatedSecrets);
|
||||
if (secretEntries.some(([suffix]) => !/^[a-z][a-z0-9_]*$/.test(suffix))) {
|
||||
throw new TypeError('Invalid connector credential suffix');
|
||||
}
|
||||
|
||||
const vault = this.readVault();
|
||||
const updatedAt = new Date().toISOString();
|
||||
vault[`connector:${connectorId}`] = {
|
||||
encrypted: this.encrypt(credential.value),
|
||||
metadata: {
|
||||
credentialType: credential.type,
|
||||
expiresAt: credential.expiresAt,
|
||||
scopes: credential.scopes,
|
||||
},
|
||||
updatedAt,
|
||||
};
|
||||
|
||||
const refreshKey = `connector:${connectorId}:refresh`;
|
||||
if (credential.refreshToken) {
|
||||
vault[refreshKey] = {
|
||||
encrypted: this.encrypt(credential.refreshToken),
|
||||
updatedAt,
|
||||
};
|
||||
} else {
|
||||
delete vault[refreshKey];
|
||||
}
|
||||
|
||||
for (const [suffix, value] of secretEntries) {
|
||||
vault[`connector:${connectorId}:${suffix}`] = {
|
||||
encrypted: this.encrypt(value),
|
||||
updatedAt,
|
||||
};
|
||||
}
|
||||
this.writeVault(vault);
|
||||
}
|
||||
|
||||
/** Get a connector credential with typed metadata */
|
||||
getConnectorCredential(connectorId: string): {
|
||||
value: string;
|
||||
type: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: string;
|
||||
scopes?: string[];
|
||||
isExpired: boolean;
|
||||
} | null {
|
||||
const entry = this.get(`connector:${connectorId}`);
|
||||
if (!entry) return null;
|
||||
const expiresAt = entry.metadata?.expiresAt as string | undefined;
|
||||
// Retrieve refresh token from its own encrypted entry
|
||||
const refreshEntry = this.get(`connector:${connectorId}:refresh`);
|
||||
return {
|
||||
value: entry.value,
|
||||
type: (entry.metadata?.credentialType as string) ?? 'api_key',
|
||||
// Review Major #7: removed dead plaintext metadata fallback that contradicted
|
||||
// the "never in plaintext metadata" security contract from setConnectorCredential.
|
||||
refreshToken: refreshEntry?.value,
|
||||
expiresAt,
|
||||
scopes: entry.metadata?.scopes as string[] | undefined,
|
||||
isExpired: expiresAt ? new Date(expiresAt) < new Date() : false,
|
||||
};
|
||||
}
|
||||
|
||||
/** Migrate plaintext providers from config.json to vault. Returns count migrated. */
|
||||
migrateFromConfig(config: { providers?: Record<string, { apiKey: string; models?: string[]; baseUrl?: string }> }): number {
|
||||
if (!config.providers) return 0;
|
||||
|
||||
Reference in New Issue
Block a user