Skip to content

Commit f35cfe0

Browse files
muodovclaude
andauthored
Pass ClickHouse credentials from the environment (#215)
ClickhouseReporter created its client with no credentials, so imports always connected as the server's default user. Read CLICKHOUSE_USER and CLICKHOUSE_PASSWORD, the variables clickhouse-client uses, and pass them on when both are set. Without them nothing changes. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d6fddc5 commit f35cfe0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎reporters/ClickhouseReporter.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ function santizeCallArgs(args) {
186186
return argsArray.map((/** @type {string} */ s) => s.replace(/'/g, ''));
187187
}
188188

189+
/**
190+
* Read ClickHouse credentials from the variables clickhouse-client uses. Without both, the client connects as the server's default user.
191+
* @param {NodeJS.ProcessEnv} env
192+
* @returns {{username?: string, password?: string}}
193+
*/
194+
function clickhouseCredentials(env) {
195+
if (!env.CLICKHOUSE_USER || !env.CLICKHOUSE_PASSWORD) {
196+
return {};
197+
}
198+
return { username: env.CLICKHOUSE_USER, password: env.CLICKHOUSE_PASSWORD };
199+
}
200+
189201
/**
190202
* Replace lone UTF-16 surrogates (e.g. from text truncated mid-emoji) with U+FFFD, as clickhouse rejects them in JSON input.
191203
* @param {string} str
@@ -286,6 +298,7 @@ class ClickhouseReporter extends BaseReporter {
286298
this.client = createClient({
287299
url: `http://${CLICKHOUSE_SERVER}:8123`,
288300
database: DB,
301+
...clickhouseCredentials(process.env),
289302
});
290303
this.crawlId = `${new Date().toISOString()}-${os.hostname()}`;
291304
this.ready = Promise.all(TABLE_DEFINITIONS.map((stmt) => this.client.query({ query: stmt })));
@@ -525,4 +538,5 @@ class ClickhouseReporter extends BaseReporter {
525538

526539
module.exports = ClickhouseReporter;
527540
module.exports.scrapedFramesToRows = scrapedFramesToRows;
541+
module.exports.clickhouseCredentials = clickhouseCredentials;
528542
module.exports.SCRAPED_FRAMES_TABLES = SCRAPED_FRAMES_TABLES;

‎tests/reporters/ClickhouseReporter.mocha.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const assert = require('assert');
22
const ClickhouseReporter = require('../../reporters/ClickhouseReporter');
3-
const { scrapedFramesToRows, SCRAPED_FRAMES_TABLES } = ClickhouseReporter;
3+
const { scrapedFramesToRows, clickhouseCredentials, SCRAPED_FRAMES_TABLES } = ClickhouseReporter;
44

55
describe('ClickhouseReporter scrapedFramesToRows', () => {
66
it('handles empty scrapedFrames', () => {
@@ -131,6 +131,21 @@ describe('ClickhouseReporter scrapedFramesToRows', () => {
131131
});
132132
});
133133

134+
describe('ClickhouseReporter clickhouseCredentials', () => {
135+
it('passes the user and password when both are set', () => {
136+
assert.deepStrictEqual(clickhouseCredentials({ CLICKHOUSE_USER: 'crawler', CLICKHOUSE_PASSWORD: 'secret' }), {
137+
username: 'crawler',
138+
password: 'secret',
139+
});
140+
});
141+
142+
it('passes nothing when either is missing', () => {
143+
assert.deepStrictEqual(clickhouseCredentials({}), {});
144+
assert.deepStrictEqual(clickhouseCredentials({ CLICKHOUSE_USER: 'crawler' }), {});
145+
assert.deepStrictEqual(clickhouseCredentials({ CLICKHOUSE_PASSWORD: 'secret' }), {});
146+
});
147+
});
148+
134149
describe('ClickhouseReporter text sanitization', () => {
135150
it('replaces lone surrogates so clickhouse can parse the JSON', () => {
136151
const truncated = 'cookies \u{1F36A}'.slice(0, -1); // ends in a lone high surrogate

0 commit comments

Comments
 (0)