@@ -1160,24 +1160,6 @@ func makeHttpRequest(ctx context.Context, url string, headers map[string]string,
11601160}
11611161
11621162// AuditMiddleware 拦截工作台odc请求进行加工
1163- //
1164- // 设计原则(issue #850, bug 修复):
1165- //
1166- // 本中间件提供的是「在 streamExecute 反代到 ODC 前叠加 SQLE 审核」的增强能力,
1167- // 不是业务流的必经环节。当:
1168- // - SQL/数据源 ID 解析失败;
1169- // - 缓存 / 用户上下文缺失;
1170- // - 该 DBService 未开启 SQL 审核;
1171- // - SQLE 服务自身调用失败;
1172- //
1173- // 均应**透传放行**(fail-open,return next(c))让 ODC 继续执行用户的 SQL,
1174- // 而不是把请求 400 掉、让用户连查询都跑不通。只有审核**明确返回需要拦截**
1175- // 的结果(如规则违反需审批)才走 buildAuditResponseWithoutExecution 路径。
1176- //
1177- // 修复前:未启用审核 / 缓存缺失 / 用户解析失败 等辅助路径异常均直接
1178- // `return errors.New(...)`,被 dms 的 HTTPErrorHandler 统一映射成 400,
1179- // 导致 case-pg-mysql-baseline-001 等用例在 SQL Console 上完全无法运行(见
1180- // docs/dev/fix-task-004-odc-streamExecute-400.md)。
11811163func (sqlWorkbenchService * SqlWorkbenchService ) AuditMiddleware () echo.MiddlewareFunc {
11821164 return func (next echo.HandlerFunc ) echo.HandlerFunc {
11831165 return func (c echo.Context ) error {
@@ -1189,8 +1171,7 @@ func (sqlWorkbenchService *SqlWorkbenchService) AuditMiddleware() echo.Middlewar
11891171 // 读取请求体
11901172 bodyBytes , err := io .ReadAll (c .Request ().Body )
11911173 if err != nil {
1192- // body 读不出来无法叠加审核,但也无法继续构造反代请求;保留 fail-closed。
1193- sqlWorkbenchService .log .Errorf ("failed to read streamExecute request body: %v" , err )
1174+ sqlWorkbenchService .log .Errorf ("failed to read request body: %v" , err )
11941175 return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditReadReqBodyErr ))
11951176 }
11961177 // 恢复请求体,供后续处理使用
@@ -1199,6 +1180,7 @@ func (sqlWorkbenchService *SqlWorkbenchService) AuditMiddleware() echo.Middlewar
11991180 // 解析请求体获取 SQL 和 datasource ID
12001181 // 注意:解析仅服务于审核辅助路径,解析失败不应直接阻塞用户的 SQL 执行;
12011182 // 否则一旦中间件辅助能力出错(如 sid 解码失败),用户连查询都跑不了。
1183+ // 真正的「未启用审核 / 审核失败」等强策略仍由后续分支按既有 fail-closed 处理。
12021184 sql , sidInfo , err := sqlWorkbenchService .parseStreamExecuteRequest (bodyBytes )
12031185 if err != nil {
12041186 sqlWorkbenchService .log .Warnf ("failed to parse streamExecute request, skipping audit: %v" , err )
@@ -1214,37 +1196,32 @@ func (sqlWorkbenchService *SqlWorkbenchService) AuditMiddleware() echo.Middlewar
12141196 // 获取当前用户 ID
12151197 dmsUserId , err := sqlWorkbenchService .getDMSUserIdFromRequest (c )
12161198 if err != nil {
1217- // 审计需要用户上下文,缺失时跳过审计而非阻塞执行(鉴权由前置 Login() 已经把关)。
1218- sqlWorkbenchService .log .Warnf ("failed to get DMS user ID, skipping audit: %v" , err )
1219- return next (c )
1199+ sqlWorkbenchService .log .Errorf ("failed to get DMS user ID: %v" , err )
1200+ return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditGetDMSUserErr ))
12201201 }
12211202
12221203 // 从缓存表获取 dms_db_service_id
12231204 dmsDBServiceID , err := sqlWorkbenchService .getDMSDBServiceIDFromCache (c .Request ().Context (), datasourceID , dmsUserId )
12241205 if err != nil {
1225- // 缓存查询失败属于辅助路径异常,不应阻塞 SQL 执行。
1226- sqlWorkbenchService .log .Warnf ("failed to get dms_db_service_id from cache, skipping audit: %v" , err )
1227- return next (c )
1206+ sqlWorkbenchService .log .Errorf ("failed to get dms_db_service_id from cache: %v" , err )
1207+ return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditGetDBServiceMappingErr ))
12281208 }
12291209
12301210 if dmsDBServiceID == "" {
1231- // 用户首次在工作台使用该数据源 / 数据源未走"通过 DMS 加载"路径时缓存为空,应放行。
1232- sqlWorkbenchService .log .Warnf ("dms_db_service_id not found in cache for datasource=%s, skipping audit" , datasourceID )
1233- return next (c )
1211+ sqlWorkbenchService .log .Debugf ("dms_db_service_id not found in cache for datasource: %s" , datasourceID )
1212+ return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditDBServiceMappingNotFoundErr ))
12341213 }
12351214
12361215 // 获取 DBService 信息
12371216 dbService , err := sqlWorkbenchService .dbServiceUsecase .GetDBService (c .Request ().Context (), dmsDBServiceID )
12381217 if err != nil {
1239- // DBService 元数据查询失败属于辅助路径异常,不应阻塞 SQL 执行。
1240- sqlWorkbenchService .log .Warnf ("failed to get DBService %s, skipping audit: %v" , dmsDBServiceID , err )
1241- return next (c )
1218+ sqlWorkbenchService .log .Errorf ("failed to get DBService: %v" , err )
1219+ return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditGetDBServiceErr ))
12421220 }
12431221
12441222 // 未开启 SQL 审核时直接放行,由 ODC 执行 SQL
12451223 if ! sqlWorkbenchService .isEnableSQLAudit (dbService ) {
1246- // 未启用审核 = 该数据源没要求审核加强,按裸 ODC 反代行为放行。
1247- sqlWorkbenchService .log .Debugf ("SQL audit is not enabled for DBService %s, skipping audit" , dmsDBServiceID )
1224+ sqlWorkbenchService .log .Debugf ("SQL audit is not enabled for DBService: %s" , dmsDBServiceID )
12481225 return next (c )
12491226 }
12501227
@@ -1261,9 +1238,8 @@ func (sqlWorkbenchService *SqlWorkbenchService) AuditMiddleware() echo.Middlewar
12611238 // 调用 SQLE 审核接口
12621239 auditResult , err := sqlWorkbenchService .callSQLEAudit (c .Request ().Context (), sql , dbService , schemaName )
12631240 if err != nil {
1264- // SQLE 服务自身故障(连不上、超时等)不应让用户的 SQL 执行链路一起挂;放行并打 Warn 便于排障。
1265- sqlWorkbenchService .log .Warnf ("call SQLE audit failed, skipping audit: %v" , err )
1266- return next (c )
1241+ sqlWorkbenchService .log .Errorf ("call SQLE audit failed: %v" , err )
1242+ return errors .New (locale .Bundle .LocalizeMsgByCtx (c .Request ().Context (), locale .SqlWorkbenchAuditCallSQLEErr ))
12671243 }
12681244
12691245 // 拦截响应并添加审核结果
0 commit comments