7 files changed
+1106
-151
No files matched your search
@@ -11,8 +11,11 @@ const mocks = vi.hoisted(() => ({
|
||||
productionGuard: vi.fn(),
|
||||
toast: vi.fn(),
|
||||
listDatabases: vi.fn(),
|
||||
listDorisCatalogs: vi.fn(),
|
||||
listDorisCatalogDatabases: vi.fn(),
|
||||
listTables: vi.fn(),
|
||||
listSchemas: vi.fn(),
|
||||
tabUiState: {} as Record<string, unknown>,
|
||||
}));
|
||||
|
||||
function passthrough(tag: string): Component {
|
||||
@@ -119,6 +122,8 @@ vi.mock("@/lib/backend/api", () => ({
|
||||
executeQuery: mocks.executeQuery,
|
||||
executeMulti: mocks.executeMulti,
|
||||
listDatabases: mocks.listDatabases,
|
||||
listDorisCatalogs: mocks.listDorisCatalogs,
|
||||
listDorisCatalogDatabases: mocks.listDorisCatalogDatabases,
|
||||
listTables: mocks.listTables,
|
||||
listSchemas: mocks.listSchemas,
|
||||
}));
|
||||
@@ -128,6 +133,9 @@ vi.mock("@/lib/database/productionExecutionGuard", () => ({
|
||||
return options.execute();
|
||||
},
|
||||
}));
|
||||
vi.mock("@/lib/tabs/tabUiState", () => ({
|
||||
useTabUiState: () => ({ initialState: mocks.tabUiState, track: vi.fn(), update: vi.fn() }),
|
||||
}));
|
||||
|
||||
import DatabaseUserAdmin from "@/components/admin/DatabaseUserAdmin.vue";
|
||||
|
||||
@@ -151,6 +159,26 @@ const nativeMysqlConnection: ConnectionConfig = {
|
||||
port: 3306,
|
||||
};
|
||||
|
||||
const postgresConnection: ConnectionConfig = {
|
||||
...connection,
|
||||
id: "postgres",
|
||||
name: "PostgreSQL",
|
||||
db_type: "postgres",
|
||||
driver_profile: "postgres",
|
||||
database: "app-db",
|
||||
port: 5432,
|
||||
};
|
||||
|
||||
const starrocksConnection: ConnectionConfig = {
|
||||
...connection,
|
||||
id: "starrocks",
|
||||
name: "StarRocks",
|
||||
db_type: "starrocks",
|
||||
driver_profile: "starrocks",
|
||||
database: "analytics",
|
||||
port: 9030,
|
||||
};
|
||||
|
||||
let app: ReturnType<typeof createApp> | undefined;
|
||||
let root: HTMLDivElement | undefined;
|
||||
|
||||
@@ -159,6 +187,7 @@ afterEach(() => {
|
||||
root?.remove();
|
||||
app = undefined;
|
||||
root = undefined;
|
||||
mocks.tabUiState = {};
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -266,6 +295,140 @@ describe("DatabaseUserAdmin MySQL create-user table grants", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("DatabaseUserAdmin PostgreSQL create-user table grants", () => {
|
||||
it("loads schema-qualified tables and keeps same-named tables distinct", async () => {
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.executeQuery.mockResolvedValueOnce({ columns: ["user", "host", "plugin"], rows: [["postgres", "LOGIN", "SUPERUSER"]] }).mockResolvedValueOnce({ columns: ["line"], rows: [["Role: postgres"]] });
|
||||
mocks.listDatabases.mockResolvedValue([{ name: "app-db" }]);
|
||||
mocks.listSchemas.mockResolvedValue(["sales", "audit", "pg_catalog"]);
|
||||
mocks.listTables.mockImplementation(async (_connectionId, _database, schema) => [{ name: "orders", table_type: "BASE TABLE" }, ...(schema === "sales" ? [{ name: 'daily"rollup', table_type: "BASE TABLE" }] : [])]);
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.append(root);
|
||||
app = createApp(DatabaseUserAdmin, { connection: postgresConnection });
|
||||
app.mount(root);
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(2));
|
||||
|
||||
findButton("userAdmin.newUser")?.click();
|
||||
await vi.waitFor(() => expect(findDialogButton("app-db")).toBeDefined());
|
||||
findDialogButton("app-db")?.click();
|
||||
const password = root.querySelector<HTMLInputElement>('[data-password-input="true"]');
|
||||
password!.value = "test-password";
|
||||
password!.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await nextTick();
|
||||
findDialogButton("userAdmin.specificTables")?.click();
|
||||
|
||||
await vi.waitFor(() => expect(mocks.listTables).toHaveBeenCalledTimes(2));
|
||||
expect(mocks.listTables.mock.calls.map((call) => call[2])).toEqual(["sales", "audit"]);
|
||||
expect(findDialogButton("sales.orders")).toBeDefined();
|
||||
expect(findDialogButton("audit.orders")).toBeDefined();
|
||||
findDialogButton("sales.orders")?.click();
|
||||
findDialogButton("audit.orders")?.click();
|
||||
findDialogButton('sales.daily"rollup')?.click();
|
||||
await nextTick();
|
||||
findDialogButton("userAdmin.previewSql")?.click();
|
||||
|
||||
await vi.waitFor(() => expect(root?.textContent).toContain('GRANT SELECT ON TABLE "sales"."orders" TO "app_user";'));
|
||||
expect(root?.textContent).toContain('GRANT SELECT ON TABLE "audit"."orders" TO "app_user";');
|
||||
expect(root?.textContent).toContain('GRANT SELECT ON TABLE "sales"."daily""rollup" TO "app_user";');
|
||||
expect(root?.textContent).not.toContain("ON ALL TABLES");
|
||||
});
|
||||
|
||||
it("loads an existing schema-qualified grant and executes its revoke in the target database", async () => {
|
||||
mocks.tabUiState = { privilegeScope: "table" };
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.executeQuery.mockImplementation(async (_connectionId, database, sql) => {
|
||||
if (sql.includes('r.rolname AS "user"')) return { columns: ["user", "host", "plugin"], rows: [["reader", "LOGIN", ""]] };
|
||||
if (sql.includes("FROM information_schema.table_privileges") && !sql.includes("UNION ALL")) return { columns: ["schema", "table", "privilege", "grant_option"], rows: [["sales", "orders", "SELECT", "NO"]] };
|
||||
return { columns: ["line"], rows: [["Role: reader"]] };
|
||||
});
|
||||
mocks.listDatabases.mockResolvedValue([{ name: "app-db" }]);
|
||||
mocks.listSchemas.mockResolvedValue(["sales"]);
|
||||
mocks.listTables.mockResolvedValue([{ name: "orders", table_type: "BASE TABLE" }]);
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.append(root);
|
||||
app = createApp(DatabaseUserAdmin, { connection: postgresConnection });
|
||||
app.mount(root);
|
||||
|
||||
await vi.waitFor(() => expect(findButton("userAdmin.revoke")?.disabled).toBe(false));
|
||||
expect(root.textContent).toContain("userAdmin.selectedTableCount");
|
||||
findButton("userAdmin.revoke")?.click();
|
||||
await vi.waitFor(() => expect(root?.textContent).toContain('REVOKE SELECT ON TABLE "sales"."orders" FROM "reader";'));
|
||||
|
||||
mocks.executeMulti.mockResolvedValue([]);
|
||||
findButton("userAdmin.applySql")?.click();
|
||||
await vi.waitFor(() => expect(mocks.executeMulti).toHaveBeenCalled());
|
||||
expect(mocks.executeMulti.mock.calls[0][1]).toBe("app-db");
|
||||
expect(mocks.executeMulti.mock.calls[0][2]).toBe('REVOKE SELECT ON TABLE "sales"."orders" FROM "reader";');
|
||||
});
|
||||
});
|
||||
|
||||
describe("DatabaseUserAdmin StarRocks table grants", () => {
|
||||
it("loads catalog-qualified tables for create and previews catalog-aware SQL", async () => {
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.executeQuery.mockResolvedValueOnce({ columns: ["User"], rows: [["'root'@'%'"]] }).mockResolvedValueOnce({ columns: ["UserIdentity", "Catalog", "Grants"], rows: [["'root'@'%'", null, "GRANT 'root' TO USER 'root'@'%'"]] });
|
||||
mocks.listDorisCatalogs.mockResolvedValue([
|
||||
{ name: "default_catalog", catalog_type: "Internal", is_current: true },
|
||||
{ name: "hive`catalog", catalog_type: "Hive", is_current: false },
|
||||
]);
|
||||
mocks.listDorisCatalogDatabases.mockResolvedValue([{ name: "analytics" }]);
|
||||
mocks.listTables.mockResolvedValue([{ name: "event`log", table_type: "BASE TABLE" }]);
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.append(root);
|
||||
app = createApp(DatabaseUserAdmin, { connection: starrocksConnection });
|
||||
app.mount(root);
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(2));
|
||||
findButton("userAdmin.newUser")?.click();
|
||||
|
||||
await vi.waitFor(() => expect(findDialogButton("hive`catalog / analytics")).toBeDefined());
|
||||
findDialogButton("hive`catalog / analytics")?.click();
|
||||
const password = root.querySelector<HTMLInputElement>('[data-password-input="true"]');
|
||||
password!.value = "test-password";
|
||||
password!.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await nextTick();
|
||||
findDialogButton("userAdmin.specificTables")?.click();
|
||||
await vi.waitFor(() => expect(mocks.listTables).toHaveBeenCalled());
|
||||
expect(mocks.listTables).toHaveBeenCalledWith("starrocks", "analytics", "", undefined, undefined, undefined, ["TABLE"], "hive`catalog");
|
||||
await vi.waitFor(() => expect(findDialogButton("event`log")).toBeDefined());
|
||||
findDialogButton("event`log")?.click();
|
||||
await nextTick();
|
||||
findDialogButton("userAdmin.previewSql")?.click();
|
||||
|
||||
await vi.waitFor(() => expect(root?.textContent).toContain("SET CATALOG `hive``catalog`;"));
|
||||
expect(root?.textContent).toContain("GRANT SELECT ON TABLE `analytics`.`event``log` TO USER 'app_user'@'%';");
|
||||
|
||||
mocks.executeMulti.mockResolvedValue([]);
|
||||
mocks.executeQuery.mockResolvedValue({ columns: ["User"], rows: [["'app_user'@'%'"]] });
|
||||
findButton("userAdmin.applySql")?.click();
|
||||
await vi.waitFor(() => expect(mocks.executeMulti).toHaveBeenCalledTimes(2));
|
||||
expect(mocks.executeMulti.mock.calls[1][5]).toEqual(expect.objectContaining({ catalog: "hive`catalog" }));
|
||||
});
|
||||
|
||||
it("restores a direct table grant and previews its revoke diff", async () => {
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.executeQuery.mockResolvedValueOnce({ columns: ["User"], rows: [["'reader'@'%'"]] }).mockResolvedValueOnce({
|
||||
columns: ["UserIdentity", "Catalog", "Grants"],
|
||||
rows: [["'reader'@'%'", "default", "GRANT SELECT ON TABLE `analytics`.`events` TO USER 'reader'@'%'"]],
|
||||
});
|
||||
mocks.listDorisCatalogs.mockResolvedValue([{ name: "default_catalog", catalog_type: "Internal", is_current: true }]);
|
||||
mocks.listDorisCatalogDatabases.mockResolvedValue([{ name: "analytics" }]);
|
||||
|
||||
root = document.createElement("div");
|
||||
document.body.append(root);
|
||||
app = createApp(DatabaseUserAdmin, { connection: starrocksConnection });
|
||||
app.mount(root);
|
||||
|
||||
await vi.waitFor(() => expect(findButton("userAdmin.revoke")?.disabled).toBe(false));
|
||||
expect(root.textContent).toContain("userAdmin.selectedTableCount");
|
||||
findButton("userAdmin.revoke")?.click();
|
||||
|
||||
await vi.waitFor(() => expect(root?.textContent).toContain("REVOKE SELECT ON TABLE `analytics`.`events` FROM USER 'reader'@'%';"));
|
||||
expect(root?.textContent).toContain("SET CATALOG `default_catalog`;");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DatabaseUserAdmin MySQL privilege editing", () => {
|
||||
it("previews one grant per selected table for the selected user", async () => {
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
|
||||
+137
-11
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { dorisUserAdminProvider, mysqlUserAdminProvider, postgresUserAdminProvider, starrocksUserAdminProvider } from "@/lib/database/databaseUserAdmin";
|
||||
import { authorizationPlanSql, buildCreateDatabaseAuthorizationPlan, buildCreateUserAuthorizationPlan, buildGrantAuthorizationPlan, executeAuthorizationPlan } from "@/lib/database/databaseAuthorizationPlan";
|
||||
import { authorizationPlanSql, buildCreateDatabaseAuthorizationPlan, buildCreateUserAuthorizationPlan, buildGrantAuthorizationPlan, databaseAuthorizationsFromTableGrants, executeAuthorizationPlan } from "@/lib/database/databaseAuthorizationPlan";
|
||||
|
||||
describe("database authorization plans", () => {
|
||||
it("grants PostgreSQL presets across user schemas and future objects", () => {
|
||||
@@ -79,6 +79,80 @@ describe("database authorization plans", () => {
|
||||
expect(sql).not.toContain('GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA "app" TO "reader";');
|
||||
});
|
||||
|
||||
it("grants only selected PostgreSQL tables across schemas with quoted identifiers", () => {
|
||||
const plan = buildCreateUserAuthorizationPlan({
|
||||
provider: postgresUserAdminProvider,
|
||||
principal: { user: 'report"reader', host: "LOGIN", password: "secret", canLogin: true },
|
||||
accountType: "standard",
|
||||
databases: [
|
||||
{
|
||||
database: "app-db",
|
||||
preset: "readWrite",
|
||||
schemas: ["public", "sales", "audit"],
|
||||
tables: [
|
||||
{ schema: "sales", name: "orders" },
|
||||
{ schema: "audit", name: "orders" },
|
||||
{ schema: 'odd"schema', name: 'daily"rollup' },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
const sql = authorizationPlanSql(plan);
|
||||
|
||||
expect(sql).toContain('GRANT CONNECT ON DATABASE "app-db" TO "report""reader";');
|
||||
expect(sql).toContain('GRANT USAGE ON SCHEMA "sales" TO "report""reader";');
|
||||
expect(sql).toContain('GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON TABLE "sales"."orders" TO "report""reader";');
|
||||
expect(sql).toContain('ON TABLE "audit"."orders"');
|
||||
expect(sql).toContain('ON TABLE "odd""schema"."daily""rollup"');
|
||||
expect(sql).not.toContain("ON ALL TABLES");
|
||||
expect(sql).not.toContain("ALTER DEFAULT PRIVILEGES");
|
||||
});
|
||||
|
||||
it("diffs PostgreSQL table grants and revokes only existing privileges", () => {
|
||||
const selection = [{ database: "app_db", preset: "custom" as const, privileges: ["SELECT", "INSERT"], tables: [{ schema: "tenant", name: "orders" }] }];
|
||||
const currentGrants = [{ database: "app_db", schema: "tenant", table: "orders", privilege: "SELECT", grantOption: false }];
|
||||
const grant = buildGrantAuthorizationPlan({ provider: postgresUserAdminProvider, user: { user: "reader", host: "LOGIN" }, databases: selection, currentGrants });
|
||||
const revoke = buildGrantAuthorizationPlan({ provider: postgresUserAdminProvider, user: { user: "reader", host: "LOGIN" }, databases: selection, currentGrants, revoke: true });
|
||||
|
||||
expect(grant.steps).toHaveLength(1);
|
||||
expect(grant.steps[0]).toMatchObject({ database: "app_db", targetDatabase: "app_db", targetSchema: "tenant", targetTable: "orders" });
|
||||
expect(grant.steps[0].sql).toBe('GRANT INSERT ON TABLE "tenant"."orders" TO "reader";');
|
||||
expect(revoke.steps).toHaveLength(1);
|
||||
expect(revoke.steps[0].sql).toBe('REVOKE SELECT ON TABLE "tenant"."orders" FROM "reader";');
|
||||
});
|
||||
|
||||
it("revokes existing PostgreSQL table privileges across a selected schema", () => {
|
||||
const plan = buildGrantAuthorizationPlan({
|
||||
provider: postgresUserAdminProvider,
|
||||
user: { user: "reader", host: "LOGIN" },
|
||||
databases: [{ database: "app_db", preset: "custom", privileges: ["SELECT"], schemas: ["sales"], tables: undefined }],
|
||||
currentGrants: [{ database: "app_db", schema: "sales", table: "orders", privilege: "SELECT", grantOption: false }],
|
||||
revoke: true,
|
||||
});
|
||||
|
||||
expect(plan.steps).toHaveLength(1);
|
||||
expect(plan.steps[0].sql).toBe('REVOKE SELECT ON ALL TABLES IN SCHEMA "sales" FROM "reader";');
|
||||
});
|
||||
|
||||
it("keeps same-named PostgreSQL tables in distinct schemas when restoring grants", () => {
|
||||
const loaded = databaseAuthorizationsFromTableGrants(postgresUserAdminProvider, [
|
||||
{ database: "app_db", schema: "sales", table: "orders", privilege: "SELECT", grantOption: false },
|
||||
{ database: "app_db", schema: "audit", table: "orders", privilege: "SELECT", grantOption: false },
|
||||
]);
|
||||
|
||||
expect(loaded.selections).toEqual([
|
||||
{
|
||||
database: "app_db",
|
||||
preset: "custom",
|
||||
privileges: ["SELECT"],
|
||||
tables: [
|
||||
{ name: "orders", schema: "sales" },
|
||||
{ name: "orders", schema: "audit" },
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps MySQL database grants scoped to the selected database", () => {
|
||||
const plan = buildCreateDatabaseAuthorizationPlan({
|
||||
provider: mysqlUserAdminProvider,
|
||||
@@ -126,7 +200,7 @@ describe("database authorization plans", () => {
|
||||
provider: mysqlUserAdminProvider,
|
||||
principal: { user: "table_reader", host: "%", password: "secret" },
|
||||
accountType: "standard",
|
||||
databases: [{ database: "app_db", preset: "readWrite", tables: ["orders", "audit`log", "orders"] }],
|
||||
databases: [{ database: "app_db", preset: "readWrite", tables: [{ name: "orders" }, { name: "audit`log" }, { name: "orders" }] }],
|
||||
});
|
||||
const sql = authorizationPlanSql(plan);
|
||||
const grants = plan.steps.filter((step) => step.operation === "grantDatabase");
|
||||
@@ -152,6 +226,61 @@ describe("database authorization plans", () => {
|
||||
expect(authorizationPlanSql(plan)).toContain("GRANT SELECT, SHOW VIEW ON `app_db`.* TO 'db_reader'@'%';");
|
||||
});
|
||||
|
||||
it("builds StarRocks grants in the selected catalog with StarRocks privileges", () => {
|
||||
const plan = buildCreateUserAuthorizationPlan({
|
||||
provider: starrocksUserAdminProvider,
|
||||
principal: { user: "table_reader", host: "%", password: "secret" },
|
||||
accountType: "standard",
|
||||
databases: [
|
||||
{
|
||||
catalog: "ice`berg",
|
||||
database: "sales-db",
|
||||
preset: "readWrite",
|
||||
tables: [{ name: "orders" }, { name: "audit`log" }],
|
||||
},
|
||||
],
|
||||
});
|
||||
const grants = plan.steps.filter((step) => step.operation === "grantDatabase");
|
||||
|
||||
expect(grants).toHaveLength(2);
|
||||
expect(grants[0]).toMatchObject({ database: "sales-db", targetCatalog: "ice`berg", targetDatabase: "sales-db", targetTable: "orders" });
|
||||
expect(grants[0].sql).toBe("SET CATALOG `ice``berg`;\nGRANT SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, EXPORT ON TABLE `sales-db`.`orders` TO USER 'table_reader'@'%';");
|
||||
expect(grants[1].sql).toContain("ON TABLE `sales-db`.`audit``log`");
|
||||
expect(authorizationPlanSql(plan)).not.toContain("SHOW VIEW");
|
||||
expect(authorizationPlanSql(plan)).not.toContain("TRIGGER");
|
||||
});
|
||||
|
||||
it("does not emit a fallback grant for an empty PostgreSQL or StarRocks table selection", () => {
|
||||
const postgres = buildCreateUserAuthorizationPlan({
|
||||
provider: postgresUserAdminProvider,
|
||||
principal: { user: "reader", host: "LOGIN", password: "secret" },
|
||||
accountType: "standard",
|
||||
databases: [{ database: "app_db", preset: "readOnly", tables: [] }],
|
||||
});
|
||||
const starrocks = buildCreateUserAuthorizationPlan({
|
||||
provider: starrocksUserAdminProvider,
|
||||
principal: { user: "reader", host: "%", password: "secret" },
|
||||
accountType: "standard",
|
||||
databases: [{ catalog: "default_catalog", database: "app_db", preset: "readOnly", tables: [] }],
|
||||
});
|
||||
|
||||
expect(postgres.steps.map((step) => step.operation)).toEqual(["createUser"]);
|
||||
expect(starrocks.steps.map((step) => step.operation)).toEqual(["createUser"]);
|
||||
expect(authorizationPlanSql(postgres)).not.toContain("ON TABLE");
|
||||
expect(authorizationPlanSql(starrocks)).not.toContain("GRANT SELECT");
|
||||
});
|
||||
|
||||
it("diffs StarRocks table grants before grant and revoke", () => {
|
||||
const databases = [{ catalog: "hive", database: "analytics", preset: "custom" as const, privileges: ["SELECT", "INSERT"], tables: [{ name: "events" }] }];
|
||||
const currentGrants = [{ catalog: "hive", database: "analytics", table: "events", privilege: "SELECT", grantOption: false }];
|
||||
const user = { user: "reporter", host: "10.%" };
|
||||
const grant = buildGrantAuthorizationPlan({ provider: starrocksUserAdminProvider, user, databases, currentGrants });
|
||||
const revoke = buildGrantAuthorizationPlan({ provider: starrocksUserAdminProvider, user, databases, currentGrants, revoke: true });
|
||||
|
||||
expect(grant.steps[0].sql).toBe("SET CATALOG `hive`;\nGRANT INSERT ON TABLE `analytics`.`events` TO USER 'reporter'@'10.%';");
|
||||
expect(revoke.steps[0].sql).toBe("SET CATALOG `hive`;\nREVOKE SELECT ON TABLE `analytics`.`events` FROM USER 'reporter'@'10.%';");
|
||||
});
|
||||
|
||||
it("reports PostgreSQL object grants independently", async () => {
|
||||
const plan = buildCreateDatabaseAuthorizationPlan({
|
||||
provider: postgresUserAdminProvider,
|
||||
@@ -189,7 +318,7 @@ describe("database authorization plans", () => {
|
||||
const plan = buildGrantAuthorizationPlan({
|
||||
provider: mysqlUserAdminProvider,
|
||||
user: { user: "app_user", host: "%" },
|
||||
databases: [{ database: "app_db", preset: "readOnly", tables: ["orders", "audit_log"] }],
|
||||
databases: [{ database: "app_db", preset: "readOnly", tables: [{ name: "orders" }, { name: "audit_log" }] }],
|
||||
grantOption: true,
|
||||
});
|
||||
const sql = authorizationPlanSql(plan);
|
||||
@@ -215,7 +344,7 @@ describe("database authorization plans", () => {
|
||||
const plan = buildGrantAuthorizationPlan({
|
||||
provider: mysqlUserAdminProvider,
|
||||
user: { user: "app_user", host: "%" },
|
||||
databases: [{ database: "app_db", preset: "readWrite", tables: ["orders"] }],
|
||||
databases: [{ database: "app_db", preset: "readWrite", tables: [{ name: "orders" }] }],
|
||||
grantOption: true,
|
||||
revoke: true,
|
||||
});
|
||||
@@ -226,13 +355,10 @@ describe("database authorization plans", () => {
|
||||
expect(authorizationPlanSql(plan)).not.toContain("GRANT OPTION");
|
||||
});
|
||||
|
||||
it("leaves Doris and StarRocks to their own privilege names", () => {
|
||||
for (const provider of [dorisUserAdminProvider, starrocksUserAdminProvider]) {
|
||||
// 两者都能生成 GRANT SQL,但权限名与 MySQL 预设不同,因此不应由本计划构造语句
|
||||
expect(provider.grantPrivilegesSql).toBeTypeOf("function");
|
||||
const plan = buildGrantAuthorizationPlan({ provider, user: { user: "app_user", host: "%" }, databases: [{ database: "app_db", preset: "readOnly" }] });
|
||||
it("keeps unsupported Doris table grants behind its capability", () => {
|
||||
expect(dorisUserAdminProvider.grantPrivilegesSql).toBeTypeOf("function");
|
||||
const plan = buildGrantAuthorizationPlan({ provider: dorisUserAdminProvider, user: { user: "app_user", host: "%" }, databases: [{ database: "app_db", preset: "readOnly" }] });
|
||||
|
||||
expect(plan.steps).toEqual([]);
|
||||
}
|
||||
expect(plan.steps).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -10,8 +10,11 @@ import {
|
||||
kingbaseUserAdminProvider,
|
||||
mysqlPrivilegeSelectionFromGrants,
|
||||
nativeMysqlUserAdminProvider,
|
||||
postgresTableGrantsResult,
|
||||
postgresTableGrantsSql,
|
||||
postgresUserAdminProvider,
|
||||
resolveDatabaseUserAdminProviderForConnection,
|
||||
starrocksTableGrantsResult,
|
||||
} from "@/lib/database/databaseUserAdmin";
|
||||
import type { ConnectionConfig, QueryResult } from "@/types/database";
|
||||
|
||||
@@ -143,11 +146,36 @@ describe("database user admin providers", () => {
|
||||
expect(sql).not.toContain(" || ");
|
||||
});
|
||||
|
||||
it("keeps native GaussDB, PostgreSQL, and Kingbase providers unchanged", () => {
|
||||
expect(resolveDatabaseUserAdminProviderForConnection(connection("gaussdb"))).toBe(postgresUserAdminProvider);
|
||||
it("limits PostgreSQL table selection to native PostgreSQL", () => {
|
||||
const gaussdb = resolveDatabaseUserAdminProviderForConnection(connection("gaussdb"));
|
||||
|
||||
expect(gaussdb).not.toBe(postgresUserAdminProvider);
|
||||
expect(gaussdb?.supportsTableGrantsOnCreate).toBeUndefined();
|
||||
expect(resolveDatabaseUserAdminProviderForConnection(connection("postgres"))).toBe(postgresUserAdminProvider);
|
||||
expect(resolveDatabaseUserAdminProviderForConnection(connection("kingbase"))).toBe(kingbaseUserAdminProvider);
|
||||
expect(getDatabaseUserAdminProvider("gaussdb")).toBe(postgresUserAdminProvider);
|
||||
expect(postgresUserAdminProvider.supportsTableGrantsOnCreate).toBe(true);
|
||||
expect(getDatabaseUserAdminProvider("gaussdb")?.supportsTableGrantsOnCreate).toBeUndefined();
|
||||
});
|
||||
|
||||
it("loads direct PostgreSQL table grants with database and schema identity", () => {
|
||||
const sql = postgresTableGrantsSql({ user: "role'o", host: "LOGIN" });
|
||||
const grants = postgresTableGrantsResult(
|
||||
result(
|
||||
["schema", "table", "privilege", "grant_option"],
|
||||
[
|
||||
["sales", "orders", "SELECT", "NO"],
|
||||
['odd"schema', 'daily"rollup', "UPDATE", "YES"],
|
||||
],
|
||||
),
|
||||
{ database: "app-db" },
|
||||
);
|
||||
|
||||
expect(sql).toContain("FROM information_schema.table_privileges");
|
||||
expect(sql).toContain("WHERE grantee = 'role''o'");
|
||||
expect(grants).toEqual([
|
||||
{ database: "app-db", schema: "sales", table: "orders", privilege: "SELECT", grantOption: false },
|
||||
{ database: "app-db", schema: 'odd"schema', table: 'daily"rollup', privilege: "UPDATE", grantOption: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it("uses Doris 2.x user and privilege syntax", () => {
|
||||
@@ -188,12 +216,33 @@ describe("database user admin providers", () => {
|
||||
expect(postgresProvider?.privilegeSelectionFromGrants).toBeUndefined();
|
||||
expect(dorisProvider?.privilegeSelectionFromGrants).toBeUndefined();
|
||||
expect(starrocksProvider?.privilegeSelectionFromGrants).toBeUndefined();
|
||||
expect(postgresProvider?.parseTableGrants).toBe(postgresTableGrantsResult);
|
||||
expect(starrocksProvider?.parseTableGrants).toBe(starrocksTableGrantsResult);
|
||||
expect(mysqlProvider?.defaultPrivilegesForScope?.("mysql")).toEqual(["SELECT"]);
|
||||
expect(postgresProvider?.defaultPrivilegesForScope?.("database")).toEqual(["CONNECT"]);
|
||||
expect(dorisProvider?.defaultPrivilegesForScope?.("table")).toEqual(["SELECT_PRIV"]);
|
||||
expect(starrocksProvider?.defaultPrivilegesForScope?.("table")).toEqual(["SELECT"]);
|
||||
});
|
||||
|
||||
it("parses StarRocks table grants with catalog and escaped identifiers", () => {
|
||||
const grants = starrocksTableGrantsResult(
|
||||
result(
|
||||
["UserIdentity", "Catalog", "Grants"],
|
||||
[
|
||||
["'reader'@'%'", "default", "GRANT SELECT ON ALL TABLES IN DATABASE `sales.db` TO USER 'reader'@'%'"],
|
||||
["'reader'@'%'", "ice`berg", "GRANT INSERT, UPDATE ON TABLE `sales.db`.`daily``rollup` TO USER 'reader'@'%' WITH GRANT OPTION"],
|
||||
["'reader'@'%'", null, "GRANT analyst TO USER 'reader'@'%'"],
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
expect(grants).toEqual([
|
||||
{ catalog: "default_catalog", database: "sales.db", table: "*", privilege: "SELECT", grantOption: false },
|
||||
{ catalog: "ice`berg", database: "sales.db", table: "daily`rollup", privilege: "INSERT", grantOption: true },
|
||||
{ catalog: "ice`berg", database: "sales.db", table: "daily`rollup", privilege: "UPDATE", grantOption: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it("uses sys_catalog for Kingbase role metadata", () => {
|
||||
const provider = getDatabaseUserAdminProvider("kingbase");
|
||||
|
||||
|
||||
Reference in new issue
Block a user