feat(config): support reading Brave API key from a file (#313)
* feat: support reading Brave API key from a file * chore: add secret file template and gitignore local keys * docs: document optional Docker Compose secrets workflow * fix: trim whitespace API keys and exclude secrets from Docker build * docs: clarify container paths for BRAVE_API_KEY_FILE * test: cover API key file config and expand Docker build context ignores * test: isolate getOptions tests from local env vars * docs: clarify optional use of BRAVE_API_KEY_FILE * docs: document BRAVE_API_KEY_FILE in AWS Marketplace catalog template
This commit is contained in:
1 parent
e3f77ced06
commit
fd28e3e7b3
11 files changed
+225
-9
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { afterEach, beforeEach, describe, it } from 'node:test';
|
||||
import { getOptions } from './config.js';
|
||||
|
||||
const CONFIG_ENV_VARS = [
|
||||
'BRAVE_API_KEY',
|
||||
'BRAVE_API_KEY_FILE',
|
||||
'BRAVE_MCP_LOG_LEVEL',
|
||||
'BRAVE_MCP_TRANSPORT',
|
||||
'BRAVE_MCP_ENABLED_TOOLS',
|
||||
'BRAVE_MCP_DISABLED_TOOLS',
|
||||
'BRAVE_MCP_PORT',
|
||||
'BRAVE_MCP_HOST',
|
||||
'BRAVE_MCP_STATELESS',
|
||||
] as const;
|
||||
|
||||
describe('getOptions', () => {
|
||||
const originalArgv = process.argv;
|
||||
const originalEnv = Object.fromEntries(CONFIG_ENV_VARS.map((key) => [key, process.env[key]]));
|
||||
|
||||
beforeEach(() => {
|
||||
for (const key of CONFIG_ENV_VARS) {
|
||||
delete process.env[key];
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.argv = originalArgv;
|
||||
for (const key of CONFIG_ENV_VARS) {
|
||||
if (originalEnv[key] === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = originalEnv[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects whitespace-only API keys', () => {
|
||||
process.argv = ['node', 'index.js', '--brave-api-key', ' '];
|
||||
|
||||
assert.equal(getOptions(), false);
|
||||
});
|
||||
|
||||
it('reads API key from --brave-api-key-file', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
|
||||
const filePath = join(dir, 'key.txt');
|
||||
writeFileSync(filePath, 'from-file\n');
|
||||
|
||||
process.argv = ['node', 'index.js', '--brave-api-key-file', filePath];
|
||||
const options = getOptions();
|
||||
|
||||
assert.notEqual(options, false);
|
||||
if (options) {
|
||||
assert.equal(options.braveApiKey, 'from-file');
|
||||
}
|
||||
});
|
||||
|
||||
it('prefers file key over --brave-api-key', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
|
||||
const filePath = join(dir, 'key.txt');
|
||||
writeFileSync(filePath, 'from-file\n');
|
||||
|
||||
process.argv = [
|
||||
'node',
|
||||
'index.js',
|
||||
'--brave-api-key',
|
||||
'from-env',
|
||||
'--brave-api-key-file',
|
||||
filePath,
|
||||
];
|
||||
const options = getOptions();
|
||||
|
||||
assert.notEqual(options, false);
|
||||
if (options) {
|
||||
assert.equal(options.braveApiKey, 'from-file');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user