fix(audit_log): prevent deadlock in Stop() when Start() is never called
Updated the AuditLogRetentionRunner to include a 'started' flag, allowing Stop() to return immediately if Start() was never invoked. This change prevents potential deadlocks during graceful shutdown. Additionally, added tests to ensure that Stop() behaves correctly when called before Start() and when the service is nil. Refs: #1303
This commit is contained in:
+34
-24
@@ -83,36 +83,46 @@ func Auth(
|
||||
crossTenantSwitch := targetTenantID != user.TenantID
|
||||
tenantHeader := c.GetHeader("X-Tenant-ID")
|
||||
if tenantHeader != "" {
|
||||
// 解析目标租户ID
|
||||
// 解析目标租户ID。畸形 / 零值必须显式拒绝:静默忽略会让坏掉的
|
||||
// 前端/SDK 悄悄写错租户,反而看不到问题。与 RequirePathTenantMatch
|
||||
// 中对 :id 的校验保持一致(非空、可解析、>0)。
|
||||
parsedTenantID, err := strconv.ParseUint(tenantHeader, 10, 64)
|
||||
if err == nil {
|
||||
// 检查用户是否有权限访问目标租户:自家租户、跨租户超管、或
|
||||
// 有 active membership 行——三选一,由 IsTenantAccessible
|
||||
// 统一判定。
|
||||
if IsTenantAccessible(c.Request.Context(), user, parsedTenantID, memberService, cfg) {
|
||||
// 验证目标租户是否存在
|
||||
targetTenant, err := tenantService.GetTenantByID(c.Request.Context(), parsedTenantID)
|
||||
if err == nil && targetTenant != nil {
|
||||
targetTenantID = parsedTenantID
|
||||
crossTenantSwitch = parsedTenantID != user.TenantID
|
||||
log.Printf("User %s switching to tenant %d", user.ID, targetTenantID)
|
||||
} else {
|
||||
log.Printf("Error getting target tenant by ID: %v, tenantID: %d", err, parsedTenantID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid target tenant ID",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if err != nil || parsedTenantID == 0 {
|
||||
logger.Warnf(c.Request.Context(),
|
||||
"Invalid X-Tenant-ID header from user=%s: %q (err=%v)",
|
||||
user.ID, tenantHeader, err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid X-Tenant-ID header",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// 检查用户是否有权限访问目标租户:自家租户、跨租户超管、或
|
||||
// 有 active membership 行——三选一,由 IsTenantAccessible
|
||||
// 统一判定。
|
||||
if IsTenantAccessible(c.Request.Context(), user, parsedTenantID, memberService, cfg) {
|
||||
// 验证目标租户是否存在
|
||||
targetTenant, err := tenantService.GetTenantByID(c.Request.Context(), parsedTenantID)
|
||||
if err == nil && targetTenant != nil {
|
||||
targetTenantID = parsedTenantID
|
||||
crossTenantSwitch = parsedTenantID != user.TenantID
|
||||
log.Printf("User %s switching to tenant %d", user.ID, targetTenantID)
|
||||
} else {
|
||||
// 用户没有权限访问目标租户
|
||||
log.Printf("User %s attempted to access tenant %d without permission", user.ID, parsedTenantID)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: insufficient permissions to access target tenant",
|
||||
log.Printf("Error getting target tenant by ID: %v, tenantID: %d", err, parsedTenantID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid target tenant ID",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 用户没有权限访问目标租户
|
||||
log.Printf("User %s attempted to access tenant %d without permission", user.ID, parsedTenantID)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: insufficient permissions to access target tenant",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user