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');
|
||||
}
|
||||
});
|
||||
});
|
||||
+24
-4
@@ -2,7 +2,7 @@ import { LoggingLevel, LoggingLevelSchema } from '@modelcontextprotocol/sdk/type
|
||||
import { Command } from 'commander';
|
||||
import dotenv from 'dotenv';
|
||||
import tools from './tools/index.js';
|
||||
import { parsePort } from './utils.js';
|
||||
import { parsePort, readBraveApiKeyFromFile } from './utils.js';
|
||||
|
||||
dotenv.config({ debug: false, quiet: true });
|
||||
|
||||
@@ -48,6 +48,11 @@ export function isToolPermittedByUser(toolName: string): boolean {
|
||||
export function getOptions(): Configuration | false {
|
||||
const program = new Command()
|
||||
.option('--brave-api-key <string>', 'Brave API key', process.env.BRAVE_API_KEY ?? '')
|
||||
.option(
|
||||
'--brave-api-key-file <string>',
|
||||
'Path to file containing Brave API key',
|
||||
process.env.BRAVE_API_KEY_FILE ?? ''
|
||||
)
|
||||
.option('--logging-level <string>', 'Logging level', process.env.BRAVE_MCP_LOG_LEVEL ?? 'info')
|
||||
.option(
|
||||
'--transport <stdio|http>',
|
||||
@@ -118,9 +123,23 @@ export function getOptions(): Configuration | false {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!options.braveApiKey) {
|
||||
const apiKeyFile =
|
||||
typeof options.braveApiKeyFile === 'string' ? options.braveApiKeyFile.trim() : '';
|
||||
let braveApiKey = typeof options.braveApiKey === 'string' ? options.braveApiKey.trim() : '';
|
||||
|
||||
if (apiKeyFile) {
|
||||
const apiKeyFromFile = readBraveApiKeyFromFile(apiKeyFile);
|
||||
if (!apiKeyFromFile.ok) {
|
||||
console.error(`Error: ${apiKeyFromFile.error}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
braveApiKey = apiKeyFromFile.key;
|
||||
}
|
||||
|
||||
if (!braveApiKey) {
|
||||
console.error(
|
||||
'Error: --brave-api-key is required. You can get one at https://brave.com/search/api/.'
|
||||
'Error: A Brave API key is required via --brave-api-key, BRAVE_API_KEY, --brave-api-key-file, or BRAVE_API_KEY_FILE. You can get one at https://brave.com/search/api/.'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -143,9 +162,10 @@ export function getOptions(): Configuration | false {
|
||||
|
||||
// Normalize stateless to boolean (CLI passes it as string)
|
||||
options.stateless = options.stateless === true || options.stateless === 'true';
|
||||
options.braveApiKey = braveApiKey;
|
||||
|
||||
// Update state
|
||||
state.braveApiKey = options.braveApiKey;
|
||||
state.braveApiKey = braveApiKey;
|
||||
state.transport = options.transport;
|
||||
state.port = options.port;
|
||||
state.host = options.host;
|
||||
|
||||
+41
-1
@@ -1,6 +1,9 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { describe, it } from 'node:test';
|
||||
import { parsePort } from './utils.js';
|
||||
import { parsePort, readBraveApiKeyFromFile } from './utils.js';
|
||||
|
||||
describe('parsePort', () => {
|
||||
it('accepts valid integer ports', () => {
|
||||
@@ -22,3 +25,40 @@ describe('parsePort', () => {
|
||||
assert.equal(parsePort(undefined), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readBraveApiKeyFromFile', () => {
|
||||
it('reads and trims API keys from a file', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
|
||||
const filePath = join(dir, 'key.txt');
|
||||
writeFileSync(filePath, 'test-api-key\n');
|
||||
|
||||
const result = readBraveApiKeyFromFile(filePath);
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
if (result.ok) {
|
||||
assert.equal(result.key, 'test-api-key');
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects empty files', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
|
||||
const filePath = join(dir, 'empty.txt');
|
||||
writeFileSync(filePath, '\n');
|
||||
|
||||
const result = readBraveApiKeyFromFile(filePath);
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
if (!result.ok) {
|
||||
assert.match(result.error, /empty/i);
|
||||
}
|
||||
});
|
||||
|
||||
it('reports missing files with the path and reason', () => {
|
||||
const result = readBraveApiKeyFromFile('definitely-not-a-real-api-key-file');
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
if (!result.ok) {
|
||||
assert.match(result.error, /definitely-not-a-real-api-key-file/);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { RATE_LIMIT } from './constants.js';
|
||||
|
||||
let requestCount = {
|
||||
@@ -23,6 +24,24 @@ export function stringify(data: any, pretty = false) {
|
||||
return pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
|
||||
}
|
||||
|
||||
export type ReadBraveApiKeyFromFileResult =
|
||||
| { ok: true; key: string }
|
||||
| { ok: false; error: string };
|
||||
|
||||
export function readBraveApiKeyFromFile(filePath: string): ReadBraveApiKeyFromFileResult {
|
||||
try {
|
||||
const key = readFileSync(filePath, 'utf8').trim();
|
||||
if (key.length === 0) {
|
||||
return { ok: false, error: `API key file is empty: ${filePath}` };
|
||||
}
|
||||
|
||||
return { ok: true, key };
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
return { ok: false, error: `Unable to read API key file '${filePath}': ${reason}` };
|
||||
}
|
||||
}
|
||||
|
||||
export function parsePort(value: unknown): number | null {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user