refactor(server): move the config.json provider-key copy out of VaultStore (P1.2)

VaultStore.migrateFromConfig had two callers, both in the server. It is now
copyProviderKeysToVault in provider-env.ts, next to
migrateLegacyProviderKeysToVault, with the same rules: copy keys that have a
value, keep keys the vault already holds, store models and baseUrl as metadata.
Its tests moved from core to provider-env.test.ts.
This commit is contained in:
shumeiko.it
2026-10-24 16:40:39 +02:00
parent 3528a242f6
commit 2226ec1bd7
5 changed files with 65 additions and 61 deletions

View File

@@ -327,20 +327,4 @@ export class VaultStore implements SecretStore {
const vault = this.readVault();
return name in vault;
}
/** 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;
let migrated = 0;
for (const [name, provider] of Object.entries(config.providers)) {
if (provider.apiKey && !this.has(name)) {
this.set(name, provider.apiKey, {
models: provider.models,
baseUrl: provider.baseUrl,
});
migrated++;
}
}
return migrated;
}
}

View File

@@ -171,47 +171,6 @@ describe('VaultStore', () => {
expect(entry!.value).toBe('sk-ant-shared-secret');
});
it('migration from config — providers migrated to vault', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const config = {
providers: {
anthropic: { apiKey: 'sk-ant-key-1', models: ['claude-sonnet-4-6'], baseUrl: undefined },
openai: { apiKey: 'sk-openai-key-1', models: ['gpt-4o'] },
},
};
const migrated = vault.migrateFromConfig(config);
expect(migrated).toBe(2);
const anthropic = vault.get('anthropic');
expect(anthropic).not.toBeNull();
expect(anthropic!.value).toBe('sk-ant-key-1');
expect(anthropic!.metadata?.models).toEqual(['claude-sonnet-4-6']);
const openai = vault.get('openai');
expect(openai).not.toBeNull();
expect(openai!.value).toBe('sk-openai-key-1');
});
it('migration skips existing — migrate twice, count stays same', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const config = {
providers: {
anthropic: { apiKey: 'sk-ant-key-1', models: ['claude-sonnet-4-6'] },
},
};
const first = vault.migrateFromConfig(config);
expect(first).toBe(1);
const second = vault.migrateFromConfig(config);
expect(second).toBe(0);
});
it('key file is generated on first use and reused', () => {
const dir = makeTempDir();
new VaultStore(dir);

View File

@@ -181,7 +181,7 @@ import { EventEmitter } from 'node:events';
import { LocalJobStore } from './job-store.js';
import { AgentRunRegistry } from './agent-run-registry.js';
import { ExecutorRegistry } from './executor-registry.js';
import { hydrateProviderEnvFromVault } from './provider-env.js';
import { copyProviderKeysToVault, hydrateProviderEnvFromVault } from './provider-env.js';
export interface LocalConfig {
port: number;
@@ -568,7 +568,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
const waggleConfig = new WaggleConfig(fullConfig.dataDir);
const configProviders = waggleConfig.getProviders();
if (Object.keys(configProviders).length > 0) {
const migrated = vault.migrateFromConfig({ providers: configProviders });
const migrated = copyProviderKeysToVault(vault, configProviders);
let scrubbed = 0;
for (const [name, provider] of Object.entries(configProviders)) {
if (!provider.apiKey) continue;

View File

@@ -1,4 +1,4 @@
import { WaggleConfig, type VaultStore } from '@waggle/core';
import { WaggleConfig, type ProviderEntry, type VaultStore } from '@waggle/core';
/** Provider credentials consumed by LiteLLM and provider SDKs. */
export const PROVIDER_ENV_NAMES: Record<string, readonly string[]> = {
@@ -68,11 +68,32 @@ export function hydrateProviderEnvFromVault(vault: VaultStore, overwrite = false
return updated;
}
/**
* Copy plaintext provider keys from config.json into the vault, keeping any key
* the vault already holds. Returns the number of keys copied.
*/
export function copyProviderKeysToVault(
vault: VaultStore,
providers: Readonly<Record<string, ProviderEntry>>,
): number {
let copied = 0;
for (const [name, provider] of Object.entries(providers)) {
if (provider.apiKey && !vault.has(name)) {
vault.set(name, provider.apiKey, {
models: provider.models,
baseUrl: provider.baseUrl,
});
copied++;
}
}
return copied;
}
/** Move legacy config.json secrets before LiteLLM starts, then scrub plaintext. */
export function migrateLegacyProviderKeysToVault(dataDir: string, vault: VaultStore): number {
const config = new WaggleConfig(dataDir);
const providers = config.getProviders();
const migrated = vault.migrateFromConfig({ providers });
const migrated = copyProviderKeysToVault(vault, providers);
let scrubbed = 0;
for (const [providerId, provider] of Object.entries(providers)) {

View File

@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it } from 'vitest';
import { VaultStore } from '@waggle/core';
import {
applyProviderKeyToEnv,
copyProviderKeysToVault,
hydrateProviderEnvFromVault,
migrateLegacyProviderKeysToVault,
} from '../../src/local/provider-env.js';
@@ -50,6 +51,45 @@ describe('provider environment hydration', () => {
}
});
it('copies config.json provider keys into the vault with their models and base URL', () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-provider-copy-'));
try {
const vault = new VaultStore(dataDir);
expect(copyProviderKeysToVault(vault, {
anthropic: { apiKey: 'sk-ant-key-1', models: ['claude-sonnet-4-6'], baseUrl: undefined },
openai: { apiKey: 'sk-openai-key-1', models: ['gpt-4o'], baseUrl: 'https://api.openai.com' },
empty: { apiKey: '', models: [] },
})).toBe(2);
expect(vault.get('anthropic')).toMatchObject({
value: 'sk-ant-key-1',
metadata: { models: ['claude-sonnet-4-6'] },
});
expect(vault.get('openai')).toMatchObject({
value: 'sk-openai-key-1',
metadata: { models: ['gpt-4o'], baseUrl: 'https://api.openai.com' },
});
expect(vault.has('empty')).toBe(false);
} finally {
fs.rmSync(dataDir, { recursive: true, force: true });
}
});
it('keeps a key the vault already holds', () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-provider-copy-'));
try {
const vault = new VaultStore(dataDir);
vault.set('anthropic', 'sk-ant-from-settings');
const providers = { anthropic: { apiKey: 'sk-ant-from-config', models: ['claude-sonnet-4-6'] } };
expect(copyProviderKeysToVault(vault, providers)).toBe(0);
expect(vault.get('anthropic')?.value).toBe('sk-ant-from-settings');
} finally {
fs.rmSync(dataDir, { recursive: true, force: true });
}
});
it('migrates and scrubs legacy keys before startup catalog discovery', () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-provider-migrate-'));
const configPath = path.join(dataDir, 'config.json');