Merge pull request #13989 from wy65701436/fixes-robot-issues

fix robot issues
This commit is contained in:
stonezdj(Daojun Zhang) 2021-01-14 15:54:46 +08:00 committed by GitHub
commit 2b068c3f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -28,10 +28,9 @@ func (r *robot2) Generate(req *http.Request) security.Context {
if !strings.HasPrefix(name, config.RobotPrefix()) { if !strings.HasPrefix(name, config.RobotPrefix()) {
return nil return nil
} }
name = strings.TrimPrefix(name, config.RobotPrefix())
// The robot name can be used as the unique identifier to locate robot as it contains the project name. // The robot name can be used as the unique identifier to locate robot as it contains the project name.
robots, err := robot_ctl.Ctl.List(req.Context(), q.New(q.KeyWords{ robots, err := robot_ctl.Ctl.List(req.Context(), q.New(q.KeyWords{
"name": name, "name": strings.TrimPrefix(name, config.RobotPrefix()),
}), &robot_ctl.Option{ }), &robot_ctl.Option{
WithPermission: true, WithPermission: true,
}) })

View File

@ -15,6 +15,7 @@ import (
"github.com/goharbor/harbor/src/server/v2.0/handler/model" "github.com/goharbor/harbor/src/server/v2.0/handler/model"
"github.com/goharbor/harbor/src/server/v2.0/models" "github.com/goharbor/harbor/src/server/v2.0/models"
operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/robot" operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/robot"
"math"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -254,11 +255,11 @@ func (rAPI *robotAPI) requireAccess(ctx context.Context, level string, projectID
// more validation // more validation
func (rAPI *robotAPI) validate(d int64, level string, permissions []*models.Permission) error { func (rAPI *robotAPI) validate(d int64, level string, permissions []*models.Permission) error {
if !isValidDuration(d) { if !isValidDuration(d) {
return errors.New(nil).WithMessage("bad request error duration input").WithCode(errors.BadRequestCode) return errors.New(nil).WithMessage("bad request error duration input: %d", d).WithCode(errors.BadRequestCode)
} }
if !isValidLevel(level) { if !isValidLevel(level) {
return errors.New(nil).WithMessage("bad request error level input").WithCode(errors.BadRequestCode) return errors.New(nil).WithMessage("bad request error level input: %s", level).WithCode(errors.BadRequestCode)
} }
if len(permissions) == 0 { if len(permissions) == 0 {
@ -337,7 +338,7 @@ func isValidLevel(l string) bool {
} }
func isValidDuration(d int64) bool { func isValidDuration(d int64) bool {
return d >= int64(-1) return d >= int64(-1) && d < math.MaxInt32
} }
func isValidSec(sec string) bool { func isValidSec(sec string) bool {

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"math"
"testing" "testing"
) )
@ -62,6 +63,18 @@ func TestValidDuration(t *testing.T) {
9999, 9999,
true, true,
}, },
{"duration max",
math.MaxInt32 - 1,
true,
},
{"duration max",
math.MaxInt32,
false,
},
{"duration 999999999999",
999999999999,
false,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {