@git.zone/cli 7.4.1 → 8.1.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.
Files changed (39) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/mod_services/classes.dockercontainer.d.ts +13 -2
  3. package/dist_ts/mod_services/classes.dockercontainer.js +40 -40
  4. package/dist_ts/mod_services/classes.globalregistry.d.ts +12 -0
  5. package/dist_ts/mod_services/classes.globalregistry.js +96 -13
  6. package/dist_ts/mod_services/classes.globalregistrystore.d.ts +7 -1
  7. package/dist_ts/mod_services/classes.globalregistrystore.js +6 -6
  8. package/dist_ts/mod_services/classes.legacymongomigration.d.ts +239 -0
  9. package/dist_ts/mod_services/classes.legacymongomigration.js +970 -0
  10. package/dist_ts/mod_services/classes.serviceconfiguration.d.ts +112 -15
  11. package/dist_ts/mod_services/classes.serviceconfiguration.js +274 -62
  12. package/dist_ts/mod_services/classes.servicedatamarker.d.ts +42 -1
  13. package/dist_ts/mod_services/classes.servicedatamarker.js +101 -8
  14. package/dist_ts/mod_services/classes.servicemanager.d.ts +99 -33
  15. package/dist_ts/mod_services/classes.servicemanager.js +652 -384
  16. package/dist_ts/mod_services/classes.servicepruner.js +27 -20
  17. package/dist_ts/mod_services/index.js +62 -13
  18. package/dist_ts/mod_services/mod.plugins.d.ts +2 -1
  19. package/dist_ts/mod_services/mod.plugins.js +4 -2
  20. package/dist_ts_migration/002.runtime-config.d.ts +1 -1
  21. package/dist_ts_migration/002.runtime-config.js +8 -2
  22. package/dist_ts_migration/003.service-data-marker.js +125 -30
  23. package/dist_ts_migration/004.global-registry.js +30 -8
  24. package/package.json +1 -1
  25. package/readme.md +197 -32
  26. package/ts/00_commitinfo_data.ts +1 -1
  27. package/ts/mod_services/classes.dockercontainer.ts +58 -50
  28. package/ts/mod_services/classes.globalregistry.ts +115 -14
  29. package/ts/mod_services/classes.globalregistrystore.ts +16 -6
  30. package/ts/mod_services/classes.legacymongomigration.ts +1308 -0
  31. package/ts/mod_services/classes.serviceconfiguration.ts +387 -69
  32. package/ts/mod_services/classes.servicedatamarker.ts +149 -8
  33. package/ts/mod_services/classes.servicemanager.ts +877 -454
  34. package/ts/mod_services/classes.servicepruner.ts +27 -19
  35. package/ts/mod_services/index.ts +81 -12
  36. package/ts/mod_services/mod.plugins.ts +4 -1
  37. package/ts_migration/002.runtime-config.ts +8 -1
  38. package/ts_migration/003.service-data-marker.ts +168 -29
  39. package/ts_migration/004.global-registry.ts +38 -7
@@ -100,7 +100,46 @@ export const deriveS3BucketName = (projectNameArg: string): string => {
100
100
  return canonical;
101
101
  };
102
102
 
103
- export interface IServiceConfig {
103
+ /**
104
+ * The serve.zone capability environment
105
+ * (`serve.zone/docs/developers/app-integration.md`, "Capability environment")
106
+ * that GitZone does not already manage under the same name. `MONGODB_HOST`,
107
+ * `MONGODB_PORT`, `MONGODB_URL`, `S3_PORT`, `S3_REGION` and `S3_BUCKET` share
108
+ * their platform name and meaning with the GitZone fields. `S3_ENDPOINT` shares
109
+ * the name only: GitZone keeps the user's value (a host, `host:port`, or an
110
+ * origin, see `resolveS3Endpoint`), and `AWS_ENDPOINT_URL` carries the endpoint
111
+ * origin. Every value is derived from the GitZone fields and rewritten on each
112
+ * reconciliation.
113
+ */
114
+ export interface IServicePlatformEnvironment {
115
+ /** Identical to `MONGODB_URL` and `MONGO_URL`. */
116
+ MONGODB_URI: string;
117
+ MONGO_URL: string;
118
+ MONGODB_DATABASE: string;
119
+ MONGO_DBNAME: string;
120
+ /** The four credential names are present only while MongoDB auth is enabled. */
121
+ MONGODB_USERNAME?: string;
122
+ MONGO_DBUSER?: string;
123
+ MONGODB_PASSWORD?: string;
124
+ MONGO_DBPASS?: string;
125
+ /** Endpoint origin, e.g. `http://localhost:29000`. */
126
+ AWS_ENDPOINT_URL: string;
127
+ /** Host of `AWS_ENDPOINT_URL`; an IPv6 address without brackets. */
128
+ S3_ENDPOINT_HOST: string;
129
+ /** `true` or `false`, the scheme of `AWS_ENDPOINT_URL`. */
130
+ S3_USE_SSL: string;
131
+ AWS_REGION: string;
132
+ /** Always `true`. */
133
+ AWS_S3_FORCE_PATH_STYLE: string;
134
+ S3_ACCESS_KEY: string;
135
+ S3_ACCESS_KEY_ID: string;
136
+ AWS_ACCESS_KEY_ID: string;
137
+ S3_SECRET_KEY: string;
138
+ S3_SECRET_ACCESS_KEY: string;
139
+ AWS_SECRET_ACCESS_KEY: string;
140
+ }
141
+
142
+ export interface IServiceConfig extends IServicePlatformEnvironment {
104
143
  PROJECT_NAME: string;
105
144
  MONGODB_HOST: string;
106
145
  MONGODB_NAME: string;
@@ -109,11 +148,16 @@ export interface IServiceConfig {
109
148
  MONGODB_PASS: string;
110
149
  MONGODB_URL: string;
111
150
  /**
112
- * When false, mongod runs without authentication and is published on
113
- * loopback only. Opt-in, for runtimes whose `node:crypto` cannot complete a
114
- * SCRAM handshake (notably Deno). Defaults to true.
151
+ * When false, the NoSQLDB engine runs without authentication. It is always
152
+ * published on loopback only. Opt-in, for runtimes whose `node:crypto` cannot
153
+ * complete a SCRAM handshake (notably Deno). Defaults to true.
115
154
  */
116
155
  MONGODB_AUTH_ENABLED: boolean;
156
+ /**
157
+ * Password of the NoSQLDB engine's management UI. Generated per project,
158
+ * never printed, and never published: the UI port stays inside the container.
159
+ */
160
+ MONGODB_ADMIN_PASSWORD: string;
117
161
  S3_HOST: string;
118
162
  S3_PORT: string;
119
163
  S3_UI_PORT: string;
@@ -131,6 +175,231 @@ export interface IServiceConfig {
131
175
  ELASTICSEARCH_URL: string;
132
176
  }
133
177
 
178
+ /** The fields derived from GitZone's database and object-storage primitives. */
179
+ export type TDerivedServiceConnectionEnvironment = IServicePlatformEnvironment &
180
+ Pick<IServiceConfig, 'MONGODB_URL'>;
181
+
182
+ /** Every runtime field that is not derived by `deriveServiceConnectionEnvironment`. */
183
+ export type TServiceConfigPrimitives = Omit<IServiceConfig, keyof TDerivedServiceConnectionEnvironment>;
184
+
185
+ /** The primitives the connection environment is derived from. */
186
+ export type TServiceConnectionPrimitives = Pick<
187
+ IServiceConfig,
188
+ | 'MONGODB_HOST'
189
+ | 'MONGODB_PORT'
190
+ | 'MONGODB_NAME'
191
+ | 'MONGODB_USER'
192
+ | 'MONGODB_PASS'
193
+ | 'MONGODB_AUTH_ENABLED'
194
+ | 'S3_HOST'
195
+ | 'S3_ENDPOINT'
196
+ | 'S3_PORT'
197
+ | 'S3_USESSL'
198
+ | 'S3_REGION'
199
+ | 'S3_ACCESSKEY'
200
+ | 'S3_SECRETKEY'
201
+ >;
202
+
203
+ // A complete record, so the compiler proves the name list matches the type.
204
+ const derivedConnectionFieldPresence: {
205
+ [TName in keyof TDerivedServiceConnectionEnvironment]-?: true;
206
+ } = {
207
+ MONGODB_URL: true,
208
+ MONGODB_URI: true,
209
+ MONGO_URL: true,
210
+ MONGODB_DATABASE: true,
211
+ MONGO_DBNAME: true,
212
+ MONGODB_USERNAME: true,
213
+ MONGO_DBUSER: true,
214
+ MONGODB_PASSWORD: true,
215
+ MONGO_DBPASS: true,
216
+ AWS_ENDPOINT_URL: true,
217
+ S3_ENDPOINT_HOST: true,
218
+ S3_USE_SSL: true,
219
+ AWS_REGION: true,
220
+ AWS_S3_FORCE_PATH_STYLE: true,
221
+ S3_ACCESS_KEY: true,
222
+ S3_ACCESS_KEY_ID: true,
223
+ AWS_ACCESS_KEY_ID: true,
224
+ S3_SECRET_KEY: true,
225
+ S3_SECRET_ACCESS_KEY: true,
226
+ AWS_SECRET_ACCESS_KEY: true,
227
+ };
228
+
229
+ /** Names of every derived connection field, in `.nogit/env.json` order. */
230
+ export const serviceDerivedConnectionFieldNames = Object.keys(
231
+ derivedConnectionFieldPresence,
232
+ ) as Array<keyof TDerivedServiceConnectionEnvironment>;
233
+
234
+ /** Platform credential names that exist only while MongoDB auth is enabled. */
235
+ const platformDatabaseCredentialFields = [
236
+ 'MONGODB_USERNAME',
237
+ 'MONGO_DBUSER',
238
+ 'MONGODB_PASSWORD',
239
+ 'MONGO_DBPASS',
240
+ ] as const;
241
+
242
+ /** Runtime fields whose value is or contains a secret; `services config --json` redacts them. */
243
+ export const serviceSecretEnvironmentNames: ReadonlyArray<keyof IServiceConfig> = [
244
+ 'MONGODB_ADMIN_PASSWORD',
245
+ 'S3_ACCESSKEY',
246
+ 'S3_SECRETKEY',
247
+ 'S3_ADMIN_PASSWORD',
248
+ 'MONGODB_URI',
249
+ 'MONGO_URL',
250
+ 'MONGODB_PASSWORD',
251
+ 'MONGO_DBPASS',
252
+ 'S3_ACCESS_KEY',
253
+ 'S3_ACCESS_KEY_ID',
254
+ 'AWS_ACCESS_KEY_ID',
255
+ 'S3_SECRET_KEY',
256
+ 'S3_SECRET_ACCESS_KEY',
257
+ 'AWS_SECRET_ACCESS_KEY',
258
+ ];
259
+
260
+ /**
261
+ * The MongoDB connection URI. The credential lives in `admin`, so a local URI
262
+ * authenticates there; the path names the database, and `directConnection`
263
+ * matches the standalone NoSQLDB node. Credentials and `authSource` are
264
+ * omitted when auth is disabled.
265
+ */
266
+ export const buildMongoConnectionUri = (
267
+ primitivesArg: TServiceConnectionPrimitives,
268
+ hostArg: string = primitivesArg.MONGODB_HOST,
269
+ ): string => {
270
+ const authEnabled = primitivesArg.MONGODB_AUTH_ENABLED !== false;
271
+ const credentials = authEnabled
272
+ ? `${primitivesArg.MONGODB_USER}:${primitivesArg.MONGODB_PASS}@`
273
+ : '';
274
+ const authQuery = authEnabled ? 'authSource=admin&' : '';
275
+ return `mongodb://${credentials}${hostArg}:${primitivesArg.MONGODB_PORT}/${primitivesArg.MONGODB_NAME}?${authQuery}directConnection=true`;
276
+ };
277
+
278
+ /** The object-storage endpoint the platform names describe. */
279
+ export interface IS3Endpoint {
280
+ /** Host only, exactly as written; an IPv6 address without brackets. */
281
+ host: string;
282
+ /** Endpoint origin, e.g. `http://localhost:29000`; the scheme's default port is omitted. */
283
+ origin: string;
284
+ useSsl: boolean;
285
+ }
286
+
287
+ const s3EndpointSchemePattern = /^([a-z][a-z0-9+.-]*):\/\//iu;
288
+
289
+ /** The port as a decimal string when `portTextArg` is a TCP port, otherwise undefined. */
290
+ const parseTcpPort = (portTextArg: string): string | undefined => {
291
+ if (!/^\d{1,5}$/u.test(portTextArg)) {
292
+ return undefined;
293
+ }
294
+ const port = Number(portTextArg);
295
+ return port >= 1 && port <= 65_535 ? String(port) : undefined;
296
+ };
297
+
298
+ /**
299
+ * Resolve the object-storage endpoint from `S3_ENDPOINT`, read but never
300
+ * rewritten. It is user-controlled and accepted in three forms:
301
+ *
302
+ * - a bare host or IP address (`localhost`, `::1`): scheme from `S3_USESSL`,
303
+ * port from `S3_PORT`;
304
+ * - `host:port` or `[ipv6]:port`: its port wins over `S3_PORT`, the scheme
305
+ * comes from `S3_USESSL`;
306
+ * - an origin, `http(s)://host[:port]`: a complete origin, so its scheme wins
307
+ * over `S3_USESSL` and its port, or the scheme's default port, over `S3_PORT`.
308
+ *
309
+ * The host is taken as written, so no URL normalization rewrites IPv4
310
+ * shorthand, IPv4-mapped IPv6, or letter case. User info, a path, a query, a
311
+ * fragment, a scheme other than `http`/`https`, and a port outside 1-65535 are
312
+ * not part of an origin and are not carried over. An endpoint without a host
313
+ * (`:9000`) takes its host from `S3_HOST`, as a missing `S3_ENDPOINT` does.
314
+ * Never throws: 8.0.0 loaded any non-empty `S3_ENDPOINT`.
315
+ */
316
+ export const resolveS3Endpoint = (primitivesArg: TServiceConnectionPrimitives): IS3Endpoint => {
317
+ let rest = primitivesArg.S3_ENDPOINT;
318
+ let useSsl = primitivesArg.S3_USESSL === true;
319
+ let port = primitivesArg.S3_PORT;
320
+ const schemeMatch = s3EndpointSchemePattern.exec(rest);
321
+ if (schemeMatch) {
322
+ rest = rest.slice(schemeMatch[0].length);
323
+ const scheme = schemeMatch[1].toLowerCase();
324
+ if (scheme === 'http' || scheme === 'https') {
325
+ useSsl = scheme === 'https';
326
+ port = useSsl ? '443' : '80';
327
+ }
328
+ }
329
+ const authority = rest.split(/[/?#]/u, 1)[0];
330
+ const hostPort = authority.slice(authority.lastIndexOf('@') + 1);
331
+
332
+ // `[ipv6]:port` or `host:port`; more than one colon without brackets is a bare IPv6 address.
333
+ let host = hostPort;
334
+ let portText: string | undefined;
335
+ const closingBracket = hostPort.startsWith('[') ? hostPort.indexOf(']') : -1;
336
+ const colon = hostPort.indexOf(':');
337
+ if (closingBracket > 0) {
338
+ host = hostPort.slice(1, closingBracket);
339
+ const afterBracket = hostPort.slice(closingBracket + 1);
340
+ portText = afterBracket.startsWith(':') ? afterBracket.slice(1) : undefined;
341
+ } else if (colon !== -1 && colon === hostPort.lastIndexOf(':')) {
342
+ host = hostPort.slice(0, colon);
343
+ portText = hostPort.slice(colon + 1);
344
+ }
345
+ const explicitPort = portText === undefined ? undefined : parseTcpPort(portText);
346
+ if (explicitPort) {
347
+ port = explicitPort;
348
+ }
349
+ if (!host) {
350
+ host = primitivesArg.S3_HOST;
351
+ }
352
+
353
+ const urlHost = host.includes(':') ? `[${host}]` : host;
354
+ const defaultPort = useSsl ? '443' : '80';
355
+ const portSuffix = port === defaultPort ? '' : `:${port}`;
356
+ return {
357
+ host,
358
+ origin: `${useSsl ? 'https' : 'http'}://${urlHost}${portSuffix}`,
359
+ useSsl,
360
+ };
361
+ };
362
+
363
+ /**
364
+ * Derive `MONGODB_URL` and the serve.zone capability environment from the
365
+ * primitives GitZone manages. The single mapping from primitives to both name
366
+ * sets.
367
+ */
368
+ export const deriveServiceConnectionEnvironment = (
369
+ primitivesArg: TServiceConnectionPrimitives,
370
+ ): TDerivedServiceConnectionEnvironment => {
371
+ const mongoUri = buildMongoConnectionUri(primitivesArg);
372
+ const mongoCredentials =
373
+ primitivesArg.MONGODB_AUTH_ENABLED !== false
374
+ ? {
375
+ MONGODB_USERNAME: primitivesArg.MONGODB_USER,
376
+ MONGO_DBUSER: primitivesArg.MONGODB_USER,
377
+ MONGODB_PASSWORD: primitivesArg.MONGODB_PASS,
378
+ MONGO_DBPASS: primitivesArg.MONGODB_PASS,
379
+ }
380
+ : {};
381
+ const s3Endpoint = resolveS3Endpoint(primitivesArg);
382
+ return {
383
+ MONGODB_URL: mongoUri,
384
+ MONGODB_URI: mongoUri,
385
+ MONGO_URL: mongoUri,
386
+ MONGODB_DATABASE: primitivesArg.MONGODB_NAME,
387
+ MONGO_DBNAME: primitivesArg.MONGODB_NAME,
388
+ ...mongoCredentials,
389
+ AWS_ENDPOINT_URL: s3Endpoint.origin,
390
+ S3_ENDPOINT_HOST: s3Endpoint.host,
391
+ S3_USE_SSL: String(s3Endpoint.useSsl),
392
+ AWS_REGION: primitivesArg.S3_REGION,
393
+ AWS_S3_FORCE_PATH_STYLE: 'true',
394
+ S3_ACCESS_KEY: primitivesArg.S3_ACCESSKEY,
395
+ S3_ACCESS_KEY_ID: primitivesArg.S3_ACCESSKEY,
396
+ AWS_ACCESS_KEY_ID: primitivesArg.S3_ACCESSKEY,
397
+ S3_SECRET_KEY: primitivesArg.S3_SECRETKEY,
398
+ S3_SECRET_ACCESS_KEY: primitivesArg.S3_SECRETKEY,
399
+ AWS_SECRET_ACCESS_KEY: primitivesArg.S3_SECRETKEY,
400
+ };
401
+ };
402
+
134
403
  const runtimeStringFields: Array<keyof IServiceConfig> = [
135
404
  'PROJECT_NAME',
136
405
  'MONGODB_HOST',
@@ -139,6 +408,7 @@ const runtimeStringFields: Array<keyof IServiceConfig> = [
139
408
  'MONGODB_USER',
140
409
  'MONGODB_PASS',
141
410
  'MONGODB_URL',
411
+ 'MONGODB_ADMIN_PASSWORD',
142
412
  'S3_HOST',
143
413
  'S3_PORT',
144
414
  'S3_UI_PORT',
@@ -153,6 +423,19 @@ const runtimeStringFields: Array<keyof IServiceConfig> = [
153
423
  'ELASTICSEARCH_USER',
154
424
  'ELASTICSEARCH_PASS',
155
425
  'ELASTICSEARCH_URL',
426
+ 'MONGODB_URI',
427
+ 'MONGO_URL',
428
+ 'MONGODB_DATABASE',
429
+ 'MONGO_DBNAME',
430
+ 'AWS_ENDPOINT_URL',
431
+ 'S3_ENDPOINT_HOST',
432
+ 'AWS_REGION',
433
+ 'S3_ACCESS_KEY',
434
+ 'S3_ACCESS_KEY_ID',
435
+ 'AWS_ACCESS_KEY_ID',
436
+ 'S3_SECRET_KEY',
437
+ 'S3_SECRET_ACCESS_KEY',
438
+ 'AWS_SECRET_ACCESS_KEY',
156
439
  ];
157
440
 
158
441
  const runtimePortFields: Array<keyof IServiceConfig> = [
@@ -194,6 +477,21 @@ export const assertCanonicalServiceRuntimeConfig: (
194
477
  if (config.S3_PORT === config.S3_UI_PORT) {
195
478
  throw new Error('.nogit/env.json S3 API and UI ports must differ.');
196
479
  }
480
+ if (config.S3_USE_SSL !== 'true' && config.S3_USE_SSL !== 'false') {
481
+ throw new Error('.nogit/env.json field S3_USE_SSL must be the string "true" or "false".');
482
+ }
483
+ if (config.AWS_S3_FORCE_PATH_STYLE !== 'true') {
484
+ throw new Error('.nogit/env.json field AWS_S3_FORCE_PATH_STYLE must be the string "true".');
485
+ }
486
+ for (const field of platformDatabaseCredentialFields) {
487
+ if (config.MONGODB_AUTH_ENABLED) {
488
+ if (typeof config[field] !== 'string' || !(config[field] as string).trim()) {
489
+ throw new Error(`.nogit/env.json field ${field} must be a non-empty string.`);
490
+ }
491
+ } else if (Object.hasOwn(config, field)) {
492
+ throw new Error(`.nogit/env.json field ${field} must be absent while MongoDB auth is disabled.`);
493
+ }
494
+ }
197
495
  };
198
496
 
199
497
  export class ServiceConfiguration {
@@ -226,8 +524,9 @@ export class ServiceConfiguration {
226
524
  // value, so a fresh checkout reproduces the declared setup.
227
525
  await this.applyDeclaredServiceOptions();
228
526
 
229
- // Only non-S3 services retain legacy port synchronization. ObjectStorage
230
- // configuration is authoritative and is reconciled on its guarded start path.
527
+ // Only Elasticsearch retains legacy port synchronization. NoSQLDB and
528
+ // ObjectStorage configuration is authoritative and is reconciled on their
529
+ // guarded start paths.
231
530
  await this.syncPortsFromDocker();
232
531
 
233
532
  return this.config;
@@ -272,7 +571,7 @@ export class ServiceConfiguration {
272
571
  }
273
572
 
274
573
  /**
275
- * Whether mongod should enforce authentication. Defaults to true; only an
574
+ * Whether the NoSQLDB engine should enforce authentication. Defaults to true; only an
276
575
  * explicit `false` in `.nogit/env.json` disables it.
277
576
  */
278
577
  public isMongoAuthEnabled(): boolean {
@@ -280,29 +579,34 @@ export class ServiceConfiguration {
280
579
  }
281
580
 
282
581
  /**
283
- * Build MONGODB_URL from the current fields.
284
- *
285
- * Credentials are omitted entirely when auth is disabled, so the stored URL
286
- * never advertises a username that mongod would not accept.
582
+ * Recompute every derived field from the primitive ones.
287
583
  */
288
- private buildMongoUrl(): string {
289
- const credentials = this.isMongoAuthEnabled()
290
- ? `${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@`
291
- : '';
292
- const authQuery = this.isMongoAuthEnabled() ? '?authSource=admin' : '';
293
- return `mongodb://${credentials}${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}${authQuery}`;
584
+ private updateDerivedFields(): void {
585
+ this.reconcileDerivedConnectionFields();
586
+ this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
294
587
  }
295
588
 
296
589
  /**
297
- * Recompute every derived field from the primitive ones.
590
+ * Rewrite `MONGODB_URL` and the serve.zone capability environment from the
591
+ * primitives, in place, and drop the platform database credential names while
592
+ * auth is disabled. Returns the names whose value changed, never the values.
298
593
  */
299
- private updateDerivedFields(): void {
300
- this.config.MONGODB_URL = this.buildMongoUrl();
301
- this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
594
+ private reconcileDerivedConnectionFields(): string[] {
595
+ const previous: Partial<IServiceConfig> = { ...this.config };
596
+ const derived = deriveServiceConnectionEnvironment(this.config);
597
+ Object.assign(this.config, derived);
598
+ for (const field of platformDatabaseCredentialFields) {
599
+ if (!Object.hasOwn(derived, field)) {
600
+ delete this.config[field];
601
+ }
602
+ }
603
+ return serviceDerivedConnectionFieldNames.filter(
604
+ (fieldArg) => previous[fieldArg] !== this.config[fieldArg],
605
+ );
302
606
  }
303
607
 
304
608
  /**
305
- * Refuse an unauthenticated mongod that would be reachable off-host.
609
+ * Refuse an unauthenticated database that would be reachable off-host.
306
610
  *
307
611
  * Disabling auth is only defensible while the database is bound to loopback.
308
612
  * Rather than trusting the operator to keep MONGODB_HOST local, this fails
@@ -321,6 +625,32 @@ export class ServiceConfiguration {
321
625
  }
322
626
  }
323
627
 
628
+ /** Reject NoSQLDB's prohibited defaults and credential reuse before Docker mutation. */
629
+ public assertNoSqlDbCredentialsAreSafe(): void {
630
+ const config = this.config;
631
+ if (
632
+ typeof config.MONGODB_ADMIN_PASSWORD !== 'string' ||
633
+ config.MONGODB_ADMIN_PASSWORD.length === 0
634
+ ) {
635
+ throw new Error('MONGODB_ADMIN_PASSWORD must be a non-empty string in .nogit/env.json.');
636
+ }
637
+ if (config.MONGODB_ADMIN_PASSWORD === 'admin') {
638
+ throw new Error(
639
+ 'NoSQLDB refuses the default admin password. Set a non-default MONGODB_ADMIN_PASSWORD in .nogit/env.json.',
640
+ );
641
+ }
642
+ if (config.MONGODB_ADMIN_PASSWORD === config.MONGODB_PASS) {
643
+ throw new Error(
644
+ 'MONGODB_ADMIN_PASSWORD must be separate from MONGODB_PASS in .nogit/env.json.',
645
+ );
646
+ }
647
+ if (this.isMongoAuthEnabled() && config.MONGODB_PASS === 'admin') {
648
+ throw new Error(
649
+ 'NoSQLDB refuses the default root password. Set a non-default MONGODB_PASS in .nogit/env.json.',
650
+ );
651
+ }
652
+ }
653
+
324
654
  /** Reject ObjectStorage's prohibited defaults and credential reuse before Docker mutation. */
325
655
  public assertObjectStorageCredentialsAreSafe(): void {
326
656
  const config = this.config;
@@ -373,6 +703,8 @@ export class ServiceConfiguration {
373
703
  * Save the configuration to file
374
704
  */
375
705
  public async saveConfig(): Promise<void> {
706
+ // Derived connection fields never reach disk stale, whichever primitive changed.
707
+ this.reconcileDerivedConnectionFields();
376
708
  assertCanonicalServiceRuntimeConfig(this.config);
377
709
  await withInterProcessLock(
378
710
  {
@@ -471,15 +803,15 @@ export class ServiceConfiguration {
471
803
  const esUser = 'elastic';
472
804
  const esPass = 'elastic';
473
805
 
474
- this.config = {
806
+ const primitives: TServiceConfigPrimitives = {
475
807
  PROJECT_NAME: projectName,
476
808
  MONGODB_HOST: mongoHost,
477
809
  MONGODB_NAME: mongoName,
478
810
  MONGODB_PORT: mongoPortStr,
479
811
  MONGODB_USER: mongoUser,
480
812
  MONGODB_PASS: mongoPass,
481
- MONGODB_URL: `mongodb://${mongoUser}:${mongoPass}@${mongoHost}:${mongoPortStr}/${mongoName}?authSource=admin`,
482
813
  MONGODB_AUTH_ENABLED: true,
814
+ MONGODB_ADMIN_PASSWORD: plugins.crypto.randomBytes(24).toString('base64url'),
483
815
  S3_HOST: s3Host,
484
816
  S3_PORT: s3PortStr,
485
817
  S3_UI_PORT: s3UiPort.toString(),
@@ -496,6 +828,7 @@ export class ServiceConfiguration {
496
828
  ELASTICSEARCH_PASS: esPass,
497
829
  ELASTICSEARCH_URL: `http://${esUser}:${esPass}@${esHost}:${esPort}`
498
830
  };
831
+ this.config = { ...primitives, ...deriveServiceConnectionEnvironment(primitives) };
499
832
 
500
833
  assertCanonicalServiceRuntimeConfig(this.config);
501
834
  await this.saveConfig();
@@ -559,14 +892,6 @@ export class ServiceConfiguration {
559
892
  updated = true;
560
893
  }
561
894
 
562
- // Always update MONGODB_URL based on current settings
563
- const oldUrl = this.config.MONGODB_URL;
564
- this.config.MONGODB_URL = this.buildMongoUrl();
565
- if (oldUrl !== this.config.MONGODB_URL) {
566
- fieldsAdded.push('MONGODB_URL');
567
- updated = true;
568
- }
569
-
570
895
  if (!this.config.S3_HOST) {
571
896
  this.config.S3_HOST = 'localhost';
572
897
  fieldsAdded.push('S3_HOST');
@@ -625,6 +950,13 @@ export class ServiceConfiguration {
625
950
  updated = true;
626
951
  }
627
952
 
953
+ // Always rederive MONGODB_URL and the serve.zone capability environment.
954
+ const reconciledFields = this.reconcileDerivedConnectionFields();
955
+ if (reconciledFields.length > 0) {
956
+ fieldsAdded.push(...reconciledFields);
957
+ updated = true;
958
+ }
959
+
628
960
  if (!this.config.ELASTICSEARCH_HOST) {
629
961
  this.config.ELASTICSEARCH_HOST = 'localhost';
630
962
  fieldsAdded.push('ELASTICSEARCH_HOST');
@@ -671,11 +1003,7 @@ export class ServiceConfiguration {
671
1003
  */
672
1004
  public getMongoConnectionString(useNetworkIp: boolean = false): string {
673
1005
  const host = useNetworkIp ? '${networkIp}' : this.config.MONGODB_HOST;
674
- const credentials = this.isMongoAuthEnabled()
675
- ? `${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@`
676
- : '';
677
- const authQuery = this.isMongoAuthEnabled() ? 'authSource=admin&' : '';
678
- return `mongodb://${credentials}${host}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?${authQuery}directConnection=true`;
1006
+ return buildMongoConnectionUri(this.config, host);
679
1007
  }
680
1008
 
681
1009
  /**
@@ -683,7 +1011,8 @@ export class ServiceConfiguration {
683
1011
  */
684
1012
  public getContainerNames() {
685
1013
  return {
686
- mongo: `${this.config.PROJECT_NAME}-mongodb`,
1014
+ nosqldb: `${this.config.PROJECT_NAME}-nosqldb`,
1015
+ legacyMongo: `${this.config.PROJECT_NAME}-mongodb`,
687
1016
  objectstorage: `${this.config.PROJECT_NAME}-objectstorage`,
688
1017
  legacyMinio: `${this.config.PROJECT_NAME}-minio`,
689
1018
  elasticsearch: `${this.config.PROJECT_NAME}-elasticsearch`
@@ -698,7 +1027,7 @@ export class ServiceConfiguration {
698
1027
  */
699
1028
  public getDataDirectories() {
700
1029
  return {
701
- mongo: getServiceDataDirectory(process.cwd(), 'mongodb'),
1030
+ nosqldb: getServiceDataDirectory(process.cwd(), 'mongodb'),
702
1031
  objectstorage: getServiceDataDirectory(process.cwd(), 'objectstorage'),
703
1032
  elasticsearch: getServiceDataDirectory(process.cwd(), 'elasticsearch')
704
1033
  };
@@ -711,22 +1040,6 @@ export class ServiceConfiguration {
711
1040
  const containers = this.getContainerNames();
712
1041
  let updated = false;
713
1042
 
714
- // Check MongoDB container
715
- const mongoStatus = await this.docker.getStatus(containers.mongo);
716
- if (mongoStatus !== 'not_exists') {
717
- const portMappings = await this.docker.getPortMappings(containers.mongo);
718
- // legacy containers map 27017; replica-set containers map port:port
719
- const dockerPort =
720
- portMappings && (portMappings['27017'] ?? Object.values(portMappings)[0]);
721
- if (dockerPort) {
722
- if (this.config.MONGODB_PORT !== dockerPort) {
723
- logger.log('note', `📍 Syncing MongoDB port from Docker: ${dockerPort}`);
724
- this.config.MONGODB_PORT = dockerPort;
725
- updated = true;
726
- }
727
- }
728
- }
729
-
730
1043
  // Check Elasticsearch container
731
1044
  const esStatus = await this.docker.getStatus(containers.elasticsearch);
732
1045
  if (esStatus !== 'not_exists') {
@@ -755,22 +1068,12 @@ export class ServiceConfiguration {
755
1068
  let updated = false;
756
1069
  const containers = this.getContainerNames();
757
1070
 
758
- // Check if containers exist - if they do, ports are fine
759
- const mongoExists = await this.docker.exists(containers.mongo);
1071
+ // Check if the container exists - if it does, its port is fine. NoSQLDB and
1072
+ // ObjectStorage ports are validated on their guarded start paths, after
1073
+ // legacy state has been handled: a legacy MongoDB container legitimately
1074
+ // holds MONGODB_PORT until its data has been migrated.
760
1075
  const esExists = await this.docker.exists(containers.elasticsearch);
761
1076
 
762
- // Only check port availability if containers don't exist
763
- if (!mongoExists) {
764
- const mongoPort = parseInt(this.config.MONGODB_PORT);
765
- if (!(await helpers.isPortAvailable(mongoPort))) {
766
- logger.log('note', `⚠️ MongoDB port ${mongoPort} is in use, finding new port...`);
767
- const newPort = await helpers.getRandomAvailablePort();
768
- this.config.MONGODB_PORT = newPort.toString();
769
- logger.log('ok', `✅ New MongoDB port: ${newPort}`);
770
- updated = true;
771
- }
772
- }
773
-
774
1077
  if (!esExists) {
775
1078
  const esPort = parseInt(this.config.ELASTICSEARCH_PORT);
776
1079
  if (!(await helpers.isPortAvailable(esPort))) {
@@ -790,6 +1093,21 @@ export class ServiceConfiguration {
790
1093
  return updated;
791
1094
  }
792
1095
 
1096
+ /** Validate the NoSQLDB host port only after legacy/conflict checks pass. */
1097
+ public async validateNoSqlDbPort(): Promise<boolean> {
1098
+ const mongoPort = parseInt(this.config.MONGODB_PORT);
1099
+ if (await helpers.isPortAvailable(mongoPort)) {
1100
+ return false;
1101
+ }
1102
+ logger.log('note', `⚠️ NoSQLDB port ${mongoPort} is in use, finding new port...`);
1103
+ const newPort = await helpers.getRandomAvailablePort();
1104
+ this.config.MONGODB_PORT = newPort.toString();
1105
+ logger.log('ok', `✅ New NoSQLDB port: ${newPort}`);
1106
+ this.updateDerivedFields();
1107
+ await this.saveConfig();
1108
+ return true;
1109
+ }
1110
+
793
1111
  /** Validate ObjectStorage host ports only after legacy/conflict checks pass. */
794
1112
  public async validateObjectStoragePorts(): Promise<boolean> {
795
1113
  let updated = false;