Skip to content

Commit c6eb84f

Browse files
authored
Merge pull request #3043 from actiontech/abnormal_token_status
Abnormal token status
2 parents 4133fa4 + 41bf5a0 commit c6eb84f

File tree

7 files changed

+141
-52
lines changed

7 files changed

+141
-52
lines changed

sqle/api/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
257257
v1ProjectOpRouter.DELETE("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.DeleteInstanceAuditPlan)
258258
v1ProjectOpRouter.PUT("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.UpdateInstanceAuditPlan)
259259
v1ProjectOpRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/", v1.UpdateInstanceAuditPlanStatus)
260-
v1ProjectOpRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/token", v1.GenerateAuditPlanToken)
260+
v1ProjectOpRouter.PATCH("/:project_name/instance_audit_plans/:instance_audit_plan_id/token", v1.RefreshAuditPlanToken)
261261

262262
// audit plan; 智能扫描任务
263263
v1ProjectOpRouter.DELETE("/:project_name/instance_audit_plans/:instance_audit_plan_id/audit_plans/:audit_plan_id/", v1.DeleteAuditPlanById)

sqle/api/controller/v1/instance_audit_plan.go

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ func CreateInstanceAuditPlan(c echo.Context) error {
199199
if err != nil {
200200
return controller.JSONBaseErrorReq(c, err)
201201
}
202-
203202
// generate token , 生成ID后根据ID生成token
204-
if err := generateAndUpdateAuditPlanToken(ap, tokenExpire); err != nil {
203+
err = HandleAuditPlanToken(ap.GetIDStr())
204+
if err != nil {
205205
return controller.JSONBaseErrorReq(c, err)
206206
}
207207

@@ -214,18 +214,60 @@ func CreateInstanceAuditPlan(c echo.Context) error {
214214
})
215215
}
216216

217-
func generateAndUpdateAuditPlanToken(ap *model.InstanceAuditPlan, tokenExpire time.Duration) error {
218-
t, err := dmsCommonJwt.GenJwtToken(dmsCommonJwt.WithExpiredTime(tokenExpire), dmsCommonJwt.WithAuditPlanName(utils.Md5(ap.GetIDStr())))
219-
if err != nil {
220-
return errors.New(errors.DataConflict, err)
221-
}
222-
err = model.GetStorage().UpdateInstanceAuditPlanByID(ap.ID, map[string]interface{}{"token": t})
217+
func HandleAuditPlanToken(instanceAuditPlanID string) error {
218+
s := model.GetStorage()
219+
220+
ap, exist, err := s.GetInstanceAuditPlanDetail(instanceAuditPlanID)
223221
if err != nil {
224222
return err
225223
}
224+
if !exist {
225+
return errors.NewInstanceAuditPlanNotExistErr()
226+
}
227+
228+
return UpdateInstanceAuditPlanToken(ap, tokenExpire)
229+
}
230+
231+
func UpdateInstanceAuditPlanToken(ap *model.InstanceAuditPlan, tokenExpire time.Duration) error {
232+
// 存在scanner依赖的任务类型时候,重新生成token
233+
needGenerate := HasScannerTypeSubPlans(ap)
234+
// 当前token是否为为空
235+
currentTokenEmpty := ap.Token == ""
236+
237+
var token string
238+
var err error
239+
if needGenerate {
240+
token, err = newAuditPlanToken(ap, tokenExpire)
241+
if err != nil {
242+
return errors.New(errors.DataConflict, err)
243+
}
244+
}
245+
246+
// 1. 添加token: 存在scanner类型任务并且原本token为空
247+
// 2. 删除token: 不存在scanner类型任务并且原本token不为空
248+
if needGenerate == currentTokenEmpty {
249+
return model.GetStorage().UpdateInstanceAuditPlanByID(ap.ID, map[string]interface{}{"token": token})
250+
}
226251
return nil
227252
}
228253

254+
func HasScannerTypeSubPlans(ap *model.InstanceAuditPlan) bool {
255+
supportedTypes := auditplan.GetSupportedScannerAuditPlanType()
256+
for _, plan := range ap.AuditPlans {
257+
if _, ok := supportedTypes[plan.Type]; ok {
258+
return true
259+
}
260+
}
261+
return false
262+
}
263+
264+
func newAuditPlanToken(ap *model.InstanceAuditPlan, tokenExpire time.Duration) (string, error) {
265+
return dmsCommonJwt.GenJwtToken(
266+
dmsCommonJwt.WithExpiredTime(tokenExpire),
267+
dmsCommonJwt.WithAuditPlanName(utils.Md5(ap.GetIDStr())),
268+
)
269+
}
270+
229271
// @Summary 删除实例扫描任务
230272
// @Description delete instance audit plan
231273
// @Id deleteInstanceAuditPlanV1
@@ -385,6 +427,10 @@ func UpdateInstanceAuditPlan(c echo.Context) error {
385427
if err != nil {
386428
return controller.JSONBaseErrorReq(c, err)
387429
}
430+
err = HandleAuditPlanToken(instanceAuditPlanID)
431+
if err != nil {
432+
return controller.JSONBaseErrorReq(c, err)
433+
}
388434
return controller.JSONBaseErrorReq(c, nil)
389435
}
390436

@@ -760,6 +806,10 @@ func DeleteAuditPlanById(c echo.Context) error {
760806
if err != nil {
761807
return controller.JSONBaseErrorReq(c, err)
762808
}
809+
err = HandleAuditPlanToken(instanceAuditPlanID)
810+
if err != nil {
811+
return controller.JSONBaseErrorReq(c, err)
812+
}
763813
return controller.JSONBaseErrorReq(c, nil)
764814
}
765815

@@ -1298,22 +1348,22 @@ func AuditPlanTriggerSqlAudit(c echo.Context) error {
12981348
return controller.JSONBaseErrorReq(c, nil)
12991349
}
13001350

1301-
type GenerateAuditPlanTokenReqV1 struct {
1351+
type RefreshAuditPlanTokenReqV1 struct {
13021352
ExpiresInDays *int `json:"expires_in_days"`
13031353
}
13041354

1305-
// @Summary 生成扫描任务token
1306-
// @Description generate audit plan token
1307-
// @Id generateAuditPlanTokenV1
1355+
// @Summary 重置扫描任务token
1356+
// @Description refresh audit plan token
1357+
// @Id refreshAuditPlanTokenV1
13081358
// @Tags instance_audit_plan
13091359
// @Security ApiKeyAuth
1310-
// @param audit_plan body v1.GenerateAuditPlanTokenReqV1 false "update instance audit plan token"
1360+
// @param audit_plan body v1.RefreshAuditPlanTokenReqV1 false "update instance audit plan token"
13111361
// @Param project_name path string true "project name"
13121362
// @Param instance_audit_plan_id path string true "instance audit plan id"
13131363
// @Success 200 {object} controller.BaseRes
13141364
// @router /v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/token [patch]
1315-
func GenerateAuditPlanToken(c echo.Context) error {
1316-
req := new(GenerateAuditPlanTokenReqV1)
1365+
func RefreshAuditPlanToken(c echo.Context) error {
1366+
req := new(RefreshAuditPlanTokenReqV1)
13171367
if err := controller.BindAndValidateReq(c, req); err != nil {
13181368
return controller.JSONBaseErrorReq(c, err)
13191369
}
@@ -1338,9 +1388,22 @@ func GenerateAuditPlanToken(c echo.Context) error {
13381388
expireDuration = time.Duration(expiresInDays) * 24 * time.Hour
13391389
}
13401390
}
1341-
err = generateAndUpdateAuditPlanToken(instanceAuditPlan, expireDuration)
1391+
1392+
err = RefreshInstanceAuditPlanToken(instanceAuditPlan, expireDuration)
13421393
if err != nil {
13431394
return controller.JSONBaseErrorReq(c, err)
13441395
}
13451396
return controller.JSONBaseErrorReq(c, nil)
13461397
}
1398+
1399+
func RefreshInstanceAuditPlanToken(ap *model.InstanceAuditPlan, tokenExpire time.Duration) error {
1400+
var token string
1401+
var err error
1402+
if HasScannerTypeSubPlans(ap) {
1403+
token, err = newAuditPlanToken(ap, tokenExpire)
1404+
if err != nil {
1405+
return errors.New(errors.DataConflict, err)
1406+
}
1407+
}
1408+
return model.GetStorage().UpdateInstanceAuditPlanByID(ap.ID, map[string]interface{}{"token": token})
1409+
}

sqle/api/controller/v1/sql_manage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ type GetAbnormalAuditPlanInstancesResp struct {
547547
type AbnormalAuditPlanInstance struct {
548548
InstanceName string `json:"instance_name" example:"MySQL"`
549549
InstanceAuditPlanID uint `json:"instance_audit_plan_id"`
550+
AbnormalStatusCode uint `json:"abnormal_status_code"`
551+
TokenEXP int64 `json:"token_exp" example:"1747129752"`
550552
}
551553

552554
// GetAbnormalInstanceAuditPlans get the instance of audit plan execution abnormal

sqle/docs/docs.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,19 +3843,19 @@ var doc = `{
38433843
"ApiKeyAuth": []
38443844
}
38453845
],
3846-
"description": "generate audit plan token",
3846+
"description": "refresh audit plan token",
38473847
"tags": [
38483848
"instance_audit_plan"
38493849
],
3850-
"summary": "生成扫描任务token",
3851-
"operationId": "generateAuditPlanTokenV1",
3850+
"summary": "重置扫描任务token",
3851+
"operationId": "refreshAuditPlanTokenV1",
38523852
"parameters": [
38533853
{
38543854
"description": "update instance audit plan token",
38553855
"name": "audit_plan",
38563856
"in": "body",
38573857
"schema": {
3858-
"$ref": "#/definitions/v1.GenerateAuditPlanTokenReqV1"
3858+
"$ref": "#/definitions/v1.RefreshAuditPlanTokenReqV1"
38593859
}
38603860
},
38613861
{
@@ -12409,12 +12409,19 @@ var doc = `{
1240912409
"v1.AbnormalAuditPlanInstance": {
1241012410
"type": "object",
1241112411
"properties": {
12412+
"abnormal_status_code": {
12413+
"type": "integer"
12414+
},
1241212415
"instance_audit_plan_id": {
1241312416
"type": "integer"
1241412417
},
1241512418
"instance_name": {
1241612419
"type": "string",
1241712420
"example": "MySQL"
12421+
},
12422+
"token_exp": {
12423+
"type": "integer",
12424+
"example": 1747129752
1241812425
}
1241912426
}
1242012427
},
@@ -14560,14 +14567,6 @@ var doc = `{
1456014567
}
1456114568
}
1456214569
},
14563-
"v1.GenerateAuditPlanTokenReqV1": {
14564-
"type": "object",
14565-
"properties": {
14566-
"expires_in_days": {
14567-
"type": "integer"
14568-
}
14569-
}
14570-
},
1457114570
"v1.GetAbnormalAuditPlanInstancesResp": {
1457214571
"type": "object",
1457314572
"properties": {
@@ -17876,6 +17875,14 @@ var doc = `{
1787617875
}
1787717876
}
1787817877
},
17878+
"v1.RefreshAuditPlanTokenReqV1": {
17879+
"type": "object",
17880+
"properties": {
17881+
"expires_in_days": {
17882+
"type": "integer"
17883+
}
17884+
}
17885+
},
1787917886
"v1.RejectWorkflowReqV1": {
1788017887
"type": "object",
1788117888
"properties": {

sqle/docs/swagger.json

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,19 +3827,19 @@
38273827
"ApiKeyAuth": []
38283828
}
38293829
],
3830-
"description": "generate audit plan token",
3830+
"description": "refresh audit plan token",
38313831
"tags": [
38323832
"instance_audit_plan"
38333833
],
3834-
"summary": "生成扫描任务token",
3835-
"operationId": "generateAuditPlanTokenV1",
3834+
"summary": "重置扫描任务token",
3835+
"operationId": "refreshAuditPlanTokenV1",
38363836
"parameters": [
38373837
{
38383838
"description": "update instance audit plan token",
38393839
"name": "audit_plan",
38403840
"in": "body",
38413841
"schema": {
3842-
"$ref": "#/definitions/v1.GenerateAuditPlanTokenReqV1"
3842+
"$ref": "#/definitions/v1.RefreshAuditPlanTokenReqV1"
38433843
}
38443844
},
38453845
{
@@ -12393,12 +12393,19 @@
1239312393
"v1.AbnormalAuditPlanInstance": {
1239412394
"type": "object",
1239512395
"properties": {
12396+
"abnormal_status_code": {
12397+
"type": "integer"
12398+
},
1239612399
"instance_audit_plan_id": {
1239712400
"type": "integer"
1239812401
},
1239912402
"instance_name": {
1240012403
"type": "string",
1240112404
"example": "MySQL"
12405+
},
12406+
"token_exp": {
12407+
"type": "integer",
12408+
"example": 1747129752
1240212409
}
1240312410
}
1240412411
},
@@ -14544,14 +14551,6 @@
1454414551
}
1454514552
}
1454614553
},
14547-
"v1.GenerateAuditPlanTokenReqV1": {
14548-
"type": "object",
14549-
"properties": {
14550-
"expires_in_days": {
14551-
"type": "integer"
14552-
}
14553-
}
14554-
},
1455514554
"v1.GetAbnormalAuditPlanInstancesResp": {
1455614555
"type": "object",
1455714556
"properties": {
@@ -17860,6 +17859,14 @@
1786017859
}
1786117860
}
1786217861
},
17862+
"v1.RefreshAuditPlanTokenReqV1": {
17863+
"type": "object",
17864+
"properties": {
17865+
"expires_in_days": {
17866+
"type": "integer"
17867+
}
17868+
}
17869+
},
1786317870
"v1.RejectWorkflowReqV1": {
1786417871
"type": "object",
1786517872
"properties": {

sqle/docs/swagger.yaml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ definitions:
3636
type: object
3737
v1.AbnormalAuditPlanInstance:
3838
properties:
39+
abnormal_status_code:
40+
type: integer
3941
instance_audit_plan_id:
4042
type: integer
4143
instance_name:
4244
example: MySQL
4345
type: string
46+
token_exp:
47+
example: 1747129752
48+
type: integer
4449
type: object
4550
v1.AffectRows:
4651
properties:
@@ -1505,11 +1510,6 @@ definitions:
15051510
$ref: '#/definitions/v1.DatabaseSchemaObject'
15061511
type: array
15071512
type: object
1508-
v1.GenerateAuditPlanTokenReqV1:
1509-
properties:
1510-
expires_in_days:
1511-
type: integer
1512-
type: object
15131513
v1.GetAbnormalAuditPlanInstancesResp:
15141514
properties:
15151515
code:
@@ -3782,6 +3782,11 @@ definitions:
37823782
value:
37833783
type: string
37843784
type: object
3785+
v1.RefreshAuditPlanTokenReqV1:
3786+
properties:
3787+
expires_in_days:
3788+
type: integer
3789+
type: object
37853790
v1.RejectWorkflowReqV1:
37863791
properties:
37873792
reason:
@@ -9304,14 +9309,14 @@ paths:
93049309
- instance_audit_plan
93059310
/v1/projects/{project_name}/instance_audit_plans/{instance_audit_plan_id}/token:
93069311
patch:
9307-
description: generate audit plan token
9308-
operationId: generateAuditPlanTokenV1
9312+
description: refresh audit plan token
9313+
operationId: refreshAuditPlanTokenV1
93099314
parameters:
93109315
- description: update instance audit plan token
93119316
in: body
93129317
name: audit_plan
93139318
schema:
9314-
$ref: '#/definitions/v1.GenerateAuditPlanTokenReqV1'
9319+
$ref: '#/definitions/v1.RefreshAuditPlanTokenReqV1'
93159320
- description: project name
93169321
in: path
93179322
name: project_name
@@ -9329,7 +9334,7 @@ paths:
93299334
$ref: '#/definitions/controller.BaseRes'
93309335
security:
93319336
- ApiKeyAuth: []
9332-
summary: 生成扫描任务token
9337+
summary: 重置扫描任务token
93339338
tags:
93349339
- instance_audit_plan
93359340
/v1/projects/{project_name}/instance_tips:

0 commit comments

Comments
 (0)