@aifabrix/builder 2.47.4 → 2.49.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 (93) hide show
  1. package/jest.isolated-projects.js +7 -0
  2. package/jest.projects.js +16 -0
  3. package/lib/api/applications.api.js +23 -0
  4. package/lib/api/dev.api.js +1 -1
  5. package/lib/api/index.js +5 -2
  6. package/lib/api/types/dev.types.js +4 -3
  7. package/lib/app/deploy-config.js +6 -4
  8. package/lib/app/register.js +21 -19
  9. package/lib/app/rotate-secret.js +39 -16
  10. package/lib/app/run-container-start.js +0 -9
  11. package/lib/app/run-helpers.js +8 -4
  12. package/lib/app/run-reload-sync.js +16 -18
  13. package/lib/app/run-stop-containers.js +48 -5
  14. package/lib/app/run.js +6 -3
  15. package/lib/app/show.js +2 -2
  16. package/lib/build/index.js +12 -49
  17. package/lib/build/integration-docker-staging.js +35 -0
  18. package/lib/build/standard-docker-build.js +113 -0
  19. package/lib/cli/infra-guided-auth.js +42 -6
  20. package/lib/cli/infra-guided-footers.js +164 -0
  21. package/lib/cli/infra-guided-spinners.js +57 -0
  22. package/lib/cli/infra-guided.js +41 -179
  23. package/lib/cli/setup-app.js +5 -1
  24. package/lib/cli/setup-dev.js +3 -2
  25. package/lib/cli/setup-infra.js +3 -2
  26. package/lib/cli/setup-platform.js +3 -0
  27. package/lib/commands/dev-init.js +18 -0
  28. package/lib/commands/login-device.js +6 -2
  29. package/lib/commands/login.js +4 -1
  30. package/lib/commands/setup-modes.js +19 -3
  31. package/lib/commands/setup-platform-auth.js +25 -0
  32. package/lib/commands/setup-prompts-admin.js +9 -0
  33. package/lib/commands/setup-prompts-platform-mode.js +11 -1
  34. package/lib/commands/setup.js +60 -7
  35. package/lib/commands/up-dataplane-bootstrap.js +149 -0
  36. package/lib/commands/up-dataplane-credentials.js +104 -6
  37. package/lib/commands/up-dataplane.js +74 -11
  38. package/lib/core/secrets-env-declarative-expand.js +35 -23
  39. package/lib/core/setup-platform-mode.js +59 -9
  40. package/lib/deployment/deployer-status.js +26 -3
  41. package/lib/deployment/deployer.js +7 -14
  42. package/lib/external-system/download-helpers.js +10 -1
  43. package/lib/infrastructure/compose.js +40 -12
  44. package/lib/infrastructure/services.js +15 -3
  45. package/lib/infrastructure/traefik-tls-dynamic.js +88 -0
  46. package/lib/parameters/infra-kv-discovery.js +4 -3
  47. package/lib/utils/aifabrix-runtime-config-dir.js +2 -1
  48. package/lib/utils/app-bootstrap-api.js +53 -0
  49. package/lib/utils/app-run-containers.js +14 -3
  50. package/lib/utils/applications-config-defaults.js +19 -3
  51. package/lib/utils/compose-generate-docker-compose.js +18 -6
  52. package/lib/utils/compose-generator.js +34 -15
  53. package/lib/utils/compose-traefik-ingress-base.js +13 -14
  54. package/lib/utils/config-paths.js +8 -3
  55. package/lib/utils/dev-ca-docker-augment.js +54 -0
  56. package/lib/utils/dev-user-groups.js +1 -0
  57. package/lib/utils/docker-daemon-tls-ca.js +363 -0
  58. package/lib/utils/docker-reload-mount.js +12 -1
  59. package/lib/utils/ensure-dev-certs-for-remote-docker.js +5 -0
  60. package/lib/utils/external-env-template.js +53 -18
  61. package/lib/utils/health-check-poll-ui.js +84 -0
  62. package/lib/utils/health-check-url.js +54 -34
  63. package/lib/utils/health-check.js +43 -16
  64. package/lib/utils/infra-optional-service-flags.js +31 -2
  65. package/lib/utils/mutagen.js +7 -4
  66. package/lib/utils/platform-controller-url.js +23 -1
  67. package/lib/utils/promote-dev-docker-image.js +170 -0
  68. package/lib/utils/refresh-remote-settings.js +67 -0
  69. package/lib/utils/remote-docker-env.js +22 -7
  70. package/lib/utils/remote-host-for-front-door.js +43 -0
  71. package/lib/utils/run-cli-flags.js +37 -1
  72. package/lib/utils/secrets-helpers.js +9 -14
  73. package/lib/utils/secrets-kv-refs.js +47 -3
  74. package/lib/utils/secrets-missing-error.js +1 -9
  75. package/lib/utils/token-manager-client-credentials.js +11 -1
  76. package/lib/utils/url-declarative-public-base.js +11 -4
  77. package/lib/utils/url-declarative-resolve-build-urls.js +11 -2
  78. package/lib/utils/url-declarative-resolve-expand-token.js +12 -1
  79. package/lib/utils/url-declarative-resolve-load-doc.js +30 -11
  80. package/lib/utils/url-declarative-resolve.js +2 -0
  81. package/lib/utils/urls-local-registry-scan.js +95 -10
  82. package/lib/utils/urls-local-registry.js +24 -1
  83. package/lib/utils/worker-sidecars.js +28 -1
  84. package/lib/validation/external-system-auth-rules.js +45 -7
  85. package/package.json +2 -1
  86. package/scripts/test-dataplane-bootstrap.js +331 -0
  87. package/templates/applications/dataplane/env.template +22 -5
  88. package/templates/applications/dataplane/rbac.yaml +11 -0
  89. package/templates/applications/miso-controller/application.yaml +1 -1
  90. package/templates/applications/miso-controller/env.template +3 -0
  91. package/templates/infra/compose.yaml.hbs +13 -9
  92. package/templates/python/docker-compose.hbs +3 -0
  93. package/templates/typescript/docker-compose.hbs +3 -0
@@ -116,7 +116,7 @@ function buildImageConfig(config, appName, imageOverride) {
116
116
  * @param {string|null|undefined} [readinessSuffix] - Path after vdir (default /health/ready or healthCheck.readinessPathSuffix)
117
117
  * @returns {string} Absolute path starting with /
118
118
  */
119
- function computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, readinessSuffix) {
119
+ function computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, readinessSuffix, developerRootDomain) {
120
120
  const hc = config.healthCheck || {};
121
121
  let suffix = '/health/ready';
122
122
  if (
@@ -128,7 +128,7 @@ function computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, r
128
128
  } else if (typeof hc.readinessPathSuffix === 'string' && hc.readinessPathSuffix.startsWith('/')) {
129
129
  suffix = hc.readinessPathSuffix;
130
130
  }
131
- const traefik = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer);
131
+ const traefik = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer, developerRootDomain);
132
132
  if (traefik.enabled === true) {
133
133
  const p = traefik.path;
134
134
  if (!p || p === '/') {
@@ -158,7 +158,7 @@ function computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, r
158
158
  * @param {{ skipVdirMergeWhenPathIsBareHealth?: boolean }} [opts] - When `skipVdirMergeWhenPathIsBareHealth`, path `/health` is left as-is (in-container probe; ingress strips vdir for miso/dataplane). Deployment JSON health paths do **not** use this helper — see `buildHealthCheck` in `builders.js` (YAML paths only).
159
159
  * @returns {string} Absolute path starting with /
160
160
  */
161
- function resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remoteServer, opts) {
161
+ function resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remoteServer, opts, developerRootDomain) {
162
162
  const hc = config.healthCheck || {};
163
163
  const raw = typeof hc.path === 'string' && hc.path.startsWith('/') ? hc.path : '/health';
164
164
  const fd = config.frontDoorRouting;
@@ -168,7 +168,7 @@ function resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remot
168
168
  if (opts && opts.skipVdirMergeWhenPathIsBareHealth === true && raw === '/health') {
169
169
  return raw;
170
170
  }
171
- const traefik = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer);
171
+ const traefik = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer, developerRootDomain);
172
172
  if (traefik.enabled !== true) {
173
173
  return raw;
174
174
  }
@@ -178,7 +178,7 @@ function resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remot
178
178
  ? ''
179
179
  : String(vdirRaw).replace(/\/+$/, '');
180
180
  if (!vdir) {
181
- return computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, raw);
181
+ return computeAlignedHealthCheckPath(config, devId, scopeOpts, remoteServer, raw, developerRootDomain);
182
182
  }
183
183
  if (raw === vdir || raw.startsWith(`${vdir}/`)) {
184
184
  return raw;
@@ -192,13 +192,14 @@ function resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remot
192
192
  * @param {string|number} devId - Developer id
193
193
  * @param {Object|null} scopeOpts - Env-scoped compose options
194
194
  * @param {string|null|undefined} remoteServer - Remote server URL
195
+ * @param {string|null|undefined} [developerRootDomain] - App hostname root
195
196
  * @returns {Object} Health check configuration
196
197
  */
197
- function buildHealthCheckConfig(config, devId, scopeOpts, remoteServer) {
198
+ function buildHealthCheckConfig(config, devId, scopeOpts, remoteServer, developerRootDomain) {
198
199
  const hc = config.healthCheck || {};
199
200
  const path = resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remoteServer, {
200
201
  skipVdirMergeWhenPathIsBareHealth: true
201
- });
202
+ }, developerRootDomain);
202
203
  return {
203
204
  path,
204
205
  interval: hc.interval || 30,
@@ -230,10 +231,11 @@ function buildDevUsername(devId) {
230
231
  * @param {Object|null} scopeOpts - Env-scoped Traefik path
231
232
  * @param {string|null|undefined} remoteServer - From config (for ${REMOTE_HOST})
232
233
  * @param {string|undefined} [resolvedHealthPathForStrip] - Compose health path; when omitted, derived via resolveHealthCheckPathWithFrontDoorVdir (same as probe)
234
+ * @param {string|null|undefined} [developerRootDomain] - App hostname root from settings
233
235
  * @returns {Object} Traefik configuration object
234
236
  */
235
- function buildTraefikConfig(config, devId, scopeOpts = null, remoteServer = null, resolvedHealthPathForStrip) {
236
- const base = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer);
237
+ function buildTraefikConfig(config, devId, scopeOpts = null, remoteServer = null, resolvedHealthPathForStrip, developerRootDomain) {
238
+ const base = buildTraefikIngressBase(config, devId, scopeOpts, remoteServer, developerRootDomain);
237
239
  if (!base.enabled) {
238
240
  return { enabled: false };
239
241
  }
@@ -242,7 +244,7 @@ function buildTraefikConfig(config, devId, scopeOpts = null, remoteServer = null
242
244
  ? String(resolvedHealthPathForStrip)
243
245
  : resolveHealthCheckPathWithFrontDoorVdir(config, devId, scopeOpts, remoteServer, {
244
246
  skipVdirMergeWhenPathIsBareHealth: true
245
- });
247
+ }, developerRootDomain);
246
248
  const devNum = parseDeveloperIdNum(devId);
247
249
  // Shared builder host: nginx terminates TLS on :443 and proxies HTTP to per-dev Traefik :80 (e.g. 280).
248
250
  // Traefik must only attach routers to `web` for developer-id > 0; `websecure`+tls labels without
@@ -282,6 +284,7 @@ function buildRequiresConfig(config) {
282
284
  * @param {string} [runExtras.imageOverride] - Full image reference for run (e.g. from --image)
283
285
  * @param {Object|null} [runExtras.scopeOpts] - Traefik / scoped compose options
284
286
  * @param {string|null|undefined} [runExtras.remoteServer] - For ${REMOTE_HOST} in frontDoorRouting.host
287
+ * @param {string|null|undefined} [runExtras.developerRootDomain] - App hostname root (overrides remote-server host)
285
288
  * @param {boolean} [runExtras.omitAppTraefikLabels] - When true (user config `traefik: false`), omit Traefik labels and BASE_PATH env from compose even if application.yaml enables frontDoorRouting
286
289
  * @returns {Object} Service configuration
287
290
  */
@@ -290,6 +293,7 @@ function buildServiceConfig(appName, config, port, devId, runExtras = {}) {
290
293
  imageOverride = null,
291
294
  scopeOpts = null,
292
295
  remoteServer = null,
296
+ developerRootDomain = null,
293
297
  omitAppTraefikLabels = false
294
298
  } = runExtras;
295
299
  const containerPortValue = getContainerPort(config, 3000);
@@ -303,7 +307,7 @@ function buildServiceConfig(appName, config, port, devId, runExtras = {}) {
303
307
  ? composeTraefikServiceKey(appName, scopeOpts.runEnvKey)
304
308
  : null;
305
309
  const scopeForHealthAndTraefik = useTraefikScope ? scopeOpts : null;
306
- const healthCheck = buildHealthCheckConfig(config, devId, scopeForHealthAndTraefik, remoteServer);
310
+ const healthCheck = buildHealthCheckConfig(config, devId, scopeForHealthAndTraefik, remoteServer, developerRootDomain);
307
311
  return {
308
312
  app: buildAppConfig(appName, config, composeAppKey),
309
313
  image: buildImageConfig(config, appName, imageOverride),
@@ -319,7 +323,8 @@ function buildServiceConfig(appName, config, port, devId, runExtras = {}) {
319
323
  devId,
320
324
  scopeForHealthAndTraefik,
321
325
  remoteServer,
322
- healthCheck.path
326
+ healthCheck.path,
327
+ developerRootDomain
323
328
  ),
324
329
  ...buildRequiresConfig(config)
325
330
  };
@@ -379,20 +384,34 @@ function buildNetworkAndContainerNames(appName, devId, idNum, scopeOpts = null)
379
384
  }
380
385
 
381
386
  const { createGenerateDockerCompose } = require('./compose-generate-docker-compose');
382
- async function getRemoteServerForCompose() {
387
+ async function getFrontDoorHostContextForCompose() {
383
388
  try {
384
389
  const rs = await config.getRemoteServer();
385
- return rs && String(rs).trim() ? String(rs).trim() : null;
390
+ const remoteServer = rs && String(rs).trim() ? String(rs).trim() : null;
391
+ let developerRootDomain = null;
392
+ try {
393
+ const root = await config.getDeveloperRootDomain();
394
+ developerRootDomain = root && String(root).trim() ? String(root).trim() : null;
395
+ } catch {
396
+ developerRootDomain = null;
397
+ }
398
+ return { remoteServer, developerRootDomain };
386
399
  } catch {
387
- return null;
400
+ return { remoteServer: null, developerRootDomain: null };
388
401
  }
389
402
  }
390
403
 
404
+ async function getRemoteServerForCompose() {
405
+ const ctx = await getFrontDoorHostContextForCompose();
406
+ return ctx.remoteServer;
407
+ }
408
+
391
409
  const generateDockerCompose = createGenerateDockerCompose({
392
410
  loadDockerComposeTemplate,
393
411
  resolveComposeImageOverrideString,
394
412
  getDeveloperIdAndNumeric,
395
413
  getRemoteServerForCompose,
414
+ getFrontDoorHostContextForCompose,
396
415
  buildNetworkAndContainerNames,
397
416
  buildServiceConfig,
398
417
  buildVolumesConfig,
@@ -10,6 +10,10 @@ const {
10
10
  buildEnvScopedTraefikPath
11
11
  } = require('./environment-scoped-resources');
12
12
  const { parseDeveloperIdNum } = require('./declarative-url-ports');
13
+ const {
14
+ resolveRemoteHostForFrontDoor,
15
+ hasFrontDoorRemoteHostContext
16
+ } = require('./remote-host-for-front-door');
13
17
 
14
18
  /**
15
19
  * Derives base path from routing pattern by removing trailing wildcards
@@ -66,21 +70,15 @@ function buildDevUsernameForFrontDoorHost(devId) {
66
70
  * @param {string} template
67
71
  * @param {string|number|null|undefined} developerIdRaw
68
72
  * @param {string|null|undefined} remoteServer
73
+ * @param {string|null|undefined} [developerRootDomain] - App hostname root (overrides remote-server hostname for ${REMOTE_HOST})
69
74
  * @returns {string}
70
75
  */
71
- function expandFrontDoorHostPlaceholders(template, developerIdRaw, remoteServer) {
76
+ function expandFrontDoorHostPlaceholders(template, developerIdRaw, remoteServer, developerRootDomain) {
72
77
  let t = String(template || '');
73
78
  t = t.replace(/\$\{DEV_USERNAME\}\$\{REMOTE_HOST\}/g, '${DEV_USERNAME}.${REMOTE_HOST}');
74
79
  const devU = buildDevUsernameForFrontDoorHost(developerIdRaw);
75
80
  t = t.replace(/\$\{DEV_USERNAME\}/g, devU);
76
- let remoteHost = '';
77
- try {
78
- if (remoteServer && String(remoteServer).trim()) {
79
- remoteHost = new URL(String(remoteServer).trim()).hostname;
80
- }
81
- } catch {
82
- remoteHost = '';
83
- }
81
+ const remoteHost = resolveRemoteHostForFrontDoor({ remoteServer, developerRootDomain });
84
82
  t = t.replace(/\$\{REMOTE_HOST\}/g, remoteHost);
85
83
  t = t.replace(/^\.+/g, '').replace(/\.{2,}/g, '.').replace(/\.+$/g, '').trim();
86
84
  return t;
@@ -90,13 +88,13 @@ function templateUsesRemoteHostPlaceholder(hostTemplate) {
90
88
  return /\$\{REMOTE_HOST\}/.test(String(hostTemplate || ''));
91
89
  }
92
90
 
93
- function resolveTraefikRouterHost(hostTemplate, developerIdRaw, remoteServer) {
91
+ function resolveTraefikRouterHost(hostTemplate, developerIdRaw, remoteServer, developerRootDomain) {
94
92
  const usesRemote = templateUsesRemoteHostPlaceholder(hostTemplate);
95
- const hasRemote = Boolean(remoteServer && String(remoteServer).trim());
93
+ const hasRemote = hasFrontDoorRemoteHostContext({ remoteServer, developerRootDomain });
96
94
  if (usesRemote && !hasRemote) {
97
95
  return 'localhost';
98
96
  }
99
- return expandFrontDoorHostPlaceholders(hostTemplate, developerIdRaw, remoteServer);
97
+ return expandFrontDoorHostPlaceholders(hostTemplate, developerIdRaw, remoteServer, developerRootDomain);
100
98
  }
101
99
 
102
100
  /**
@@ -108,9 +106,10 @@ function resolveTraefikRouterHost(hostTemplate, developerIdRaw, remoteServer) {
108
106
  * @param {string|number} devId - Developer id for host expansion
109
107
  * @param {Object|null} scopeOpts - Env-scoped Traefik path (effectiveEnvironmentScopedResources, runEnvKey)
110
108
  * @param {string|null|undefined} remoteServer - For ${REMOTE_HOST}
109
+ * @param {string|null|undefined} [developerRootDomain] - App hostname root from settings
111
110
  * @returns {{ enabled: false } | { enabled: true, host: string, path: string, tls: boolean, certStore: string|null }}
112
111
  */
113
- function buildTraefikIngressBase(config, devId, scopeOpts, remoteServer) {
112
+ function buildTraefikIngressBase(config, devId, scopeOpts, remoteServer, developerRootDomain) {
114
113
  const frontDoor = config.frontDoorRouting;
115
114
  if (!frontDoor || frontDoor.enabled !== true) {
116
115
  return { enabled: false };
@@ -118,7 +117,7 @@ function buildTraefikIngressBase(config, devId, scopeOpts, remoteServer) {
118
117
  if (!frontDoor.host || typeof frontDoor.host !== 'string') {
119
118
  throw new Error('frontDoorRouting.host is required when frontDoorRouting.enabled is true');
120
119
  }
121
- const host = resolveTraefikRouterHost(frontDoor.host, devId, remoteServer);
120
+ const host = resolveTraefikRouterHost(frontDoor.host, devId, remoteServer, developerRootDomain);
122
121
  const basePath = derivePathFromPattern(frontDoor.pattern);
123
122
  let pathOut = basePath;
124
123
  if (
@@ -22,6 +22,7 @@ const SETTINGS_RESPONSE_KEYS = [
22
22
  'aifabrix-env-config',
23
23
  'remote-server',
24
24
  'docker-endpoint',
25
+ 'developer-root-domain',
25
26
  'docker-tls-skip-verify',
26
27
  'sync-ssh-user',
27
28
  'sync-ssh-host'
@@ -264,8 +265,8 @@ function createRemoteConfigGetters(getConfigFn) {
264
265
  return getPathConfig(getConfigFn, 'docker-endpoint');
265
266
  },
266
267
  /**
267
- * When true, Docker CLI may use DOCKER_TLS_VERIFY=0 only when ca.pem is absent (no trust anchor).
268
- * If ca.pem exists (e.g. after issue-cert), the daemon is always verified regardless of this flag.
268
+ * When true, Docker CLI uses DOCKER_TLS_VERIFY=0 (client certs still sent when present).
269
+ * Applies even when ca.pem exists (internal issue-cert CA vs public Docker daemon cert).
269
270
  */
270
271
  async getDockerTlsSkipVerify() {
271
272
  const envRaw = process.env.AIFABRIX_DOCKER_TLS_SKIP_VERIFY;
@@ -290,6 +291,9 @@ function createRemoteConfigGetters(getConfigFn) {
290
291
  },
291
292
  async getSyncSshHost() {
292
293
  return getPathConfig(getConfigFn, 'sync-ssh-host');
294
+ },
295
+ async getDeveloperRootDomain() {
296
+ return getPathConfig(getConfigFn, 'developer-root-domain');
293
297
  }
294
298
  };
295
299
  }
@@ -424,6 +428,7 @@ module.exports = {
424
428
  resolveEnvConfigPathToAbsolute,
425
429
  SETTINGS_RESPONSE_KEYS,
426
430
  applySecretsUrlFromRemote,
427
- applySyncAndDockerFromHost
431
+ applySyncAndDockerFromHost,
432
+ hostnameFromUrl
428
433
  };
429
434
 
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Persist ca.pem after merging Docker daemon TLS trust (wildcard/public CA).
3
+ *
4
+ * @fileoverview Write augmented ca.pem for remote Docker
5
+ * @author AI Fabrix Team
6
+ * @version 1.0.0
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ const fs = require('fs').promises;
12
+ const path = require('path');
13
+ const { normalizePemNewlines, getCertDir } = require('./dev-cert-helper');
14
+ const {
15
+ resolveDockerEndpointForCaAugment,
16
+ augmentCaPemWithDockerDaemonChain,
17
+ writeDockerDaemonCaMarker
18
+ } = require('./docker-daemon-tls-ca');
19
+
20
+ /**
21
+ * @param {string} configDir
22
+ * @param {string} devId
23
+ * @param {string|null|undefined} existingCaPem
24
+ * @param {{ settings?: Object|null, remoteServerUrl?: string|null, warn?: (msg: string) => void }} ctx
25
+ * @returns {Promise<string|null>}
26
+ */
27
+ async function augmentAndSaveCaPemForRemoteDocker(configDir, devId, existingCaPem, ctx) {
28
+ const dockerEndpoint = resolveDockerEndpointForCaAugment({
29
+ settings: ctx && ctx.settings,
30
+ remoteServerUrl: ctx && ctx.remoteServerUrl
31
+ });
32
+ if (!dockerEndpoint) {
33
+ return existingCaPem || null;
34
+ }
35
+
36
+ const augmented = await augmentCaPemWithDockerDaemonChain(existingCaPem, dockerEndpoint, {
37
+ warn: ctx && ctx.warn
38
+ });
39
+ if (!augmented || augmented === existingCaPem) {
40
+ return existingCaPem || null;
41
+ }
42
+
43
+ const certDir = getCertDir(configDir, devId);
44
+ await fs.mkdir(certDir, { recursive: true });
45
+ await fs.writeFile(
46
+ path.join(certDir, 'ca.pem'),
47
+ normalizePemNewlines(augmented),
48
+ { mode: 0o600 }
49
+ );
50
+ await writeDockerDaemonCaMarker(certDir);
51
+ return augmented;
52
+ }
53
+
54
+ module.exports = { augmentAndSaveCaPemForRemoteDocker };
@@ -8,6 +8,7 @@
8
8
  const ALLOWED_DEV_GROUPS = Object.freeze([
9
9
  'admin',
10
10
  'secret-manager',
11
+ 'secret-reader',
11
12
  'developer',
12
13
  'docker'
13
14
  ]);
@@ -0,0 +1,363 @@
1
+ /**
2
+ * Fetch Docker Engine TLS trust anchors when the daemon uses a public/wildcard server
3
+ * certificate (e.g. Sectigo *.aifabrix.dev) while issue-cert ca.pem holds the internal CA.
4
+ * Docker CLI verifies the daemon with ca.pem only (not the OS trust store).
5
+ *
6
+ * @fileoverview Docker daemon TLS CA chain for remote docker-endpoint
7
+ * @author AI Fabrix Team
8
+ * @version 1.0.0
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const tls = require('tls');
14
+ const { mergeCaPemBlocks } = require('./dev-cert-helper');
15
+
16
+ const DEFAULT_DOCKER_TLS_PORT = 2376;
17
+ const DEFAULT_FETCH_TIMEOUT_MS = 10000;
18
+ /** Written under ~/.aifabrix/certs/<developer-id>/ when ca.pem includes Docker daemon trust. */
19
+ const DOCKER_DAEMON_CA_MARKER = '.docker-daemon-ca-augmented';
20
+
21
+ /**
22
+ * @param {Buffer|string} der
23
+ * @returns {string}
24
+ */
25
+ function derCertToPem(der) {
26
+ const buf = Buffer.isBuffer(der) ? der : Buffer.from(der);
27
+ const lines = buf.toString('base64').match(/.{1,64}/g) || [];
28
+ return `-----BEGIN CERTIFICATE-----\n${lines.join('\n')}\n-----END CERTIFICATE-----`;
29
+ }
30
+
31
+ /**
32
+ * @param {import('tls').PeerCertificate} peerCert
33
+ * @returns {string[]}
34
+ */
35
+ function collectIssuerChainPems(peerCert) {
36
+ const pems = [];
37
+ const seen = new Set();
38
+ let cert = peerCert && peerCert.issuerCertificate;
39
+ while (cert && cert.raw) {
40
+ const fp = cert.fingerprint256 || cert.fingerprint;
41
+ if (fp) {
42
+ if (seen.has(fp)) break;
43
+ seen.add(fp);
44
+ }
45
+ if (peerCert.fingerprint && cert.fingerprint === peerCert.fingerprint) break;
46
+ pems.push(derCertToPem(cert.raw));
47
+ const next = cert.issuerCertificate;
48
+ if (!next || next === cert) break;
49
+ cert = next;
50
+ }
51
+ return pems;
52
+ }
53
+
54
+ /**
55
+ * @param {string} rest - Host[:port] after tcp://
56
+ * @returns {{ host: string, port: number }|null}
57
+ */
58
+ function parseBracketedTcpHostPort(rest) {
59
+ if (!rest.startsWith('[')) return null;
60
+ const end = rest.indexOf(']');
61
+ if (end === -1) return null;
62
+ const host = rest.slice(1, end);
63
+ let port = DEFAULT_DOCKER_TLS_PORT;
64
+ const after = rest.slice(end + 1);
65
+ if (after.startsWith(':')) {
66
+ const p = parseInt(after.slice(1), 10);
67
+ if (!Number.isNaN(p)) port = p;
68
+ }
69
+ return { host, port };
70
+ }
71
+
72
+ /**
73
+ * @param {string} rest - Host[:port] after tcp://
74
+ * @returns {{ host: string, port: number }}
75
+ */
76
+ function parsePlainTcpHostPort(rest) {
77
+ const colonIdx = rest.lastIndexOf(':');
78
+ if (colonIdx > 0 && /^\d+$/.test(rest.slice(colonIdx + 1))) {
79
+ return {
80
+ host: rest.slice(0, colonIdx),
81
+ port: parseInt(rest.slice(colonIdx + 1), 10)
82
+ };
83
+ }
84
+ return { host: rest, port: DEFAULT_DOCKER_TLS_PORT };
85
+ }
86
+
87
+ /**
88
+ * @param {string} trimmed
89
+ * @returns {{ host: string, port: number }|null}
90
+ */
91
+ function parseTcpSchemeEndpoint(trimmed) {
92
+ const rest = trimmed.slice(6).split('/')[0];
93
+ return parseBracketedTcpHostPort(rest) || parsePlainTcpHostPort(rest);
94
+ }
95
+
96
+ /**
97
+ * @param {string} trimmed
98
+ * @returns {{ host: string, port: number }|null}
99
+ */
100
+ function parseDockerEndpointViaUrl(trimmed) {
101
+ try {
102
+ const u = new URL(trimmed.includes('://') ? trimmed : `tcp://${trimmed}`);
103
+ const host = String(u.hostname || '').trim();
104
+ if (!host) return null;
105
+ let port = DEFAULT_DOCKER_TLS_PORT;
106
+ if (u.port) {
107
+ const p = parseInt(u.port, 10);
108
+ if (!Number.isNaN(p)) port = p;
109
+ }
110
+ return { host, port };
111
+ } catch {
112
+ return null;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Parse docker-endpoint (tcp://host:port) into TLS connect parameters.
118
+ * @param {string} endpoint
119
+ * @returns {{ host: string, port: number, servername: string }|null}
120
+ */
121
+ function parseDockerTlsEndpoint(endpoint) {
122
+ const trimmed = String(endpoint || '').trim();
123
+ if (!trimmed) return null;
124
+ const lower = trimmed.toLowerCase();
125
+ if (lower.startsWith('unix:')) return null;
126
+
127
+ const parsed = lower.startsWith('tcp://')
128
+ ? parseTcpSchemeEndpoint(trimmed)
129
+ : parseDockerEndpointViaUrl(trimmed);
130
+ if (!parsed || !parsed.host) return null;
131
+ return { host: parsed.host, port: parsed.port, servername: parsed.host };
132
+ }
133
+
134
+ /**
135
+ * @param {string} remoteServerUrl
136
+ * @returns {string|null}
137
+ */
138
+ function dockerEndpointFromRemoteServer(remoteServerUrl) {
139
+ try {
140
+ const u = new URL(String(remoteServerUrl || '').trim().replace(/\/+$/, ''));
141
+ if (!u.hostname) return null;
142
+ return `tcp://${u.hostname}:${DEFAULT_DOCKER_TLS_PORT}`;
143
+ } catch {
144
+ return null;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Resolve docker-endpoint for CA augmentation (settings override, else derive from remote-server).
150
+ * @param {{ settings?: Object|null, remoteServerUrl?: string|null }} ctx
151
+ * @returns {string|null}
152
+ */
153
+ function resolveDockerEndpointForCaAugment(ctx) {
154
+ const settings = ctx && ctx.settings;
155
+ const fromSettings = settings && settings['docker-endpoint'];
156
+ if (typeof fromSettings === 'string' && fromSettings.trim()) {
157
+ return fromSettings.trim();
158
+ }
159
+ return dockerEndpointFromRemoteServer(ctx && ctx.remoteServerUrl);
160
+ }
161
+
162
+ /**
163
+ * Connect to Docker Engine TLS port and return issuer-chain PEM for daemon server verification.
164
+ * @param {string} dockerEndpoint - e.g. tcp://builder01.aifabrix.dev:2376
165
+ * @param {{ timeoutMs?: number }} [opts]
166
+ * @returns {Promise<string|null>}
167
+ */
168
+ function fetchDockerDaemonTlsCaPem(dockerEndpoint, opts) {
169
+ const parsed = parseDockerTlsEndpoint(dockerEndpoint);
170
+ if (!parsed) {
171
+ return Promise.resolve(null);
172
+ }
173
+ const timeoutMs =
174
+ opts && typeof opts.timeoutMs === 'number' && opts.timeoutMs > 0
175
+ ? opts.timeoutMs
176
+ : DEFAULT_FETCH_TIMEOUT_MS;
177
+
178
+ return new Promise((resolve, reject) => {
179
+ let settled = false;
180
+
181
+ const socket = tls.connect(
182
+ {
183
+ host: parsed.host,
184
+ port: parsed.port,
185
+ servername: parsed.servername,
186
+ rejectUnauthorized: false
187
+ },
188
+ () => {
189
+ try {
190
+ const peer = socket.getPeerCertificate(true);
191
+ const chain = collectIssuerChainPems(peer);
192
+ socket.end();
193
+ finish(resolve, chain.length ? chain.join('\n\n') : null);
194
+ } catch (err) {
195
+ socket.destroy();
196
+ finish(reject, err);
197
+ }
198
+ }
199
+ );
200
+
201
+ const timer = setTimeout(() => {
202
+ socket.destroy(new Error(`Timed out fetching Docker daemon TLS chain (${timeoutMs}ms)`));
203
+ }, timeoutMs);
204
+
205
+ function finish(fn, value) {
206
+ if (settled) return;
207
+ settled = true;
208
+ clearTimeout(timer);
209
+ fn(value);
210
+ }
211
+
212
+ socket.on('error', (err) => {
213
+ finish(reject, err);
214
+ });
215
+ });
216
+ }
217
+
218
+ /**
219
+ * Merge Docker daemon public/wildcard trust anchors into ca.pem blocks.
220
+ * @param {string|null|undefined} existingCaPem
221
+ * @param {string|null|undefined} dockerEndpoint
222
+ * @param {{ timeoutMs?: number, warn?: (msg: string) => void }} [opts]
223
+ * @returns {Promise<string|null>}
224
+ */
225
+ async function augmentCaPemWithDockerDaemonChain(existingCaPem, dockerEndpoint, opts) {
226
+ if (!dockerEndpoint || typeof dockerEndpoint !== 'string' || !dockerEndpoint.trim()) {
227
+ return existingCaPem || null;
228
+ }
229
+ try {
230
+ const dockerCaPem = await fetchDockerDaemonTlsCaPem(dockerEndpoint.trim(), opts);
231
+ if (!dockerCaPem) {
232
+ return existingCaPem || null;
233
+ }
234
+ return mergeCaPemBlocks(existingCaPem, dockerCaPem);
235
+ } catch (err) {
236
+ const warn = opts && typeof opts.warn === 'function' ? opts.warn : null;
237
+ const msg = err && err.message ? err.message : String(err);
238
+ if (warn) {
239
+ warn(
240
+ `Could not fetch Docker daemon TLS chain from ${dockerEndpoint.trim()} (${msg}). ` +
241
+ 'Remote docker may fail until ca.pem includes the daemon trust anchor (re-run dev init or dev refresh).'
242
+ );
243
+ }
244
+ return existingCaPem || null;
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Split PEM file into individual certificate blocks.
250
+ * @param {string} pem
251
+ * @returns {string[]}
252
+ */
253
+ function splitPemCertificateBlocks(pem) {
254
+ if (!pem || typeof pem !== 'string') return [];
255
+ return pem
256
+ .replace(/\r\n/g, '\n')
257
+ .split(/(?=-----BEGIN CERTIFICATE-----)/)
258
+ .map((block) => block.trim())
259
+ .filter(Boolean);
260
+ }
261
+
262
+ /**
263
+ * True when existing ca.pem already contains every issuer from the live Docker daemon chain.
264
+ * @param {string|null|undefined} existingCaPem
265
+ * @param {string|null|undefined} dockerEndpoint
266
+ * @param {{ timeoutMs?: number }} [opts]
267
+ * @returns {Promise<boolean>}
268
+ */
269
+ async function caPemTrustsDockerDaemon(existingCaPem, dockerEndpoint, opts) {
270
+ if (!existingCaPem || !dockerEndpoint) return false;
271
+ try {
272
+ const dockerChain = await fetchDockerDaemonTlsCaPem(dockerEndpoint.trim(), opts);
273
+ if (!dockerChain) return false;
274
+ const normalizedExisting = String(existingCaPem).replace(/\r\n/g, '\n');
275
+ const blocks = splitPemCertificateBlocks(dockerChain);
276
+ if (!blocks.length) return false;
277
+ return blocks.every((block) => normalizedExisting.includes(block));
278
+ } catch {
279
+ return false;
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @param {string} certDir
285
+ * @returns {Promise<void>}
286
+ */
287
+ async function writeDockerDaemonCaMarker(certDir) {
288
+ const fs = require('fs').promises;
289
+ const markerPath = require('path').join(certDir, DOCKER_DAEMON_CA_MARKER);
290
+ await fs.writeFile(markerPath, `augmentedAt=${new Date().toISOString()}\n`, { mode: 0o600 });
291
+ }
292
+
293
+ /**
294
+ * @param {string} certDir
295
+ * @param {string} existingCaPem
296
+ * @param {string} dockerEndpoint
297
+ * @param {{ timeoutMs?: number }} fetchOpts
298
+ * @returns {Promise<boolean>}
299
+ */
300
+ async function augmentCaPemForDockerVerify(certDir, existingCaPem, dockerEndpoint, fetchOpts) {
301
+ const fs = require('fs');
302
+ const path = require('path');
303
+ if (await caPemTrustsDockerDaemon(existingCaPem, dockerEndpoint, fetchOpts)) {
304
+ await writeDockerDaemonCaMarker(certDir);
305
+ return true;
306
+ }
307
+ const augmented = await augmentCaPemWithDockerDaemonChain(existingCaPem, dockerEndpoint, fetchOpts);
308
+ if (!augmented || augmented === existingCaPem) return false;
309
+ const { normalizePemNewlines } = require('./dev-cert-helper');
310
+ await fs.promises.writeFile(
311
+ path.join(certDir, 'ca.pem'),
312
+ normalizePemNewlines(augmented),
313
+ { mode: 0o600 }
314
+ );
315
+ await writeDockerDaemonCaMarker(certDir);
316
+ return true;
317
+ }
318
+
319
+ /**
320
+ * Decide whether Docker CLI should verify the daemon certificate (DOCKER_TLS_VERIFY=1).
321
+ * When ca.pem holds only the internal issue-cert CA but the daemon uses a public/wildcard cert,
322
+ * returns false so client mTLS still works with DOCKER_TLS_VERIFY=0.
323
+ *
324
+ * @param {{ certDir: string, existingCaPem: string, dockerEndpoint: string|null, remoteServerUrl?: string|null, settings?: Object|null, skipVerify?: boolean }} ctx
325
+ * @returns {Promise<boolean>}
326
+ */
327
+ async function resolveRemoteDockerDaemonVerify(ctx) {
328
+ if (ctx && ctx.skipVerify) return false;
329
+ const certDir = ctx && ctx.certDir;
330
+ const existingCaPem = ctx && ctx.existingCaPem;
331
+ if (!certDir || !existingCaPem) return false;
332
+
333
+ const fs = require('fs');
334
+ const path = require('path');
335
+ const markerPath = path.join(certDir, DOCKER_DAEMON_CA_MARKER);
336
+ if (fs.existsSync(markerPath)) return true;
337
+
338
+ const dockerEndpoint = resolveDockerEndpointForCaAugment({
339
+ settings: ctx.settings,
340
+ remoteServerUrl: ctx.remoteServerUrl
341
+ }) || (ctx.dockerEndpoint && String(ctx.dockerEndpoint).trim()) || null;
342
+ if (!dockerEndpoint) return true;
343
+
344
+ const fetchOpts = { timeoutMs: DEFAULT_FETCH_TIMEOUT_MS };
345
+ return augmentCaPemForDockerVerify(certDir, existingCaPem, dockerEndpoint, fetchOpts);
346
+ }
347
+
348
+ module.exports = {
349
+ DEFAULT_DOCKER_TLS_PORT,
350
+ DEFAULT_FETCH_TIMEOUT_MS,
351
+ DOCKER_DAEMON_CA_MARKER,
352
+ parseDockerTlsEndpoint,
353
+ dockerEndpointFromRemoteServer,
354
+ resolveDockerEndpointForCaAugment,
355
+ fetchDockerDaemonTlsCaPem,
356
+ augmentCaPemWithDockerDaemonChain,
357
+ caPemTrustsDockerDaemon,
358
+ writeDockerDaemonCaMarker,
359
+ resolveRemoteDockerDaemonVerify,
360
+ splitPemCertificateBlocks,
361
+ derCertToPem,
362
+ collectIssuerChainPems
363
+ };