fix(kafka): support scoped IPv6 bootstrap addresses

This commit is contained in:
jearhe authored and GitHub committed 2026-08-21 12:20:19 +08:00
1 parent c210efbcc5
commit 25d5b885ba
4 files changed
+42 -17

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { normalizeKafkaBootstrapServers, parseKafkaBootstrapServers, resolveKafkaSecurityProtocol } from "@/lib/connection/kafkaBootstrapServers";
import { normalizeKafkaBootstrapServers, parseKafkaBootstrapServers, resolveKafkaSecurityProtocol } from "../../connection/kafkaBootstrapServers";
describe("Kafka bootstrap servers", () => {
it("keeps comma-separated bootstrap servers", () => {
@@ -14,6 +14,10 @@ describe("Kafka bootstrap servers", () => {
expect(normalizeKafkaBootstrapServers("[::1]:9092;[2001:db8::1]:9092")).toBe("[::1]:9092,[2001:db8::1]:9092");
});
it("accepts IPv6 scope identifiers used by Windows hosts", () => {
expect(normalizeKafkaBootstrapServers("[fe80::1%12]:9092,[fe80::2%eth0]:9093")).toBe("[fe80::1%12]:9092,[fe80::2%eth0]:9093");
});
it("normalizes Kafka listener URIs and exposes the inferred security protocol", () => {
expect(parseKafkaBootstrapServers("PLAINTEXT://broker1:9092, broker2:9092")).toEqual({
bootstrapServers: "broker1:9092,broker2:9092",
@@ -23,6 +27,8 @@ describe("Kafka bootstrap servers", () => {
bootstrapServers: "secure-broker:9093",
inferredSecurityProtocol: "SASL_SSL",
});
expect(parseKafkaBootstrapServers("SSL://secure-broker:9093").inferredSecurityProtocol).toBe("SSL");
expect(parseKafkaBootstrapServers("SASL_PLAINTEXT://broker1:9092").inferredSecurityProtocol).toBe("SASL_PLAINTEXT");
});
it("rejects bootstrap servers that declare conflicting security protocols", () => {
@@ -42,6 +48,9 @@ describe("Kafka bootstrap servers", () => {
it("rejects invalid bootstrap server values", () => {
expect(() => normalizeKafkaBootstrapServers("broker1:9092/path,broker2:9092")).toThrow("Kafka bootstrap servers are invalid");
expect(() => normalizeKafkaBootstrapServers("broker1")).toThrow("Kafka bootstrap servers must be host:port values");
expect(() => normalizeKafkaBootstrapServers("broker1:0")).toThrow("Kafka bootstrap servers are invalid");
expect(() => normalizeKafkaBootstrapServers("broker1:70000")).toThrow("Kafka bootstrap servers are invalid");
expect(() => normalizeKafkaBootstrapServers("broker1:9092?query=x")).toThrow("Kafka bootstrap servers are invalid");
expect(() => normalizeKafkaBootstrapServers("user:pass@broker1:9092")).toThrow("Kafka bootstrap servers are invalid");
});
});
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { buildMqKafkaConnectionExtra, mqKafkaConnectionTarget, normalizeMqKafkaZooKeeperServers, resolveMqKafkaConnectionSource } from "@/lib/connection/mqKafkaConnection";
import { buildMqKafkaConnectionExtra, mqKafkaConnectionTarget, normalizeMqKafkaZooKeeperServers, resolveMqKafkaConnectionSource } from "../../connection/mqKafkaConnection";
describe("MQ Kafka connection", () => {
it("keeps existing configurations on the Bootstrap source", () => {
@@ -72,5 +72,14 @@ describe("MQ Kafka connection", () => {
securityProtocol: "SASL_SSL",
}),
).toEqual({ host: "zk-1", port: 2281, ssl: false });
expect(
mqKafkaConnectionTarget({
connectionSource: "bootstrap",
bootstrapServers: "[fe80::1%12]:9092",
zookeeperServers: "",
securityProtocol: "",
}),
).toEqual({ host: "fe80::1%12", port: 9092, ssl: false });
});
});
@@ -1,5 +1,6 @@
const KAFKA_BOOTSTRAP_SERVER_SEPARATOR = /[\s,;,;]+/u;
const KAFKA_BOOTSTRAP_SERVER_SCHEME = /^([a-z][a-z0-9_-]*):\/\/(.+)$/iu;
const KAFKA_BOOTSTRAP_SERVER_ADDRESS = /^(?:\[([0-9a-z%._:-]+)\]|([0-9a-z%._-]+)):(\d+)$/iu;
const KAFKA_SECURITY_PROTOCOLS = new Set(["PLAINTEXT", "SSL", "SASL_PLAINTEXT", "SASL_SSL"] as const);
export type KafkaSecurityProtocol = "PLAINTEXT" | "SSL" | "SASL_PLAINTEXT" | "SASL_SSL";
@@ -9,12 +10,27 @@ export interface ParsedKafkaBootstrapServers {
inferredSecurityProtocol?: KafkaSecurityProtocol;
}
export interface KafkaBootstrapServerAddress {
host: string;
port: number;
}
function requireKafkaBootstrapServers(value: string): string {
const trimmed = value.trim();
if (!trimmed) throw new Error("Kafka bootstrap servers are required");
return trimmed;
}
export function parseKafkaBootstrapServerAddress(address: string): KafkaBootstrapServerAddress {
const match = address.match(KAFKA_BOOTSTRAP_SERVER_ADDRESS);
const host = match?.[1] || match?.[2];
const port = match ? Number(match[3]) : Number.NaN;
if (!host || (match?.[1] && !host.includes(":")) || !Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error("Kafka bootstrap servers are invalid");
}
return { host, port };
}
function normalizeKafkaBootstrapServer(server: string): { address: string; securityProtocol?: KafkaSecurityProtocol } {
const schemeMatch = server.match(KAFKA_BOOTSTRAP_SERVER_SCHEME);
let address = server;
@@ -30,18 +46,10 @@ function normalizeKafkaBootstrapServer(server: string): { address: string; secur
throw new Error("Kafka bootstrap server protocol is invalid");
}
let parsed: URL;
try {
parsed = new URL(`kafka://${address}`);
} catch {
throw new Error("Kafka bootstrap servers are invalid");
}
if (!parsed.hostname || parsed.username || parsed.password || parsed.search || parsed.hash || (parsed.pathname && parsed.pathname !== "/")) {
throw new Error("Kafka bootstrap servers are invalid");
}
if (!parsed.port) {
if (!address.includes(":")) {
throw new Error("Kafka bootstrap servers must be host:port values");
}
parseKafkaBootstrapServerAddress(address);
return { address, securityProtocol };
}
@@ -1,4 +1,4 @@
import { parseKafkaBootstrapServers, resolveKafkaSecurityProtocol } from "@/lib/connection/kafkaBootstrapServers";
import { parseKafkaBootstrapServerAddress, parseKafkaBootstrapServers, resolveKafkaSecurityProtocol } from "@/lib/connection/kafkaBootstrapServers";
import { firstZooKeeperEndpoint, normalizeZooKeeperConnectString } from "@/lib/zookeeper/zookeeperConnection";
export type MqKafkaConnectionSource = "bootstrap" | "zookeeper";
@@ -72,12 +72,11 @@ export function mqKafkaConnectionTarget(input: MqKafkaConnectionInput): MqKafkaC
}
const parsed = parseKafkaBootstrapServers(input.bootstrapServers);
const first = parsed.bootstrapServers.split(",")[0];
const endpoint = new URL(`kafka://${first}`);
const first = parseKafkaBootstrapServerAddress(parsed.bootstrapServers.split(",")[0]);
const securityProtocol = resolveKafkaSecurityProtocol(input.securityProtocol || "", parsed.inferredSecurityProtocol);
return {
host: endpoint.hostname,
port: Number(endpoint.port),
host: first.host,
port: first.port,
ssl: securityProtocol === "SSL" || securityProtocol === "SASL_SSL",
};
}