Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: get data source by policy (#16260)
ecmadao8885207bc286
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.tsindex d12f1648dd..63d9f3a8b2 100644--- a/frontend/src/composables/useExecuteSQL.ts+++ b/frontend/src/composables/useExecuteSQL.ts@@ -37,7 +37,7 @@ import {CheckRequest_ChangeType,} from "@/types/proto/v1/sql_service";import {- ensureDataSourceSelection,+ getValidDataSourceByPolicy,hasPermissionToCreateChangeDatabaseIssue,} from "@/utils";import { extractGrpcErrorMessage } from "@/utils/grpcweb";@@ -174,15 +174,14 @@ const useExecuteSQL = () => {connection: SQLEditorConnection,mode?: QueryDataSourceType) => {- return (- ensureDataSourceSelection(- database.instance === connection.instance- ? connection.dataSourceId- : undefined,- database,- mode- ) ?? ""- );+ if (+ database.instance === connection.instance &&+ !!connection.dataSourceId+ ) {+ return connection.dataSourceId;+ }++ return getValidDataSourceByPolicy(database, mode) ?? "";};const preExecute = async (params: SQLEditorQueryParams) => {diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.tsindex b4a2f534e2..7099b1218a 100644--- a/frontend/src/utils/sqlEditor.ts+++ b/frontend/src/utils/sqlEditor.ts@@ -23,7 +23,6 @@ import {import {DataSourceType,type InstanceResource,- type DataSource,} from "@/types/proto/v1/instance_service";import {DataSourceQueryPolicy_Restriction,@@ -196,22 +195,6 @@ export const getAdminDataSourceRestrictionOfDatabase = (};};-const getDataSourceIdByType = (- dataSource: {- admin: DataSource;- readonly: DataSource[];- },- type?: QueryDataSourceType-) => {- switch (type) {- case DataSourceType.ADMIN:- return dataSource.admin.id;- default:- // try to use read-only data source first.- return head(dataSource.readonly)?.id ?? dataSource.admin.id;- }-};-const getDataSourceBehavior = (database: ComposedDatabase) => {const restriction = getAdminDataSourceRestrictionOfDatabase(database);let behavior: "RO" | "FALLBACK" | "ALLOW_ADMIN";@@ -233,8 +216,7 @@ const getDataSourceBehavior = (database: ComposedDatabase) => {return behavior;};-export const ensureDataSourceSelection = (- current: string | undefined,+export const getValidDataSourceByPolicy = (database: ComposedDatabase,type?: QueryDataSourceType) => {@@ -248,37 +230,20 @@ export const ensureDataSourceSelection = (const behavior = getDataSourceBehavior(database);switch (behavior) {- case "ALLOW_ADMIN": {- // ALLOW_ADMIN means no policy restriction.- if (current) {- return current;- }- return getDataSourceIdByType(- { admin: adminDataSource, readonly: readonlyDataSources },- type- );- }+ case "ALLOW_ADMIN":+ // ALLOW_ADMIN means no policy restriction.case "FALLBACK": {// FALLBACK means try to use read-only data source, it can also accept admin data source if no read-only data source exists.- if (- current &&- (current !== adminDataSource.id || readonlyDataSources.length === 0)- ) {- return current;+ switch (type) {+ case DataSourceType.ADMIN:+ return adminDataSource.id;+ default:+ // try to use read-only data source first.+ return head(readonlyDataSources)?.id ?? adminDataSource.id;}- return getDataSourceIdByType(- { admin: adminDataSource, readonly: readonlyDataSources },- type- );}case "RO": {// RO only accept the read-only data source.- if (- current &&- readonlyDataSources.findIndex((ds) => ds.id === current) >= 0- ) {- return current;- }return head(readonlyDataSources)?.id;}}diff --git a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vueindex b526a78de5..e5664e3fcc 100644--- a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue+++ b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue@@ -131,7 +131,7 @@ import {PolicyType,} from "@/types/proto/v1/org_policy_service";import { QueryOption_RedisRunCommandsOn } from "@/types/proto/v1/sql_service";-import { ensureDataSourceSelection, readableDataSourceType } from "@/utils";+import { getValidDataSourceByPolicy, readableDataSourceType } from "@/utils";import { getAdminDataSourceRestrictionOfDatabase } from "@/utils";import ResultLimitSelect from "./ResultLimitSelect.vue";@@ -227,11 +227,11 @@ const onDataSourceSelected = (dataSourceId?: string) => {};watch(- [selectedDataSourceId, database],+ [() => selectedDataSourceId.value, () => database.value],([current, database]) => {if (!isValidDatabaseName(database.name)) return;- const fixed = ensureDataSourceSelection(current, database);- if (fixed !== current) {+ if (!current) {+ const fixed = getValidDataSourceByPolicy(database);onDataSourceSelected(fixed);}},