mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-26 18:48:02 +01:00
Merge pull request #15985 from chlins/fix/replication-rule-enhancement
fix(replication): enhance the replication rule validation
This commit is contained in:
commit
d260e632d8
@ -17,6 +17,7 @@ package project
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/utils"
|
||||
"github.com/goharbor/harbor/src/lib/errors"
|
||||
@ -105,6 +106,10 @@ func (m *manager) Get(ctx context.Context, idOrName interface{}) (*models.Projec
|
||||
}
|
||||
name, ok := idOrName.(string)
|
||||
if ok {
|
||||
// check white space in project name
|
||||
if strings.Contains(name, " ") {
|
||||
return nil, errors.BadRequestError(nil).WithMessage("invalid project name: '%s'", name)
|
||||
}
|
||||
return m.dao.GetByName(ctx, name)
|
||||
}
|
||||
return nil, errors.Errorf("invalid parameter: %v, should be ID(int64) or name(string)", idOrName)
|
||||
|
@ -224,6 +224,8 @@ func (a *Adapter) ListProjects(filters []*model.Filter) ([]*Project, error) {
|
||||
names, ok := util.IsSpecificPathComponent(projectPattern)
|
||||
if ok {
|
||||
for _, name := range names {
|
||||
// trim white space in project name
|
||||
name = strings.TrimSpace(name)
|
||||
project, err := a.Client.GetProject(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -251,3 +251,47 @@ func TestAbstractPublicMetadata(t *testing.T) {
|
||||
require.Equal(t, 1, len(meta))
|
||||
require.Equal(t, "true", meta["public"].(string))
|
||||
}
|
||||
|
||||
func TestListProjects(t *testing.T) {
|
||||
server := test.NewServer(
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/projects",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`[{"name": "p1"}, {"name": "p2"}]`))
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
defer server.Close()
|
||||
|
||||
registry := &model.Registry{
|
||||
URL: server.URL,
|
||||
}
|
||||
adapter, err := New(registry)
|
||||
require.Nil(t, err)
|
||||
|
||||
validPattern := "{p1,p2}/**"
|
||||
// has " " in the p2 project name
|
||||
invalidPattern := "{p1, p2}/**"
|
||||
filters := []*model.Filter{
|
||||
{
|
||||
Type: "name",
|
||||
Value: validPattern,
|
||||
},
|
||||
}
|
||||
projects, err := adapter.ListProjects(filters)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, projects, 2)
|
||||
require.Equal(t, "p1", projects[0].Name)
|
||||
require.Equal(t, "p2", projects[1].Name)
|
||||
|
||||
// invalid pattern, should also work with trim white space in project name.
|
||||
filters[0].Value = invalidPattern
|
||||
_, err = adapter.ListProjects(filters)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, projects, 2)
|
||||
require.Equal(t, "p1", projects[0].Name)
|
||||
require.Equal(t, "p2", projects[1].Name)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user