|
8 | 8 | // Trace cases use the OPENCLAW_OTEL_PRELOADED seam to retain this file's tracer provider. |
9 | 9 | // Collector-boundary cases start the real NodeSDK, so teardown restores every global SDK |
10 | 10 | // registration; otherwise a shutdown provider would poison later real-SDK cases. |
| 11 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import path from "node:path"; |
11 | 14 | import { context, diag, DiagLogLevel, metrics, propagation, trace } from "@opentelemetry/api"; |
12 | 15 | import { logs } from "@opentelemetry/api-logs"; |
13 | 16 | import { |
@@ -37,6 +40,18 @@ const ENDPOINT_ENV_KEYS = [ |
37 | 40 | "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", |
38 | 41 | "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", |
39 | 42 | "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", |
| 43 | + "OTEL_EXPORTER_OTLP_CERTIFICATE", |
| 44 | + "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE", |
| 45 | + "OTEL_EXPORTER_OTLP_CLIENT_KEY", |
| 46 | + "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE", |
| 47 | + "OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE", |
| 48 | + "OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY", |
| 49 | + "OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE", |
| 50 | + "OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE", |
| 51 | + "OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY", |
| 52 | + "OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE", |
| 53 | + "OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE", |
| 54 | + "OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY", |
40 | 55 | "OTEL_LOG_LEVEL", |
41 | 56 | ] as const; |
42 | 57 | const OTEL_GLOBAL_API_KEY = Symbol.for("opentelemetry.js.api.1"); |
@@ -471,3 +486,144 @@ test.each([ |
471 | 486 | } |
472 | 487 | }, |
473 | 488 | ); |
| 489 | + |
| 490 | +const OTEL_TLS_MATERIAL_CASES = [ |
| 491 | + { suffix: "CERTIFICATE", label: "TLS root certificate" }, |
| 492 | + { suffix: "CLIENT_CERTIFICATE", label: "mTLS client certificate" }, |
| 493 | + { suffix: "CLIENT_KEY", label: "mTLS client private key" }, |
| 494 | +] as const; |
| 495 | + |
| 496 | +const OTEL_TLS_FILE_SECURITY_CASES = OTEL_ENDPOINT_SIGNAL_CASES.flatMap((signal) => |
| 497 | + OTEL_TLS_MATERIAL_CASES.flatMap((material) => |
| 498 | + (["shared", "signal"] as const).map((scope) => Object.assign({ scope }, signal, material)), |
| 499 | + ), |
| 500 | +); |
| 501 | + |
| 502 | +test.each(OTEL_TLS_FILE_SECURITY_CASES)( |
| 503 | + "refuses the real $signal exporter when its $scope $label file cannot be read", |
| 504 | + async ({ signal, suffix, label, scope, flags }) => { |
| 505 | + process.env[PRELOAD_ENV] = "0"; |
| 506 | + const pathSentinel = `qa-otel-${signal}-${suffix.toLowerCase()}-file-sentinel`; |
| 507 | + const missingPath = `/definitely-missing/${pathSentinel}.pem`; |
| 508 | + const envKey = |
| 509 | + scope === "shared" |
| 510 | + ? `OTEL_EXPORTER_OTLP_${suffix}` |
| 511 | + : `OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_${suffix}`; |
| 512 | + process.env[envKey] = missingPath; |
| 513 | + |
| 514 | + const diagnostics = captureOtelDiagnostics(); |
| 515 | + const ctx = createOtelContext("https://collector.example.com/otlp", flags); |
| 516 | + ctx.internalDiagnostics!.emit = () => {}; |
| 517 | + const service = createDiagnosticsOtelService(); |
| 518 | + let failure: unknown; |
| 519 | + try { |
| 520 | + await service.start(ctx); |
| 521 | + } catch (error) { |
| 522 | + failure = error; |
| 523 | + } finally { |
| 524 | + await service.stop?.(ctx); |
| 525 | + } |
| 526 | + |
| 527 | + expect(failure).toBeInstanceOf(Error); |
| 528 | + const startupError = failure as Error; |
| 529 | + expect(startupError.message).toBe( |
| 530 | + `Configured OpenTelemetry ${label} file is missing, empty, or unreadable; refusing insecure export`, |
| 531 | + ); |
| 532 | + expect(startupError.stack).not.toContain(pathSentinel); |
| 533 | + expect(startupError).not.toHaveProperty("cause"); |
| 534 | + expect(diagnostics.join("\n")).not.toContain(pathSentinel); |
| 535 | + expect(JSON.stringify(vi.mocked(ctx.logger.error).mock.calls)).not.toContain(pathSentinel); |
| 536 | + expect(JSON.stringify(vi.mocked(ctx.logger.warn).mock.calls)).not.toContain(pathSentinel); |
| 537 | + }, |
| 538 | +); |
| 539 | + |
| 540 | +test.each(OTEL_ENDPOINT_SIGNAL_CASES)( |
| 541 | + "refuses invalid TLS material before the real default $signal exporter is constructed", |
| 542 | + async ({ signal, flags }) => { |
| 543 | + process.env[PRELOAD_ENV] = "0"; |
| 544 | + process.env[`OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_CERTIFICATE`] = |
| 545 | + "/definitely-missing/qa-otel-default-root.pem"; |
| 546 | + const ctx = createOtelContext("", flags); |
| 547 | + ctx.internalDiagnostics!.emit = () => {}; |
| 548 | + const service = createDiagnosticsOtelService(); |
| 549 | + |
| 550 | + try { |
| 551 | + await expect(service.start(ctx)).rejects.toThrow( |
| 552 | + "Configured OpenTelemetry TLS root certificate file is missing, empty, or unreadable; refusing insecure export", |
| 553 | + ); |
| 554 | + } finally { |
| 555 | + await service.stop?.(ctx); |
| 556 | + } |
| 557 | + }, |
| 558 | +); |
| 559 | + |
| 560 | +test.each( |
| 561 | + OTEL_ENDPOINT_SIGNAL_CASES.flatMap((signal) => |
| 562 | + OTEL_TLS_MATERIAL_CASES.map((material) => Object.assign({}, signal, material)), |
| 563 | + ), |
| 564 | +)( |
| 565 | + "rejects an empty $signal $label file before the SDK can silently downgrade trust", |
| 566 | + async ({ signal, suffix, label, flags }) => { |
| 567 | + process.env[PRELOAD_ENV] = "0"; |
| 568 | + const certDir = mkdtempSync(path.join(tmpdir(), "openclaw-otel-empty-tls-")); |
| 569 | + const emptyMaterialPath = path.join(certDir, "empty.pem"); |
| 570 | + writeFileSync(emptyMaterialPath, ""); |
| 571 | + process.env[`OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_${suffix}`] = emptyMaterialPath; |
| 572 | + const ctx = createOtelContext("https://collector.example.com/otlp", flags); |
| 573 | + ctx.internalDiagnostics!.emit = () => {}; |
| 574 | + const service = createDiagnosticsOtelService(); |
| 575 | + |
| 576 | + try { |
| 577 | + await expect(service.start(ctx)).rejects.toThrow( |
| 578 | + `Configured OpenTelemetry ${label} file is missing, empty, or unreadable; refusing insecure export`, |
| 579 | + ); |
| 580 | + } finally { |
| 581 | + await service.stop?.(ctx); |
| 582 | + rmSync(certDir, { force: true, recursive: true }); |
| 583 | + } |
| 584 | + }, |
| 585 | +); |
| 586 | + |
| 587 | +test.each(OTEL_ENDPOINT_SIGNAL_CASES)( |
| 588 | + "rejects the raw whitespace-padded $signal TLS certificate path the SDK cannot read", |
| 589 | + async ({ signal, flags }) => { |
| 590 | + process.env[PRELOAD_ENV] = "0"; |
| 591 | + process.env[`OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_CERTIFICATE`] = ` ${process.execPath} `; |
| 592 | + const ctx = createOtelContext("https://collector.example.com/otlp", flags); |
| 593 | + ctx.internalDiagnostics!.emit = () => {}; |
| 594 | + const service = createDiagnosticsOtelService(); |
| 595 | + |
| 596 | + try { |
| 597 | + await expect(service.start(ctx)).rejects.toThrow( |
| 598 | + "Configured OpenTelemetry TLS root certificate file is missing, empty, or unreadable; refusing insecure export", |
| 599 | + ); |
| 600 | + } finally { |
| 601 | + await service.stop?.(ctx); |
| 602 | + } |
| 603 | + }, |
| 604 | +); |
| 605 | + |
| 606 | +test.each( |
| 607 | + OTEL_ENDPOINT_SIGNAL_CASES.flatMap((signal) => |
| 608 | + (["CLIENT_CERTIFICATE", "CLIENT_KEY"] as const).map((suffix) => |
| 609 | + Object.assign({ suffix }, signal), |
| 610 | + ), |
| 611 | + ), |
| 612 | +)( |
| 613 | + "rejects the real $signal exporter when only $suffix mTLS material is configured", |
| 614 | + async ({ signal, suffix, flags }) => { |
| 615 | + process.env[PRELOAD_ENV] = "0"; |
| 616 | + process.env[`OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_${suffix}`] = process.execPath; |
| 617 | + const ctx = createOtelContext("https://collector.example.com/otlp", flags); |
| 618 | + ctx.internalDiagnostics!.emit = () => {}; |
| 619 | + const service = createDiagnosticsOtelService(); |
| 620 | + |
| 621 | + try { |
| 622 | + await expect(service.start(ctx)).rejects.toThrow( |
| 623 | + "Configured OpenTelemetry mTLS requires both a client certificate and private key; refusing insecure export", |
| 624 | + ); |
| 625 | + } finally { |
| 626 | + await service.stop?.(ctx); |
| 627 | + } |
| 628 | + }, |
| 629 | +); |
0 commit comments