mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-18 00:05:12 +01:00
Merge pull request #7806 from wy65701436/valid-robot
add validation for robot account registration
This commit is contained in:
commit
c163062431
@ -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 ...
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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{
|
||||||
|
Loading…
Reference in New Issue
Block a user