@@ -55,7 +55,8 @@ func AuthHandler(ctlr *Controller) mux.MiddlewareFunc {
5555 log : ctlr .Log ,
5656 }
5757
58- if ctlr .Config .IsBearerAuthEnabled () {
58+ authConfig := ctlr .Config .GetAuthConfig ()
59+ if authConfig .IsBearerAuthEnabled () {
5960 return bearerAuthHandler (ctlr )
6061 }
6162
@@ -103,6 +104,12 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
103104) (bool , error ) {
104105 cookieStore := ctlr .CookieStore
105106
107+ // Get auth config once to avoid multiple calls
108+ authConfig := ctlr .Config .GetAuthConfig ()
109+ if authConfig == nil {
110+ return false , nil
111+ }
112+
106113 identity , passphrase , err := getUsernamePasswordBasicAuth (request )
107114 if err != nil {
108115 ctlr .Log .Error ().Err (err ).Msg ("failed to parse authorization header" )
@@ -116,7 +123,8 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
116123 // Process request
117124 var groups []string
118125
119- if ctlr .Config .HTTP .AccessControl != nil {
126+ accessControl := ctlr .Config .GetAccessControlConfig ()
127+ if accessControl != nil {
120128 ac := NewAccessController (ctlr .Config )
121129 groups = ac .getUserGroups (identity )
122130 }
@@ -145,13 +153,14 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
145153 }
146154
147155 // next, LDAP if configured (network-based which can lose connectivity)
148- if ctlr . Config . HTTP . Auth != nil && ctlr . Config . HTTP . Auth . LDAP != nil {
156+ if authConfig . IsLdapAuthEnabled () {
149157 ok , _ , ldapgroups , err := amw .ldapClient .Authenticate (identity , passphrase )
150158 if ok && err == nil {
151159 // Process request
152160 var groups []string
153161
154- if ctlr .Config .HTTP .AccessControl != nil {
162+ accessControl := ctlr .Config .GetAccessControlConfig ()
163+ if accessControl != nil {
155164 ac := NewAccessController (ctlr .Config )
156165 groups = ac .getUserGroups (identity )
157166 }
@@ -181,7 +190,7 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
181190 }
182191
183192 // last try API keys
184- if ctlr . Config .IsAPIKeyEnabled () {
193+ if authConfig .IsAPIKeyEnabled () {
185194 apiKey := passphrase
186195
187196 if ! strings .HasPrefix (apiKey , constants .APIKeysPrefix ) {
@@ -248,16 +257,22 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
248257}
249258
250259func (amw * AuthnMiddleware ) tryAuthnHandlers (ctlr * Controller ) mux.MiddlewareFunc { //nolint: gocyclo
260+ // Get auth config once to avoid multiple calls
261+ authConfig := ctlr .Config .GetAuthConfig ()
262+ if authConfig == nil {
263+ return noPasswdAuth (ctlr )
264+ }
265+
251266 // no password based authN, if neither LDAP nor HTTP BASIC is enabled
252- if ! ctlr . Config .IsBasicAuthnEnabled () {
267+ if ! authConfig .IsBasicAuthnEnabled () {
253268 return noPasswdAuth (ctlr )
254269 }
255270
256- delay := ctlr . Config . HTTP . Auth . FailDelay
271+ delay := authConfig . GetFailDelay ()
257272
258273 // ldap and htpasswd based authN
259- if ctlr . Config .IsLdapAuthEnabled () {
260- ldapConfig := ctlr . Config . HTTP . Auth .LDAP
274+ if authConfig .IsLdapAuthEnabled () {
275+ ldapConfig := authConfig .LDAP
261276
262277 ctlr .LDAPClient = & LDAPClient {
263278 Host : ldapConfig .Address ,
@@ -278,17 +293,17 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
278293
279294 amw .ldapClient = ctlr .LDAPClient
280295
281- if ctlr . Config . HTTP . Auth .LDAP .CACert != "" {
282- caCert , err := os .ReadFile (ctlr . Config . HTTP . Auth .LDAP .CACert )
296+ if authConfig .LDAP .CACert != "" {
297+ caCert , err := os .ReadFile (authConfig .LDAP .CACert )
283298 if err != nil {
284- amw .log .Panic ().Err (err ).Str ("caCert" , ctlr . Config . HTTP . Auth .LDAP .CACert ).
299+ amw .log .Panic ().Err (err ).Str ("caCert" , authConfig .LDAP .CACert ).
285300 Msg ("failed to read caCert" )
286301 }
287302
288303 caCertPool := x509 .NewCertPool ()
289304
290305 if ! caCertPool .AppendCertsFromPEM (caCert ) {
291- amw .log .Panic ().Err (zerr .ErrBadCACert ).Str ("caCert" , ctlr . Config . HTTP . Auth .LDAP .CACert ).
306+ amw .log .Panic ().Err (zerr .ErrBadCACert ).Str ("caCert" , authConfig .LDAP .CACert ).
292307 Msg ("failed to read caCert" )
293308 }
294309
@@ -297,34 +312,34 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
297312 // default to system cert pool
298313 caCertPool , err := x509 .SystemCertPool ()
299314 if err != nil {
300- amw .log .Panic ().Err (zerr .ErrBadCACert ).Str ("caCert" , ctlr . Config . HTTP . Auth .LDAP .CACert ).
315+ amw .log .Panic ().Err (zerr .ErrBadCACert ).Str ("caCert" , authConfig .LDAP .CACert ).
301316 Msg ("failed to get system cert pool" )
302317 }
303318
304319 amw .ldapClient .ClientCAs = caCertPool
305320 }
306321 }
307322
308- if ctlr . Config .IsHtpasswdAuthEnabled () {
309- err := amw .htpasswd .Reload (ctlr . Config . HTTP . Auth .HTPasswd .Path )
323+ if authConfig .IsHtpasswdAuthEnabled () {
324+ err := amw .htpasswd .Reload (authConfig .HTPasswd .Path )
310325 if err != nil {
311- amw .log .Panic ().Err (err ).Str ("credsFile" , ctlr . Config . HTTP . Auth .HTPasswd .Path ).
326+ amw .log .Panic ().Err (err ).Str ("credsFile" , authConfig .HTPasswd .Path ).
312327 Msg ("failed to open creds-file" )
313328 }
314329 }
315330
316331 // openid based authN
317- if ctlr . Config .IsOpenIDAuthEnabled () {
332+ if authConfig .IsOpenIDAuthEnabled () {
318333 ctlr .RelyingParties = make (map [string ]rp.RelyingParty )
319334
320- for provider := range ctlr . Config . HTTP . Auth .OpenID .Providers {
335+ for provider := range authConfig .OpenID .Providers {
321336 if config .IsOpenIDSupported (provider ) {
322- rp := NewRelyingPartyOIDC (context .TODO (), ctlr .Config , provider , ctlr . Config . HTTP . Auth .SessionHashKey ,
323- ctlr . Config . HTTP . Auth .SessionEncryptKey , ctlr .Log )
337+ rp := NewRelyingPartyOIDC (context .TODO (), ctlr .Config , provider , authConfig .SessionHashKey ,
338+ authConfig .SessionEncryptKey , ctlr .Log )
324339 ctlr .RelyingParties [provider ] = rp
325340 } else if config .IsOauth2Supported (provider ) {
326- rp := NewRelyingPartyGithub (ctlr .Config , provider , ctlr . Config . HTTP . Auth .SessionHashKey ,
327- ctlr . Config . HTTP . Auth .SessionEncryptKey , ctlr .Log )
341+ rp := NewRelyingPartyGithub (ctlr .Config , provider , authConfig .SessionHashKey ,
342+ authConfig .SessionEncryptKey , ctlr .Log )
328343 ctlr .RelyingParties [provider ] = rp
329344 }
330345 }
@@ -340,7 +355,10 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
340355 }
341356
342357 isMgmtRequested := request .RequestURI == constants .FullMgmt
343- allowAnonymous := ctlr .Config .HTTP .AccessControl .AnonymousPolicyExists ()
358+
359+ // Get access control config safely
360+ accessControlConfig := ctlr .Config .GetAccessControlConfig ()
361+ allowAnonymous := accessControlConfig != nil && accessControlConfig .AnonymousPolicyExists ()
344362
345363 // build user access control info
346364 userAc := reqCtx .NewUserAccessControl ()
@@ -370,7 +388,9 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
370388 if errors .Is (err , zerr .ErrUserDataNotFound ) {
371389 ctlr .Log .Err (err ).Msg ("failed to find user profile in DB" )
372390
373- authFail (response , request , ctlr .Config .HTTP .Realm , delay )
391+ // Get HTTP config safely for realm
392+ httpConfig := ctlr .Config .GetHTTPConfig ()
393+ authFail (response , request , httpConfig .Realm , delay )
374394 }
375395
376396 response .WriteHeader (http .StatusInternalServerError )
@@ -397,22 +417,27 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
397417 return
398418 }
399419
400- authFail (response , request , ctlr .Config .HTTP .Realm , delay )
420+ // Get HTTP config safely for realm
421+ httpConfig := ctlr .Config .GetHTTPConfig ()
422+ authFail (response , request , httpConfig .Realm , delay )
401423 })
402424 }
403425}
404426
405427func bearerAuthHandler (ctlr * Controller ) mux.MiddlewareFunc {
428+ // Get auth config safely
429+ authConfig := ctlr .Config .GetAuthConfig ()
430+
406431 // although the configuration option is called 'cert', this function will also parse a public key directly
407432 // see https://github.com/project-zot/zot/issues/3173 for info
408- publicKey , err := loadPublicKeyFromFile (ctlr . Config . HTTP . Auth .Bearer .Cert )
433+ publicKey , err := loadPublicKeyFromFile (authConfig .Bearer .Cert )
409434 if err != nil {
410435 ctlr .Log .Panic ().Err (err ).Msg ("failed to load public key for bearer authentication" )
411436 }
412437
413438 authorizer := NewBearerAuthorizer (
414- ctlr . Config . HTTP . Auth .Bearer .Realm ,
415- ctlr . Config . HTTP . Auth .Bearer .Service ,
439+ authConfig .Bearer .Realm ,
440+ authConfig .Bearer .Service ,
416441 publicKey ,
417442 )
418443
@@ -509,7 +534,12 @@ func noPasswdAuth(ctlr *Controller) mux.MiddlewareFunc {
509534 }
510535
511536 if ctlr .Config .IsMTLSAuthEnabled () && userAc .IsAnonymous () {
512- authFail (response , request , ctlr .Config .HTTP .Realm , ctlr .Config .HTTP .Auth .FailDelay )
537+ // Get HTTP config safely for realm and fail delay
538+ httpConfig := ctlr .Config .GetHTTPConfig ()
539+ authConfig := ctlr .Config .GetAuthConfig ()
540+ failDelay := authConfig .GetFailDelay ()
541+
542+ authFail (response , request , httpConfig .Realm , failDelay )
513543
514544 return
515545 }
0 commit comments