Skip to content

Commit a018feb

Browse files
authored
Merge pull request #136 from drivecore/feature/api-keys-in-config
Add ability to store API keys in configuration
2 parents c23746d + 51f46bc commit a018feb

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

packages/cli/src/commands/$default.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,25 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
107107

108108
if (providerSettings) {
109109
const { keyName } = providerSettings;
110-
const apiKey = process.env[keyName];
110+
111+
// First check if the API key is in the config
112+
const configApiKey = userConfig[keyName as keyof typeof userConfig] as string;
113+
// Then fall back to environment variable
114+
const envApiKey = process.env[keyName];
115+
// Use config key if available, otherwise use env key
116+
const apiKey = configApiKey || envApiKey;
111117

112118
if (!apiKey) {
113119
logger.error(getProviderApiKeyError(userModelProvider));
114120
throw new Error(`${userModelProvider} API key not found`);
115121
}
122+
123+
// If we're using a key from config, set it as an environment variable
124+
// This ensures it's available to the provider libraries
125+
if (configApiKey && !envApiKey) {
126+
process.env[keyName] = configApiKey;
127+
logger.debug(`Using ${keyName} from configuration`);
128+
}
116129
}
117130
// No API key check needed for Ollama as it uses a local server
118131

packages/cli/src/commands/config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
7373
'$0 config clear customPrompt',
7474
'Reset customPrompt to default value',
7575
)
76+
.example(
77+
'$0 config set ANTHROPIC_API_KEY <your-key>',
78+
'Store your Anthropic API key in configuration',
79+
)
7680
.example(
7781
'$0 config clear --all',
7882
'Clear all configuration settings',
@@ -151,6 +155,30 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
151155
return;
152156
}
153157

158+
// Check if this is an API key and add a warning
159+
if (argv.key.includes('API_KEY')) {
160+
logger.warn(
161+
chalk.yellow(
162+
'Warning: Storing API keys in configuration is less secure than using environment variables.'
163+
)
164+
);
165+
logger.warn(
166+
chalk.yellow(
167+
'Your API key will be stored in plaintext in the configuration file.'
168+
)
169+
);
170+
171+
// Ask for confirmation
172+
const isConfirmed = await confirm(
173+
'Do you want to continue storing your API key in the configuration?'
174+
);
175+
176+
if (!isConfirmed) {
177+
logger.info('Operation cancelled.');
178+
return;
179+
}
180+
}
181+
154182
// Parse the value based on current type or infer boolean/number
155183
let parsedValue: string | boolean | number = argv.value;
156184

packages/cli/src/settings/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const defaultConfig = {
2020
customPrompt: '',
2121
profile: false,
2222
tokenCache: true,
23+
// API keys (empty by default)
24+
ANTHROPIC_API_KEY: '',
25+
OPENAI_API_KEY: '',
26+
XAI_API_KEY: '',
27+
MISTRAL_API_KEY: '',
2328
};
2429

2530
export type Config = typeof defaultConfig;

packages/cli/tests/settings/config.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ describe('Config', () => {
4949
profile: false,
5050
customPrompt: '',
5151
tokenCache: true,
52+
// API keys
53+
ANTHROPIC_API_KEY: '',
54+
OPENAI_API_KEY: '',
55+
XAI_API_KEY: '',
56+
MISTRAL_API_KEY: '',
5257
});
5358
expect(fs.existsSync).toHaveBeenCalledWith(mockConfigFile);
5459
});
@@ -86,6 +91,11 @@ describe('Config', () => {
8691
profile: false,
8792
customPrompt: '',
8893
tokenCache: true,
94+
// API keys
95+
ANTHROPIC_API_KEY: '',
96+
OPENAI_API_KEY: '',
97+
XAI_API_KEY: '',
98+
MISTRAL_API_KEY: '',
8999
});
90100
});
91101
});
@@ -117,4 +127,4 @@ describe('Config', () => {
117127
expect(result).toEqual({ githubMode: true, existingSetting: 'value' });
118128
});
119129
});
120-
});
130+
});

0 commit comments

Comments
 (0)