diff --git a/internal/sql_workbench/client/sql_workbench_client.go b/internal/sql_workbench/client/sql_workbench_client.go index c4004af31..082e6b8be 100644 --- a/internal/sql_workbench/client/sql_workbench_client.go +++ b/internal/sql_workbench/client/sql_workbench_client.go @@ -978,9 +978,10 @@ type CreateDatasourceRequest struct { JdbcURLParameters map[string]interface{} `json:"jdbcUrlParameters"` Host string `json:"host"` Port string `json:"port"` + DefaultSchema *string `json:"defaultSchema,omitempty"` } -// UpdateDatasourceRequest 创建数据源请求结构 +// UpdateDatasourceRequest 更新数据源请求结构 type UpdateDatasourceRequest struct { Id int64 `json:"id"` CreatorID *int64 `json:"creatorId"` @@ -997,6 +998,7 @@ type UpdateDatasourceRequest struct { JdbcURLParameters *map[string]interface{} `json:"jdbcUrlParameters"` Host string `json:"host"` Port string `json:"port"` + DefaultSchema *string `json:"defaultSchema,omitempty"` } // DataSourceStatus 数据源状态结构 diff --git a/internal/sql_workbench/service/sql_workbench_service.go b/internal/sql_workbench/service/sql_workbench_service.go index 17823c3e2..520b89add 100644 --- a/internal/sql_workbench/service/sql_workbench_service.go +++ b/internal/sql_workbench/service/sql_workbench_service.go @@ -839,6 +839,7 @@ type datasourceBaseInfo struct { Port string ServiceName *string EnvironmentID int64 + DefaultSchema *string } // buildDatasourceBaseInfo 构建数据源基础信息 @@ -885,6 +886,7 @@ func (sqlWorkbenchService *SqlWorkbenchService) buildCreateDatasourceRequest(ctx ServiceName: baseInfo.ServiceName, SSLConfig: client.SSLConfig{Enabled: false}, EnvironmentID: baseInfo.EnvironmentID, + DefaultSchema: baseInfo.DefaultSchema, }, nil } @@ -905,6 +907,7 @@ func (sqlWorkbenchService *SqlWorkbenchService) buildUpdateDatasourceRequest(ctx ServiceName: baseInfo.ServiceName, SSLConfig: client.SSLConfig{Enabled: false}, EnvironmentID: baseInfo.EnvironmentID, + DefaultSchema: baseInfo.DefaultSchema, }, nil } @@ -930,13 +933,15 @@ func (sqlWorkbenchService *SqlWorkbenchService) convertDBType(dmsDBType string) return "DM" case "TiDB": return "TIDB" + case "GoldenDB": + return "MYSQL" default: return dmsDBType } } func (sqlWorkbenchService *SqlWorkbenchService) SupportDBType(dbType pkgConst.DBType) bool { - return dbType == pkgConst.DBTypeMySQL || dbType == pkgConst.DBTypeOracle || dbType == pkgConst.DBTypeOceanBaseMySQL || dbType == pkgConst.DBTypeDM || dbType == pkgConst.DBTypeTiDB + return dbType == pkgConst.DBTypeMySQL || dbType == pkgConst.DBTypeOracle || dbType == pkgConst.DBTypeOceanBaseMySQL || dbType == pkgConst.DBTypeDM || dbType == pkgConst.DBTypeTiDB || dbType == pkgConst.DBTypeGoldenDB } // buildDatabaseUser 当是ob-mysql时需要给账号管理的账号附加租户名集群名等字符: root@oms_mysql#oms_resource_4250 @@ -1070,6 +1075,12 @@ func (sqlWorkbenchService *SqlWorkbenchService) AuditMiddleware() echo.Middlewar return errors.New(locale.Bundle.LocalizeMsgByCtx(c.Request().Context(), locale.SqlWorkbenchAuditGetDBServiceErr)) } + // 对 SQLE 不支持审核的数据库类型(如 GoldenDB)直接放行,不做审核拦截 + if !sqlWorkbenchService.isSQLAuditSupportedDBType(dbService.DBType) { + sqlWorkbenchService.log.Infof("SQL audit not supported for db type %s (DBService: %s), bypassing audit", dbService.DBType, dmsDBServiceID) + return next(c) + } + // 检查是否启用 SQL 审核 if !sqlWorkbenchService.isEnableSQLAudit(dbService) { sqlWorkbenchService.log.Debugf("SQL audit is not enabled for DBService: %s", dmsDBServiceID) @@ -1225,6 +1236,23 @@ func (sqlWorkbenchService *SqlWorkbenchService) isEnableSQLAudit(dbService *biz. return dbService.SQLEConfig.AuditEnabled && dbService.SQLEConfig.SQLQueryConfig.AuditEnabled } +// isSQLAuditSupportedDBType 判断当前数据源类型是否被 SQLE 审核插件支持。 +// SQLE 仅对有对应审核插件的数据库类型提供审核能力;对于不在此列表中的类型 +// (如 GoldenDB、TBase、DB2 等),AuditMiddleware 应跳过审核直接放行。 +func (sqlWorkbenchService *SqlWorkbenchService) isSQLAuditSupportedDBType(dbType string) bool { + supportedTypes := map[string]struct{}{ + string(pkgConst.DBTypeMySQL): {}, + string(pkgConst.DBTypeOracle): {}, + string(pkgConst.DBTypeOceanBaseMySQL): {}, + string(pkgConst.DBTypeTiDB): {}, + string(pkgConst.DBTypePostgreSQL): {}, + string(pkgConst.DBTypeSQLServer): {}, + string(pkgConst.DBTypeDM): {}, + } + _, ok := supportedTypes[dbType] + return ok +} + // callSQLEAudit 调用 SQLE 直接审核接口 func (sqlWorkbenchService *SqlWorkbenchService) callSQLEAudit(ctx context.Context, sql string, dbService *biz.DBService) (*cloudbeaver.AuditSQLReply, error) { // 获取 SQLE 服务地址 diff --git a/internal/sql_workbench/service/sql_workbench_service_test.go b/internal/sql_workbench/service/sql_workbench_service_test.go index e35809f11..b14ba3064 100644 --- a/internal/sql_workbench/service/sql_workbench_service_test.go +++ b/internal/sql_workbench/service/sql_workbench_service_test.go @@ -20,6 +20,7 @@ func Test_convertDBType(t *testing.T) { "OB Oracle": {input: "OceanBase For Oracle", expected: "OB_ORACLE"}, "OB MySQL": {input: "OceanBase For MySQL", expected: "OB_MYSQL"}, "TiDB": {input: "TiDB", expected: "TIDB"}, + "GoldenDB": {input: "GoldenDB", expected: "MYSQL"}, "Unknown passthrough": {input: "UnknownDB", expected: "UnknownDB"}, } for name, tc := range cases { @@ -32,6 +33,36 @@ func Test_convertDBType(t *testing.T) { } } +func Test_isSQLAuditSupportedDBType(t *testing.T) { + svc := &SqlWorkbenchService{} + cases := map[string]struct { + input string + expected bool + }{ + "MySQL supported": {input: string(pkgConst.DBTypeMySQL), expected: true}, + "Oracle supported": {input: string(pkgConst.DBTypeOracle), expected: true}, + "OB MySQL supported": {input: string(pkgConst.DBTypeOceanBaseMySQL), expected: true}, + "TiDB supported": {input: string(pkgConst.DBTypeTiDB), expected: true}, + "PostgreSQL supported": {input: string(pkgConst.DBTypePostgreSQL), expected: true}, + "SQL Server supported": {input: string(pkgConst.DBTypeSQLServer), expected: true}, + "DM supported": {input: string(pkgConst.DBTypeDM), expected: true}, + "GoldenDB not supported": {input: string(pkgConst.DBTypeGoldenDB), expected: false}, + "TBase not supported": {input: string(pkgConst.DBTypeTBase), expected: false}, + "DB2 not supported": {input: string(pkgConst.DBTypeDB2), expected: false}, + "Hive not supported": {input: string(pkgConst.DBTypeHive), expected: false}, + "GaussDB not supported": {input: string(pkgConst.DBTypeGaussDB), expected: false}, + "Unknown not supported": {input: "UnknownDB", expected: false}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := svc.isSQLAuditSupportedDBType(tc.input) + if got != tc.expected { + t.Errorf("isSQLAuditSupportedDBType(%q) = %v, want %v", tc.input, got, tc.expected) + } + }) + } +} + func Test_SupportDBType(t *testing.T) { svc := &SqlWorkbenchService{} cases := map[string]struct { @@ -43,6 +74,7 @@ func Test_SupportDBType(t *testing.T) { "Oracle supported": {input: pkgConst.DBTypeOracle, expected: true}, "OB MySQL supported": {input: pkgConst.DBTypeOceanBaseMySQL, expected: true}, "TiDB supported": {input: pkgConst.DBTypeTiDB, expected: true}, + "GoldenDB supported": {input: pkgConst.DBTypeGoldenDB, expected: true}, "PostgreSQL unsupported": {input: pkgConst.DBTypePostgreSQL, expected: false}, "SQL Server unsupported": {input: pkgConst.DBTypeSQLServer, expected: false}, }