Merge pull request #7806 from wy65701436/valid-robot

add validation for robot account registration
This commit is contained in:
Daniel Jiang 2019-05-16 03:58:45 +08:00 committed by GitHub
commit c163062431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -17,6 +17,7 @@ package models
import ( import (
"github.com/astaxie/beego/validation" "github.com/astaxie/beego/validation"
"github.com/goharbor/harbor/src/common/rbac" "github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/utils"
"time" "time"
) )
@ -52,9 +53,14 @@ type RobotReq struct {
Access []*rbac.Policy `json:"access"` Access []*rbac.Policy `json:"access"`
} }
// Valid put request validation // Valid ...
func (rq *RobotReq) Valid(v *validation.Validation) { func (rq *RobotReq) Valid(v *validation.Validation) {
// ToDo: add validation for access info. if utils.IsIllegalLength(rq.Name, 1, 255) {
v.SetError("name", "robot name with illegal length")
}
if utils.IsContainIllegalChar(rq.Name, []string{",", "~", "#", "$", "%"}) {
v.SetError("name", "robot name contains illegal characters")
}
} }
// RobotRep ... // RobotRep ...

View File

@ -107,7 +107,8 @@ func (r *RobotAPI) Post() {
} }
var robotReq models.RobotReq var robotReq models.RobotReq
if err := r.DecodeJSONReq(&robotReq); err != nil { isValid, err := r.DecodeJSONReqAndValidate(&robotReq)
if !isValid {
r.SendBadRequestError(err) r.SendBadRequestError(err)
return return
} }
@ -230,8 +231,7 @@ func (r *RobotAPI) Put() {
} }
var robotReq models.RobotReq var robotReq models.RobotReq
isValid, err := r.DecodeJSONReqAndValidate(&robotReq) if err := r.DecodeJSONReq(&robotReq); err != nil {
if !isValid {
r.SendBadRequestError(err) r.SendBadRequestError(err)
return return
} }

View File

@ -70,6 +70,19 @@ func TestRobotAPIPost(t *testing.T) {
}, },
code: http.StatusCreated, code: http.StatusCreated,
}, },
// 400
{
request: &testingRequest{
method: http.MethodPost,
url: robotPath,
bodyJSON: &models.RobotReq{
Name: "testIllgel#",
Description: "test desc",
},
credential: projAdmin4Robot,
},
code: http.StatusBadRequest,
},
// 403 -- developer // 403 -- developer
{ {
request: &testingRequest{ request: &testingRequest{