Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: optimize grant access (#14233)

ecmadaoac515528bfdc
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index b4b75d9778..da1af59061 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1106,7 +1106,10 @@ func (s *SQLService) accessCheck(
return status.Errorf(codes.Internal, "failed to check access control for database: %q, error %v", column.Database, err)
}
if !ok {
- return status.Errorf(codes.PermissionDenied, "permission denied to access resource: %q", column.String())
+ return status.Errorf(
+ codes.PermissionDenied,
+ "permission denied to access resource: %s", fmt.Sprintf("%s/schemas/%s/tables/%s", attributes["resource.database"], attributes["resource.schema"], attributes["resource.table"]),
+ )
}
}
}
diff --git a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
index 8874cfb32d..e8224f0ebf 100644
--- a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
+++ b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
@@ -30,20 +30,17 @@ import {
useDBSchemaV1Store,
useProjectByName,
} from "@/store";
-import {
- databaseNamePrefix,
- instanceNamePrefix,
-} from "@/store/modules/v1/common";
import { useDatabaseV1List } from "@/store/modules/v1/databaseList";
import type { DatabaseResource } from "@/types";
import { DatabaseMetadataView } from "@/types/proto/v1/database_service";
import { wrapRefAsPromise } from "@/utils";
import Label from "./Label.vue";
-import type { DatabaseTreeOption, DatabaseResourceType } from "./common";
+import type { DatabaseTreeOption } from "./common";
import {
flattenTreeOptions,
getSchemaOrTableTreeOptions,
mapTreeOptions,
+ parseStringToResource,
} from "./common";
const props = defineProps<{
@@ -64,47 +61,6 @@ const databaseStore = useDatabaseV1Store();
const dbSchemaStore = useDBSchemaV1Store();
const { project } = useProjectByName(props.projectName);
-const parseKeyToResource = (key: string): DatabaseResource | undefined => {
- const sections = key.split("/");
- const resource: DatabaseResource = {
- databaseName: "",
- };
-
- while (sections.length > 0) {
- const keyword = sections.shift() as DatabaseResourceType | "instances";
- const data = sections.shift() || "";
-
- switch (keyword) {
- case "instances":
- resource.instanceResourceId = data;
- break;
- case "databases":
- if (!resource.instanceResourceId) {
- return;
- }
- resource.databaseName = `${instanceNamePrefix}${resource.instanceResourceId}/${databaseNamePrefix}${data}`;
- break;
- case "schemas":
- resource.schema = data;
- break;
- case "tables":
- resource.table = data;
- break;
- case "columns":
- resource.column = data;
- break;
- default:
- return;
- }
- }
-
- if (!resource.databaseName) {
- return;
- }
-
- return resource;
-};
-
const parseResourceToKey = (resource: DatabaseResource): string => {
const data = [
resource.databaseName,
@@ -461,7 +417,7 @@ watch(selectedValueList, (selectedValueList) => {
emit(
"update:databaseResources",
filteredKeyList
- .map(parseKeyToResource)
+ .map(parseStringToResource)
.filter((data) => data) as DatabaseResource[]
);
});
diff --git a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/common.ts b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/common.ts
index d06e309bf0..2bc634a4fa 100644
--- a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/common.ts
+++ b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/common.ts
@@ -1,7 +1,11 @@
import { flatten, isUndefined } from "lodash-es";
import type { TransferOption, TreeOption } from "naive-ui";
import { useDBSchemaV1Store } from "@/store";
-import type { ComposedDatabase } from "@/types";
+import {
+ databaseNamePrefix,
+ instanceNamePrefix,
+} from "@/store/modules/v1/common";
+import type { ComposedDatabase, DatabaseResource } from "@/types";
import type { TableMetadata } from "@/types/proto/v1/database_service";
import { hasSchemaProperty } from "@/utils";
@@ -174,3 +178,46 @@ export const flattenTreeOptions = (
];
});
};
+
+export const parseStringToResource = (
+ key: string
+): DatabaseResource | undefined => {
+ const sections = key.split("/");
+ const resource: DatabaseResource = {
+ databaseName: "",
+ };
+
+ while (sections.length > 0) {
+ const keyword = sections.shift() as DatabaseResourceType | "instances";
+ const data = sections.shift() || "";
+
+ switch (keyword) {
+ case "instances":
+ resource.instanceResourceId = data;
+ break;
+ case "databases":
+ if (!resource.instanceResourceId) {
+ return;
+ }
+ resource.databaseName = `${instanceNamePrefix}${resource.instanceResourceId}/${databaseNamePrefix}${data}`;
+ break;
+ case "schemas":
+ resource.schema = data;
+ break;
+ case "tables":
+ resource.table = data;
+ break;
+ case "columns":
+ resource.column = data;
+ break;
+ default:
+ return;
+ }
+ }
+
+ if (!resource.databaseName) {
+ return;
+ }
+
+ return resource;
+};
diff --git a/frontend/src/components/GrantRequestPanel/GrantRequestPanel.vue b/frontend/src/components/GrantRequestPanel/GrantRequestPanel.vue
index c632f881d3..7669896383 100644
--- a/frontend/src/components/GrantRequestPanel/GrantRequestPanel.vue
+++ b/frontend/src/components/GrantRequestPanel/GrantRequestPanel.vue
@@ -88,7 +88,7 @@ import RequiredStar from "@/components/RequiredStar.vue";
import { Drawer, DrawerContent } from "@/components/v2";
import { issueServiceClient } from "@/grpcweb";
import { useCurrentUserV1, useProjectV1Store } from "@/store";
-import type { ComposedDatabase, DatabaseResource } from "@/types";
+import type { DatabaseResource } from "@/types";
import { PresetRoleType, isValidDatabaseName } from "@/types";
import { Duration } from "@/types/proto/google/protobuf/duration";
import { Expr } from "@/types/proto/google/type/expr";
@@ -113,11 +113,11 @@ const props = withDefaults(
defineProps<{
projectName: string;
role: PresetRoleType.PROJECT_QUERIER | PresetRoleType.PROJECT_EXPORTER;
- database?: ComposedDatabase;
+ databaseResource?: DatabaseResource;
placement?: "left" | "right";
}>(),
{
- database: undefined,
+ databaseResource: undefined,
placement: "right",
}
);
@@ -130,8 +130,11 @@ const extractDatabaseResourcesFromProps = (): Pick<
LocalState,
"databaseResources"
> => {
- const { database } = props;
- if (!database || !isValidDatabaseName(database.name)) {
+ const { databaseResource } = props;
+ if (
+ !databaseResource ||
+ !isValidDatabaseName(databaseResource.databaseName)
+ ) {
return {
databaseResources: [],
};
@@ -139,7 +142,7 @@ const extractDatabaseResourcesFromProps = (): Pick<
return {
databaseResources: [
{
- databaseName: database.name,
+ ...databaseResource,
},
],
};
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/TreeNode/DatabaseNode.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/TreeNode/DatabaseNode.vue
index e4ddd12fd8..f1fd36e027 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/TreeNode/DatabaseNode.vue
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/TreeNode/DatabaseNode.vue
@@ -26,7 +26,9 @@
</span>
<RequestQueryButton
v-if="!disallowRequestQuery && !canQuery"
- :database="database"
+ :database-resource="{
+ databaseName: database.name,
+ }"
:size="'tiny'"
/>
</div>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/RequestQueryButton.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/RequestQueryButton.vue
index 2602f98e1a..da0c3b18f5 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/RequestQueryButton.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/RequestQueryButton.vue
@@ -7,7 +7,7 @@
<GrantRequestPanel
v-if="showPanel"
:project-name="database.project"
- :database="database"
+ :database-resource="databaseResource"
:placement="'right'"
:role="PresetRoleType.PROJECT_QUERIER"
@close="showPanel = false"
@@ -19,16 +19,17 @@
import { NButton } from "naive-ui";
import { computed, ref } from "vue";
import GrantRequestPanel from "@/components/GrantRequestPanel";
+import { useDatabaseV1Store } from "@/store";
import {
isValidDatabaseName,
PresetRoleType,
- type ComposedDatabase,
+ type DatabaseResource,
} from "@/types";
import { hasPermissionToCreateRequestGrantIssue } from "@/utils";
const props = withDefaults(
defineProps<{
- database: ComposedDatabase;
+ databaseResource: DatabaseResource;
size?: "tiny" | "medium";
}>(),
{
@@ -37,13 +38,18 @@ const props = withDefaults(
);
const showPanel = ref(false);
+const dbStore = useDatabaseV1Store();
+
+const database = computed(() =>
+ dbStore.getDatabaseByName(props.databaseResource.databaseName)
+);
const available = computed(() => {
- if (!isValidDatabaseName(props.database.name)) {
+ if (!isValidDatabaseName(props.databaseResource.databaseName)) {
return false;
}
- return hasPermissionToCreateRequestGrantIssue(props.database);
+ return hasPermissionToCreateRequestGrantIssue(database.value);
});
const onClick = (e: MouseEvent) => {
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
index e1e055403f..5873e5a850 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
@@ -56,11 +56,8 @@
>
<template #suffix>
<RequestQueryButton
- v-if="
- !disallowRequestQuery &&
- resultSet.status === Status.PERMISSION_DENIED
- "
- :database="database ?? connectedDatabase"
+ v-if="!disallowRequestQuery && missingResource"
+ :database-resource="missingResource"
/>
<SyncDatabaseButton
v-else-if="
@@ -106,6 +103,7 @@ import { useI18n } from "vue-i18n";
import { darkThemeOverrides } from "@/../naive-ui.config";
import { BBSpin } from "@/bbkit";
import SyncDatabaseButton from "@/components/DatabaseDetail/SyncDatabaseButton.vue";
+import { parseStringToResource } from "@/components/GrantRequestPanel/DatabaseResourceForm/common";
import { Drawer } from "@/components/v2";
import {
useAppFeature,
@@ -116,6 +114,7 @@ import type {
ComposedDatabase,
SQLEditorQueryParams,
SQLResultSetV1,
+ DatabaseResource,
} from "@/types";
import { PolicyType } from "@/types/proto/v1/org_policy_service";
import type { QueryResult } from "@/types/proto/v1/sql_service";
@@ -176,6 +175,21 @@ const detail: SQLResultViewContext["detail"] = ref({
table: undefined,
});
+const missingResource = computed((): DatabaseResource | undefined => {
+ if (props.resultSet?.status !== Status.PERMISSION_DENIED) {
+ return;
+ }
+ const prefix = "permission denied to access resource: ";
+ if (!props.resultSet.error.includes(prefix)) {
+ return;
+ }
+ const resource = props.resultSet.error.split(prefix).pop();
+ if (!resource) {
+ return;
+ }
+ return parseStringToResource(resource);
+});
+
const viewMode = computed((): ViewMode => {
const { resultSet } = props;
if (!resultSet) {