mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-02 18:18:43 +00:00
fix: apply api-key updates during token regeneration
This commit is contained in:
@@ -71,25 +71,19 @@ async function showAuthStatus(showUnmasked: boolean): Promise<void> {
|
|||||||
export async function handleTokensCommand(args: string[]): Promise<number> {
|
export async function handleTokensCommand(args: string[]): Promise<number> {
|
||||||
await initUI();
|
await initUI();
|
||||||
|
|
||||||
// Parse flags
|
|
||||||
const showFlag = args.includes('--show');
|
const showFlag = args.includes('--show');
|
||||||
const resetFlag = args.includes('--reset');
|
const resetFlag = args.includes('--reset');
|
||||||
const regenerateSecretFlag = args.includes('--regenerate-secret');
|
const regenerateSecretFlag = args.includes('--regenerate-secret');
|
||||||
const helpFlag = args.includes('--help') || args.includes('-h');
|
const helpFlag = args.includes('--help') || args.includes('-h');
|
||||||
|
|
||||||
// Find --api-key value
|
|
||||||
const apiKeyIndex = args.indexOf('--api-key');
|
const apiKeyIndex = args.indexOf('--api-key');
|
||||||
|
const hasApiKeyFlag = apiKeyIndex !== -1;
|
||||||
const apiKeyValue = apiKeyIndex !== -1 ? args[apiKeyIndex + 1] : undefined;
|
const apiKeyValue = apiKeyIndex !== -1 ? args[apiKeyIndex + 1] : undefined;
|
||||||
|
|
||||||
// Find --secret value
|
|
||||||
const secretIndex = args.indexOf('--secret');
|
const secretIndex = args.indexOf('--secret');
|
||||||
|
const hasSecretFlag = secretIndex !== -1;
|
||||||
const secretValue = secretIndex !== -1 ? args[secretIndex + 1] : undefined;
|
const secretValue = secretIndex !== -1 ? args[secretIndex + 1] : undefined;
|
||||||
|
|
||||||
// Find --variant value
|
|
||||||
const variantIndex = args.indexOf('--variant');
|
const variantIndex = args.indexOf('--variant');
|
||||||
const variantValue = variantIndex !== -1 ? args[variantIndex + 1] : undefined;
|
const variantValue = variantIndex !== -1 ? args[variantIndex + 1] : undefined;
|
||||||
|
|
||||||
// Help
|
|
||||||
if (helpFlag) {
|
if (helpFlag) {
|
||||||
console.log(header('CCS Tokens Management'));
|
console.log(header('CCS Tokens Management'));
|
||||||
console.log('');
|
console.log('');
|
||||||
@@ -124,40 +118,45 @@ export async function handleTokensCommand(args: string[]): Promise<number> {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset to defaults
|
|
||||||
if (resetFlag) {
|
if (resetFlag) {
|
||||||
resetAuthToDefaults();
|
resetAuthToDefaults();
|
||||||
// Regenerate CLIProxy config to apply changes
|
|
||||||
regenerateConfig();
|
regenerateConfig();
|
||||||
console.log(ok('Auth tokens reset to defaults'));
|
console.log(ok('Auth tokens reset to defaults'));
|
||||||
console.log(info('CLIProxy config regenerated'));
|
console.log(info('CLIProxy config regenerated'));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regenerate management secret
|
if (regenerateSecretFlag && hasSecretFlag) {
|
||||||
|
console.error(fail('Cannot combine --secret with --regenerate-secret'));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasApiKeyFlag && (!apiKeyValue || apiKeyValue.startsWith('-'))) {
|
||||||
|
console.error(fail('Missing value for --api-key'));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSecretFlag && (!secretValue || secretValue.startsWith('-'))) {
|
||||||
|
console.error(fail('Missing value for --secret'));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let updated = false;
|
||||||
|
|
||||||
if (regenerateSecretFlag) {
|
if (regenerateSecretFlag) {
|
||||||
const newSecret = generateSecureToken(32);
|
const newSecret = generateSecureToken(32);
|
||||||
setGlobalManagementSecret(newSecret);
|
setGlobalManagementSecret(newSecret);
|
||||||
// Regenerate CLIProxy config to apply changes
|
|
||||||
regenerateConfig();
|
|
||||||
console.log(ok('New management secret generated'));
|
console.log(ok('New management secret generated'));
|
||||||
console.log(` Secret: ${maskToken(newSecret)}`);
|
console.log(` Secret: ${maskToken(newSecret)}`);
|
||||||
console.log(info('CLIProxy config regenerated'));
|
updated = true;
|
||||||
console.log(warn('Restart CLIProxy to apply: ccs cliproxy restart'));
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set API key
|
if (hasApiKeyFlag) {
|
||||||
if (apiKeyValue !== undefined) {
|
const resolvedApiKey = apiKeyValue;
|
||||||
if (!apiKeyValue || apiKeyValue.startsWith('-')) {
|
|
||||||
console.error(fail('Missing value for --api-key'));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (variantValue) {
|
if (variantValue) {
|
||||||
// Per-variant API key
|
|
||||||
try {
|
try {
|
||||||
setVariantApiKey(variantValue, apiKeyValue);
|
setVariantApiKey(variantValue, resolvedApiKey);
|
||||||
console.log(ok(`API key set for variant '${variantValue}'`));
|
console.log(ok(`API key set for variant '${variantValue}'`));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const error = err instanceof Error ? err.message : 'Unknown error';
|
const error = err instanceof Error ? err.message : 'Unknown error';
|
||||||
@@ -165,35 +164,25 @@ export async function handleTokensCommand(args: string[]): Promise<number> {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Global API key
|
setGlobalApiKey(resolvedApiKey);
|
||||||
setGlobalApiKey(apiKeyValue);
|
|
||||||
console.log(ok('Global API key updated'));
|
console.log(ok('Global API key updated'));
|
||||||
}
|
}
|
||||||
|
updated = true;
|
||||||
// Regenerate CLIProxy config to apply changes
|
|
||||||
regenerateConfig();
|
|
||||||
console.log(info('CLIProxy config regenerated'));
|
|
||||||
console.log(warn('Restart CLIProxy to apply: ccs cliproxy restart'));
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set management secret
|
if (hasSecretFlag) {
|
||||||
if (secretValue !== undefined) {
|
|
||||||
if (!secretValue || secretValue.startsWith('-')) {
|
|
||||||
console.error(fail('Missing value for --secret'));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
setGlobalManagementSecret(secretValue);
|
setGlobalManagementSecret(secretValue);
|
||||||
// Regenerate CLIProxy config to apply changes
|
|
||||||
regenerateConfig();
|
|
||||||
console.log(ok('Management secret updated'));
|
console.log(ok('Management secret updated'));
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
regenerateConfig();
|
||||||
console.log(info('CLIProxy config regenerated'));
|
console.log(info('CLIProxy config regenerated'));
|
||||||
console.log(warn('Restart CLIProxy to apply: ccs cliproxy restart'));
|
console.log(warn('Restart CLIProxy to apply: ccs cliproxy restart'));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default: show status
|
|
||||||
await showAuthStatus(showFlag);
|
await showAuthStatus(showFlag);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as os from 'os';
|
||||||
|
import * as path from 'path';
|
||||||
|
import { getCliproxyConfigPath } from '../../../src/cliproxy';
|
||||||
|
import { handleTokensCommand } from '../../../src/commands/tokens-command';
|
||||||
|
import { getConfigYamlPath, loadUnifiedConfig } from '../../../src/config/unified-config-loader';
|
||||||
|
|
||||||
|
describe('tokens command auth rotation', () => {
|
||||||
|
let tempHome = '';
|
||||||
|
let logLines: string[] = [];
|
||||||
|
let errorLines: string[] = [];
|
||||||
|
let originalCcsHome: string | undefined;
|
||||||
|
let originalNoColor: string | undefined;
|
||||||
|
let originalConsoleLog: typeof console.log;
|
||||||
|
let originalConsoleError: typeof console.error;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-tokens-rotation-'));
|
||||||
|
logLines = [];
|
||||||
|
errorLines = [];
|
||||||
|
originalCcsHome = process.env.CCS_HOME;
|
||||||
|
originalNoColor = process.env.NO_COLOR;
|
||||||
|
originalConsoleLog = console.log;
|
||||||
|
originalConsoleError = console.error;
|
||||||
|
|
||||||
|
process.env.CCS_HOME = tempHome;
|
||||||
|
process.env.NO_COLOR = '1';
|
||||||
|
console.log = (...args: unknown[]) => {
|
||||||
|
logLines.push(args.map(String).join(' '));
|
||||||
|
};
|
||||||
|
console.error = (...args: unknown[]) => {
|
||||||
|
errorLines.push(args.map(String).join(' '));
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
if (originalCcsHome !== undefined) process.env.CCS_HOME = originalCcsHome;
|
||||||
|
else delete process.env.CCS_HOME;
|
||||||
|
|
||||||
|
if (originalNoColor !== undefined) process.env.NO_COLOR = originalNoColor;
|
||||||
|
else delete process.env.NO_COLOR;
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
fs.rmSync(tempHome, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('applies api-key and regenerated secret in a single invocation', async () => {
|
||||||
|
const exitCode = await handleTokensCommand([
|
||||||
|
'--api-key',
|
||||||
|
'ccs-custom-key-123',
|
||||||
|
'--regenerate-secret',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(exitCode).toBe(0);
|
||||||
|
expect(errorLines).toHaveLength(0);
|
||||||
|
expect(logLines.some((line) => line.includes('New management secret generated'))).toBe(true);
|
||||||
|
expect(logLines.some((line) => line.includes('Global API key updated'))).toBe(true);
|
||||||
|
expect(logLines.filter((line) => line.includes('CLIProxy config regenerated'))).toHaveLength(1);
|
||||||
|
|
||||||
|
const config = loadUnifiedConfig();
|
||||||
|
const managementSecret = config?.cliproxy.auth?.management_secret;
|
||||||
|
expect(config?.cliproxy.auth?.api_key).toBe('ccs-custom-key-123');
|
||||||
|
expect(typeof managementSecret).toBe('string');
|
||||||
|
expect((managementSecret ?? '').length).toBeGreaterThan(20);
|
||||||
|
|
||||||
|
const cliproxyConfig = fs.readFileSync(getCliproxyConfigPath(), 'utf8');
|
||||||
|
expect(cliproxyConfig).toContain('"ccs-custom-key-123"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects conflicting manual and generated secret flags', async () => {
|
||||||
|
const exitCode = await handleTokensCommand([
|
||||||
|
'--secret',
|
||||||
|
'manual-secret',
|
||||||
|
'--regenerate-secret',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(exitCode).toBe(1);
|
||||||
|
expect(
|
||||||
|
errorLines.some((line) => line.includes('Cannot combine --secret with --regenerate-secret'))
|
||||||
|
).toBe(true);
|
||||||
|
expect(fs.existsSync(getConfigYamlPath())).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user