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:
shumeiko.it
2026-10-24 16:34:20 +02:00
parent 23431550ae
commit 3528a242f6
60 changed files with 602 additions and 606 deletions

View File

@@ -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,

View File

@@ -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>;

View File

@@ -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;

View File

@@ -229,102 +229,6 @@ describe('VaultStore', () => {
expect(keyHex2).toBe(keyHex);
});
it('setConnectorCredential — refresh token is NOT stored as plaintext in vault.json', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const refreshToken = 'rt-super-secret-refresh-token-xyz';
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_access_token_123',
refreshToken,
expiresAt: '2099-01-01T00:00:00Z',
scopes: ['repo', 'user'],
});
// Read raw vault.json and verify the refresh token is NOT in plaintext
const rawContent = fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8');
expect(rawContent).not.toContain(refreshToken);
// The metadata for the main connector entry must NOT contain refreshToken
const parsed = JSON.parse(rawContent);
const mainEntry = parsed['connector:github'];
expect(mainEntry).toBeDefined();
expect(mainEntry.metadata).not.toHaveProperty('refreshToken');
// The refresh token should be stored as a separate encrypted entry
const refreshEntry = parsed['connector:github:refresh'];
expect(refreshEntry).toBeDefined();
expect(refreshEntry.encrypted).toMatch(/^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$/);
});
it('getConnectorCredential — returns decrypted refresh token correctly', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const accessToken = 'gho_access_token_456';
const refreshToken = 'rt-secret-refresh-token-abc';
vault.setConnectorCredential('github', {
type: 'oauth2',
value: accessToken,
refreshToken,
expiresAt: '2099-01-01T00:00:00Z',
scopes: ['repo'],
});
const cred = vault.getConnectorCredential('github');
expect(cred).not.toBeNull();
expect(cred!.value).toBe(accessToken);
expect(cred!.refreshToken).toBe(refreshToken);
expect(cred!.type).toBe('oauth2');
expect(cred!.expiresAt).toBe('2099-01-01T00:00:00Z');
expect(cred!.scopes).toEqual(['repo']);
expect(cred!.isExpired).toBe(false);
});
it('setConnectorCredential — without refresh token does not create refresh entry', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
vault.setConnectorCredential('slack', {
type: 'bearer',
value: 'xoxb-token-123',
});
const rawContent = fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8');
const parsed = JSON.parse(rawContent);
expect(parsed['connector:slack']).toBeDefined();
expect(parsed['connector:slack:refresh']).toBeUndefined();
const cred = vault.getConnectorCredential('slack');
expect(cred).not.toBeNull();
expect(cred!.value).toBe('xoxb-token-123');
expect(cred!.refreshToken).toBeUndefined();
});
it('setConnectorCredential — clears refresh token when re-set without one', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
// First set with refresh token
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_token_1',
refreshToken: 'rt-old-refresh',
});
expect(vault.getConnectorCredential('github')!.refreshToken).toBe('rt-old-refresh');
// Re-set without refresh token
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_token_2',
});
const cred = vault.getConnectorCredential('github');
expect(cred!.value).toBe('gho_token_2');
expect(cred!.refreshToken).toBeUndefined();
expect(vault.has('connector:github:refresh')).toBe(false);
});
it('corrupted key file — truncated hex throws clear error', () => {
const dir = makeTempDir();
const keyPath = path.join(dir, '.vault-key');