Skip to content

Commit

Permalink
Merge pull request #2491 from actiontech/refacor_audit_plan_ce_2
Browse files Browse the repository at this point in the history
Refacor audit plan ce
  • Loading branch information
sjjian authored Jul 26, 2024
2 parents 17298e2 + 83553a3 commit 4d361ae
Show file tree
Hide file tree
Showing 62 changed files with 8,597 additions and 2,521 deletions.
17 changes: 16 additions & 1 deletion sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,20 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
v1ProjectRouter.POST("/:project_name/audit_plans/:audit_plan_name/sqls/full", v1.FullSyncAuditPlanSQLs, sqleMiddleware.ScannerVerifier())
v1ProjectRouter.POST("/:project_name/audit_plans/:audit_plan_name/sqls/partial", v1.PartialSyncAuditPlanSQLs, sqleMiddleware.ScannerVerifier())

// instance audti plan 实例智能扫描任务
v1ProjectRouter.POST("/:project_name/instance_audit_plans", v1.CreateInstanceAuditPlan)
v1ProjectRouter.DELETE("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.DeleteInstanceAuditPlan)
v1ProjectRouter.PUT("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.UpdateInstanceAuditPlan)
v1ProjectRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.UpdateInstanceAuditPlanStatus)
v1ProjectRouter.GET("/:project_name/instance_audit_plans", v1.GetInstanceAuditPlans)
v1ProjectRouter.GET("/:project_name/instance_audit_plans/:instance_audit_plan_id", v1.GetInstanceAuditPlanDetail)
v1ProjectRouter.GET("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans", v1.GetInstanceAuditPlanOverview)

// audit plan; 智能扫描任务
v1ProjectRouter.DELETE("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/", v1.DeleteAuditPlanByType)
v1ProjectRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/", v1.UpdateAuditPlanStatus)
v1ProjectRouter.GET("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/sqls", v1.GetInstanceAuditPlanSQLs)

// sql manager
v1ProjectRouter.GET("/:project_name/sql_manages", v1.GetSqlManageList)
v1ProjectRouter.PATCH("/:project_name/sql_manages/batch", v1.BatchUpdateSqlManage)
Expand Down Expand Up @@ -315,13 +329,14 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
v2ProjectRouter.GET("/:project_name/audit_plans", v2.GetAuditPlans)
v2ProjectRouter.GET("/:project_name/audit_plans/:audit_plan_name/reports/:audit_plan_report_id/sqls/:number/analysis", v2.GetAuditPlanAnalysisData)
v2ProjectRouter.GET("/:project_name/audit_plans/:audit_plan_name/reports/:audit_plan_report_id/sqls", v2.GetAuditPlanReportSQLs)

// sql managers
v2ProjectRouter.GET("/:project_name/sql_manages", v2.GetSqlManageList)

// scanner token auth
v2ProjectRouter.POST("/:project_name/audit_plans/:audit_plan_name/sqls/full", v2.FullSyncAuditPlanSQLs, sqleMiddleware.ScannerVerifier())
v2ProjectRouter.POST("/:project_name/audit_plans/:audit_plan_name/sqls/partial", v2.PartialSyncAuditPlanSQLs, sqleMiddleware.ScannerVerifier())

v2ProjectRouter.POST("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/sqls/upload", v2.UploadInstanceAuditPlanSQLs, sqleMiddleware.ScannerVerifier())
}

{
Expand Down
29 changes: 16 additions & 13 deletions sqle/api/controller/v1/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ type AuditPlanMetaV1 struct {
}

type AuditPlanParamResV1 struct {
Key string `json:"key"`
Desc string `json:"desc"`
Value string `json:"value"`
Type string `json:"type" enums:"string,int,bool,password"`
Key string `json:"key"`
Desc string `json:"desc"`
Value string `json:"value"`
Type string `json:"type" enums:"string,int,bool,password"`
EnumsValues []params.EnumsValue `json:"enums_value"`
}

func ConvertAuditPlanMetaToRes(meta auditplan.Meta) AuditPlanMetaV1 {
Expand All @@ -77,10 +78,11 @@ func ConvertAuditPlanMetaToRes(meta auditplan.Meta) AuditPlanMetaV1 {
val = ""
}
paramRes := AuditPlanParamResV1{
Key: p.Key,
Desc: p.Desc,
Type: string(p.Type),
Value: val,
Key: p.Key,
Desc: p.Desc,
Type: string(p.Type),
Value: val,
EnumsValues: p.Enums,
}
paramsRes = append(paramsRes, paramRes)
}
Expand Down Expand Up @@ -936,7 +938,7 @@ func FullSyncAuditPlanSQLs(c echo.Context) error {
return controller.JSONBaseErrorReq(c, err)
}

return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, ap, sqls, false))
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, auditplan.ConvertModelToAuditPlan(ap), sqls, false))
}

type PartialSyncAuditPlanSQLsReqV1 struct {
Expand Down Expand Up @@ -991,7 +993,7 @@ func PartialSyncAuditPlanSQLs(c echo.Context) error {
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, ap, sqls, true))
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, auditplan.ConvertModelToAuditPlan(ap), sqls, true))
}

func convertToModelAuditPlanSQL(c echo.Context, auditPlan *model.AuditPlan, reqSQLs []*AuditPlanSQLReqV1) ([]*auditplan.SQL, error) {
Expand Down Expand Up @@ -1077,6 +1079,7 @@ type TriggerAuditPlanResV1 struct {
Data AuditPlanReportResV1 `json:"data"`
}

// @Deprecated v3.2407
// @Summary 触发扫描任务
// @Description trigger audit plan
// @Id triggerAuditPlanV1
Expand All @@ -1101,7 +1104,7 @@ func TriggerAuditPlan(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errAuditPlanNotExist)
}

report, err := auditplan.Audit(log.NewEntry(), ap)
report, err := auditplan.Audit(log.NewEntry(), auditplan.ConvertModelToAuditPlan(ap))
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
Expand Down Expand Up @@ -1361,7 +1364,7 @@ type AuditPlanSQLResV1 struct {
}

type AuditPlanSQLHeadV1 struct {
Name string `json:"name"`
Name string `json:"field_name"`
Desc string `json:"desc"`
Type string `json:"type,omitempty" enums:"sql"`
}
Expand Down Expand Up @@ -1404,7 +1407,7 @@ func GetAuditPlanSQLs(c echo.Context) error {
"offset": offset,
}

head, rows, count, err := auditplan.GetSQLs(log.NewEntry(), ap, data)
head, rows, count, err := auditplan.GetSQLs(log.NewEntry(), auditplan.ConvertModelToAuditPlan(ap), data)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
Expand Down
41 changes: 41 additions & 0 deletions sqle/api/controller/v1/dms_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/actiontech/dms/pkg/dms-common/dmsobject"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/common"
dms "github.com/actiontech/sqle/sqle/dms"
"github.com/actiontech/sqle/sqle/model"
"github.com/labstack/echo/v4"
)

Expand All @@ -21,6 +23,7 @@ func init() {
BeforeArchiveProject{},
AfterCreateProject{},
BeforeDeleteDbService{},
AfterUpdateDbService{},
})
}

Expand Down Expand Up @@ -54,6 +57,9 @@ type AfterCreateProject struct {
type BeforeDeleteDbService struct {
}

type AfterUpdateDbService struct {
}

func (h BeforeDeleteDbService) Handle(ctx context.Context, currentUserId string, instanceIdStr string) error {
instanceId, err := strconv.ParseInt(instanceIdStr, 10, 64)
if err != nil {
Expand All @@ -62,3 +68,38 @@ func (h BeforeDeleteDbService) Handle(ctx context.Context, currentUserId string,

return common.CheckDeleteInstance(instanceId)
}

func (h AfterUpdateDbService) Handle(ctx context.Context, currentUserId string, instanceIdStr string) error {
instanceId, err := strconv.ParseInt(instanceIdStr, 10, 64)
if err != nil {
return err
}
instance, _, err := dms.GetInstancesById(ctx, instanceIdStr)
if err != nil {
return err
}
s := model.GetStorage()
insAuditPlan, exist, err := s.GetInstanceAuditPlanByInstanceID(instanceId)
if err != nil {
return err
}
if !exist {
return nil
}
if needUpdate := h.isNeedUpdateService(instance, insAuditPlan); needUpdate {
insAuditPlan.Business = instance.Business
err := s.Save(insAuditPlan)
if err != nil {
return err
}
}
return nil
}

func (h AfterUpdateDbService) isNeedUpdateService(instance *model.Instance, insAuditPlan *model.InstanceAuditPlan) bool {
needUpdate := false
if instance.Business != insAuditPlan.Business {
return true
}
return needUpdate
}
Loading

0 comments on commit 4d361ae

Please sign in to comment.