diff --git a/sqle/api/app.go b/sqle/api/app.go index 276577b73..fd52be5d3 100644 --- a/sqle/api/app.go +++ b/sqle/api/app.go @@ -274,9 +274,9 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti 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) // 弃用 + v1ProjectRouter.DELETE("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_id/", v1.DeleteAuditPlanById) + v1ProjectRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_id/", v1.UpdateAuditPlanStatus) + v1ProjectRouter.GET("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_id/sqls", v1.GetInstanceAuditPlanSQLs) // 弃用 v1ProjectRouter.GET("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/sql_meta", v1.GetInstanceAuditPlanSQLMeta) v1ProjectRouter.POST("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/sql_data", v1.GetInstanceAuditPlanSQLData) v1ProjectRouter.POST("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_type/sql_export", v1.GetInstanceAuditPlanSQLExport) diff --git a/sqle/api/controller/v1/dms_handle.go b/sqle/api/controller/v1/dms_handle.go index b5dabd3c0..218537a9d 100644 --- a/sqle/api/controller/v1/dms_handle.go +++ b/sqle/api/controller/v1/dms_handle.go @@ -11,8 +11,6 @@ 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" ) @@ -70,36 +68,5 @@ func (h BeforeDeleteDbService) Handle(ctx context.Context, currentUserId string, } 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 -} diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index 5d119c96f..c0f494501 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -6,6 +6,7 @@ import ( "net/http" baseV1 "github.com/actiontech/dms/pkg/dms-common/api/base/v1" + dmsV1 "github.com/actiontech/dms/pkg/dms-common/api/dms/v1" v1 "github.com/actiontech/dms/pkg/dms-common/api/dms/v1" "github.com/actiontech/sqle/sqle/api/controller" "github.com/actiontech/sqle/sqle/common" @@ -334,6 +335,7 @@ const ( // InstanceTipReqV1.FunctionalModule Enums type InstanceTipReqV1 struct { FilterDBType string `json:"filter_db_type" query:"filter_db_type"` + FilterByBusiness string `json:"filter_by_business" query:"filter_by_business"` FilterWorkflowTemplateId uint32 `json:"filter_workflow_template_id" query:"filter_workflow_template_id"` FunctionalModule string `json:"functional_module" query:"functional_module" enums:"create_audit_plan,create_workflow,sql_manage,create_optimization" valid:"omitempty,oneof=create_audit_plan create_workflow sql_manage create_optimization"` } @@ -360,6 +362,7 @@ type GetInstanceTipsResV1 struct { // @Security ApiKeyAuth // @Param project_name path string true "project name" // @Param filter_db_type query string false "filter db type" +// @Param filter_by_business query string false "filter by business" // @Param filter_workflow_template_id query string false "filter workflow template id" // @Param functional_module query string false "functional module" Enums(create_audit_plan,create_workflow,sql_manage,create_optimization) // @Success 200 {object} v1.GetInstanceTipsResV1 @@ -389,8 +392,12 @@ func GetInstanceTips(c echo.Context) error { operationType = v1.OpPermissionTypeCreateOptimization default: } - - instances, err := GetCanOperationInstances(c.Request().Context(), user, req.FilterDBType, projectUid, operationType) + dbServiceReq := &dmsV1.ListDBServiceReq{ + FilterByBusiness: req.FilterByBusiness, + ProjectUid: projectUid, + FilterByDBType: req.FilterDBType, + } + instances, err := GetCanOperationInstances(c.Request().Context(), user, dbServiceReq, operationType) if err != nil { return controller.JSONBaseErrorReq(c, err) } diff --git a/sqle/api/controller/v1/instance_audit_plan.go b/sqle/api/controller/v1/instance_audit_plan.go index 4e40e344d..8a9c0c579 100644 --- a/sqle/api/controller/v1/instance_audit_plan.go +++ b/sqle/api/controller/v1/instance_audit_plan.go @@ -5,6 +5,7 @@ import ( "net" "net/http" "net/url" + "strconv" "strings" "time" @@ -20,19 +21,14 @@ import ( "github.com/actiontech/sqle/sqle/model" "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/server/auditplan" + dry "github.com/ungerik/go-dry" "github.com/actiontech/sqle/sqle/utils" "github.com/labstack/echo/v4" - dry "github.com/ungerik/go-dry" ) type CreateInstanceAuditPlanReqV1 struct { - // 静态审核 - StaticAudit bool `json:"static_audit" form:"static_audit" example:"true"` - Business string `json:"business" form:"business" example:"test"` - InstanceType string `json:"instance_type" form:"instance_type" example:"mysql" ` - InstanceName string `json:"instance_name" form:"instance_name" example:"test_mysql"` - + InstanceId string `json:"instance_id" form:"instance_id" valid:"required"` // 扫描类型 AuditPlans []AuditPlan `json:"audit_plans" form:"audit_plans" valid:"required"` } @@ -69,54 +65,45 @@ func CreateInstanceAuditPlan(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } - if !dry.StringInSlice(req.InstanceType, driver.GetPluginManager().AllDrivers()) { - return controller.JSONBaseErrorReq(c, errors.New(errors.DriverNotExist, &driverV2.DriverNotSupportedError{DriverTyp: req.InstanceType})) - } - projectUid, err := dms.GetPorjectUIDByName(c.Request().Context(), c.Param("project_name"), true) if err != nil { return controller.JSONBaseErrorReq(c, err) } // check instance - var instanceType string - var instanceID uint64 - if req.InstanceName != "" { - inst, exist, err := dms.GetInstanceInProjectByName(c.Request().Context(), projectUid, req.InstanceName) - if !exist { - return controller.JSONBaseErrorReq(c, ErrInstanceNotExist) - } else if err != nil { - return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err)) - } - instanceID = inst.ID - instanceType = inst.DbType - // check instance audit plan exist - _, exist, err = model.GetStorage().GetInstanceAuditPlanByInstanceID(int64(inst.ID)) - if err != nil { - return controller.JSONBaseErrorReq(c, err) - } - if exist { - return controller.JSONBaseErrorReq(c, fmt.Errorf("current instance has audit plan")) - } - // check operation - user, err := controller.GetCurrentUser(c, dms.GetUser) - if err != nil { - return controller.JSONBaseErrorReq(c, err) - } - canCreateAuditPlan, err := CheckUserCanCreateAuditPlan(c.Request().Context(), projectUid, user, []*model.Instance{inst}) - if err != nil { - return controller.JSONBaseErrorReq(c, err) - } - if !canCreateAuditPlan { - return controller.JSONBaseErrorReq(c, errors.NewUserNotPermissionError(model.GetOperationCodeDesc(uint(model.OP_AUDIT_PLAN_SAVE)))) - } + inst, exist, err := dms.GetInstancesById(c.Request().Context(), req.InstanceId) + if !exist { + return controller.JSONBaseErrorReq(c, ErrInstanceNotExist) + } else if err != nil { + return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err)) + } - } else { - instanceType = req.InstanceType + if !dry.StringInSlice(inst.DbType, driver.GetPluginManager().AllDrivers()) { + return controller.JSONBaseErrorReq(c, errors.New(errors.DriverNotExist, &driverV2.DriverNotSupportedError{DriverTyp: inst.DbType})) } - userId := controller.GetUserID(c) + // check instance audit plan exist + _, exist, err = model.GetStorage().GetInstanceAuditPlanByInstanceID(int64(inst.ID)) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if exist { + return controller.JSONBaseErrorReq(c, fmt.Errorf("current instance has audit plan")) + } + // check operation + user, err := controller.GetCurrentUser(c, dms.GetUser) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + canCreateAuditPlan, err := CheckUserCanCreateAuditPlan(c.Request().Context(), projectUid, user, []*model.Instance{inst}) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if !canCreateAuditPlan { + return controller.JSONBaseErrorReq(c, errors.NewUserNotPermissionError(model.GetOperationCodeDesc(uint(model.OP_AUDIT_PLAN_SAVE)))) + } + userId := controller.GetUserID(c) s := model.GetStorage() auditPlans := make([]*model.AuditPlanV2, 0) @@ -131,7 +118,7 @@ func CreateInstanceAuditPlan(c echo.Context) error { } } // check rule template name - ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlan.RuleTemplateName, req.InstanceName, req.InstanceType, projectUid) + ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlan.RuleTemplateName, inst.Name, inst.DbType, projectUid) if err != nil { return controller.JSONBaseErrorReq(c, err) } @@ -140,7 +127,7 @@ func CreateInstanceAuditPlan(c echo.Context) error { if auditPlan.Type == "" { auditPlan.Type = auditplan.TypeDefault } - ps, err := checkAndGenerateAuditPlanParams(auditPlan.Type, instanceType, auditPlan.Params) + ps, err := checkAndGenerateAuditPlanParams(auditPlan.Type, inst.DbType, auditPlan.Params) if err != nil { return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err)) } @@ -155,10 +142,8 @@ func CreateInstanceAuditPlan(c echo.Context) error { ap := &model.InstanceAuditPlan{ ProjectId: model.ProjectUID(projectUid), - Business: req.Business, - InstanceName: req.InstanceName, - InstanceID: instanceID, - DBType: instanceType, + InstanceID: inst.ID, + DBType: inst.DbType, CreateUserID: userId, AuditPlans: auditPlans, ActiveStatus: model.ActiveStatusNormal, @@ -273,6 +258,7 @@ func UpdateInstanceAuditPlan(c echo.Context) error { if err != nil { return controller.JSONBaseErrorReq(c, err) } + inst := dms.GetInstancesByIdWithoutError(fmt.Sprintf("%d", dbAuditPlans.InstanceID)) for _, auditPlan := range req.AuditPlans { if auditPlan.RuleTemplateName != "" { exist, err := s.IsRuleTemplateExist(auditPlan.RuleTemplateName, []string{projectUid, model.ProjectIdForGlobalRuleTemplate}) @@ -284,7 +270,7 @@ func UpdateInstanceAuditPlan(c echo.Context) error { } } // check rule template name - ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlan.RuleTemplateName, dbAuditPlans.InstanceName, dbAuditPlans.DBType, projectUid) + ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlan.RuleTemplateName, inst.Name, dbAuditPlans.DBType, projectUid) if err != nil { return controller.JSONBaseErrorReq(c, err) } @@ -375,6 +361,7 @@ func UpdateInstanceAuditPlanStatus(c echo.Context) error { } type AuditPlanTypeResBase struct { + AuditPlanId uint `json:"audit_plan_id"` AuditPlanType string `json:"type"` AuditPlanTypeDesc string `json:"desc"` } @@ -382,7 +369,7 @@ type AuditPlanTypeResBase struct { type GetInstanceAuditPlansReqV1 struct { FilterByBusiness string `json:"filter_by_business" query:"filter_by_business"` FilterByDBType string `json:"filter_by_db_type" query:"filter_by_db_type"` - FilterByInstanceName string `json:"filter_by_instance_name" query:"filter_by_instance_name"` + FilterByInstanceID string `json:"filter_by_instance_id" query:"filter_by_instance_id"` FilterByAuditPlanType string `json:"filter_by_audit_plan_type" query:"filter_by_audit_plan_type"` FilterByActiveStatus string `json:"filter_by_active_status" query:"filter_by_active_status"` FuzzySearch string `json:"fuzzy_search" query:"fuzzy_search"` @@ -418,7 +405,7 @@ type InstanceAuditPlanResV1 struct { // @Param project_name path string true "project name" // @Param filter_by_business query string false "filter by business" // @Param filter_by_db_type query string false "filter by db type" -// @Param filter_by_instance_name query string false "filter by instance name" +// @Param filter_by_instance_id query string false "filter by instance id" // @Param filter_by_audit_plan_type query string false "filter instance audit plan type" // @Param filter_by_active_status query bool false "filter instance audit plan active status" // @Param fuzzy_search query string false "fuzzy search" @@ -450,7 +437,7 @@ func GetInstanceAuditPlans(c echo.Context) error { data := map[string]interface{}{ "filter_instance_audit_plan_db_type": req.FilterByDBType, "filter_audit_plan_type": req.FilterByAuditPlanType, - "filter_audit_plan_instance_name": req.FilterByInstanceName, + "filter_audit_plan_instance_id": req.FilterByInstanceID, "filter_by_business": req.FilterByBusiness, "filter_project_id": projectUid, "current_user_id": userId, @@ -460,12 +447,9 @@ func GetInstanceAuditPlans(c echo.Context) error { "offset": offset, } if !up.IsAdmin() { - instanceNames, err := dms.GetInstanceNamesInProjectByIds(c.Request().Context(), projectUid, up.GetInstancesByOP(v1.OpPermissionTypeViewOtherAuditPlan)) - if err != nil { - return controller.JSONBaseErrorReq(c, err) - } - if len(instanceNames) > 0 { - data["accessible_instances_name"] = fmt.Sprintf("\"%s\"", strings.Join(instanceNames, "\",\"")) + accessinstanceId := up.GetInstancesByOP(v1.OpPermissionTypeViewOtherAuditPlan) + if len(accessinstanceId) > 0 { + data["accessible_instances_id"] = fmt.Sprintf("\"%s\"", strings.Join(accessinstanceId, "\",\"")) } } @@ -480,15 +464,16 @@ func GetInstanceAuditPlans(c echo.Context) error { typeBases := make([]AuditPlanTypeResBase, 0, len(apTypes)) for _, apType := range apTypes { if apType != "" { - typeBase := ConvertAuditPlanTypeToRes(apType) + typeBase := ConvertAuditPlanTypeToRes(v.Id, apType) typeBases = append(typeBases, typeBase) + } } - + inst := dms.GetInstancesByIdWithoutError(v.InstanceID) resData[i] = InstanceAuditPlanResV1{ InstanceAuditPlanId: v.Id, - InstanceName: v.InstanceName, - Business: v.Business, + InstanceName: inst.Name, + Business: inst.Business, InstanceType: v.DBType, AuditPlanTypes: typeBases, ActiveStatus: v.ActiveStatus, @@ -503,12 +488,13 @@ func GetInstanceAuditPlans(c echo.Context) error { }) } -func ConvertAuditPlanTypeToRes(auditPlanType string) AuditPlanTypeResBase { +func ConvertAuditPlanTypeToRes(id uint, auditPlanType string) AuditPlanTypeResBase { for _, meta := range auditplan.Metas { if meta.Type == auditPlanType { return AuditPlanTypeResBase{ AuditPlanType: auditPlanType, AuditPlanTypeDesc: meta.Desc, + AuditPlanId: id, } } } @@ -521,8 +507,6 @@ type GetInstanceAuditPlanDetailResV1 struct { } type InstanceAuditPlanDetailResV1 struct { - // 静态审核 - StaticAudit bool `json:"static_audit" example:"true"` Business string `json:"business" example:"test"` InstanceType string `json:"instance_type" example:"mysql" ` InstanceName string `json:"instance_name" example:"test_mysql"` @@ -560,17 +544,16 @@ func GetInstanceAuditPlanDetail(c echo.Context) error { if !exist { return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) } - isStaticAudit := (detail.InstanceName == "") auditPlans, err := ConvertAuditPlansToRes(detail.AuditPlans) if err != nil { return controller.JSONBaseErrorReq(c, err) } + inst := dms.GetInstancesByIdWithoutError(fmt.Sprintf("%d", detail.InstanceID)) resData := InstanceAuditPlanDetailResV1{ - StaticAudit: isStaticAudit, - Business: detail.Business, + Business: inst.Business, InstanceType: detail.DBType, - InstanceName: detail.InstanceName, - InstanceID: fmt.Sprintf("%d", detail.InstanceID), + InstanceName: inst.Name, + InstanceID: inst.GetIDStr(), AuditPlans: auditPlans, } return c.JSON(http.StatusOK, &GetInstanceAuditPlanDetailResV1{ @@ -582,7 +565,7 @@ func GetInstanceAuditPlanDetail(c echo.Context) error { func ConvertAuditPlansToRes(auditPlans []*model.AuditPlanV2) ([]AuditPlanRes, error) { resAuditPlans := make([]AuditPlanRes, 0, len(auditPlans)) for _, v := range auditPlans { - typeBase := ConvertAuditPlanTypeToRes(v.Type) + typeBase := ConvertAuditPlanTypeToRes(v.ID, v.Type) resAuditPlan := AuditPlanRes{ RuleTemplateName: v.RuleTemplateName, Type: typeBase, @@ -663,6 +646,8 @@ func GetInstanceAuditPlanOverview(c echo.Context) error { return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) } s := model.GetStorage() + + inst := dms.GetInstancesByIdWithoutError(fmt.Sprintf("%d", detail.InstanceID)) resAuditPlans := make([]InstanceAuditPlanInfo, 0, len(detail.AuditPlans)) for _, v := range detail.AuditPlans { execCmd := GetAuditPlanExecCmd(projectName, detail, v) @@ -686,12 +671,12 @@ func GetInstanceAuditPlanOverview(c echo.Context) error { ruleTemplate.IsGlobalRuleTemplate = (template.ProjectId == model.ProjectIdForGlobalRuleTemplate) } - typeBase := ConvertAuditPlanTypeToRes(v.Type) + typeBase := ConvertAuditPlanTypeToRes(v.ID, v.Type) resAuditPlan := InstanceAuditPlanInfo{ ID: v.ID, Type: typeBase, DBType: detail.DBType, - InstanceName: detail.InstanceName, + InstanceName: inst.Name, ExecCmd: execCmd, RuleTemplate: ruleTemplate, TotalSQLNums: totalSQLNums, @@ -709,7 +694,7 @@ func GetInstanceAuditPlanOverview(c echo.Context) error { } func GetAuditPlanExecCmd(projectName string, iap *model.InstanceAuditPlan, ap *model.AuditPlanV2) string { - logger := log.NewEntry().WithField("get audit plan exec cmd", fmt.Sprintf("inst name:%s,audit plan type : %s", iap.InstanceName, ap.Type)) + logger := log.NewEntry().WithField("get audit plan exec cmd", fmt.Sprintf("inst id:%d,audit plan type : %s", iap.InstanceID, ap.Type)) _, ok := auditplan.GetSupportedScannerAuditPlanType()[ap.Type] if !ok { return "" @@ -742,11 +727,11 @@ type UpdateInstanceAuditPlanStatusReqV1 struct { // @Tags instance_audit_plan // @Security ApiKeyAuth // @Param project_name path string true "project name" -// @Param instance_audit_plan_id path string true "instance audit plan type" -// @Param audit_plan_type path string true "audit plan type" +// @Param instance_audit_plan_id path string true "instance audit plan id" +// @Param audit_plan_id path string true "audit plan id" // @Success 200 {object} controller.BaseRes -// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/ [delete] -func DeleteAuditPlanByType(c echo.Context) error { +// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/ [delete] +func DeleteAuditPlanById(c echo.Context) error { instanceAuditPlanID := c.Param("instance_audit_plan_id") projectUID, err := dms.GetPorjectUIDByName(c.Request().Context(), c.Param("project_name"), true) if err != nil { @@ -761,7 +746,12 @@ func DeleteAuditPlanByType(c echo.Context) error { return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) } s := model.GetStorage() - err = s.DeleteAuditPlan(instanceAuditPlanID, c.Param("audit_plan_type")) + + audit_plan_id, err := strconv.Atoi(c.Param("audit_plan_id")) + if err != nil { + return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) + } + err = s.DeleteAuditPlan(audit_plan_id) if err != nil { return controller.JSONBaseErrorReq(c, err) } @@ -780,10 +770,10 @@ type UpdateAuditPlanStatusReqV1 struct { // @Security ApiKeyAuth // @Param project_name path string true "project name" // @Param instance_audit_plan_id path string true "instance audit plan id" -// @Param audit_plan_type path string true "audit plan type" +// @Param audit_plan_id path string true "audit plan id" // @param audit_plan body v1.UpdateAuditPlanStatusReqV1 true "update audit plan status" // @Success 200 {object} controller.BaseRes -// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/ [patch] +// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/ [patch] func UpdateAuditPlanStatus(c echo.Context) error { req := new(UpdateAuditPlanStatusReqV1) if err := controller.BindAndValidateReq(c, req); err != nil { @@ -805,8 +795,12 @@ func UpdateAuditPlanStatus(c echo.Context) error { if deatil.ActiveStatus != model.ActiveStatusNormal && req.Active == model.ActiveStatusNormal { return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, fmt.Errorf("instance audit plan active status not normal"))) } + audit_plan_id, err := strconv.Atoi(c.Param("audit_plan_id")) + if err != nil { + return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) + } s := model.GetStorage() - auditPlan, exist, err := s.GetAuditPlanByInstanceIdAndType(instanceAuditPlanID, c.Param("audit_plan_type")) + auditPlan, exist, err := s.GetAuditPlanByID(audit_plan_id) if err != nil { return controller.JSONBaseErrorReq(c, err) } @@ -828,11 +822,11 @@ func UpdateAuditPlanStatus(c echo.Context) error { // @Security ApiKeyAuth // @Param project_name path string true "project name" // @Param instance_audit_plan_id path string true "instance audit plan id" -// @Param audit_plan_type path string true "audit plan type" +// @Param audit_plan_id path string true "audit plan id" // @Param page_index query uint32 true "page index" // @Param page_size query uint32 true "size of per page" // @Success 200 {object} v1.GetAuditPlanSQLsResV1 -// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sqls [get] +// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/sqls [get] func GetInstanceAuditPlanSQLs(c echo.Context) error { req := new(GetAuditPlanSQLsReqV1) if err := controller.BindAndValidateReq(c, req); err != nil { @@ -851,16 +845,16 @@ func GetInstanceAuditPlanSQLs(c echo.Context) error { if !exist { return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) } - apType := c.Param("audit_plan_type") + apID, err := strconv.Atoi(c.Param("audit_plan_id")) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } s := model.GetStorage() - ap, exist, err := s.GetAuditPlanDetailByType(instanceAuditPlanID, apType) + ap, err := s.GetAuditPlanDetailByID(uint(apID)) if err != nil { return controller.JSONBaseErrorReq(c, err) } - if !exist { - return controller.JSONBaseErrorReq(c, errors.NewInstanceAuditPlanNotExistErr()) - } limit, offset := controller.GetLimitAndOffset(req.PageIndex, req.PageSize) diff --git a/sqle/api/controller/v1/project_permission.go b/sqle/api/controller/v1/project_permission.go index 060682903..b6b48ff25 100644 --- a/sqle/api/controller/v1/project_permission.go +++ b/sqle/api/controller/v1/project_permission.go @@ -157,7 +157,10 @@ func GetAuditPlanIfCurrentUserCanAccess(c echo.Context, projectId, auditPlanName } if opType != "" { - instances, err := GetCanOperationInstances(c.Request().Context(), user, "", projectId, opType) + dbServiceReq := &dmsV1.ListDBServiceReq{ + ProjectUid: projectId, + } + instances, err := GetCanOperationInstances(c.Request().Context(), user, dbServiceReq, opType) if err != nil { return nil, false, errors.NewUserNotPermissionError(string(opType)) } @@ -196,12 +199,15 @@ func GetInstanceAuditPlanIfCurrentUserCanAccess(c echo.Context, projectId, insta } if opType != "" { - instances, err := GetCanOperationInstances(c.Request().Context(), user, "", projectId, opType) + dbServiceReq := &dmsV1.ListDBServiceReq{ + ProjectUid: projectId, + } + instances, err := GetCanOperationInstances(c.Request().Context(), user, dbServiceReq, opType) if err != nil { return nil, false, errors.NewUserNotPermissionError(string(opType)) } for _, instance := range instances { - if ap.InstanceName == instance.Name { + if ap.InstanceID == instance.ID { return ap, true, nil } } @@ -318,14 +324,14 @@ func CheckUserCanCreateOptimization(ctx context.Context, projectUID string, user } // 根据用户权限获取能访问/操作的实例列表 -func GetCanOperationInstances(ctx context.Context, user *model.User, dbType, projectUid string, operationType v1.OpPermissionType) ([]*model.Instance, error) { +func GetCanOperationInstances(ctx context.Context, user *model.User, req *dmsV1.ListDBServiceReq, operationType v1.OpPermissionType) ([]*model.Instance, error) { // 获取当前项目下指定数据库类型的全部实例 - instances, err := dms.GetInstancesInProjectByType(ctx, projectUid, dbType) + instances, err := dms.GetInstancesInProjectByTypeAndBusiness(ctx, req.ProjectUid, req.FilterByDBType, req.FilterByBusiness) if err != nil { return nil, err } - userOpPermissions, isAdmin, err := dmsobject.GetUserOpPermission(ctx, projectUid, user.GetIDStr(), controller.GetDMSServerAddress()) + userOpPermissions, isAdmin, err := dmsobject.GetUserOpPermission(ctx, req.ProjectUid, user.GetIDStr(), controller.GetDMSServerAddress()) if err != nil { return nil, err } diff --git a/sqle/dms/instance.go b/sqle/dms/instance.go index 8dd8da95f..48d2d07e0 100644 --- a/sqle/dms/instance.go +++ b/sqle/dms/instance.go @@ -148,10 +148,11 @@ func GetInstancesInProject(ctx context.Context, projectUid string) ([]*model.Ins }) } -func GetInstancesInProjectByType(ctx context.Context, projectUid, dbType string) ([]*model.Instance, error) { +func GetInstancesInProjectByTypeAndBusiness(ctx context.Context, projectUid, dbType, business string) ([]*model.Instance, error) { return getInstances(ctx, dmsV1.ListDBServiceReq{ - ProjectUid: projectUid, - FilterByDBType: dbType, + ProjectUid: projectUid, + FilterByDBType: dbType, + FilterByBusiness: business, }) } @@ -263,6 +264,21 @@ func GetInstanceNamesInProject(ctx context.Context, projectUid string) ([]string return ret, nil } +func GetInstancesByIdWithoutError(instanceId string) (instance model.Instance) { + if instanceId == "" { + return + } + + inst, exist, err := getInstance(context.TODO(), dmsV1.ListDBServiceReq{ + PageSize: 1, + FilterByUID: instanceId, + }) + if err != nil || !exist { + return instance + } + return *inst +} + func GetInstancesById(ctx context.Context, instanceId string) (*model.Instance, bool, error) { if instanceId == "" { return nil, false, nil @@ -438,7 +454,7 @@ func ListActiveAuditPlansWithInstanceV2(fn func() ([]*model.AuditPlanDetail, err } for i, item := range auditPlans { - instance, exists, err := GetInstanceInProjectByName(context.Background(), string(item.GetBaseInfo().ProjectId), item.InstanceName) + instance, exists, err := GetInstancesById(context.Background(), item.InstanceID) if err != nil { continue } @@ -455,7 +471,7 @@ func GetAuditPlansWithInstanceV2(id uint, fn func(id uint) (*model.AuditPlanDeta return nil, err } - instance, exists, err := GetInstanceInProjectByName(context.Background(), auditPlan.ProjectId, auditPlan.InstanceName) + instance, exists, err := GetInstancesById(context.Background(), auditPlan.InstanceID) if err != nil { return nil, err } diff --git a/sqle/docs/docs.go b/sqle/docs/docs.go index d447036d7..b242acc22 100644 --- a/sqle/docs/docs.go +++ b/sqle/docs/docs.go @@ -2429,7 +2429,7 @@ var doc = `{ } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/": { "delete": { "security": [ { @@ -2452,15 +2452,15 @@ var doc = `{ }, { "type": "string", - "description": "instance audit plan type", + "description": "instance audit plan id", "name": "instance_audit_plan_id", "in": "path", "required": true }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true } @@ -2503,8 +2503,8 @@ var doc = `{ }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true }, @@ -2528,8 +2528,8 @@ var doc = `{ } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_data": { - "post": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/sqls": { + "get": { "security": [ { "ApiKeyAuth": [] @@ -2539,8 +2539,8 @@ var doc = `{ "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQL列表", - "operationId": "getInstanceAuditPlanSQLDataV1", + "summary": "获取指定扫描任务的SQLs信息", + "operationId": "getInstanceAuditPlanSQLsV1", "parameters": [ { "type": "string", @@ -2558,44 +2558,49 @@ var doc = `{ }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true }, { - "description": "audit plan sql data request", - "name": "audit_plan_sql_request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLDataReqV1" - } + "type": "integer", + "description": "page index", + "name": "page_index", + "in": "query", + "required": true + }, + { + "type": "integer", + "description": "size of per page", + "name": "page_size", + "in": "query", + "required": true } ], "responses": { "200": { "description": "OK", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLDataResV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLsResV1" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_export": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_data": { "post": { "security": [ { "ApiKeyAuth": [] } ], - "description": "export audit plan SQL report as CSV", + "description": "get audit plan SQLs", "tags": [ "instance_audit_plan" ], - "summary": "导出指定扫描任务的 SQL CSV 列表", - "operationId": "getInstanceAuditPlanSQLExportV1", + "summary": "获取指定扫描任务的SQL列表", + "operationId": "getInstanceAuditPlanSQLDataV1", "parameters": [ { "type": "string", @@ -2619,38 +2624,38 @@ var doc = `{ "required": true }, { - "description": "audit plan sql export request", + "description": "audit plan sql data request", "name": "audit_plan_sql_request", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLExportReqV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLDataReqV1" } } ], "responses": { "200": { - "description": "export audit plan sql report", + "description": "OK", "schema": { - "type": "file" + "$ref": "#/definitions/v1.GetAuditPlanSQLDataResV1" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_meta": { - "get": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_export": { + "post": { "security": [ { "ApiKeyAuth": [] } ], - "description": "get audit plan SQL meta", + "description": "export audit plan SQL report as CSV", "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQL列表元信息", - "operationId": "getInstanceAuditPlanSQLMetaV1", + "summary": "导出指定扫描任务的 SQL CSV 列表", + "operationId": "getInstanceAuditPlanSQLExportV1", "parameters": [ { "type": "string", @@ -2672,31 +2677,40 @@ var doc = `{ "name": "audit_plan_type", "in": "path", "required": true + }, + { + "description": "audit plan sql export request", + "name": "audit_plan_sql_request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.GetAuditPlanSQLExportReqV1" + } } ], "responses": { "200": { - "description": "OK", + "description": "export audit plan sql report", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLMetaResV1" + "type": "file" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sqls": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_meta": { "get": { "security": [ { "ApiKeyAuth": [] } ], - "description": "get audit plan SQLs", + "description": "get audit plan SQL meta", "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQLs信息", - "operationId": "getInstanceAuditPlanSQLsV1", + "summary": "获取指定扫描任务的SQL列表元信息", + "operationId": "getInstanceAuditPlanSQLMetaV1", "parameters": [ { "type": "string", @@ -2718,27 +2732,13 @@ var doc = `{ "name": "audit_plan_type", "in": "path", "required": true - }, - { - "type": "integer", - "description": "page index", - "name": "page_index", - "in": "query", - "required": true - }, - { - "type": "integer", - "description": "size of per page", - "name": "page_size", - "in": "query", - "required": true } ], "responses": { "200": { "description": "OK", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLsResV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLMetaResV1" } } } @@ -2771,6 +2771,12 @@ var doc = `{ "name": "filter_db_type", "in": "query" }, + { + "type": "string", + "description": "filter by business", + "name": "filter_by_business", + "in": "query" + }, { "type": "string", "description": "filter workflow template id", @@ -9527,6 +9533,9 @@ var doc = `{ "v1.AuditPlanTypeResBase": { "type": "object", "properties": { + "audit_plan_id": { + "type": "integer" + }, "desc": { "type": "string" }, @@ -10118,22 +10127,8 @@ var doc = `{ "$ref": "#/definitions/v1.AuditPlan" } }, - "business": { - "type": "string", - "example": "test" - }, - "instance_name": { - "type": "string", - "example": "test_mysql" - }, - "instance_type": { - "type": "string", - "example": "mysql" - }, - "static_audit": { - "description": "静态审核", - "type": "boolean", - "example": true + "instance_id": { + "type": "string" } } }, @@ -12400,11 +12395,6 @@ var doc = `{ "instance_type": { "type": "string", "example": "mysql" - }, - "static_audit": { - "description": "静态审核", - "type": "boolean", - "example": true } } }, diff --git a/sqle/docs/swagger.json b/sqle/docs/swagger.json index 6a080a869..24bba8262 100644 --- a/sqle/docs/swagger.json +++ b/sqle/docs/swagger.json @@ -2413,7 +2413,7 @@ } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/": { "delete": { "security": [ { @@ -2436,15 +2436,15 @@ }, { "type": "string", - "description": "instance audit plan type", + "description": "instance audit plan id", "name": "instance_audit_plan_id", "in": "path", "required": true }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true } @@ -2487,8 +2487,8 @@ }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true }, @@ -2512,8 +2512,8 @@ } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_data": { - "post": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/sqls": { + "get": { "security": [ { "ApiKeyAuth": [] @@ -2523,8 +2523,8 @@ "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQL列表", - "operationId": "getInstanceAuditPlanSQLDataV1", + "summary": "获取指定扫描任务的SQLs信息", + "operationId": "getInstanceAuditPlanSQLsV1", "parameters": [ { "type": "string", @@ -2542,44 +2542,49 @@ }, { "type": "string", - "description": "audit plan type", - "name": "audit_plan_type", + "description": "audit plan id", + "name": "audit_plan_id", "in": "path", "required": true }, { - "description": "audit plan sql data request", - "name": "audit_plan_sql_request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLDataReqV1" - } + "type": "integer", + "description": "page index", + "name": "page_index", + "in": "query", + "required": true + }, + { + "type": "integer", + "description": "size of per page", + "name": "page_size", + "in": "query", + "required": true } ], "responses": { "200": { "description": "OK", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLDataResV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLsResV1" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_export": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_data": { "post": { "security": [ { "ApiKeyAuth": [] } ], - "description": "export audit plan SQL report as CSV", + "description": "get audit plan SQLs", "tags": [ "instance_audit_plan" ], - "summary": "导出指定扫描任务的 SQL CSV 列表", - "operationId": "getInstanceAuditPlanSQLExportV1", + "summary": "获取指定扫描任务的SQL列表", + "operationId": "getInstanceAuditPlanSQLDataV1", "parameters": [ { "type": "string", @@ -2603,38 +2608,38 @@ "required": true }, { - "description": "audit plan sql export request", + "description": "audit plan sql data request", "name": "audit_plan_sql_request", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLExportReqV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLDataReqV1" } } ], "responses": { "200": { - "description": "export audit plan sql report", + "description": "OK", "schema": { - "type": "file" + "$ref": "#/definitions/v1.GetAuditPlanSQLDataResV1" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_meta": { - "get": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_export": { + "post": { "security": [ { "ApiKeyAuth": [] } ], - "description": "get audit plan SQL meta", + "description": "export audit plan SQL report as CSV", "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQL列表元信息", - "operationId": "getInstanceAuditPlanSQLMetaV1", + "summary": "导出指定扫描任务的 SQL CSV 列表", + "operationId": "getInstanceAuditPlanSQLExportV1", "parameters": [ { "type": "string", @@ -2656,31 +2661,40 @@ "name": "audit_plan_type", "in": "path", "required": true + }, + { + "description": "audit plan sql export request", + "name": "audit_plan_sql_request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.GetAuditPlanSQLExportReqV1" + } } ], "responses": { "200": { - "description": "OK", + "description": "export audit plan sql report", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLMetaResV1" + "type": "file" } } } } }, - "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sqls": { + "/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_meta": { "get": { "security": [ { "ApiKeyAuth": [] } ], - "description": "get audit plan SQLs", + "description": "get audit plan SQL meta", "tags": [ "instance_audit_plan" ], - "summary": "获取指定扫描任务的SQLs信息", - "operationId": "getInstanceAuditPlanSQLsV1", + "summary": "获取指定扫描任务的SQL列表元信息", + "operationId": "getInstanceAuditPlanSQLMetaV1", "parameters": [ { "type": "string", @@ -2702,27 +2716,13 @@ "name": "audit_plan_type", "in": "path", "required": true - }, - { - "type": "integer", - "description": "page index", - "name": "page_index", - "in": "query", - "required": true - }, - { - "type": "integer", - "description": "size of per page", - "name": "page_size", - "in": "query", - "required": true } ], "responses": { "200": { "description": "OK", "schema": { - "$ref": "#/definitions/v1.GetAuditPlanSQLsResV1" + "$ref": "#/definitions/v1.GetAuditPlanSQLMetaResV1" } } } @@ -2755,6 +2755,12 @@ "name": "filter_db_type", "in": "query" }, + { + "type": "string", + "description": "filter by business", + "name": "filter_by_business", + "in": "query" + }, { "type": "string", "description": "filter workflow template id", @@ -9511,6 +9517,9 @@ "v1.AuditPlanTypeResBase": { "type": "object", "properties": { + "audit_plan_id": { + "type": "integer" + }, "desc": { "type": "string" }, @@ -10102,22 +10111,8 @@ "$ref": "#/definitions/v1.AuditPlan" } }, - "business": { - "type": "string", - "example": "test" - }, - "instance_name": { - "type": "string", - "example": "test_mysql" - }, - "instance_type": { - "type": "string", - "example": "mysql" - }, - "static_audit": { - "description": "静态审核", - "type": "boolean", - "example": true + "instance_id": { + "type": "string" } } }, @@ -12384,11 +12379,6 @@ "instance_type": { "type": "string", "example": "mysql" - }, - "static_audit": { - "description": "静态审核", - "type": "boolean", - "example": true } } }, diff --git a/sqle/docs/swagger.yaml b/sqle/docs/swagger.yaml index 92cb9e441..346d71c6d 100644 --- a/sqle/docs/swagger.yaml +++ b/sqle/docs/swagger.yaml @@ -251,6 +251,8 @@ definitions: type: object v1.AuditPlanTypeResBase: properties: + audit_plan_id: + type: integer desc: type: string type: @@ -657,19 +659,8 @@ definitions: items: $ref: '#/definitions/v1.AuditPlan' type: array - business: - example: test - type: string - instance_name: - example: test_mysql - type: string - instance_type: - example: mysql + instance_id: type: string - static_audit: - description: 静态审核 - example: true - type: boolean type: object v1.CreateProjectRuleTemplateReqV1: properties: @@ -2234,10 +2225,6 @@ definitions: instance_type: example: mysql type: string - static_audit: - description: 静态审核 - example: true - type: boolean type: object v1.InstanceAuditPlanInfo: properties: @@ -6218,7 +6205,7 @@ paths: summary: 获取实例扫描任务概览 tags: - instance_audit_plan - /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/: + /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/: delete: description: delete audit plan by type operationId: deleteAuditPlanByTypeV1 @@ -6228,14 +6215,14 @@ paths: name: project_name required: true type: string - - description: instance audit plan type + - description: instance audit plan id in: path name: instance_audit_plan_id required: true type: string - - description: audit plan type + - description: audit plan id in: path - name: audit_plan_type + name: audit_plan_id required: true type: string responses: @@ -6262,9 +6249,9 @@ paths: name: instance_audit_plan_id required: true type: string - - description: audit plan type + - description: audit plan id in: path - name: audit_plan_type + name: audit_plan_id required: true type: string - description: update audit plan status @@ -6283,6 +6270,46 @@ paths: summary: 更新扫描任务状态 tags: - instance_audit_plan + /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_id}/sqls: + get: + description: get audit plan SQLs + operationId: getInstanceAuditPlanSQLsV1 + parameters: + - description: project name + in: path + name: project_name + required: true + type: string + - description: instance audit plan id + in: path + name: instance_audit_plan_id + required: true + type: string + - description: audit plan id + in: path + name: audit_plan_id + required: true + type: string + - description: page index + in: query + name: page_index + required: true + type: integer + - description: size of per page + in: query + name: page_size + required: true + type: integer + responses: + "200": + description: OK + schema: + $ref: '#/definitions/v1.GetAuditPlanSQLsResV1' + security: + - ApiKeyAuth: [] + summary: 获取指定扫描任务的SQLs信息 + tags: + - instance_audit_plan /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sql_data: post: description: get audit plan SQLs @@ -6385,46 +6412,6 @@ paths: summary: 获取指定扫描任务的SQL列表元信息 tags: - instance_audit_plan - /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/audit_plans/{audit_plan_type}/sqls: - get: - description: get audit plan SQLs - operationId: getInstanceAuditPlanSQLsV1 - parameters: - - description: project name - in: path - name: project_name - required: true - type: string - - description: instance audit plan id - in: path - name: instance_audit_plan_id - required: true - type: string - - description: audit plan type - in: path - name: audit_plan_type - required: true - type: string - - description: page index - in: query - name: page_index - required: true - type: integer - - description: size of per page - in: query - name: page_size - required: true - type: integer - responses: - "200": - description: OK - schema: - $ref: '#/definitions/v1.GetAuditPlanSQLsResV1' - security: - - ApiKeyAuth: [] - summary: 获取指定扫描任务的SQLs信息 - tags: - - instance_audit_plan /v1/projects/{project_name}/instance_tips: get: description: get instance tip list @@ -6439,6 +6426,10 @@ paths: in: query name: filter_db_type type: string + - description: filter by business + in: query + name: filter_by_business + type: string - description: filter workflow template id in: query name: filter_workflow_template_id diff --git a/sqle/model/audit_plan.go b/sqle/model/audit_plan.go index 9800c2385..423c96013 100644 --- a/sqle/model/audit_plan.go +++ b/sqle/model/audit_plan.go @@ -136,10 +136,6 @@ func (s *Storage) GetAuditPlanById(id uint) (*AuditPlan, bool, error) { return ap, true, errors.New(errors.ConnectStorageError, err) } -func (s *Storage) GetActiveAuditPlanByIdV2(id uint) (*AuditPlanDetail, bool, error) { - return s.getAuditPlanDetailByID(id) -} - func (s *Storage) GetActiveAuditPlanById(id uint) (*AuditPlan, bool, error) { ap := &AuditPlan{} err := s.db.Model(AuditPlan{}). diff --git a/sqle/model/instance_audit_plan.go b/sqle/model/instance_audit_plan.go index e241caae1..57194fb3d 100644 --- a/sqle/model/instance_audit_plan.go +++ b/sqle/model/instance_audit_plan.go @@ -14,8 +14,6 @@ import ( type InstanceAuditPlan struct { Model ProjectId ProjectUID `gorm:"index; not null"` - Business string `json:"business" gorm:"type:varchar(255)"` - InstanceName string `json:"instance_name" gorm:"type:varchar(255)"` InstanceID uint64 `json:"instance_id"` DBType string `json:"db_type" gorm:"type:varchar(255) not null"` Token string `json:"token" gorm:"not null"` @@ -45,9 +43,8 @@ type AuditPlanDetail struct { ProjectId string `json:"project_id"` DBType string `json:"db_type"` Token string `json:"token" ` - InstanceName string `json:"instance_name"` + InstanceID string `json:"instance_id"` CreateUserID string `json:"create_user_id"` - SchemaName string `json:"schema_name"` Instance *Instance `gorm:"-"` } @@ -56,7 +53,7 @@ func (s *Storage) ListActiveAuditPlanDetail() ([]*AuditPlanDetail, error) { var aps []*AuditPlanDetail err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.active_status = ? AND instance_audit_plans.active_status = ?", ActiveStatusNormal, ActiveStatusNormal). - Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). + Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_id,instance_audit_plans.create_user_id"). Scan(&aps).Error return aps, errors.New(errors.ConnectStorageError, err) } @@ -67,7 +64,7 @@ func (s *Storage) GetAuditPlanDetailByIDType(id int, typ string) (*AuditPlanDeta err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.instance_audit_plan_id = ?", id). Where("audit_plans_v2.type = ?", typ). - Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). + Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_id,instance_audit_plans.create_user_id"). Scan(&aps).Error if aps == nil { @@ -77,7 +74,7 @@ func (s *Storage) GetAuditPlanDetailByIDType(id int, typ string) (*AuditPlanDeta } func (s *Storage) GetAuditPlanDetailByID(id uint) (*AuditPlanDetail, error) { - ap, exist, err := s.getAuditPlanDetailByID(id) + ap, exist, err := s.GetAuditPlanDetailByIDExist(id) if err != nil { return nil, err } @@ -87,14 +84,21 @@ func (s *Storage) GetAuditPlanDetailByID(id uint) (*AuditPlanDetail, error) { return ap, errors.New(errors.ConnectStorageError, err) } -func (s *Storage) getAuditPlanDetailByID(id uint) (*AuditPlanDetail, bool, error) { - var ap *AuditPlanDetail +func (s *Storage) GetAuditPlanDetailByIDExist(id uint) (*AuditPlanDetail, bool, error) { + return s.getAuditPlanDetailByID(id, "") +} - err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). +func (s *Storage) getAuditPlanDetailByID(id uint, status string) (*AuditPlanDetail, bool, error) { + var ap *AuditPlanDetail + query := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.id = ?", id). - Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). - Where("audit_plans_v2.active_status = ? AND instance_audit_plans.active_status = ?", ActiveStatusNormal, ActiveStatusNormal). - Scan(&ap).Error + Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_id,instance_audit_plans.create_user_id") + + if status != "" { + query = query.Where("audit_plans_v2.active_status = ?", status) + } + + err := query.Scan(&ap).Error if err != nil { return ap, false, errors.New(errors.ConnectStorageError, err) } @@ -233,6 +237,17 @@ type SQLManager struct { Remark string `json:"remark" gorm:"type:varchar(4000)"` } +func (s *Storage) GetAuditPlanByID(auditPlanID int) (*AuditPlanV2, bool, error) { + auditPlan := &AuditPlanV2{} + err := s.db.Model(AuditPlanV2{}). + Where("audit_plans_v2.id = ?", auditPlanID). + First(auditPlan).Error + if err == gorm.ErrRecordNotFound { + return auditPlan, false, nil + } + return auditPlan, true, errors.New(errors.ConnectStorageError, err) +} + func (s *Storage) GetAuditPlanByInstanceIdAndType(instanceAuditPlanID string, auditPlanType string) (*AuditPlanV2, bool, error) { auditPlan := &AuditPlanV2{} err := s.db.Model(AuditPlanV2{}). @@ -297,24 +312,22 @@ func (s *Storage) DeleteInstanceAuditPlan(instanceAuditPlanId string) error { }) } -func (s *Storage) DeleteAuditPlan(instanceAuditPlanId, auditPlanType string) error { +func (s *Storage) DeleteAuditPlan(auditPlanID int) error { return s.Tx(func(txDB *gorm.DB) error { // 删除队列表中数据 err := txDB.Exec(`DELETE FROM origin_manage_sql_queues USING origin_manage_sql_queues JOIN audit_plans_v2 ap ON ap.id=origin_manage_sql_queues.source_id - JOIN instance_audit_plans iap ON iap.id = ap.instance_audit_plan_id - WHERE iap.ID = ? AND ap.type = ?`, instanceAuditPlanId, auditPlanType).Error + WHERE ap.id = ?`, auditPlanID).Error if err != nil { return err } - err = txDB.Exec(`UPDATE instance_audit_plans iap - LEFT JOIN audit_plans_v2 ap ON iap.id = ap.instance_audit_plan_id + err = txDB.Exec(`UPDATE audit_plans_v2 ap LEFT JOIN origin_manage_sqls oms ON oms.source_id = ap.id LEFT JOIN sql_managers sm ON sm.origin_manage_sql_id = oms.id SET ap.deleted_at = now(), oms.deleted_at = now(), sm.deleted_at = now() - WHERE iap.ID = ? AND ap.type = ?`, instanceAuditPlanId, auditPlanType).Error + WHERE ap.type = ?`, auditPlanID).Error if err != nil { return err } @@ -346,17 +359,15 @@ func (s *Storage) GetInstanceAuditPlanByInstanceID(instanceID int64) (*InstanceA type OriginManageSQLQueue struct { Model - Source string `json:"source" gorm:"type:varchar(255)"` // 智能扫描SQL/快速审核SQL/IDE审核SQL/CB审核SQL - SourceId uint `json:"source_id" gorm:"type:varchar(255)"` - ProjectId string `json:"project_id" gorm:"type:varchar(255)"` - InstanceName string `json:"instance_name" gorm:"type:varchar(255)"` - SchemaName string `json:"schema_name" gorm:"type:varchar(255)"` - SqlFingerprint string `json:"sql_fingerprint" gorm:"type:mediumtext;not null"` - SqlText string `json:"sql_text" gorm:"type:mediumtext;not null"` - Info JSON `gorm:"type:json"` // 慢日志的 执行时间等特殊属性 - AuditLevel string `json:"audit_level" gorm:"type:varchar(255)"` - AuditResults AuditResults `json:"audit_results" gorm:"type:json"` - EndPoint string `json:"endpoint" gorm:"type:varchar(255)"` + Source string `json:"source" gorm:"type:varchar(255)"` // 智能扫描SQL/快速审核SQL/IDE审核SQL/CB审核SQL + SourceId uint `json:"source_id" gorm:"type:varchar(255)"` + ProjectId string `json:"project_id" gorm:"type:varchar(255)"` + InstanceName string `json:"instance_name" gorm:"type:varchar(255)"` + SchemaName string `json:"schema_name" gorm:"type:varchar(255)"` + SqlFingerprint string `json:"sql_fingerprint" gorm:"type:mediumtext;not null"` + SqlText string `json:"sql_text" gorm:"type:mediumtext;not null"` + Info JSON `gorm:"type:json"` // 慢日志的 执行时间等特殊属性 + EndPoint string `json:"endpoint" gorm:"type:varchar(255)"` // 需要将这个MD5实现与SQLManager关联的效果(审核结果也要加入md5,避免修改规则导致结果变化 SQLID string `json:"sql_id" gorm:"type:varchar(255);not null"` diff --git a/sqle/model/instance_audit_plan_list.go b/sqle/model/instance_audit_plan_list.go index ced5796e1..7658ae72a 100644 --- a/sqle/model/instance_audit_plan_list.go +++ b/sqle/model/instance_audit_plan_list.go @@ -7,9 +7,8 @@ import ( type InstanceAuditPlanListDetail struct { Id uint `json:"id"` - Business string `json:"business"` DBType string `json:"db_type"` - InstanceName string `json:"instance_name"` + InstanceID string `json:"instance_id"` Token string `json:"token"` ActiveStatus string `json:"active_status"` CreateUserId string `json:"create_user_id"` @@ -20,9 +19,8 @@ type InstanceAuditPlanListDetail struct { var instanceAuditPlanQueryTpl = ` SELECT instance_audit_plans.id, - instance_audit_plans.business, instance_audit_plans.db_type, - instance_audit_plans.instance_name, + instance_audit_plans.instance_id, instance_audit_plans.token, instance_audit_plans.active_status, instance_audit_plans.create_user_id, @@ -61,8 +59,8 @@ WHERE {{- if not .current_user_is_admin }} AND ( instance_audit_plans.create_user_id = :current_user_id - {{- if .accessible_instances_name }} - OR instance_audit_plans.instance_name IN ( {{ .accessible_instances_name }} ) + {{- if .accessible_instances_id }} + OR instance_audit_plans.instance_id IN ( {{ .accessible_instances_id }} ) {{- end }} ) {{- end }} @@ -71,20 +69,12 @@ AND ( AND instance_audit_plans.db_type = :filter_instance_audit_plan_db_type {{- end }} -{{- if .filter_by_business }} -AND instance_audit_plans.business = :filter_by_business -{{- end }} - -{{- if .fuzzy_search }} -AND instance_audit_plans.business LIKE '%{{ .fuzzy_search }}%' -{{- end }} - {{- if .filter_audit_plan_type }} AND FIND_IN_SET(:filter_audit_plan_type, audit_plans.types) {{- end }} -{{- if .filter_audit_plan_instance_name }} -AND instance_audit_plans.instance_name = :filter_audit_plan_instance_name +{{- if .filter_audit_plan_instance_id }} +AND instance_audit_plans.instance_id = :filter_audit_plan_instance_id {{- end }} {{- if .filter_project_id }} diff --git a/sqle/server/auditplan/manager.go b/sqle/server/auditplan/manager.go index a2c655930..fb47e874c 100644 --- a/sqle/server/auditplan/manager.go +++ b/sqle/server/auditplan/manager.go @@ -52,10 +52,9 @@ func ConvertModelToAuditPlanV2(a *model.AuditPlanDetail) *AuditPlan { return &AuditPlan{ ID: a.ID, ProjectId: a.ProjectId, - Name: a.InstanceName + "-" + a.Type, DBType: a.DBType, Token: a.Token, - InstanceName: a.InstanceName, + InstanceName: dms.GetInstancesByIdWithoutError(a.InstanceID).Name, CreateUserID: a.CreateUserID, Type: a.Type, RuleTemplateName: a.RuleTemplateName, @@ -236,7 +235,7 @@ func (mgr *Manager) Stop() { } func (mgr *Manager) syncTask(auditPlanId uint) error { - ap, exist, err := mgr.persist.GetActiveAuditPlanByIdV2(auditPlanId) + ap, exist, err := mgr.persist.GetAuditPlanDetailByIDExist(auditPlanId) if err != nil { return err } diff --git a/sqle/server/auditplan/task_v2.go b/sqle/server/auditplan/task_v2.go index 78789933a..b9da2a3cf 100644 --- a/sqle/server/auditplan/task_v2.go +++ b/sqle/server/auditplan/task_v2.go @@ -59,9 +59,6 @@ func auditSQLs(sqls []*model.OriginManageSQL) (*AuditResultResp, error) { return nil, err } schema := sqls[0].SchemaName - if schema == "" { - schema = auditPlan.SchemaName - } task := &model.Task{Instance: auditPlan.Instance, Schema: schema, DBType: auditPlan.DBType}