Merge pull request #10967 from ywk253100/200307_auth_header

Only set "Www-Authenticate" header for registry API
This commit is contained in:
Wenkai Yin(尹文开) 2020-03-09 11:58:09 +08:00 committed by GitHub
commit 52c6d354d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 4 deletions

View File

@ -59,9 +59,6 @@ func SendError(w http.ResponseWriter, err error) {
// only log the error whose status code < 500 when debugging to avoid log flooding // only log the error whose status code < 500 when debugging to avoid log flooding
log.Debug(errPayload) log.Debug(errPayload)
} }
if statusCode == http.StatusUnauthorized {
w.Header().Set("Www-Authenticate", `Basic realm="harbor"`)
}
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
fmt.Fprintln(w, errPayload) fmt.Fprintln(w, errPayload)
} }

View File

@ -31,7 +31,6 @@ func TestSendError(t *testing.T) {
SendError(rw, err) SendError(rw, err)
assert.Equal(t, http.StatusUnauthorized, rw.Code) assert.Equal(t, http.StatusUnauthorized, rw.Code)
assert.Equal(t, `{"errors":[{"code":"UNAUTHORIZED","message":"unauthorized"}]}`+"\n", rw.Body.String()) assert.Equal(t, `{"errors":[{"code":"UNAUTHORIZED","message":"unauthorized"}]}`+"\n", rw.Body.String())
assert.Equal(t, `Basic realm="harbor"`, rw.Header().Get("Www-Authenticate"))
// internal server error // internal server error
rw = httptest.NewRecorder() rw = httptest.NewRecorder()

View File

@ -132,6 +132,7 @@ func Middleware() func(http.Handler) http.Handler {
if err := checker.check(req); err != nil { if err := checker.check(req); err != nil {
// the header is needed for "docker manifest" commands: https://github.com/docker/cli/issues/989 // the header is needed for "docker manifest" commands: https://github.com/docker/cli/issues/989
rw.Header().Set("Docker-Distribution-Api-Version", "registry/2.0") rw.Header().Set("Docker-Distribution-Api-Version", "registry/2.0")
rw.Header().Set("Www-Authenticate", `Basic realm="harbor"`)
serror.SendError(rw, ierror.UnauthorizedError(err).WithMessage(err.Error())) serror.SendError(rw, ierror.UnauthorizedError(err).WithMessage(err.Error()))
return return
} }