feat(nacos): add access control support
This commit is contained in:
67 files changed
+9062
-135
No files matched your search
+102
@@ -0,0 +1,102 @@
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ConnectionConfig, TreeNode } from "@/types/database";
|
||||
|
||||
function installLocalStorage() {
|
||||
const data = new Map<string, string>();
|
||||
vi.stubGlobal("localStorage", {
|
||||
getItem: vi.fn((key: string) => data.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => data.set(key, value)),
|
||||
removeItem: vi.fn((key: string) => data.delete(key)),
|
||||
});
|
||||
}
|
||||
|
||||
describe("connectionStore Nacos namespace access", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.unstubAllGlobals();
|
||||
installLocalStorage();
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
async function loadNacosTree(listUsersSupported: boolean, listRoleBindingsSupported = false) {
|
||||
const connection = {
|
||||
id: "nacos-bb",
|
||||
name: "Nacos bb",
|
||||
db_type: "nacos",
|
||||
host: "127.0.0.1",
|
||||
port: 8848,
|
||||
username: "bb",
|
||||
password: "secret",
|
||||
database: "",
|
||||
visible_databases: ["aa", "bb", "cc", ""],
|
||||
} as ConnectionConfig;
|
||||
// The backend has already removed `bb` and `public`, which the restricted
|
||||
// account cannot read even when the server's raw list endpoint exposes them.
|
||||
const readableNamespaces = ["aa", "cc"].map((namespace) => ({
|
||||
namespace,
|
||||
namespaceShowName: namespace || "public",
|
||||
}));
|
||||
const nacosListConfigs = vi.fn();
|
||||
const nacosSidebarSnapshot = vi.fn().mockResolvedValue({
|
||||
namespaces: readableNamespaces,
|
||||
accessControl: {
|
||||
listUsers: { supported: listUsersSupported },
|
||||
listRoleBindings: { supported: listRoleBindingsSupported },
|
||||
},
|
||||
});
|
||||
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
|
||||
listInstalledAgents: vi.fn().mockResolvedValue([]),
|
||||
loadSchemaCache: vi.fn().mockResolvedValue(null),
|
||||
nacosListConfigs,
|
||||
nacosSidebarSnapshot,
|
||||
saveConnections: vi.fn().mockResolvedValue(undefined),
|
||||
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
|
||||
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
const root: TreeNode = {
|
||||
id: connection.id,
|
||||
label: connection.name,
|
||||
type: "connection",
|
||||
connectionId: connection.id,
|
||||
isExpanded: false,
|
||||
children: [],
|
||||
};
|
||||
store.connections = [connection];
|
||||
store.treeNodes = [root];
|
||||
store.connectedIds.add(connection.id);
|
||||
|
||||
await store.loadNacosNamespaces(connection.id, { force: true });
|
||||
|
||||
return { root, store, nacosListConfigs, nacosSidebarSnapshot };
|
||||
}
|
||||
|
||||
it("hides unreadable namespaces without adding per-namespace sidebar requests", async () => {
|
||||
const { root, store, nacosListConfigs, nacosSidebarSnapshot } = await loadNacosTree(false);
|
||||
|
||||
expect(nacosListConfigs).not.toHaveBeenCalled();
|
||||
expect(nacosSidebarSnapshot).toHaveBeenCalledTimes(1);
|
||||
expect(root.children?.filter((node) => node.type === "nacos-namespace").map((node) => node.label)).toEqual(["aa", "cc"]);
|
||||
expect(store.getSidebarVisibleFilterSummary("nacos-bb")).toEqual({ mode: "namespace", isActive: false, selected: 2, total: 2 });
|
||||
expect(root.children?.some((node) => node.type === "nacos-access-control")).toBe(false);
|
||||
});
|
||||
|
||||
it("shows access control when the current account can list users", async () => {
|
||||
const { root } = await loadNacosTree(true);
|
||||
|
||||
expect(root.children?.find((node) => node.type === "nacos-access-control")?.label).toBe("nacos.accessControlSidebarLabel");
|
||||
});
|
||||
|
||||
it("shows access control for a roles-only account", async () => {
|
||||
const { root } = await loadNacosTree(false, true);
|
||||
|
||||
expect(root.children?.find((node) => node.type === "nacos-access-control")?.label).toBe("nacos.accessControlSidebarLabel");
|
||||
});
|
||||
});
|
||||
@@ -67,6 +67,32 @@ describe("connectionStore timeout recovery", () => {
|
||||
expect(store.connectedIds.has(connection.id)).toBe(true);
|
||||
}, 10_000);
|
||||
|
||||
it("does not block pure navigation on a connected health check", async () => {
|
||||
const checkConnectionHealth = vi.fn(() => new Promise(() => undefined));
|
||||
const connectDb = vi.fn().mockResolvedValue(undefined);
|
||||
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth,
|
||||
connectDb,
|
||||
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
|
||||
saveConnections: vi.fn().mockResolvedValue(undefined),
|
||||
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
const connection = postgresConnection();
|
||||
store.connections = [connection];
|
||||
store.connectedIds.add(connection.id);
|
||||
|
||||
await store.ensureConnected(connection.id, { verifyHealth: false });
|
||||
|
||||
expect(checkConnectionHealth).not.toHaveBeenCalled();
|
||||
expect(connectDb).not.toHaveBeenCalled();
|
||||
expect(store.connectedIds.has(connection.id)).toBe(true);
|
||||
});
|
||||
|
||||
it("normalizes missing keepalive interval to 30 seconds", async () => {
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
|
||||
@@ -3370,8 +3370,12 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
invalidateObjectBrowserRowsCache({ connectionId, database });
|
||||
}
|
||||
|
||||
async function ensureConnected(connectionId: string, options: { activate?: boolean } = {}) {
|
||||
async function ensureConnected(connectionId: string, options: { activate?: boolean; verifyHealth?: boolean } = {}) {
|
||||
if (connectedIds.value.has(connectionId)) {
|
||||
// Pure navigation can safely trust the existing connected state. Its
|
||||
// destination will perform the real API request, while blocking here on
|
||||
// a health probe makes an otherwise local tab switch take up to 5s.
|
||||
if (options.verifyHealth === false) return;
|
||||
if (hasRecentConnectionHealthCheck(connectionId)) return;
|
||||
// Optimistic: verify backend pool is actually healthy
|
||||
try {
|
||||
@@ -4067,7 +4071,8 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
load = reclaimTreeNodeLoad(load, node);
|
||||
if (useCachedChildren(node, options, load)) return;
|
||||
|
||||
const namespaces = normalizeNacosNamespacesForDisplay(await api.nacosListNamespaces(connectionId));
|
||||
const sidebarSnapshot = await api.nacosSidebarSnapshot(connectionId);
|
||||
const namespaces = normalizeNacosNamespacesForDisplay(sidebarSnapshot.namespaces);
|
||||
const visibleNamespaces = filterNacosNamespacesForSidebar(namespaces, getConfig(connectionId)?.visible_databases);
|
||||
const sorted = [...visibleNamespaces].sort((left, right) => {
|
||||
const leftLabel = left.namespaceShowName || left.namespace || "public";
|
||||
@@ -4080,9 +4085,8 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
connectionId,
|
||||
namespaces.map((namespace) => namespace.namespace),
|
||||
);
|
||||
setChildren(
|
||||
targetNode,
|
||||
sorted.map((namespace) => {
|
||||
const children: TreeNode[] = [
|
||||
...sorted.map((namespace) => {
|
||||
const value = namespace.namespace || "";
|
||||
const label = namespace.namespaceShowName || value || "public";
|
||||
return {
|
||||
@@ -4096,7 +4100,19 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
objectCount: namespace.configCount,
|
||||
};
|
||||
}),
|
||||
);
|
||||
];
|
||||
if (sidebarSnapshot.accessControl.listUsers.supported === true || sidebarSnapshot.accessControl.listRoleBindings.supported === true) {
|
||||
children.push({
|
||||
id: `${connectionId}:nacos-access-control`,
|
||||
label: "nacos.accessControlSidebarLabel",
|
||||
type: "nacos-access-control" as const,
|
||||
connectionId,
|
||||
database: "",
|
||||
isExpanded: false,
|
||||
children: [],
|
||||
});
|
||||
}
|
||||
setChildren(targetNode, children);
|
||||
targetNode.isExpanded = true;
|
||||
} catch (e) {
|
||||
recordMetadataLoadError(connectionId, e, load);
|
||||
|
||||
Reference in new issue
Block a user