Skip to content

Commit

Permalink
Remove check and set header every time
Browse files Browse the repository at this point in the history
Signed-off-by: Nicol Draghici <[email protected]>
  • Loading branch information
nicoldr committed Aug 4, 2022
1 parent b740383 commit 17c406e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/api/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func basicAuthHandler(ctlr *Controller) mux.MiddlewareFunc {

return
}
if request.Header.Get("Authorization") == "" && checkAnonymousPolicyExists(ctlr.Config.AccessControl) {
if request.Header.Get("Authorization") == "" && anonymousPolicyExists(ctlr.Config.AccessControl) {
// Process request
next.ServeHTTP(response, request)

Expand All @@ -198,7 +198,7 @@ func basicAuthHandler(ctlr *Controller) mux.MiddlewareFunc {

// some client tools might send Authorization: Basic Og== (decoded into ":")
// empty username and password
if username == "" && passphrase == "" && checkAnonymousPolicyExists(ctlr.Config.AccessControl) {
if username == "" && passphrase == "" && anonymousPolicyExists(ctlr.Config.AccessControl) {
// Process request
next.ServeHTTP(response, request)

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func authzFail(w http.ResponseWriter, realm string, delay int) {
WriteJSON(w, http.StatusForbidden, NewErrorList(NewError(DENIED)))
}

func checkAnonymousPolicyExists(config *config.AccessControlConfig) bool {
func anonymousPolicyExists(config *config.AccessControlConfig) bool {
if config == nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (c *Controller) Run(reloadCtx context.Context) error {
if c.Config.HTTP.TLS.CACert != "" {
clientAuth := tls.VerifyClientCertIfGiven
if (c.Config.HTTP.Auth == nil || c.Config.HTTP.Auth.HTPasswd.Path == "") &&
!checkAnonymousPolicyExists(c.Config.AccessControl) {
!anonymousPolicyExists(c.Config.AccessControl) {
clientAuth = tls.RequireAndVerifyClientCert
}

Expand Down
12 changes: 5 additions & 7 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,11 @@ func (rh *RouteHandler) CheckVersionSupport(response http.ResponseWriter, reques
response.Header().Set(constants.DistAPIVersion, "registry/2.0")
// NOTE: compatibility workaround - return this header in "allowed-read" mode to allow for clients to
// work correctly
if checkAnonymousPolicyExists(rh.c.Config.AccessControl) {
if rh.c.Config.HTTP.Auth != nil {
if rh.c.Config.HTTP.Auth.Bearer != nil {
response.Header().Set("WWW-Authenticate", fmt.Sprintf("bearer realm=%s", rh.c.Config.HTTP.Auth.Bearer.Realm))
} else {
response.Header().Set("WWW-Authenticate", fmt.Sprintf("basic realm=%s", rh.c.Config.HTTP.Realm))
}
if rh.c.Config.HTTP.Auth != nil {
if rh.c.Config.HTTP.Auth.Bearer != nil {
response.Header().Set("WWW-Authenticate", fmt.Sprintf("bearer realm=%s", rh.c.Config.HTTP.Auth.Bearer.Realm))
} else {
response.Header().Set("WWW-Authenticate", fmt.Sprintf("basic realm=%s", rh.c.Config.HTTP.Realm))
}
}

Expand Down

0 comments on commit 17c406e

Please sign in to comment.