feat(login): added support for otel push-based logs, metrics, and traces (#11429)

This commit is contained in:
Mridang Agarwalla
2026-03-04 15:58:56 +05:30
committed by GitHub
parent 685be24dfe
commit 3f3413a230
41 changed files with 4182 additions and 350 deletions
+71 -134
View File
@@ -1,165 +1,117 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { GenericContainer, type StartedTestContainer, Network, Wait } from "testcontainers";
import {
GenericContainer,
type StartedTestContainer,
Network,
Wait,
} from "testcontainers";
import type { StartedNetwork } from "testcontainers";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as esbuild from "esbuild";
import { generateCertificates } from "./utils/tls.ts";
const TEST_DIR = path.dirname(new URL(import.meta.url).pathname);
const OUTPUT_DIR = path.join(TEST_DIR, "output");
const CERTS_DIR = path.join(OUTPUT_DIR, "certs");
const LOGIN_APP_DIR = path.join(TEST_DIR, "../..");
async function bundleMockServer(): Promise<string> {
const bundleDir = path.join(OUTPUT_DIR, "bundle");
fs.mkdirSync(bundleDir, { recursive: true });
const outfile = path.join(bundleDir, "mock-server.js");
await esbuild.build({
entryPoints: [path.join(TEST_DIR, "mock-server.ts")],
bundle: true,
platform: "node",
target: "node22",
outfile,
format: "cjs",
});
return outfile;
}
function readCapturedRequests(): Array<{ method: string; url: string; tlsConnected: boolean }> {
const filePath = path.join(OUTPUT_DIR, "requests.json");
if (!fs.existsSync(filePath)) {
return [];
}
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
const SMOCKER_MOCK_PORT = 443;
const SMOCKER_ADMIN_PORT = 8081;
/**
* Integration tests for custom CA certificate support in the login application.
*
* These tests verify that the Dockerfile correctly configures Node.js to trust custom
* Certificate Authorities via the SSL_CERT_FILE environment variable and the
* NODE_OPTIONS=--use-openssl-ca flag. This is essential for deployments where the
* ZITADEL API is served behind a TLS-terminating proxy using internal or self-signed
* certificates.
* These tests verify that the Dockerfile correctly configures Node.js to trust
* custom Certificate Authorities via the SSL_CERT_FILE environment variable and
* the NODE_OPTIONS=--use-openssl-ca flag.
*
* The test suite spins up a mock TLS server with a self-signed certificate inside a
* Docker network. The login application container connects to this mock server, and
* the tests verify that TLS connections succeed when the custom CA is mounted and
* fail when it is not. The mock server records all incoming requests to a shared
* volume, allowing the tests to verify that TLS handshakes completed successfully.
*
* The Docker network ensures container-to-container communication uses internal DNS
* resolution, avoiding port conflicts on the host machine and ensuring reliable
* execution in CI environments.
* We use Smocker as a TLS server and test CA loading by making curl requests
* from inside the container - this verifies TLS at the transport layer without
* needing gRPC/HTTP2 support.
*/
const LOGIN_IMAGE_TAG = `zitadel-login-test:${process.env.GITHUB_SHA || "local"}`;
async function testTlsConnection(
container: StartedTestContainer,
url: string,
): Promise<{ exitCode: number; output: string }> {
return container.exec([
"node",
"-e",
`fetch('${url}').then(() => process.exit(0)).catch(() => process.exit(1))`,
]);
}
describe("Custom CA Certificate Integration", () => {
let certs: ReturnType<typeof generateCertificates>;
let network: StartedNetwork;
let mockServer: StartedTestContainer;
let mockServerBundle: string;
let loginImage: GenericContainer;
let certsDir: string;
beforeAll(async () => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true });
}
fs.mkdirSync(CERTS_DIR, { recursive: true });
certsDir = fs.mkdtempSync(path.join(os.tmpdir(), "ca-test-certs-"));
certs = generateCertificates();
fs.writeFileSync(path.join(CERTS_DIR, "ca.crt"), certs.ca.cert);
fs.writeFileSync(path.join(CERTS_DIR, "server.key"), certs.server.key);
fs.writeFileSync(path.join(CERTS_DIR, "server.crt"), certs.server.cert);
mockServerBundle = await bundleMockServer();
const certs = generateCertificates();
fs.writeFileSync(path.join(certsDir, "ca.crt"), certs.ca.cert);
fs.writeFileSync(path.join(certsDir, "server.key"), certs.server.key);
fs.writeFileSync(path.join(certsDir, "server.crt"), certs.server.cert);
network = await new Network().start();
mockServer = await new GenericContainer("node:22-alpine")
mockServer = await new GenericContainer("ghcr.io/smocker-dev/smocker")
.withNetwork(network)
.withNetworkAliases("mock-zitadel")
.withCopyFilesToContainer([
{ source: mockServerBundle, target: "/app/server.js" },
{ source: path.join(CERTS_DIR, "server.key"), target: "/certs/server.key" },
{ source: path.join(CERTS_DIR, "server.crt"), target: "/certs/server.crt" },
])
.withBindMounts([{ source: OUTPUT_DIR, target: "/output" }])
.withCommand(["node", "/app/server.js"])
.withExposedPorts(443)
.withWaitStrategy(Wait.forLogMessage("Mock TLS server listening"))
.withBindMounts([{ source: certsDir, target: "/certs", mode: "ro" }])
.withEnvironment({
SMOCKER_MOCK_SERVER_LISTEN_PORT: "443",
SMOCKER_CONFIG_LISTEN_PORT: "8081",
SMOCKER_TLS_ENABLE: "true",
SMOCKER_TLS_CERT_FILE: "/certs/server.crt",
SMOCKER_TLS_PRIVATE_KEY_FILE: "/certs/server.key",
})
.withExposedPorts(SMOCKER_MOCK_PORT, SMOCKER_ADMIN_PORT)
.withStartupTimeout(120_000)
.withWaitStrategy(Wait.forLogMessage(/Starting mock server/))
.start();
loginImage = await GenericContainer.fromDockerfile(LOGIN_APP_DIR).build();
});
try {
await GenericContainer.fromDockerfile(LOGIN_APP_DIR).build(LOGIN_IMAGE_TAG);
} catch (err) {
throw new Error(`Failed to build login Docker image: ${err instanceof Error ? err.message : err}`);
}
}, 180_000);
afterAll(async () => {
await mockServer?.stop();
await network?.stop();
await mockServer?.stop().catch(() => {});
await network?.stop().catch(() => {});
});
describe("when custom CA certificate is provided", () => {
let container: StartedTestContainer;
let appUrl: string;
beforeAll(async () => {
if (fs.existsSync(path.join(OUTPUT_DIR, "requests.json"))) {
fs.unlinkSync(path.join(OUTPUT_DIR, "requests.json"));
}
container = await loginImage
container = await new GenericContainer(LOGIN_IMAGE_TAG)
.withNetwork(network)
.withExposedPorts(3000)
.withEnvironment({
ZITADEL_API_URL: "https://mock-zitadel",
ZITADEL_SERVICE_USER_TOKEN: "test-token",
SSL_CERT_FILE: "/etc/ssl/certs/custom-ca.crt",
})
.withCopyFilesToContainer([
{
source: path.join(CERTS_DIR, "ca.crt"),
source: path.join(certsDir, "ca.crt"),
target: "/etc/ssl/certs/custom-ca.crt",
},
])
.withStartupTimeout(180_000)
.withWaitStrategy(Wait.forHttp("/ui/v2/login/healthy", 3000))
.start();
appUrl = `http://${container.getHost()}:${container.getMappedPort(3000)}`;
await fetch(`${appUrl}/ui/v2/login/loginname`, { redirect: "manual" });
for (let i = 0; i < 30; i++) {
const requests = readCapturedRequests();
if (requests.length > 0) break;
await new Promise((r) => setTimeout(r, 1000));
}
});
}, 180_000);
afterAll(async () => {
await container?.stop();
await container?.stop().catch(() => {});
});
it("connects to mock server over TLS", () => {
const requests = readCapturedRequests();
expect(requests.length).toBeGreaterThan(0);
});
it("marks requests as TLS connected", () => {
const requests = readCapturedRequests();
const tlsRequests = requests.filter((r) => r.tlsConnected === true);
expect(tlsRequests.length).toBeGreaterThan(0);
});
it("returns 200 from healthy endpoint", async () => {
const response = await fetch(`${appUrl}/ui/v2/login/healthy`);
expect(response.status).toBe(200);
});
it("serves the login page", async () => {
const response = await fetch(`${appUrl}/ui/v2/login/loginname`, { redirect: "manual" });
expect([200, 302, 303, 307, 308]).toContain(response.status);
it("can establish TLS connection to server with custom CA", async () => {
const result = await testTlsConnection(container, "https://mock-zitadel/");
expect(result.exitCode).toBe(0);
});
it("has NODE_OPTIONS set with --use-openssl-ca", async () => {
@@ -184,33 +136,23 @@ describe("Custom CA Certificate Integration", () => {
describe("when custom CA certificate is not provided", () => {
let container: StartedTestContainer;
let appUrl: string;
let requestsBefore: number;
beforeAll(async () => {
requestsBefore = readCapturedRequests().length;
container = await loginImage
container = await new GenericContainer(LOGIN_IMAGE_TAG)
.withNetwork(network)
.withExposedPorts(3000)
.withEnvironment({
ZITADEL_API_URL: "https://mock-zitadel",
ZITADEL_SERVICE_USER_TOKEN: "test-token",
})
.withStartupTimeout(180_000)
.withWaitStrategy(Wait.forHttp("/ui/v2/login/healthy", 3000))
.start();
appUrl = `http://${container.getHost()}:${container.getMappedPort(3000)}`;
await fetch(`${appUrl}/ui/v2/login/loginname`, { redirect: "manual" });
for (let i = 0; i < 10; i++) {
await new Promise((r) => setTimeout(r, 500));
}
});
}, 180_000);
afterAll(async () => {
await container?.stop();
await container?.stop().catch(() => {});
});
it("cannot establish TLS connection without custom CA", async () => {
const result = await testTlsConnection(container, "https://mock-zitadel/");
expect(result.exitCode).not.toBe(0);
});
it("uses system CA store by default", async () => {
@@ -226,10 +168,5 @@ describe("Custom CA Certificate Integration", () => {
]);
expect(result.output.trim()).toContain("missing");
});
it("cannot establish TLS connection to mock server", () => {
const requestsAfter = readCapturedRequests().length;
expect(requestsAfter).toBe(requestsBefore);
});
});
});
-38
View File
@@ -1,38 +0,0 @@
import * as http2 from "node:http2";
import * as fs from "node:fs";
const OUTPUT_DIR = process.env.OUTPUT_DIR || "/output";
const server = http2.createSecureServer({
key: fs.readFileSync("/certs/server.key"),
cert: fs.readFileSync("/certs/server.crt"),
allowHTTP1: true,
});
interface CapturedRequest {
method: string;
url: string;
tlsConnected: boolean;
}
const requests: CapturedRequest[] = [];
server.on("stream", (stream, headers) => {
requests.push({
method: (headers[":method"] as string) || "UNKNOWN",
url: (headers[":path"] as string) || "/",
tlsConnected: true,
});
fs.writeFileSync(`${OUTPUT_DIR}/requests.json`, JSON.stringify(requests, null, 2));
stream.respond({
":status": 200,
"content-type": "application/json",
"grpc-status": "0",
});
stream.end(JSON.stringify({ status: "ok" }));
});
server.listen(443, "0.0.0.0", () => {
console.log("Mock TLS server listening on port 443");
});
@@ -0,0 +1,155 @@
/**
* Mock Zitadel Connect/gRPC server for testing trace propagation.
* Captures incoming headers and writes them to a file for test assertions.
*
* Uses HTTP/2 with cleartext (h2c) to support gRPC protocol.
*/
const http2 = require("http2");
const http = require("http");
const fs = require("fs");
const path = require("path");
const PORT = process.env.PORT || 8080;
const OUTPUT_DIR = process.env.OUTPUT_DIR || "/tmp/otel";
const HEADERS_FILE = path.join(OUTPUT_DIR, "captured-headers.json");
/**
* Safely stringify a value to JSON with XSS-preventing character escapes.
*
* Escapes `<`, `>`, `&`, and `/` to their Unicode equivalents to prevent
* script injection when the output is reflected in HTTP responses or
* embedded in HTML contexts.
*
* @param {unknown} value - The value to stringify
* @returns {string} JSON string with dangerous characters escaped
*/
function safeJsonStringify(value) {
return JSON.stringify(value, null, 2)
.replace(/</g, "\\u003c")
.replace(/>/g, "\\u003e")
.replace(/&/g, "\\u0026")
.replace(/\//g, "\\/");
}
// Store captured requests
const capturedRequests = [];
// Handle HTTP/2 request
function handleRequest(stream, headers, isHttp2 = true) {
const method = isHttp2 ? headers[":method"] : headers.method;
const url = isHttp2 ? headers[":path"] : headers.url;
// Capture trace headers
const traceHeaders = {
timestamp: new Date().toISOString(),
method: method,
url: url,
traceparent: headers["traceparent"] || null,
tracestate: headers["tracestate"] || null,
baggage: headers["baggage"] || null,
grpcAcceptEncoding: headers["grpc-accept-encoding"] || null,
contentType: headers["content-type"] || null,
};
capturedRequests.push(traceHeaders);
// Write to file for test assertions
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(HEADERS_FILE, JSON.stringify(capturedRequests, null, 2));
console.log(`[${traceHeaders.timestamp}] ${method} ${url}`);
console.log(` traceparent: ${traceHeaders.traceparent || "(none)"}`);
// Helper to send response
const sendResponse = (statusCode, contentType, body) => {
if (isHttp2) {
stream.respond({
":status": statusCode,
"content-type": contentType,
"grpc-status": "0",
"grpc-message": "OK",
});
stream.end(body);
} else {
stream.writeHead(statusCode, {
"Content-Type": contentType,
"grpc-status": "0",
"grpc-message": "OK",
});
stream.end(body);
}
};
// Health check
if (url.includes("/health") || url === "/") {
sendResponse(200, "application/json", safeJsonStringify({ status: "ok" }));
return;
}
// Return captured headers (for debugging)
if (url === "/captured-headers") {
sendResponse(200, "application/json", safeJsonStringify(capturedRequests));
return;
}
// Mock Connect/gRPC responses
// These are minimal responses to prevent the login app from crashing
if (url.includes("GetLoginSettings") || url.includes("SettingsService")) {
sendResponse(200, "application/json", safeJsonStringify({ settings: {} }));
} else if (url.includes("GetBrandingSettings")) {
sendResponse(200, "application/json", safeJsonStringify({ settings: {} }));
} else if (url.includes("SessionService") || url.includes("Session")) {
sendResponse(200, "application/json", safeJsonStringify({ session: {} }));
} else if (url.includes("UserService") || url.includes("User")) {
sendResponse(200, "application/json", safeJsonStringify({ user: {} }));
} else if (url.includes("OrganizationService") || url.includes("Organization")) {
sendResponse(200, "application/json", safeJsonStringify({ result: [] }));
} else if (url.includes("IdentityProviderService") || url.includes("IdentityProvider")) {
sendResponse(200, "application/json", safeJsonStringify({ identityProviders: [] }));
} else {
sendResponse(200, "application/json", safeJsonStringify({}));
}
}
// Create HTTP/2 server (cleartext for testing)
const http2Server = http2.createServer();
http2Server.on("stream", (stream, headers) => {
handleRequest(stream, headers, true);
});
http2Server.on("error", (err) => console.error("HTTP/2 error:", err));
// Also create HTTP/1.1 server for health checks from wget
const httpServer = http.createServer((req, res) => {
// Create a headers object compatible with handleRequest
const headers = {
method: req.method,
url: req.url,
...req.headers,
};
handleRequest(res, headers, false);
});
// Start HTTP/2 server on main port
http2Server.listen(PORT, "0.0.0.0", () => {
console.log(`Mock Zitadel HTTP/2 server listening on port ${PORT}`);
console.log(`Captured headers will be written to: ${HEADERS_FILE}`);
});
// Start HTTP/1.1 server on a different port for health checks
const HEALTH_PORT = 7432;
httpServer.listen(HEALTH_PORT, "0.0.0.0", () => {
console.log(`Mock Zitadel HTTP/1.1 health server listening on port ${HEALTH_PORT}`);
});
// Graceful shutdown
process.on("SIGTERM", () => {
console.log("Shutting down mock servers...");
http2Server.close(() => {
httpServer.close(() => {
console.log("Mock servers stopped");
process.exit(0);
});
});
});
@@ -0,0 +1,43 @@
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
processors:
batch:
timeout: 1s
send_batch_size: 1024
exporters:
debug:
verbosity: detailed
file/traces:
path: /tmp/otel/traces.json
flush_interval: 1s
file/metrics:
path: /tmp/otel/metrics.json
flush_interval: 1s
file/logs:
path: /tmp/otel/logs.json
flush_interval: 1s
extensions:
health_check:
endpoint: 0.0.0.0:13133
service:
extensions: [health_check]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [debug, file/traces]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [debug, file/metrics]
logs:
receivers: [otlp]
processors: [batch]
exporters: [debug, file/logs]
File diff suppressed because it is too large Load Diff
@@ -10,6 +10,7 @@ const TEST_DIR = path.dirname(fileURLToPath(import.meta.url));
const OUTPUT_DIR = path.join(TEST_DIR, "output");
const CERTS_DIR = path.join(OUTPUT_DIR, "certs");
const LOGIN_APP_DIR = path.join(TEST_DIR, "../..");
const LOGIN_IMAGE_TAG = `zitadel-login-test:${process.env.GITHUB_SHA || "local"}`;
/**
* Integration tests for TLS support in the login application.
@@ -44,7 +45,8 @@ describe("Login Container TLS Support", () => {
fs.writeFileSync(path.join(CERTS_DIR, "server.crt"), certs.server.cert);
fs.writeFileSync(path.join(CERTS_DIR, "server.key"), certs.server.key);
loginImage = await GenericContainer.fromDockerfile(LOGIN_APP_DIR).build();
await GenericContainer.fromDockerfile(LOGIN_APP_DIR).build(LOGIN_IMAGE_TAG);
loginImage = new GenericContainer(LOGIN_IMAGE_TAG);
});
describe("when TLS is not enabled", () => {