fix(postgres): 支持旧版 TLS 兼容模式
This commit is contained in:
16 files changed
+537
-20
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { postgresTlsModeForForm } from "@/lib/connection/postgresTlsMode";
|
||||
import { postgresLegacyTlsEnabled, postgresTlsModeForForm, setPostgresLegacyTlsEnabled } from "@/lib/connection/postgresTlsMode";
|
||||
|
||||
describe("postgresTlsModeForForm", () => {
|
||||
it("uses prefer when legacy connections have no explicit mode", () => {
|
||||
@@ -16,3 +16,17 @@ describe("postgresTlsModeForForm", () => {
|
||||
expect(postgresTlsModeForForm("verify_identity", false)).toBe("verify-full");
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgreSQL legacy TLS compatibility", () => {
|
||||
it("recognizes explicit enabled values", () => {
|
||||
expect(postgresLegacyTlsEnabled("sslmode=require&legacy_tls=true")).toBe(true);
|
||||
expect(postgresLegacyTlsEnabled("legacy_tls=1")).toBe(true);
|
||||
expect(postgresLegacyTlsEnabled("legacy_tls=false")).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves unrelated URL parameters when toggled", () => {
|
||||
const enabled = setPostgresLegacyTlsEnabled("sslmode=require&application_name=dbx", true);
|
||||
expect(enabled).toBe("sslmode=require&application_name=dbx&legacy_tls=true");
|
||||
expect(setPostgresLegacyTlsEnabled(enabled, false)).toBe("sslmode=require&application_name=dbx");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
export type PostgresTlsMode = "disable" | "prefer" | "require" | "verify-ca" | "verify-full";
|
||||
|
||||
export const POSTGRES_LEGACY_TLS_PARAM = "legacy_tls";
|
||||
|
||||
export function postgresTlsModeForForm(value: string | undefined, ssl: boolean | undefined): PostgresTlsMode {
|
||||
switch ((value || "").trim().toLowerCase()) {
|
||||
case "disable":
|
||||
@@ -17,3 +19,15 @@ export function postgresTlsModeForForm(value: string | undefined, ssl: boolean |
|
||||
return ssl ? "require" : "prefer";
|
||||
}
|
||||
}
|
||||
|
||||
export function postgresLegacyTlsEnabled(params: string | undefined): boolean {
|
||||
const parsed = new URLSearchParams((params || "").trim().replace(/^\?/, ""));
|
||||
return ["1", "true", "yes", "on"].includes((parsed.get(POSTGRES_LEGACY_TLS_PARAM) || "").trim().toLowerCase());
|
||||
}
|
||||
|
||||
export function setPostgresLegacyTlsEnabled(params: string | undefined, enabled: boolean): string {
|
||||
const parsed = new URLSearchParams((params || "").trim().replace(/^\?/, ""));
|
||||
if (enabled) parsed.set(POSTGRES_LEGACY_TLS_PARAM, "true");
|
||||
else parsed.delete(POSTGRES_LEGACY_TLS_PARAM);
|
||||
return parsed.toString();
|
||||
}
|
||||
Reference in new issue
Block a user