Return basic auth challenge for /v2/_catalog (#12956)

Fixes #12192

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
Daniel Jiang 2020-09-03 11:31:57 +08:00 committed by GitHub
parent f4938ca966
commit 66c3fa9eec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -16,6 +16,7 @@ package v2auth
import ( import (
"fmt" "fmt"
"github.com/goharbor/harbor/src/lib"
lib_http "github.com/goharbor/harbor/src/lib/http" lib_http "github.com/goharbor/harbor/src/lib/http"
"net/http" "net/http"
"net/url" "net/url"
@ -84,8 +85,9 @@ func (rc *reqChecker) projectID(name string) (int64, error) {
func getChallenge(req *http.Request, accessList []access) string { func getChallenge(req *http.Request, accessList []access) string {
logger := log.G(req.Context()) logger := log.G(req.Context())
auth := req.Header.Get(authHeader) auth := req.Header.Get(authHeader)
if len(auth) > 0 { if len(auth) > 0 ||
// Return basic auth challenge by default len(lib.V2CatalogURLRe.FindStringSubmatch(req.URL.Path)) == 1 {
// Return basic auth challenge by default, incl. request to '/v2/_catalog'
return `Basic realm="harbor"` return `Basic realm="harbor"`
} }
// No auth header, treat it as CLI and redirect to token service // No auth header, treat it as CLI and redirect to token service

View File

@ -262,7 +262,7 @@ func TestGetChallenge(t *testing.T) {
}, },
{ {
request: req2, request: req2,
challenge: `Bearer realm="https://harbor.test/service/token",service="harbor-registry"`, challenge: `Basic realm="harbor"`,
}, },
{ {
request: req2x, request: req2x,