Skip to content

Abnormal token status #3043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 16, 2025
Merged

Abnormal token status #3043

merged 9 commits into from
May 16, 2025

Conversation

LordofAvernus
Copy link
Collaborator

@LordofAvernus LordofAvernus commented May 15, 2025

User description

关联的 issue

#3045

描述你的变更

sql管控列表页面增加token超时任务的提示

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 重置扫描任务token接口

  • 调整token生成与刷新逻辑

  • 更新swagger文档描述

  • 添加异常状态及token过期字段


Changes walkthrough 📝

Relevant files
Enhancement
4 files
app.go
更新实例扫描任务token刷新接口                                                                               
+1/-1     
instance_audit_plan.go
重构token处理及辅助函数逻辑                                                                                 
+79/-16 
sql_manage.go
增加异常状态与token过期字段输出                                                                             
+2/-0     
instance_audit_plan.go
新增获取活动任务辅助查询方法                                                                                     
+7/-2     
Documentation
3 files
docs.go
更新swagger文档中token接口描述                                                                       
+26/-12 
swagger.json
调整swagger.json中token接口信息                                                                 
+26/-12 
swagger.yaml
修改swagger.yaml中token接口说明                                                                 
+19/-9   

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @actiontech-bot actiontech-bot requested a review from winfredLIN May 15, 2025 09:21
    @CLAassistant
    Copy link

    CLAassistant commented May 15, 2025

    CLA assistant check
    All committers have signed the CLA.

    Copy link

    github-actions bot commented May 15, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 41bf5a0)

    🎫 Ticket compliance analysis 🔶

    3045 - Partially compliant

    Compliant requirements:

    • 刷新 token 接口已添加,并在相关路由配置中更新
    • Token 刷新逻辑通过新函数实现,包含生成、更新和条件判断
    • swagger 文档同步更新,描述已更改

    Non-compliant requirements:

    • 无

    Requires further human verification:

    • UI 异常状态提示是否准确展示及交互

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    逻辑验证

    请检查 token 刷新逻辑中的条件判断,确保在存在 scanner 类型任务时生成或删除 token 的逻辑在所有场景下均能满足预期行为。

    func UpdateInstanceAuditPlanToken(ap *model.InstanceAuditPlan, tokenExpire time.Duration) error {
    	// 存在scanner依赖的任务类型时候,重新生成token
    	needGenerate := HasScannerTypeSubPlans(ap)
    	// 当前token是否为为空
    	currentTokenEmpty := ap.Token == ""
    
    	var token string
    	var err error
    	if needGenerate {
    		token, err = newAuditPlanToken(ap, tokenExpire)
    		if err != nil {
    			return errors.New(errors.DataConflict, err)
    		}
    	}
    
    	// 1. 添加token: 存在scanner类型任务并且原本token为空
    	// 2. 删除token: 不存在scanner类型任务并且原本token不为空
    	if needGenerate == currentTokenEmpty {
    		return model.GetStorage().UpdateInstanceAuditPlanByID(ap.ID, map[string]interface{}{"token": token})
    	}
    	return nil
    返回处理问题

    在更新和删除任务后均调用了返回 JSONBaseErrorReq(c, nil),请确认在无错误时使用合适的成功响应返回,避免误报错误信息。

    err = HandleAuditPlanToken(instanceAuditPlanID)
    if err != nil {
    	return controller.JSONBaseErrorReq(c, err)
    }
    return controller.JSONBaseErrorReq(c, nil)

    Copy link

    github-actions bot commented May 15, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 41bf5a0

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    添加tokenExpire参数

    修改HandleAuditPlanToken函数,增加tokenExpire
    time.Duration
    参数,并相应更新函数体内对该参数的使用。确保在调用此函数时传入合法的过期时间以避免编译错误或运行时panic。

    sqle/api/controller/v1/instance_audit_plan.go [217-229]

    -func HandleAuditPlanToken(instanceAuditPlanID string) error {
    +func HandleAuditPlanToken(instanceAuditPlanID string, tokenExpire time.Duration) error {
     	s := model.GetStorage()
     
     	ap, exist, err := s.GetInstanceAuditPlanDetail(instanceAuditPlanID)
     	if err != nil {
     		return err
     	}
     	if !exist {
     		return errors.NewInstanceAuditPlanNotExistErr()
     	}
     
     	return UpdateInstanceAuditPlanToken(ap, tokenExpire)
     }
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies that the existing HandleAuditPlanToken function uses tokenExpire without it being defined, and it proposes adding a tokenExpire time.Duration parameter to fix the issue. However, this change will require updating all call sites accordingly, so while it addresses a critical compile-time issue, it adds a broader impact that must be managed.

    Medium

    Previous suggestions

    Suggestions up to commit efd7c50
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    定义 tokenExpire 参数

    在函数中使用了未定义的tokenExpire变量,可能导致编译错误或panic。建议在函数参数中传入tokenExpire或在函数体内定义一个默认值。

    sqle/api/controller/v1/instance_audit_plan.go [217-229]

    -func RefreshInstanceAuditPlanTokenByStatus(instanceAuditPlanID string) error {
    +func RefreshInstanceAuditPlanTokenByStatus(instanceAuditPlanID string, tokenExpire time.Duration) error {
     	s := model.GetStorage()
     
     	ap, exist, err := s.GetInstanceAuditPlanDetail(instanceAuditPlanID)
     	if err != nil {
     		return err
     	}
     	if !exist {
     		return errors.NewInstanceAuditPlanNotExistErr()
     	}
     	return UpdateInstanceAuditPlanToken(ap, tokenExpire)
     }
    Suggestion importance[1-10]: 10

    __

    Why: The suggestion correctly identifies that tokenExpire is undefined in the function and proposes to add it as a parameter, which prevents potential compile errors and improves code correctness.

    High
    Suggestions up to commit 64fa7c7
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    修正未定义变量问题

    当前函数使用了未定义的变量 tokenExpire,这会导致未预料的运行时错误。建议将 tokenExpire 作为参数传入
    GenerateInstanceAuditPlanToken,同时更新所有调用此函数的位置以传入有效的过期时长值,从而确保代码逻辑正确且防止运行时崩溃。

    sqle/api/controller/v1/instance_audit_plan.go [217-227]

    -func GenerateInstanceAuditPlanToken(instanceAuditPlanID string) error {
    +func GenerateInstanceAuditPlanToken(instanceAuditPlanID string, tokenExpire time.Duration) error {
     	s := model.GetStorage()
     
     	ap, exist, err := s.GetInstanceAuditPlanDetail(instanceAuditPlanID)
     	if err != nil {
     		return err
     	}
     	if !exist {
     		return errors.NewInstanceAuditPlanNotExistErr()
     	}
     	return generateAndUpdateAuditPlanToken(ap, tokenExpire)
     }
    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies a critical bug where the undefined variable tokenExpire is used in GenerateInstanceAuditPlanToken. By adding tokenExpire as a parameter, it ensures that the function receives the necessary expiry duration, thereby preventing potential runtime errors.

    High

    Copy link

    Persistent review updated to latest commit efd7c50

    @LordofAvernus LordofAvernus force-pushed the abnormal_token_status branch from efd7c50 to ed24982 Compare May 16, 2025 03:15
    Copy link

    Persistent review updated to latest commit ed24982

    Copy link

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    Copy link

    Persistent review updated to latest commit 41bf5a0

    @winfredLIN winfredLIN merged commit c6eb84f into main May 16, 2025
    4 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants