diff --git a/src/chartserver/reverse_proxy.go b/src/chartserver/reverse_proxy.go index 5d0c2479e..d17e20ad8 100644 --- a/src/chartserver/reverse_proxy.go +++ b/src/chartserver/reverse_proxy.go @@ -136,7 +136,7 @@ func singleJoiningSlash(a, b string) string { // Remove 'chartrepo' from the endpoints of manipulation API // Remove 'chartrepo' from the endpoints of repository services func rewriteURLPath(req *http.Request) { - incomingURLPath := req.RequestURI + incomingURLPath := req.URL.Path // Health check endpoint if incomingURLPath == chartRepoHealthEndpoint { diff --git a/src/core/api/base.go b/src/core/api/base.go index d19fcbdcb..2bb5c75b5 100644 --- a/src/core/api/base.go +++ b/src/core/api/base.go @@ -22,6 +22,7 @@ import ( "github.com/goharbor/harbor/src/common/security" "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/config" + "github.com/goharbor/harbor/src/core/filter" "github.com/goharbor/harbor/src/core/promgr" "github.com/goharbor/harbor/src/core/utils" ) @@ -50,7 +51,7 @@ const ( // Prepare inits security context and project manager from request // context func (b *BaseController) Prepare() { - /*ctx, err := filter.GetSecurityContext(b.Ctx.Request) + ctx, err := filter.GetSecurityContext(b.Ctx.Request) if err != nil { log.Errorf("failed to get security context: %v", err) b.CustomAbort(http.StatusInternalServerError, "") @@ -62,7 +63,7 @@ func (b *BaseController) Prepare() { log.Errorf("failed to get project manager: %v", err) b.CustomAbort(http.StatusInternalServerError, "") } - b.ProjectMgr = pm*/ + b.ProjectMgr = pm } // RenderFormatedError renders errors with well formted style `{"error": "This is an error"}` diff --git a/src/core/api/chart_repository.go b/src/core/api/chart_repository.go index 4f069d347..c595a790b 100644 --- a/src/core/api/chart_repository.go +++ b/src/core/api/chart_repository.go @@ -67,7 +67,7 @@ func (cra *ChartRepositoryAPI) Prepare() { // Exclude the following URI // -/index.yaml // -/api/chartserver/health - incomingURI := cra.Ctx.Request.RequestURI + incomingURI := cra.Ctx.Request.URL.Path if incomingURI == rootUploadingEndpoint { // Forward to the default repository cra.namespace = defaultRepo diff --git a/src/core/api/chart_repository_test.go b/src/core/api/chart_repository_test.go index 8334004c5..030fa85a8 100644 --- a/src/core/api/chart_repository_test.go +++ b/src/core/api/chart_repository_test.go @@ -80,17 +80,15 @@ func TestGetHealthStatus(t *testing.T) { err := handleAndParse(&testingRequest{ url: "/api/chartrepo/health", method: http.MethodGet, - credential: projAdmin, + credential: sysAdmin, }, &status) if err != nil { t.Fatal(err) } - if v, ok := status["healthy"]; !ok { - t.Fatal("expect 'healthy' but got nil") - } else { - t.Fatalf("expect 'healthy' but got %v", v) + if _, ok := status["health"]; !ok { + t.Fatal("expect 'health' but got nil") } } @@ -112,7 +110,7 @@ func TestGetIndex(t *testing.T) { request: &testingRequest{ url: "/chartrepo/index.yaml", method: http.MethodGet, - credential: projAdmin, + credential: sysAdmin, }, code: http.StatusOK, }) @@ -152,7 +150,7 @@ func TesListCharts(t *testing.T) { func TestListChartVersions(t *testing.T) { chartVersions := make(chartserver.ChartVersions, 0) err := handleAndParse(&testingRequest{ - url: "/api/chartrepo/library/chart/harbor", + url: "/api/chartrepo/library/charts/harbor", method: http.MethodGet, credential: projAdmin, }, &chartVersions) @@ -170,7 +168,7 @@ func TestListChartVersions(t *testing.T) { func TestGetChartVersion(t *testing.T) { chartV := &chartserver.ChartVersionDetails{} err := handleAndParse(&testingRequest{ - url: "/api/chartrepo/library/chart/harbor/0.2.0", + url: "/api/chartrepo/library/charts/harbor/0.2.0", method: http.MethodGet, credential: projAdmin, }, chartV) @@ -194,7 +192,7 @@ func TestDeleteChartVersion(t *testing.T) { request: &testingRequest{ url: "/api/chartrepo/library/charts/harbor/0.2.1", method: http.MethodDelete, - credential: projDeveloper, + credential: projAdmin, }, code: http.StatusOK, }) diff --git a/src/core/api/harborapi_test.go b/src/core/api/harborapi_test.go index 1a9efdef3..4dace6345 100644 --- a/src/core/api/harborapi_test.go +++ b/src/core/api/harborapi_test.go @@ -170,6 +170,23 @@ func init() { beego.Router("/api/system/gc/:id", &GCAPI{}, "get:GetGC") beego.Router("/api/system/gc/:id([0-9]+)/log", &GCAPI{}, "get:GetLog") beego.Router("/api/system/gc/schedule", &GCAPI{}, "get:Get;put:Put;post:Post") + + // Charts are controlled under projects + chartRepositoryAPIType := &ChartRepositoryAPI{} + beego.Router("/api/chartrepo/health", chartRepositoryAPIType, "get:GetHealthStatus") + beego.Router("/api/chartrepo/:repo/charts", chartRepositoryAPIType, "get:ListCharts") + beego.Router("/api/chartrepo/:repo/charts/:name", chartRepositoryAPIType, "get:ListChartVersions") + beego.Router("/api/chartrepo/:repo/charts/:name", chartRepositoryAPIType, "delete:DeleteChart") + beego.Router("/api/chartrepo/:repo/charts/:name/:version", chartRepositoryAPIType, "get:GetChartVersion") + beego.Router("/api/chartrepo/:repo/charts/:name/:version", chartRepositoryAPIType, "delete:DeleteChartVersion") + beego.Router("/api/chartrepo/:repo/charts", chartRepositoryAPIType, "post:UploadChartVersion") + beego.Router("/api/chartrepo/:repo/prov", chartRepositoryAPIType, "post:UploadChartProvFile") + beego.Router("/api/chartrepo/charts", chartRepositoryAPIType, "post:UploadChartVersion") + + // Repository services + beego.Router("/chartrepo/:repo/index.yaml", chartRepositoryAPIType, "get:GetIndexByRepo") + beego.Router("/chartrepo/index.yaml", chartRepositoryAPIType, "get:GetIndex") + beego.Router("/chartrepo/:repo/charts/:filename", chartRepositoryAPIType, "get:DownloadChart") // Labels for chart chartLabelAPIType := &ChartLabelAPI{} beego.Router("/api/chartrepo/:repo/charts/:name/:version/labels", chartLabelAPIType, "get:GetLabels;post:MarkLabel") diff --git a/src/testing/chart_utility.go b/src/testing/chart_utility.go index 3ccb10615..570c7aa9b 100644 --- a/src/testing/chart_utility.go +++ b/src/testing/chart_utility.go @@ -7,10 +7,14 @@ package testing import ( "encoding/json" "net/http" + "strings" + + hlog "github.com/goharbor/harbor/src/common/utils/log" ) // MockChartRepoHandler is the backend chart server handler var MockChartRepoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hlog.Infof("Incoming testing request: %s", r.RequestURI) switch r.RequestURI { case "/health": if r.Method == http.MethodGet { @@ -88,6 +92,13 @@ var MockChartRepoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http. w.Write([]byte("{}")) return } + default: + if r.Method == http.MethodGet { + if strings.HasSuffix(r.RequestURI, "/index.yaml") { + w.Write([]byte(repo2IndexYaml)) + return + } + } } w.WriteHeader(http.StatusNotImplemented)