Skip to content
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

feat: 新增 API 定义,升级了 audit_result 审核结果结构定义 #1416

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi
v1Router.POST("/projects/:project_name/tasks/audits", v1.CreateAndAuditTask)
v1Router.GET("/tasks/audits/:task_id/", v1.GetTask)
v1Router.GET("/tasks/audits/:task_id/sqls", v1.GetTaskSQLs)
v1Router.GET("/tasks/audits/:task_id/sqls", v2.GetTaskSQLs)
v1Router.GET("/tasks/audits/:task_id/sql_report", v1.DownloadTaskSQLReportFile)
v1Router.GET("/tasks/audits/:task_id/sql_file", v1.DownloadTaskSQLFile)
v1Router.GET("/tasks/audits/:task_id/sql_content", v1.GetAuditTaskSQLContent)
Expand Down Expand Up @@ -329,6 +330,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi
v1Router.GET("/projects/:project_name/audit_plans/:audit_plan_name/notify_config/test", v1.TestAuditPlanNotifyConfig)
v1Router.GET("/projects/:project_name/audit_plans/:audit_plan_name/reports/:audit_plan_report_id/sqls/:number/analysis", v1.GetAuditPlanAnalysisData)
v1Router.GET("/projects/:project_name/audit_plans/:audit_plan_name/reports/:audit_plan_report_id/sqls", v1.GetAuditPlanReportSQLsV1)
v1Router.GET("/projects/:project_name/audit_plans/:audit_plan_name/reports/:audit_plan_report_id/sqls", v2.GetAuditPlanReportSQLs)

// sql query
if err := cloudbeaver_wrapper.StartApp(e); err != nil {
Expand All @@ -339,6 +341,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi

// sql audit
v1Router.POST("/sql_audit", v1.DirectAudit)
v1Router.POST("/sql_audit", v2.DirectAudit)

// UI
e.File("/", "ui/index.html")
Expand Down
36 changes: 35 additions & 1 deletion sqle/api/controller/v2/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"net/http"
"strings"

"github.com/labstack/echo/v4"

"github.com/actiontech/sqle/sqle/api/controller"
v1 "github.com/actiontech/sqle/sqle/api/controller/v1"
"github.com/actiontech/sqle/sqle/model"
"github.com/actiontech/sqle/sqle/server/auditplan"
"github.com/actiontech/sqle/sqle/utils"
"github.com/labstack/echo/v4"
)

type GetAuditPlansReqV2 struct {
Expand Down Expand Up @@ -148,3 +149,36 @@ func GetAuditPlans(c echo.Context) error {
TotalNums: count,
})
}

type GetAuditPlanReportSQLsReqV2 struct {
PageIndex uint32 `json:"page_index" query:"page_index" valid:"required"`
PageSize uint32 `json:"page_size" query:"page_size" valid:"required"`
}

type GetAuditPlanReportSQLsResV2 struct {
controller.BaseRes
Data []*AuditPlanReportSQLResV2 `json:"data"`
TotalNums uint64 `json:"total_nums"`
}

type AuditPlanReportSQLResV2 struct {
SQL string `json:"audit_plan_report_sql" example:"select * from t1 where id = 1"`
AuditResult []*AuditResult `json:"audit_plan_report_sql_audit_result" example:"same format as task audit result"`
Number uint `json:"number" example:"1"`
}

// @Summary 获取指定扫描任务的SQL扫描详情
// @Description get audit plan report SQLs
// @Id getAuditPlanReportsSQLs
// @Tags audit_plan
// @Security ApiKeyAuth
// @Param project_name path string true "project name"
// @Param audit_plan_name path string true "audit plan name"
// @Param audit_plan_report_id path string true "audit plan report id"
// @Param page_index query uint32 true "page index"
// @Param page_size query uint32 true "size of per page"
// @Success 200 {object} v2.GetAuditPlanReportSQLsResV2
// @router /v2/projects/{project_name}/audit_plans/{audit_plan_name}/reports/{audit_plan_report_id}/sqls [get]
func GetAuditPlanReportSQLs(c echo.Context) error {
return controller.JSONNewNotImplementedErr(c)
}
45 changes: 45 additions & 0 deletions sqle/api/controller/v2/sql_audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v2

import (
"github.com/labstack/echo/v4"

"github.com/actiontech/sqle/sqle/api/controller"
)

type DirectAuditReqV2 struct {
InstanceType string `json:"instance_type" form:"instance_type" example:"MySQL" valid:"required"`
// 调用方不应该关心SQL是否被完美的拆分成独立的条目, 拆分SQL由SQLE实现
SQLContent string `json:"sql_content" form:"sql_content" example:"select * from t1; select * from t2;" valid:"required"`
SQLType string `json:"sql_type" form:"sql_type" example:"sql" enums:"sql,mybatis," valid:"omitempty,oneof=sql mybatis"`
}

type AuditResDataV2 struct {
AuditLevel string `json:"audit_level" enums:"normal,notice,warn,error,"`
Score int32 `json:"score"`
PassRate float64 `json:"pass_rate"`
SQLResults []AuditSQLResV2 `json:"sql_results"`
}

type AuditSQLResV2 struct {
Number uint `json:"number"`
ExecSQL string `json:"exec_sql"`
AuditResult []*AuditResult `json:"audit_result"`
AuditLevel string `json:"audit_level"`
}

type DirectAuditResV2 struct {
controller.BaseRes
Data *AuditResDataV2 `json:"data"`
}

// @Summary 直接审核SQL
// @Description Direct audit sql
// @Id directAuditV2
// @Tags sql_audit
// @Security ApiKeyAuth
// @Param req body v2.DirectAuditReqV2 true "sqls that should be audited"
// @Success 200 {object} v2.DirectAuditResV2
// @router /v2/sql_audit [post]
func DirectAudit(c echo.Context) error {
return controller.JSONNewNotImplementedErr(c)
}
58 changes: 58 additions & 0 deletions sqle/api/controller/v2/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v2

import (
"github.com/labstack/echo/v4"

"github.com/actiontech/sqle/sqle/api/controller"
)

type GetAuditTaskSQLsReqV2 struct {
FilterExecStatus string `json:"filter_exec_status" query:"filter_exec_status"`
FilterAuditStatus string `json:"filter_audit_status" query:"filter_audit_status"`
FilterAuditLevel string `json:"filter_audit_level" query:"filter_audit_level"`
NoDuplicate bool `json:"no_duplicate" query:"no_duplicate"`
PageIndex uint32 `json:"page_index" query:"page_index" valid:"required"`
PageSize uint32 `json:"page_size" query:"page_size" valid:"required"`
}

type GetAuditTaskSQLsResV2 struct {
controller.BaseRes
Data []*AuditTaskSQLResV2 `json:"data"`
TotalNums uint64 `json:"total_nums"`
}

type AuditTaskSQLResV2 struct {
Number uint `json:"number"`
ExecSQL string `json:"exec_sql"`
AuditResult []*AuditResult `json:"audit_result"`
AuditLevel string `json:"audit_level"`
AuditStatus string `json:"audit_status"`
ExecResult string `json:"exec_result"`
ExecStatus string `json:"exec_status"`
RollbackSQL string `json:"rollback_sql,omitempty"`
Description string `json:"description"`
}

type AuditResult struct {
Level string `json:"level"`
Message string `json:"message"`
RuleName string `json:"rule_name"`
}

// @Summary 获取指定扫描任务的SQLs信息
// @Description get information of all SQLs belong to the specified audit task
// @Tags task
// @Id getAuditTaskSQLsV2
// @Security ApiKeyAuth
// @Param task_id path string true "task id"
// @Param filter_exec_status query string false "filter: exec status of task sql" Enums(initialized,doing,succeeded,failed,manually_executed)
// @Param filter_audit_status query string false "filter: audit status of task sql" Enums(initialized,doing,finished)
// @Param filter_audit_level query string false "filter: audit level of task sql" Enums(normal,notice,warn,error)
// @Param no_duplicate query boolean false "select unique (fingerprint and audit result) for task sql"
// @Param page_index query string true "page index"
// @Param page_size query string true "page size"
// @Success 200 {object} v2.GetAuditTaskSQLsResV2
// @router /v2/tasks/audits/{task_id}/sqls [get]
func GetTaskSQLs(c echo.Context) error {
return controller.JSONNewNotImplementedErr(c)
}
Loading