fix(user-admin): sync MySQL grants to privilege editor
This commit is contained in:
1 parent
d221e0dee7
commit
b42f2cc470
4 files changed
+297
-3
No files matched your search
@@ -0,0 +1,113 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, type Component } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
ensureConnected: vi.fn(),
|
||||
executeQuery: vi.fn(),
|
||||
}));
|
||||
|
||||
function passthrough(tag: string): Component {
|
||||
return defineComponent({
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs, slots }) {
|
||||
return () => h(tag, attrs, slots.default?.());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock("@lucide/vue", () => {
|
||||
const Icon = passthrough("span");
|
||||
return {
|
||||
AlertTriangle: Icon,
|
||||
Check: Icon,
|
||||
KeyRound: Icon,
|
||||
Lock: Icon,
|
||||
Loader2: Icon,
|
||||
Plus: Icon,
|
||||
RefreshCcw: Icon,
|
||||
Search: Icon,
|
||||
ShieldCheck: Icon,
|
||||
Trash2: Icon,
|
||||
Unlock: Icon,
|
||||
UserRound: Icon,
|
||||
};
|
||||
});
|
||||
vi.mock("@/components/ui/button", () => ({ Button: passthrough("button") }));
|
||||
vi.mock("@/components/ui/badge", () => ({ Badge: passthrough("span") }));
|
||||
vi.mock("@/components/ui/dialog", () => ({
|
||||
Dialog: passthrough("div"),
|
||||
DialogContent: passthrough("div"),
|
||||
DialogFooter: passthrough("div"),
|
||||
DialogHeader: passthrough("div"),
|
||||
DialogTitle: passthrough("div"),
|
||||
}));
|
||||
vi.mock("@/components/ui/input", () => ({ Input: passthrough("input") }));
|
||||
vi.mock("@/components/ui/PasswordInput.vue", () => ({ default: passthrough("input") }));
|
||||
vi.mock("@/components/ui/select", () => ({
|
||||
Select: passthrough("div"),
|
||||
SelectContent: passthrough("div"),
|
||||
SelectItem: passthrough("div"),
|
||||
SelectTrigger: passthrough("div"),
|
||||
SelectValue: passthrough("span"),
|
||||
}));
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
useConnectionStore: () => ({ ensureConnected: mocks.ensureConnected }),
|
||||
}));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
|
||||
vi.mock("@/composables/useSqlHighlighter", () => ({ useSqlHighlighter: () => ({ highlight: (sql: string) => sql }) }));
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
executeQuery: mocks.executeQuery,
|
||||
executeMulti: vi.fn(),
|
||||
listDatabases: vi.fn(),
|
||||
listSchemas: vi.fn(),
|
||||
}));
|
||||
|
||||
import DatabaseUserAdmin from "@/components/admin/DatabaseUserAdmin.vue";
|
||||
|
||||
const connection: ConnectionConfig = {
|
||||
id: "oceanbase",
|
||||
name: "OceanBase",
|
||||
db_type: "jdbc",
|
||||
driver_profile: "mysql",
|
||||
host: "localhost",
|
||||
port: 2881,
|
||||
username: "root",
|
||||
password: "",
|
||||
};
|
||||
|
||||
let app: ReturnType<typeof createApp> | undefined;
|
||||
let root: HTMLDivElement | undefined;
|
||||
|
||||
afterEach(() => {
|
||||
app?.unmount();
|
||||
root?.remove();
|
||||
app = undefined;
|
||||
root = undefined;
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("DatabaseUserAdmin MySQL grant loading", () => {
|
||||
it("syncs privilege buttons and grant option from loaded SHOW GRANTS rows", async () => {
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.executeQuery.mockResolvedValueOnce({ columns: ["user", "host", "plugin"], rows: [["root", "%", "mysql_native_password"]] }).mockResolvedValueOnce({ columns: ["Grants for root@%"], rows: [["GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION"]] });
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.append(root);
|
||||
app = createApp(DatabaseUserAdmin, { connection });
|
||||
app.mount(root);
|
||||
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(2));
|
||||
await nextTick();
|
||||
|
||||
const privilegeButton = Array.from(root.querySelectorAll("button")).find((button) => button.textContent?.trim() === "INSERT");
|
||||
const grantOptionLabel = Array.from(root.querySelectorAll("label")).find((label) => label.textContent?.includes("userAdmin.grantOption"));
|
||||
const grantOptionInput = grantOptionLabel?.querySelector<HTMLInputElement>('input[type="checkbox"]');
|
||||
|
||||
expect(privilegeButton?.className).toContain("border-primary");
|
||||
expect(grantOptionInput?.checked).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,75 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getDatabaseUserAdminProvider, kingbaseShowGrantsSql } from "@/lib/database/databaseUserAdmin";
|
||||
import { getDatabaseUserAdminProvider, kingbaseShowGrantsSql, mysqlPrivilegeSelectionFromGrants } from "@/lib/database/databaseUserAdmin";
|
||||
|
||||
describe("MySQL grant privilege selection", () => {
|
||||
const availablePrivileges = ["SELECT", "INSERT", "UPDATE", "EXECUTE"];
|
||||
|
||||
it("expands ALL PRIVILEGES and reads WITH GRANT OPTION for the current scope", () => {
|
||||
expect(
|
||||
mysqlPrivilegeSelectionFromGrants({
|
||||
grants: ["grant all privileges on `*`.* to 'root'@'%' WITH GRANT OPTION"],
|
||||
database: "*",
|
||||
table: "*",
|
||||
availablePrivileges,
|
||||
}),
|
||||
).toEqual({ privileges: availablePrivileges, grantOption: true });
|
||||
});
|
||||
|
||||
it("merges matching grants case-insensitively", () => {
|
||||
expect(
|
||||
mysqlPrivilegeSelectionFromGrants({
|
||||
grants: ["GRANT SELECT, insert ON `Sales`.`Orders` TO 'app'@'%'", "GRANT UPDATE ON sales.orders TO 'app'@'%' WITH GRANT OPTION"],
|
||||
database: "SALES",
|
||||
table: "orders",
|
||||
availablePrivileges,
|
||||
}),
|
||||
).toEqual({ privileges: ["SELECT", "INSERT", "UPDATE"], grantOption: true });
|
||||
});
|
||||
|
||||
it("supports escaped backticks and ignores grants for other scopes or routines", () => {
|
||||
expect(
|
||||
mysqlPrivilegeSelectionFromGrants({
|
||||
grants: ["GRANT SELECT ON `tenant``db`.`audit``log` TO 'app'@'%'", "GRANT INSERT ON `tenant``db`.* TO 'app'@'%' WITH GRANT OPTION", "GRANT EXECUTE ON PROCEDURE `tenant``db`.`audit``log` TO 'app'@'%' WITH GRANT OPTION"],
|
||||
database: "tenant`db",
|
||||
table: "audit`log",
|
||||
availablePrivileges,
|
||||
}),
|
||||
).toEqual({ privileges: ["SELECT"], grantOption: false });
|
||||
});
|
||||
|
||||
it("returns an empty selection when the current scope has no grant", () => {
|
||||
expect(
|
||||
mysqlPrivilegeSelectionFromGrants({
|
||||
grants: ["GRANT SELECT ON `other`.* TO 'app'@'%' WITH GRANT OPTION"],
|
||||
database: "current",
|
||||
table: "*",
|
||||
availablePrivileges,
|
||||
}),
|
||||
).toEqual({ privileges: [], grantOption: false });
|
||||
});
|
||||
});
|
||||
|
||||
describe("database user admin providers", () => {
|
||||
it("syncs loaded grants only for the MySQL provider", () => {
|
||||
const mysqlProvider = getDatabaseUserAdminProvider("mysql");
|
||||
const postgresProvider = getDatabaseUserAdminProvider("postgres");
|
||||
const starrocksProvider = getDatabaseUserAdminProvider("starrocks");
|
||||
|
||||
expect(
|
||||
mysqlProvider?.privilegeSelectionFromGrants?.({
|
||||
grants: ["GRANT INSERT ON `app`.* TO 'user'@'%'"],
|
||||
database: "app",
|
||||
table: "*",
|
||||
availablePrivileges: mysqlProvider.privilegesForScope?.("mysql") ?? [],
|
||||
}),
|
||||
).toEqual({ privileges: ["INSERT"], grantOption: false });
|
||||
expect(postgresProvider?.privilegeSelectionFromGrants).toBeUndefined();
|
||||
expect(starrocksProvider?.privilegeSelectionFromGrants).toBeUndefined();
|
||||
expect(mysqlProvider?.defaultPrivilegesForScope?.("mysql")).toEqual(["SELECT"]);
|
||||
expect(postgresProvider?.defaultPrivilegesForScope?.("database")).toEqual(["CONNECT"]);
|
||||
expect(starrocksProvider?.defaultPrivilegesForScope?.("table")).toEqual(["SELECT"]);
|
||||
});
|
||||
|
||||
it("uses sys_catalog for Kingbase role metadata", () => {
|
||||
const provider = getDatabaseUserAdminProvider("kingbase");
|
||||
|
||||
|
||||
Reference in new issue
Block a user