@ecubelabs/mysql-mcp 1.0.0-next.7 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +296 -448
- package/dist/index.js +377 -239
- package/dist/src/catalog/collect.js +3 -2
- package/dist/src/catalog/docs.js +14 -12
- package/dist/src/catalog/index.js +59 -99
- package/dist/src/catalog/render.js +4 -10
- package/dist/src/catalog/store.js +193 -76
- package/dist/src/catalog/types.js +0 -12
- package/dist/src/config/index.js +175 -0
- package/dist/src/db/index.js +379 -379
- package/dist/src/db/permissions.js +32 -16
- package/dist/src/db/utils.js +3 -1
- package/dist/src/ssh/config.js +34 -27
- package/dist/src/ssh/tunnel.js +238 -155
- package/dist/src/utils/index.js +4 -35
- package/package.json +4 -4
- package/dist/src/catalog/context.js +0 -187
- package/dist/src/catalog/lock.js +0 -113
- package/dist/src/config/args.js +0 -30
- package/dist/src/config/load.js +0 -169
- package/dist/src/config/schema.js +0 -351
- package/dist/src/config/secrets.js +0 -128
- package/dist/src/config/ssl.js +0 -25
- package/dist/src/db/registry.js +0 -178
- package/dist/src/init/check.js +0 -31
- package/dist/src/init/cli.js +0 -26
- package/dist/src/init/config-edit.js +0 -95
- package/dist/src/init/prompt.js +0 -82
- package/dist/src/init/wizard.js +0 -223
- package/dist/src/server/catalog-tool.js +0 -91
- package/dist/src/server/connections.js +0 -117
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
export const CONFIG_VERSION = 1;
|
|
2
|
-
export const WRITE_KINDS = ['insert', 'update', 'delete', 'ddl'];
|
|
3
|
-
export class ConfigValidationError extends Error {
|
|
4
|
-
problems;
|
|
5
|
-
constructor(problems) {
|
|
6
|
-
super(`invalid config:\n${problems.map((problem) => ` - ${problem}`).join('\n')}`);
|
|
7
|
-
this.problems = problems;
|
|
8
|
-
this.name = 'ConfigValidationError';
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export const NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
12
|
-
const ENV_VAR_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
13
|
-
class Checker {
|
|
14
|
-
problems = [];
|
|
15
|
-
fail(path, message) {
|
|
16
|
-
this.problems.push(`${path}: ${message}`);
|
|
17
|
-
}
|
|
18
|
-
object(path, value) {
|
|
19
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
20
|
-
this.fail(path, 'expected an object');
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
keys(path, value, allowed) {
|
|
26
|
-
for (const key of Object.keys(value)) {
|
|
27
|
-
if (!allowed.includes(key)) {
|
|
28
|
-
this.fail(join(path, key), `unknown key (allowed: ${allowed.join(', ')})`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
string(path, value) {
|
|
33
|
-
if (typeof value !== 'string' || value.trim() === '') {
|
|
34
|
-
this.fail(path, 'expected a non-empty string');
|
|
35
|
-
return undefined;
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
boolean(path, value) {
|
|
40
|
-
if (typeof value !== 'boolean') {
|
|
41
|
-
this.fail(path, 'expected true or false');
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
positiveInt(path, value) {
|
|
47
|
-
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1) {
|
|
48
|
-
this.fail(path, 'expected a positive integer');
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
positiveNumber(path, value) {
|
|
54
|
-
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
55
|
-
this.fail(path, 'expected a positive number');
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
return value;
|
|
59
|
-
}
|
|
60
|
-
port(path, value) {
|
|
61
|
-
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1 || value > 65535) {
|
|
62
|
-
this.fail(path, 'expected an integer between 1 and 65535');
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
return value;
|
|
66
|
-
}
|
|
67
|
-
named(path, value) {
|
|
68
|
-
const map = this.object(path, value);
|
|
69
|
-
if (!map) {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
const entries = [];
|
|
73
|
-
for (const [name, entry] of Object.entries(map)) {
|
|
74
|
-
if (!NAME_PATTERN.test(name)) {
|
|
75
|
-
this.fail(join(path, name), 'name must match [A-Za-z0-9][A-Za-z0-9_.-]*');
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
entries.push([name, entry]);
|
|
79
|
-
}
|
|
80
|
-
return entries;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function join(path, key) {
|
|
84
|
-
return path ? `${path}.${key}` : key;
|
|
85
|
-
}
|
|
86
|
-
function optional(target, key, source, path, check) {
|
|
87
|
-
if (!Object.hasOwn(source, key) || source[key] === undefined) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const value = check(join(path, key), source[key]);
|
|
91
|
-
if (value !== undefined) {
|
|
92
|
-
target[key] = value;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
function checkSecretRef(c, path, value) {
|
|
96
|
-
const ref = c.object(path, value);
|
|
97
|
-
if (!ref) {
|
|
98
|
-
return undefined;
|
|
99
|
-
}
|
|
100
|
-
const kinds = Object.keys(ref);
|
|
101
|
-
if (kinds.length !== 1 || !['keychain', 'env', 'command'].includes(kinds[0])) {
|
|
102
|
-
c.fail(path, 'expected exactly one of { "keychain": { "service", "account" } }, { "env": "VAR" }, { "command": [...] }' +
|
|
103
|
-
' (plain-text secrets are not accepted)');
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
const kind = kinds[0];
|
|
107
|
-
if (kind === 'keychain') {
|
|
108
|
-
const item = c.object(join(path, 'keychain'), ref.keychain);
|
|
109
|
-
if (!item) {
|
|
110
|
-
return undefined;
|
|
111
|
-
}
|
|
112
|
-
c.keys(join(path, 'keychain'), item, ['service', 'account']);
|
|
113
|
-
const service = c.string(join(path, 'keychain.service'), item.service);
|
|
114
|
-
const account = c.string(join(path, 'keychain.account'), item.account);
|
|
115
|
-
return service && account ? { keychain: { service, account } } : undefined;
|
|
116
|
-
}
|
|
117
|
-
if (kind === 'env') {
|
|
118
|
-
const name = c.string(join(path, 'env'), ref.env);
|
|
119
|
-
if (name && !ENV_VAR_PATTERN.test(name)) {
|
|
120
|
-
c.fail(join(path, 'env'), 'expected an environment variable name');
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
return name ? { env: name } : undefined;
|
|
124
|
-
}
|
|
125
|
-
const argv = ref.command;
|
|
126
|
-
if (!Array.isArray(argv) || argv.length === 0) {
|
|
127
|
-
c.fail(join(path, 'command'), 'expected a non-empty array of strings (argv; no shell is used)');
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
const checked = argv.map((arg, index) => c.string(join(path, `command.${index}`), arg));
|
|
131
|
-
return checked.every((arg) => arg !== undefined) ? { command: checked } : undefined;
|
|
132
|
-
}
|
|
133
|
-
function checkWriteFlags(c, path, source, target) {
|
|
134
|
-
for (const kind of WRITE_KINDS) {
|
|
135
|
-
optional(target, kind, source, path, (p, v) => c.boolean(p, v));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
function checkWritePolicy(c, path, value) {
|
|
139
|
-
const source = c.object(path, value);
|
|
140
|
-
if (!source) {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
c.keys(path, source, [...WRITE_KINDS, 'schemas']);
|
|
144
|
-
const policy = {};
|
|
145
|
-
checkWriteFlags(c, path, source, policy);
|
|
146
|
-
if (Object.hasOwn(source, 'schemas')) {
|
|
147
|
-
const schemasPath = join(path, 'schemas');
|
|
148
|
-
const schemas = c.object(schemasPath, source.schemas);
|
|
149
|
-
if (schemas) {
|
|
150
|
-
policy.schemas = Object.create(null);
|
|
151
|
-
for (const [schema, flags] of Object.entries(schemas)) {
|
|
152
|
-
const flagsPath = join(schemasPath, schema);
|
|
153
|
-
const flagSource = c.object(flagsPath, flags);
|
|
154
|
-
if (!flagSource) {
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
c.keys(flagsPath, flagSource, WRITE_KINDS);
|
|
158
|
-
const checked = {};
|
|
159
|
-
checkWriteFlags(c, flagsPath, flagSource, checked);
|
|
160
|
-
policy.schemas[schema] = checked;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return policy;
|
|
165
|
-
}
|
|
166
|
-
function checkBastion(c, path, value) {
|
|
167
|
-
const source = c.object(path, value);
|
|
168
|
-
if (!source) {
|
|
169
|
-
return undefined;
|
|
170
|
-
}
|
|
171
|
-
c.keys(path, source, ['sshConfigHost', 'host', 'port', 'user', 'privateKeyPath', 'passphrase']);
|
|
172
|
-
const bastion = {};
|
|
173
|
-
optional(bastion, 'sshConfigHost', source, path, (p, v) => c.string(p, v));
|
|
174
|
-
optional(bastion, 'host', source, path, (p, v) => c.string(p, v));
|
|
175
|
-
optional(bastion, 'port', source, path, (p, v) => c.port(p, v));
|
|
176
|
-
optional(bastion, 'user', source, path, (p, v) => c.string(p, v));
|
|
177
|
-
optional(bastion, 'privateKeyPath', source, path, (p, v) => c.string(p, v));
|
|
178
|
-
optional(bastion, 'passphrase', source, path, (p, v) => checkSecretRef(c, p, v));
|
|
179
|
-
if (!bastion.sshConfigHost && !bastion.host) {
|
|
180
|
-
c.fail(path, 'set "sshConfigHost" (an alias in ~/.ssh/config) or "host"');
|
|
181
|
-
}
|
|
182
|
-
return bastion;
|
|
183
|
-
}
|
|
184
|
-
function checkInstance(c, path, value, tunneled) {
|
|
185
|
-
const source = c.object(path, value);
|
|
186
|
-
if (!source) {
|
|
187
|
-
return undefined;
|
|
188
|
-
}
|
|
189
|
-
c.keys(path, source, [
|
|
190
|
-
'host',
|
|
191
|
-
'port',
|
|
192
|
-
'socketPath',
|
|
193
|
-
'ssl',
|
|
194
|
-
'timezone',
|
|
195
|
-
'dateStrings',
|
|
196
|
-
'bigNumberStrings',
|
|
197
|
-
'disableReadOnlyTransactions',
|
|
198
|
-
]);
|
|
199
|
-
const instance = {};
|
|
200
|
-
optional(instance, 'host', source, path, (p, v) => c.string(p, v));
|
|
201
|
-
optional(instance, 'port', source, path, (p, v) => c.port(p, v));
|
|
202
|
-
optional(instance, 'socketPath', source, path, (p, v) => c.string(p, v));
|
|
203
|
-
optional(instance, 'timezone', source, path, (p, v) => c.string(p, v));
|
|
204
|
-
optional(instance, 'dateStrings', source, path, (p, v) => c.boolean(p, v));
|
|
205
|
-
optional(instance, 'bigNumberStrings', source, path, (p, v) => c.boolean(p, v));
|
|
206
|
-
optional(instance, 'disableReadOnlyTransactions', source, path, (p, v) => c.boolean(p, v));
|
|
207
|
-
optional(instance, 'ssl', source, path, (p, v) => {
|
|
208
|
-
const ssl = c.object(p, v);
|
|
209
|
-
if (!ssl) {
|
|
210
|
-
return undefined;
|
|
211
|
-
}
|
|
212
|
-
c.keys(p, ssl, ['rejectUnauthorized', 'ca', 'cert', 'key']);
|
|
213
|
-
const checked = {};
|
|
214
|
-
optional(checked, 'rejectUnauthorized', ssl, p, (pp, vv) => c.boolean(pp, vv));
|
|
215
|
-
optional(checked, 'ca', ssl, p, (pp, vv) => c.string(pp, vv));
|
|
216
|
-
optional(checked, 'cert', ssl, p, (pp, vv) => c.string(pp, vv));
|
|
217
|
-
optional(checked, 'key', ssl, p, (pp, vv) => c.string(pp, vv));
|
|
218
|
-
return checked;
|
|
219
|
-
});
|
|
220
|
-
if (instance.socketPath && tunneled) {
|
|
221
|
-
c.fail(join(path, 'socketPath'), 'cannot be used in an environment that goes through a bastion');
|
|
222
|
-
}
|
|
223
|
-
if (!instance.socketPath && !instance.host) {
|
|
224
|
-
c.fail(path, 'set "host" (as seen from the bastion when one is used) or "socketPath"');
|
|
225
|
-
}
|
|
226
|
-
return instance;
|
|
227
|
-
}
|
|
228
|
-
function checkAccount(c, path, value, instances) {
|
|
229
|
-
const source = c.object(path, value);
|
|
230
|
-
if (!source) {
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
c.keys(path, source, ['instance', 'user', 'password', 'database', 'write']);
|
|
234
|
-
const instance = c.string(join(path, 'instance'), source.instance);
|
|
235
|
-
if (instance && !Object.hasOwn(instances, instance)) {
|
|
236
|
-
c.fail(join(path, 'instance'), `no instance named "${instance}" in this environment`);
|
|
237
|
-
}
|
|
238
|
-
const user = c.string(join(path, 'user'), source.user);
|
|
239
|
-
const password = Object.hasOwn(source, 'password')
|
|
240
|
-
? checkSecretRef(c, join(path, 'password'), source.password)
|
|
241
|
-
: (c.fail(join(path, 'password'), 'required (a secret reference)'), undefined);
|
|
242
|
-
const account = { instance, user, password };
|
|
243
|
-
optional(account, 'database', source, path, (p, v) => c.string(p, v));
|
|
244
|
-
optional(account, 'write', source, path, (p, v) => checkWritePolicy(c, p, v));
|
|
245
|
-
return account;
|
|
246
|
-
}
|
|
247
|
-
function checkEnvironment(c, path, value, bastions) {
|
|
248
|
-
const source = c.object(path, value);
|
|
249
|
-
if (!source) {
|
|
250
|
-
return undefined;
|
|
251
|
-
}
|
|
252
|
-
c.keys(path, source, ['bastion', 'instances', 'accounts', 'write']);
|
|
253
|
-
const env = { instances: {}, accounts: {} };
|
|
254
|
-
optional(env, 'bastion', source, path, (p, v) => c.string(p, v));
|
|
255
|
-
if (env.bastion && !Object.hasOwn(bastions, env.bastion)) {
|
|
256
|
-
c.fail(join(path, 'bastion'), `no bastion named "${env.bastion}" under "bastions"`);
|
|
257
|
-
}
|
|
258
|
-
for (const [name, instance] of c.named(join(path, 'instances'), source.instances)) {
|
|
259
|
-
const checked = checkInstance(c, join(path, `instances.${name}`), instance, Boolean(env.bastion));
|
|
260
|
-
if (checked) {
|
|
261
|
-
env.instances[name] = checked;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
const accounts = c.named(join(path, 'accounts'), source.accounts);
|
|
265
|
-
const rawAccounts = source.accounts;
|
|
266
|
-
if (rawAccounts && typeof rawAccounts === 'object' && Object.keys(rawAccounts).length === 0) {
|
|
267
|
-
c.fail(join(path, 'accounts'), 'define at least one account; each account is one connection');
|
|
268
|
-
}
|
|
269
|
-
for (const [name, account] of accounts) {
|
|
270
|
-
const checked = checkAccount(c, join(path, `accounts.${name}`), account, env.instances);
|
|
271
|
-
if (checked) {
|
|
272
|
-
env.accounts[name] = checked;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
optional(env, 'write', source, path, (p, v) => checkWritePolicy(c, p, v));
|
|
276
|
-
return env;
|
|
277
|
-
}
|
|
278
|
-
function checkDefaults(c, path, value) {
|
|
279
|
-
const source = c.object(path, value);
|
|
280
|
-
if (!source) {
|
|
281
|
-
return {};
|
|
282
|
-
}
|
|
283
|
-
c.keys(path, source, [
|
|
284
|
-
'maxResponseRows',
|
|
285
|
-
'maxTimeoutSeconds',
|
|
286
|
-
'catalogTimeoutSeconds',
|
|
287
|
-
'poolSize',
|
|
288
|
-
'queueLimit',
|
|
289
|
-
'connectTimeoutMs',
|
|
290
|
-
'logging',
|
|
291
|
-
'catalog',
|
|
292
|
-
]);
|
|
293
|
-
const defaults = {};
|
|
294
|
-
optional(defaults, 'maxResponseRows', source, path, (p, v) => c.positiveInt(p, v));
|
|
295
|
-
optional(defaults, 'maxTimeoutSeconds', source, path, (p, v) => c.positiveInt(p, v));
|
|
296
|
-
optional(defaults, 'catalogTimeoutSeconds', source, path, (p, v) => c.positiveInt(p, v));
|
|
297
|
-
optional(defaults, 'poolSize', source, path, (p, v) => c.positiveInt(p, v));
|
|
298
|
-
optional(defaults, 'queueLimit', source, path, (p, v) => c.positiveInt(p, v));
|
|
299
|
-
optional(defaults, 'connectTimeoutMs', source, path, (p, v) => c.positiveInt(p, v));
|
|
300
|
-
optional(defaults, 'logging', source, path, (p, v) => c.boolean(p, v));
|
|
301
|
-
optional(defaults, 'catalog', source, path, (p, v) => {
|
|
302
|
-
const catalog = c.object(p, v);
|
|
303
|
-
if (!catalog) {
|
|
304
|
-
return undefined;
|
|
305
|
-
}
|
|
306
|
-
c.keys(p, catalog, ['enabled', 'ttlHours', 'path']);
|
|
307
|
-
const checked = {};
|
|
308
|
-
optional(checked, 'enabled', catalog, p, (pp, vv) => c.boolean(pp, vv));
|
|
309
|
-
optional(checked, 'ttlHours', catalog, p, (pp, vv) => c.positiveNumber(pp, vv));
|
|
310
|
-
optional(checked, 'path', catalog, p, (pp, vv) => c.string(pp, vv));
|
|
311
|
-
return checked;
|
|
312
|
-
});
|
|
313
|
-
return defaults;
|
|
314
|
-
}
|
|
315
|
-
export function validateConfig(raw) {
|
|
316
|
-
const c = new Checker();
|
|
317
|
-
const root = c.object('', raw);
|
|
318
|
-
if (!root) {
|
|
319
|
-
throw new ConfigValidationError(['(root): expected a JSON object']);
|
|
320
|
-
}
|
|
321
|
-
c.keys('', root, ['version', 'bastions', 'environments', 'defaults']);
|
|
322
|
-
if (root.version !== CONFIG_VERSION) {
|
|
323
|
-
c.fail('version', `expected ${CONFIG_VERSION} (this server reads config version ${CONFIG_VERSION} only)`);
|
|
324
|
-
}
|
|
325
|
-
const bastions = {};
|
|
326
|
-
if (Object.hasOwn(root, 'bastions')) {
|
|
327
|
-
for (const [name, bastion] of c.named('bastions', root.bastions)) {
|
|
328
|
-
const checked = checkBastion(c, `bastions.${name}`, bastion);
|
|
329
|
-
if (checked) {
|
|
330
|
-
bastions[name] = checked;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
const environments = {};
|
|
335
|
-
if (!Object.hasOwn(root, 'environments')) {
|
|
336
|
-
c.fail('environments', 'required');
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
for (const [name, env] of c.named('environments', root.environments)) {
|
|
340
|
-
const checked = checkEnvironment(c, `environments.${name}`, env, bastions);
|
|
341
|
-
if (checked) {
|
|
342
|
-
environments[name] = checked;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
const defaults = Object.hasOwn(root, 'defaults') ? checkDefaults(c, 'defaults', root.defaults) : {};
|
|
347
|
-
if (c.problems.length > 0) {
|
|
348
|
-
throw new ConfigValidationError(c.problems);
|
|
349
|
-
}
|
|
350
|
-
return { version: CONFIG_VERSION, bastions, environments, defaults };
|
|
351
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { execFile } from 'node:child_process';
|
|
2
|
-
export const SECRET_TIMEOUT_MS = 5_000;
|
|
3
|
-
function runFile(file, args, timeoutMs) {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
execFile(file, args, { timeout: timeoutMs, maxBuffer: 64 * 1024, encoding: 'utf8' }, (error, stdout, stderr) => {
|
|
6
|
-
if (error) {
|
|
7
|
-
const detail = stderr.trim().split('\n')[0]?.slice(0, 200);
|
|
8
|
-
reject(new Error(detail ? `${error.message.split('\n')[0]} (${detail})` : error.message.split('\n')[0]));
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
resolve(stdout);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export const defaultSecretRuntime = {
|
|
16
|
-
platform: process.platform,
|
|
17
|
-
run: runFile,
|
|
18
|
-
readEnv: (name) => process.env[name],
|
|
19
|
-
};
|
|
20
|
-
export function describeSecretRef(ref) {
|
|
21
|
-
if ('keychain' in ref) {
|
|
22
|
-
return `keychain item (service "${ref.keychain.service}", account "${ref.keychain.account}")`;
|
|
23
|
-
}
|
|
24
|
-
if ('env' in ref) {
|
|
25
|
-
return `environment variable ${ref.env}`;
|
|
26
|
-
}
|
|
27
|
-
return `command \`${ref.command.join(' ')}\``;
|
|
28
|
-
}
|
|
29
|
-
function stripTrailingNewline(output) {
|
|
30
|
-
return output.replace(/\r?\n$/, '');
|
|
31
|
-
}
|
|
32
|
-
function withTimeout(promise, timeoutMs, what) {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
const timer = setTimeout(() => reject(new Error(`timed out after ${timeoutMs / 1000}s reading ${what}`)), timeoutMs);
|
|
35
|
-
promise.then((value) => {
|
|
36
|
-
clearTimeout(timer);
|
|
37
|
-
resolve(value);
|
|
38
|
-
}, (error) => {
|
|
39
|
-
clearTimeout(timer);
|
|
40
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
async function lookup(ref, runtime, timeoutMs) {
|
|
45
|
-
const what = describeSecretRef(ref);
|
|
46
|
-
if ('env' in ref) {
|
|
47
|
-
const value = runtime.readEnv(ref.env);
|
|
48
|
-
if (value === undefined || value === '') {
|
|
49
|
-
throw new Error(`${what} is not set or empty in the MCP server's environment`);
|
|
50
|
-
}
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
let file;
|
|
54
|
-
let args;
|
|
55
|
-
if ('keychain' in ref) {
|
|
56
|
-
const { service, account } = ref.keychain;
|
|
57
|
-
if (runtime.platform === 'darwin') {
|
|
58
|
-
file = 'security';
|
|
59
|
-
args = ['find-generic-password', '-s', service, '-a', account, '-w'];
|
|
60
|
-
}
|
|
61
|
-
else if (runtime.platform === 'linux') {
|
|
62
|
-
file = 'secret-tool';
|
|
63
|
-
args = ['lookup', 'service', service, 'account', account];
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
throw new Error(`${what}: keychain references work on macOS and Linux only; use "env" or "command"`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
[file, ...args] = ref.command;
|
|
71
|
-
}
|
|
72
|
-
let output;
|
|
73
|
-
try {
|
|
74
|
-
output = await withTimeout(runtime.run(file, args, timeoutMs), timeoutMs, what);
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
throw new Error(`could not read ${what}: ${error.message}`);
|
|
78
|
-
}
|
|
79
|
-
const value = stripTrailingNewline(output);
|
|
80
|
-
if (value === '') {
|
|
81
|
-
throw new Error(`${what} returned an empty value`);
|
|
82
|
-
}
|
|
83
|
-
return value;
|
|
84
|
-
}
|
|
85
|
-
export class SecretResolver {
|
|
86
|
-
runtime;
|
|
87
|
-
timeoutMs;
|
|
88
|
-
cache = new Map();
|
|
89
|
-
constructor(runtime = defaultSecretRuntime, timeoutMs = SECRET_TIMEOUT_MS) {
|
|
90
|
-
this.runtime = runtime;
|
|
91
|
-
this.timeoutMs = timeoutMs;
|
|
92
|
-
}
|
|
93
|
-
resolve(ref) {
|
|
94
|
-
const key = JSON.stringify(ref);
|
|
95
|
-
let pending = this.cache.get(key);
|
|
96
|
-
if (!pending) {
|
|
97
|
-
pending = lookup(ref, this.runtime, this.timeoutMs);
|
|
98
|
-
this.cache.set(key, pending);
|
|
99
|
-
pending.catch(() => {
|
|
100
|
-
if (this.cache.get(key) === pending) {
|
|
101
|
-
this.cache.delete(key);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
return pending;
|
|
106
|
-
}
|
|
107
|
-
invalidate(ref) {
|
|
108
|
-
this.cache.delete(JSON.stringify(ref));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
export function isAccessDeniedError(error) {
|
|
112
|
-
const { errno, code } = (error ?? {});
|
|
113
|
-
return errno === 1045 || code === 'ER_ACCESS_DENIED_ERROR';
|
|
114
|
-
}
|
|
115
|
-
export async function withSecretRetry(resolver, ref, attempt) {
|
|
116
|
-
const first = await resolver.resolve(ref);
|
|
117
|
-
try {
|
|
118
|
-
return await attempt(first);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
if (!isAccessDeniedError(error)) {
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
resolver.invalidate(ref);
|
|
125
|
-
const second = await resolver.resolve(ref);
|
|
126
|
-
return attempt(second);
|
|
127
|
-
}
|
|
128
|
-
}
|
package/dist/src/config/ssl.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
export function readSSLFile(filePath, label) {
|
|
3
|
-
try {
|
|
4
|
-
if (!fs.existsSync(filePath)) {
|
|
5
|
-
throw new Error(`SSL ${label} file not found: ${filePath}`);
|
|
6
|
-
}
|
|
7
|
-
const data = fs.readFileSync(filePath);
|
|
8
|
-
if (data.length === 0) {
|
|
9
|
-
throw new Error(`SSL ${label} file is empty: ${filePath}`);
|
|
10
|
-
}
|
|
11
|
-
return data;
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
if (error instanceof Error) {
|
|
15
|
-
if (error.message.startsWith('SSL ')) {
|
|
16
|
-
throw error;
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Failed to read SSL ${label}: ${error.message}`);
|
|
19
|
-
}
|
|
20
|
-
throw error;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
export function readCACertificate(filePath) {
|
|
24
|
-
return readSSLFile(filePath, 'CA certificate');
|
|
25
|
-
}
|
package/dist/src/db/registry.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'crypto';
|
|
2
|
-
import * as mysql2 from 'mysql2/promise';
|
|
3
|
-
import { SecretResolver, withSecretRetry, isAccessDeniedError } from '../config/secrets.js';
|
|
4
|
-
import { readCACertificate, readSSLFile } from '../config/ssl.js';
|
|
5
|
-
import { expandHome } from '../ssh/config.js';
|
|
6
|
-
import { resolveBastion, SshSession } from '../ssh/tunnel.js';
|
|
7
|
-
import { log } from '../utils/index.js';
|
|
8
|
-
export function buildPoolOptions(spec, endpoint, password, defaults) {
|
|
9
|
-
const { instance } = spec;
|
|
10
|
-
const target = endpoint
|
|
11
|
-
? { host: endpoint.host, port: endpoint.port }
|
|
12
|
-
: instance.socketPath
|
|
13
|
-
? { socketPath: instance.socketPath }
|
|
14
|
-
: { host: instance.host, port: instance.port };
|
|
15
|
-
const ssl = instance.ssl;
|
|
16
|
-
return {
|
|
17
|
-
...target,
|
|
18
|
-
user: spec.user,
|
|
19
|
-
password,
|
|
20
|
-
...(spec.database ? { database: spec.database } : {}),
|
|
21
|
-
connectionLimit: defaults.poolSize,
|
|
22
|
-
waitForConnections: true,
|
|
23
|
-
queueLimit: defaults.queueLimit,
|
|
24
|
-
enableKeepAlive: true,
|
|
25
|
-
keepAliveInitialDelay: 0,
|
|
26
|
-
connectTimeout: defaults.connectTimeoutMs,
|
|
27
|
-
authPlugins: {
|
|
28
|
-
mysql_clear_password: () => () => Buffer.from(password),
|
|
29
|
-
},
|
|
30
|
-
...(ssl
|
|
31
|
-
? {
|
|
32
|
-
ssl: {
|
|
33
|
-
rejectUnauthorized: ssl.rejectUnauthorized ?? false,
|
|
34
|
-
...(ssl.ca ? { ca: readCACertificate(expandHome(ssl.ca)) } : {}),
|
|
35
|
-
...(ssl.cert ? { cert: readSSLFile(expandHome(ssl.cert), 'client certificate') } : {}),
|
|
36
|
-
...(ssl.key ? { key: readSSLFile(expandHome(ssl.key), 'client private key') } : {}),
|
|
37
|
-
},
|
|
38
|
-
}
|
|
39
|
-
: {}),
|
|
40
|
-
...(instance.timezone ? { timezone: instance.timezone } : {}),
|
|
41
|
-
...(instance.dateStrings ? { dateStrings: true } : {}),
|
|
42
|
-
...(instance.bigNumberStrings ? { supportBigNumbers: true, bigNumberStrings: true } : {}),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function poolKey(spec, endpoint, password) {
|
|
46
|
-
const { instance } = spec;
|
|
47
|
-
const where = endpoint
|
|
48
|
-
? `tunnel ${endpoint.host}:${endpoint.port}`
|
|
49
|
-
: (instance.socketPath ?? `${instance.host}:${instance.port}`);
|
|
50
|
-
const options = JSON.stringify([
|
|
51
|
-
spec.user,
|
|
52
|
-
createHash('sha256').update(password).digest('hex'),
|
|
53
|
-
spec.database ?? null,
|
|
54
|
-
instance.ssl ?? null,
|
|
55
|
-
instance.timezone ?? null,
|
|
56
|
-
instance.dateStrings ?? false,
|
|
57
|
-
instance.bigNumberStrings ?? false,
|
|
58
|
-
]);
|
|
59
|
-
return `${where}\0${options}`;
|
|
60
|
-
}
|
|
61
|
-
function sessionKey(config) {
|
|
62
|
-
return [config.sshHost, config.sshPort, config.sshUser, config.privateKeyPath].join('\0');
|
|
63
|
-
}
|
|
64
|
-
function cacheUnlessRejected(map, key, create) {
|
|
65
|
-
let pending = map.get(key);
|
|
66
|
-
if (!pending) {
|
|
67
|
-
pending = create();
|
|
68
|
-
map.set(key, pending);
|
|
69
|
-
pending.catch(() => {
|
|
70
|
-
if (map.get(key) === pending) {
|
|
71
|
-
map.delete(key);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
return pending;
|
|
76
|
-
}
|
|
77
|
-
export class ConnectionHandle {
|
|
78
|
-
spec;
|
|
79
|
-
registry;
|
|
80
|
-
endpoint = null;
|
|
81
|
-
constructor(spec, registry) {
|
|
82
|
-
this.spec = spec;
|
|
83
|
-
this.registry = registry;
|
|
84
|
-
}
|
|
85
|
-
get name() {
|
|
86
|
-
return this.spec.name;
|
|
87
|
-
}
|
|
88
|
-
describeTransport() {
|
|
89
|
-
const bastion = this.spec.bastion;
|
|
90
|
-
if (!bastion) {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
return `ssh tunnel: ${bastion.sshConfigHost ?? `${bastion.user}@${bastion.host}`}`;
|
|
94
|
-
}
|
|
95
|
-
getConnection() {
|
|
96
|
-
return this.registry.acquire(this);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
export class ConnectionRegistry {
|
|
100
|
-
env;
|
|
101
|
-
handles = new Map();
|
|
102
|
-
sessions = new Map();
|
|
103
|
-
pools = new Map();
|
|
104
|
-
secrets;
|
|
105
|
-
resolveBastionConfig;
|
|
106
|
-
openSession;
|
|
107
|
-
createPool;
|
|
108
|
-
preparePool;
|
|
109
|
-
constructor(env, deps = {}) {
|
|
110
|
-
this.env = env;
|
|
111
|
-
this.secrets = deps.secrets ?? new SecretResolver();
|
|
112
|
-
this.resolveBastionConfig = deps.resolveBastion ?? ((bastion) => resolveBastion(bastion));
|
|
113
|
-
this.openSession = deps.openSession ?? ((config) => new SshSession(config));
|
|
114
|
-
this.createPool = deps.createPool ?? ((options) => mysql2.createPool(options));
|
|
115
|
-
this.preparePool = deps.preparePool;
|
|
116
|
-
for (const spec of env.connections) {
|
|
117
|
-
this.handles.set(spec.name, new ConnectionHandle(spec, this));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
list() {
|
|
121
|
-
return [...this.handles.values()];
|
|
122
|
-
}
|
|
123
|
-
get(name) {
|
|
124
|
-
return this.handles.get(name);
|
|
125
|
-
}
|
|
126
|
-
async acquire(handle) {
|
|
127
|
-
const { spec } = handle;
|
|
128
|
-
const session = spec.bastion ? await this.sessionFor(spec.bastion) : null;
|
|
129
|
-
if (session?.fatalError) {
|
|
130
|
-
throw session.fatalError;
|
|
131
|
-
}
|
|
132
|
-
return withSecretRetry(this.secrets, spec.password, async (password) => {
|
|
133
|
-
const endpoint = session ? await session.openListener(spec.instance.host, spec.instance.port) : null;
|
|
134
|
-
const key = poolKey(spec, endpoint, password);
|
|
135
|
-
const pending = cacheUnlessRejected(this.pools, key, async () => {
|
|
136
|
-
const created = this.createPool(buildPoolOptions(spec, endpoint, password, this.env.defaults));
|
|
137
|
-
this.preparePool?.(created);
|
|
138
|
-
log('info', `MySQL pool created for connection "${spec.name}" (${spec.user}@${spec.instance.name})`);
|
|
139
|
-
return created;
|
|
140
|
-
});
|
|
141
|
-
const pool = await pending;
|
|
142
|
-
handle.endpoint = endpoint;
|
|
143
|
-
try {
|
|
144
|
-
return await pool.getConnection();
|
|
145
|
-
}
|
|
146
|
-
catch (error) {
|
|
147
|
-
if (isAccessDeniedError(error) && this.pools.get(key) === pending) {
|
|
148
|
-
this.pools.delete(key);
|
|
149
|
-
pool.end().catch((e) => log('error', 'Failed to close a rejected pool:', e));
|
|
150
|
-
}
|
|
151
|
-
throw error;
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
sessionFor(bastion) {
|
|
156
|
-
const config = this.resolveBastionConfig(bastion);
|
|
157
|
-
return cacheUnlessRejected(this.sessions, sessionKey(config), async () => {
|
|
158
|
-
const passphrase = bastion.passphrase ? await this.secrets.resolve(bastion.passphrase) : undefined;
|
|
159
|
-
return this.openSession(passphrase ? { ...config, passphrase } : config);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
async close() {
|
|
163
|
-
const pools = [...this.pools.values()];
|
|
164
|
-
const sessions = [...this.sessions.values()];
|
|
165
|
-
this.pools.clear();
|
|
166
|
-
this.sessions.clear();
|
|
167
|
-
for (const result of await Promise.allSettled(pools)) {
|
|
168
|
-
if (result.status === 'fulfilled') {
|
|
169
|
-
await result.value.end().catch((error) => log('error', 'Failed to close a pool:', error));
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
for (const result of await Promise.allSettled(sessions)) {
|
|
173
|
-
if (result.status === 'fulfilled') {
|
|
174
|
-
await result.value.close();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|