feat: import upstream ZITADEL Login V2 v4.20.4 source workspace
This commit is contained in:
1 parent
eadd0e558b
commit
4404849fc4
644 files changed
+153201
-10
No files matched your search
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
|
||||
if [ -f /.env-file/.env ]; then
|
||||
set -o allexport
|
||||
. /.env-file/.env
|
||||
set +o allexport
|
||||
fi
|
||||
|
||||
if [ -n "${ZITADEL_SERVICE_USER_TOKEN_FILE}" ]; then
|
||||
echo "ZITADEL_SERVICE_USER_TOKEN_FILE=${ZITADEL_SERVICE_USER_TOKEN_FILE} is set. Awaiting file and reading token."
|
||||
while [ ! -f "${ZITADEL_SERVICE_USER_TOKEN_FILE}" ]; do
|
||||
sleep 2
|
||||
done
|
||||
echo "token file found, reading token"
|
||||
export ZITADEL_SERVICE_USER_TOKEN=$(cat "${ZITADEL_SERVICE_USER_TOKEN_FILE}")
|
||||
fi
|
||||
|
||||
# Log OTEL configuration on startup (if enabled)
|
||||
if [ -n "${OTEL_EXPORTER_OTLP_ENDPOINT}" ]; then
|
||||
echo "OTEL enabled: service=${OTEL_SERVICE_NAME:-zitadel-login}, endpoint=${OTEL_EXPORTER_OTLP_ENDPOINT}"
|
||||
fi
|
||||
|
||||
exec $@
|
||||
@@ -0,0 +1,28 @@
|
||||
import { getFips } from "node:crypto";
|
||||
import * as http from "node:http";
|
||||
import * as https from "node:https";
|
||||
|
||||
if (process.env.ZITADEL_FIPS_REQUIRED === "true" && getFips() !== 1) {
|
||||
console.error("Healthcheck failed: FIPS mode required but not enabled");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const scheme = process.env.ZITADEL_TLS_ENABLED === "true" ? "https" : "http";
|
||||
const port = process.env.PORT || "3000";
|
||||
const url = new URL(process.argv[2] || `/ui/v2/login/healthy`, `${scheme}://localhost:${port}`);
|
||||
|
||||
const get = scheme === "https" ? https.get : http.get;
|
||||
|
||||
try {
|
||||
const res = await new Promise((resolve, reject) => {
|
||||
// Safe: localhost-only probe, self-signed certs expected
|
||||
get(url, { rejectUnauthorized: false }, (res) => {
|
||||
res.resume();
|
||||
resolve(res);
|
||||
}).on("error", reject);
|
||||
});
|
||||
process.exit(res.statusCode >= 200 && res.statusCode < 400 ? 0 : 1);
|
||||
} catch (e) {
|
||||
console.error("Healthcheck failed:", e);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Preload script that makes Node.js read CA certificates from SSL_CERT_DIR
|
||||
// the same way Go's crypto/x509 does: every regular file in the directory is
|
||||
// read regardless of filename. OpenSSL (used by Node when --use-openssl-ca
|
||||
// is set) only reads files named with a subject-hash (e.g. "9d66eef0.0"),
|
||||
// silently ignoring plain names like "corp-ca.crt". This script closes that
|
||||
// gap so operators get identical behaviour across the Go backend and the
|
||||
// Node.js login container.
|
||||
//
|
||||
// Usage: node --require /app/load-ssl-cert-dir.cjs ...
|
||||
//
|
||||
// The script is a no-op when SSL_CERT_DIR is not set.
|
||||
|
||||
"use strict";
|
||||
|
||||
const tls = require("tls");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const sslCertDir = process.env.SSL_CERT_DIR;
|
||||
if (!sslCertDir) {
|
||||
return; // nothing to do — system trust via SSL_CERT_FILE is sufficient
|
||||
}
|
||||
|
||||
// Go splits on ":" and walks every directory in order.
|
||||
const dirs = sslCertDir.split(":").filter(Boolean);
|
||||
const certs = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
let entries;
|
||||
try {
|
||||
entries = fs.readdirSync(dir);
|
||||
} catch {
|
||||
// Directory doesn't exist or isn't readable — skip, like Go does.
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
const filePath = path.join(dir, entry);
|
||||
|
||||
try {
|
||||
const stat = fs.lstatSync(filePath);
|
||||
|
||||
// Go's readUniqueDirectoryEntries skips symlinks whose target is a
|
||||
// bare filename (no slash), because those are the c_rehash hash links
|
||||
// pointing at files in the same directory. This avoids loading the
|
||||
// same certificate twice.
|
||||
if (stat.isSymbolicLink()) {
|
||||
const target = fs.readlinkSync(filePath);
|
||||
if (!target.includes("/")) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stat.isFile() && !stat.isSymbolicLink()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(filePath, "utf8");
|
||||
if (content.includes("-----BEGIN CERTIFICATE-----")) {
|
||||
certs.push(content);
|
||||
}
|
||||
} catch {
|
||||
// Unreadable file — skip silently, matching Go behaviour.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (certs.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const origCreateSecureContext = tls.createSecureContext;
|
||||
|
||||
tls.createSecureContext = function (options) {
|
||||
const ctx = origCreateSecureContext.call(this, options);
|
||||
|
||||
// Only inject directory CAs when the caller did not pass an explicit "ca"
|
||||
// option. This matches Go's SystemCertPool() semantics: the extra certs
|
||||
// are part of the system pool, not a forced override.
|
||||
if (!options || !options.ca) {
|
||||
for (let i = 0; i < certs.length; i++) {
|
||||
try {
|
||||
ctx.context.addCACert(certs[i]);
|
||||
} catch (err) {
|
||||
// Log once per bad cert, then remove it so subsequent contexts
|
||||
// don't pay the cost of retrying a known-bad certificate.
|
||||
console.error("load-ssl-cert-dir: skipping malformed certificate at index " + i + ": " + err.message);
|
||||
certs.splice(i, 1);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx;
|
||||
};
|
||||
@@ -0,0 +1,264 @@
|
||||
import { accessSync, constants, readFileSync } from "node:fs";
|
||||
import { request as httpRequest } from "node:http";
|
||||
import { createServer, request as httpsRequest } from "node:https";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
/**
|
||||
* Parses a Go-style duration string into milliseconds.
|
||||
*
|
||||
* This uses the same format as Go's time.ParseDuration (e.g. "30s", "5m",
|
||||
* "1m30s") to stay consistent with the ZITADEL backend, which uses
|
||||
* time.Duration for its own ZITADEL_DATABASE_POSTGRES_AWAITINITIALCONN
|
||||
* setting. Operators configuring both services use the same syntax.
|
||||
*
|
||||
* Valid units are "ms", "s", "m", and "h".
|
||||
* Returns null if the string is empty, zero, or contains invalid syntax.
|
||||
*/
|
||||
function parseDuration(raw) {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
|
||||
let totalMs = 0;
|
||||
let remaining = trimmed;
|
||||
const unitMap = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000 };
|
||||
|
||||
while (remaining.length > 0) {
|
||||
const match = remaining.match(/^(\d+)(ms|s|m|h)/);
|
||||
if (!match) return null;
|
||||
totalMs += parseInt(match[1], 10) * unitMap[match[2]];
|
||||
remaining = remaining.slice(match[0].length);
|
||||
}
|
||||
|
||||
return totalMs > 0 ? totalMs : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls an HTTP(S) URL until it returns HTTP 200 or the deadline is reached.
|
||||
*/
|
||||
async function pollReady(url, timeoutMs) {
|
||||
const parsedUrl = new URL(url);
|
||||
const doRequest = parsedUrl.protocol === "https:" ? httpsRequest : httpRequest;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
const ok = await new Promise((resolve) => {
|
||||
const req = doRequest(parsedUrl, { method: "GET", timeout: 5_000, rejectUnauthorized: false }, (res) => {
|
||||
res.resume();
|
||||
resolve(res.statusCode === 200);
|
||||
});
|
||||
req.on("error", () => resolve(false));
|
||||
req.on("timeout", () => {
|
||||
req.destroy();
|
||||
resolve(false);
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
|
||||
if (ok) return true;
|
||||
|
||||
await new Promise((r) => setTimeout(r, 1_000));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the ZITADEL API to become ready before starting the server.
|
||||
*
|
||||
* When ZITADEL_API_AWAITINITIALCONN is set (e.g. "60s", "5m", "120s"), this
|
||||
* function blocks until GET {ZITADEL_API_URL}/debug/ready returns HTTP 200
|
||||
* or the timeout expires. This replaces the wait4x init container that was
|
||||
* previously used in the Helm chart.
|
||||
*
|
||||
* If the timeout expires without a successful response, the process exits
|
||||
* with code 1.
|
||||
*/
|
||||
async function awaitApiReady() {
|
||||
const raw = process.env.ZITADEL_API_AWAITINITIALCONN;
|
||||
if (!raw) return;
|
||||
|
||||
const apiUrl = process.env.ZITADEL_API_URL;
|
||||
if (!apiUrl) {
|
||||
console.error("ZITADEL_API_AWAITINITIALCONN is set but ZITADEL_API_URL is not configured.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const timeoutMs = parseDuration(raw);
|
||||
if (timeoutMs === null) {
|
||||
console.error(
|
||||
`Invalid ZITADEL_API_AWAITINITIALCONN value "${raw}". Expected a Go-style duration like "30s", "5m", or "1m30s".`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const readyUrl = new URL("/debug/ready", apiUrl).toString();
|
||||
console.log(`Waiting up to ${raw} for API to be ready at ${readyUrl} ...`);
|
||||
|
||||
const ok = await pollReady(readyUrl, timeoutMs);
|
||||
if (ok) {
|
||||
console.log("API is ready.");
|
||||
} else {
|
||||
console.error(`Timed out after ${raw} waiting for API readiness at ${readyUrl}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
await awaitApiReady();
|
||||
|
||||
if (process.env.ZITADEL_TLS_ENABLED !== "true") {
|
||||
// The build script must rename the Next.js-generated server.js to next-server.js.
|
||||
// Validate that the file exists before importing to provide a clearer error if the build step fails.
|
||||
try {
|
||||
accessSync(new URL("./next-server.js", import.meta.url), constants.R_OK);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
'Unable to find or read "./next-server.js". Ensure the build script has renamed Next.js\'s server.js to next-server.js.',
|
||||
err,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
await import("./next-server.js");
|
||||
} else {
|
||||
const certPath = process.env.ZITADEL_TLS_CERTPATH;
|
||||
const keyPath = process.env.ZITADEL_TLS_KEYPATH;
|
||||
|
||||
if (!certPath || !keyPath) {
|
||||
console.error(
|
||||
"ZITADEL_TLS_ENABLED is set to true but ZITADEL_TLS_CERTPATH or ZITADEL_TLS_KEYPATH are missing. Both must be set when TLS is enabled.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
accessSync(certPath, constants.R_OK);
|
||||
} catch (err) {
|
||||
console.error(`Certificate file is not readable: ${certPath}`, err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
accessSync(keyPath, constants.R_OK);
|
||||
} catch (err) {
|
||||
console.error(`Key file is not readable: ${keyPath}`, err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cert = readFileSync(certPath);
|
||||
const key = readFileSync(keyPath);
|
||||
|
||||
// Requires Node.js >= 20.11.0 (or >= 21.2.0) for import.meta.dirname support.
|
||||
const appDir = import.meta.dirname;
|
||||
|
||||
const requiredServerFilesPath = new URL(".next/required-server-files.json", import.meta.url);
|
||||
|
||||
let requiredServerFiles;
|
||||
try {
|
||||
accessSync(requiredServerFilesPath, constants.R_OK);
|
||||
const requiredServerFilesContent = readFileSync(requiredServerFilesPath, "utf8");
|
||||
requiredServerFiles = JSON.parse(requiredServerFilesContent);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"Failed to load .next/required-server-files.json. The Next.js build may be incomplete or the file may contain invalid JSON.",
|
||||
err,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(requiredServerFiles.config);
|
||||
|
||||
process.env.NODE_ENV = "production";
|
||||
process.chdir(appDir);
|
||||
|
||||
const currentPort = parseInt(process.env.PORT, 10) || 3000;
|
||||
const hostname = process.env.HOSTNAME || "0.0.0.0";
|
||||
|
||||
// KEEP_ALIVE_TIMEOUT is expected to be in milliseconds.
|
||||
// Reject values that are negative or unreasonably large to avoid resource issues.
|
||||
const MAX_KEEP_ALIVE_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes
|
||||
|
||||
let keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
|
||||
if (
|
||||
Number.isNaN(keepAliveTimeout) ||
|
||||
!Number.isFinite(keepAliveTimeout) ||
|
||||
keepAliveTimeout < 0 ||
|
||||
keepAliveTimeout > MAX_KEEP_ALIVE_TIMEOUT_MS
|
||||
) {
|
||||
keepAliveTimeout = undefined;
|
||||
}
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
require("next");
|
||||
// NOTE: This imports an internal Next.js API that is not part of the public, semver-stable surface.
|
||||
// - Verified with Next.js version 15.5.10 (see package.json).
|
||||
// - This may break on Next.js upgrades; if Next.js is updated, re-check that
|
||||
// `next/dist/server/lib/start-server` still exists and that `getRequestHandlers` behaves as expected.
|
||||
// - This is currently required to support HTTPS with self-signed certificates in production mode,
|
||||
// as there is no equivalent public API in Next.js at this time.
|
||||
const { getRequestHandlers } = require("next/dist/server/lib/start-server");
|
||||
|
||||
const httpsServer = createServer({ key, cert });
|
||||
|
||||
if (keepAliveTimeout !== undefined) {
|
||||
httpsServer.keepAliveTimeout = keepAliveTimeout;
|
||||
}
|
||||
|
||||
const { requestHandler, upgradeHandler } = await getRequestHandlers({
|
||||
dir: appDir,
|
||||
port: currentPort,
|
||||
isDev: false,
|
||||
server: httpsServer,
|
||||
hostname,
|
||||
keepAliveTimeout,
|
||||
});
|
||||
|
||||
httpsServer.on("request", async (req, res) => {
|
||||
try {
|
||||
await requestHandler(req, res);
|
||||
} catch (err) {
|
||||
res.statusCode = 500;
|
||||
res.end("Internal Server Error");
|
||||
console.error(`Failed to handle request for ${req.url}`);
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
httpsServer.on("upgrade", async (req, socket, head) => {
|
||||
try {
|
||||
await upgradeHandler(req, socket, head);
|
||||
} catch (err) {
|
||||
socket.destroy();
|
||||
console.error(`Failed to handle upgrade for ${req.url}`);
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
httpsServer.on("error", (err) => {
|
||||
console.error("Failed to start HTTPS server");
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
let cleanupStarted = false;
|
||||
const cleanup = () => {
|
||||
if (cleanupStarted) return;
|
||||
cleanupStarted = true;
|
||||
httpsServer.close((err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
|
||||
process.on("SIGINT", cleanup);
|
||||
process.on("SIGTERM", cleanup);
|
||||
}
|
||||
|
||||
httpsServer.listen(currentPort, hostname, () => {
|
||||
const formattedHost = hostname.includes(":") ? `[${hostname}]` : hostname;
|
||||
const readyUrl = new URL(`https://${formattedHost}:${currentPort}`);
|
||||
console.log(`> Ready on ${readyUrl}`);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user