@@ -55,7 +55,8 @@ func AuthHandler(ctlr *Controller) mux.MiddlewareFunc {
55
55
log : ctlr .Log ,
56
56
}
57
57
58
- if ctlr .Config .IsBearerAuthEnabled () {
58
+ authConfig := ctlr .Config .GetAuthConfig ()
59
+ if authConfig .IsBearerAuthEnabled () {
59
60
return bearerAuthHandler (ctlr )
60
61
}
61
62
@@ -103,6 +104,12 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
103
104
) (bool , error ) {
104
105
cookieStore := ctlr .CookieStore
105
106
107
+ // Get auth config once to avoid multiple calls
108
+ authConfig := ctlr .Config .GetAuthConfig ()
109
+ if authConfig == nil {
110
+ return false , nil
111
+ }
112
+
106
113
identity , passphrase , err := getUsernamePasswordBasicAuth (request )
107
114
if err != nil {
108
115
ctlr .Log .Error ().Err (err ).Msg ("failed to parse authorization header" )
@@ -116,7 +123,8 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
116
123
// Process request
117
124
var groups []string
118
125
119
- if ctlr .Config .HTTP .AccessControl != nil {
126
+ accessControl := ctlr .Config .GetAccessControlConfig ()
127
+ if accessControl != nil {
120
128
ac := NewAccessController (ctlr .Config )
121
129
groups = ac .getUserGroups (identity )
122
130
}
@@ -145,13 +153,14 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
145
153
}
146
154
147
155
// 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 () {
149
157
ok , _ , ldapgroups , err := amw .ldapClient .Authenticate (identity , passphrase )
150
158
if ok && err == nil {
151
159
// Process request
152
160
var groups []string
153
161
154
- if ctlr .Config .HTTP .AccessControl != nil {
162
+ accessControl := ctlr .Config .GetAccessControlConfig ()
163
+ if accessControl != nil {
155
164
ac := NewAccessController (ctlr .Config )
156
165
groups = ac .getUserGroups (identity )
157
166
}
@@ -181,7 +190,7 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
181
190
}
182
191
183
192
// last try API keys
184
- if ctlr . Config .IsAPIKeyEnabled () {
193
+ if authConfig .IsAPIKeyEnabled () {
185
194
apiKey := passphrase
186
195
187
196
if ! strings .HasPrefix (apiKey , constants .APIKeysPrefix ) {
@@ -248,16 +257,22 @@ func (amw *AuthnMiddleware) basicAuthn(ctlr *Controller, userAc *reqCtx.UserAcce
248
257
}
249
258
250
259
func (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
+
251
266
// no password based authN, if neither LDAP nor HTTP BASIC is enabled
252
- if ! ctlr . Config .IsBasicAuthnEnabled () {
267
+ if ! authConfig .IsBasicAuthnEnabled () {
253
268
return noPasswdAuth (ctlr )
254
269
}
255
270
256
- delay := ctlr . Config . HTTP . Auth . FailDelay
271
+ delay := authConfig . GetFailDelay ()
257
272
258
273
// 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
261
276
262
277
ctlr .LDAPClient = & LDAPClient {
263
278
Host : ldapConfig .Address ,
@@ -278,17 +293,17 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
278
293
279
294
amw .ldapClient = ctlr .LDAPClient
280
295
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 )
283
298
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 ).
285
300
Msg ("failed to read caCert" )
286
301
}
287
302
288
303
caCertPool := x509 .NewCertPool ()
289
304
290
305
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 ).
292
307
Msg ("failed to read caCert" )
293
308
}
294
309
@@ -297,34 +312,34 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
297
312
// default to system cert pool
298
313
caCertPool , err := x509 .SystemCertPool ()
299
314
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 ).
301
316
Msg ("failed to get system cert pool" )
302
317
}
303
318
304
319
amw .ldapClient .ClientCAs = caCertPool
305
320
}
306
321
}
307
322
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 )
310
325
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 ).
312
327
Msg ("failed to open creds-file" )
313
328
}
314
329
}
315
330
316
331
// openid based authN
317
- if ctlr . Config .IsOpenIDAuthEnabled () {
332
+ if authConfig .IsOpenIDAuthEnabled () {
318
333
ctlr .RelyingParties = make (map [string ]rp.RelyingParty )
319
334
320
- for provider := range ctlr . Config . HTTP . Auth .OpenID .Providers {
335
+ for provider := range authConfig .OpenID .Providers {
321
336
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 )
324
339
ctlr .RelyingParties [provider ] = rp
325
340
} 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 )
328
343
ctlr .RelyingParties [provider ] = rp
329
344
}
330
345
}
@@ -340,7 +355,10 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
340
355
}
341
356
342
357
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 ()
344
362
345
363
// build user access control info
346
364
userAc := reqCtx .NewUserAccessControl ()
@@ -370,7 +388,9 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
370
388
if errors .Is (err , zerr .ErrUserDataNotFound ) {
371
389
ctlr .Log .Err (err ).Msg ("failed to find user profile in DB" )
372
390
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 )
374
394
}
375
395
376
396
response .WriteHeader (http .StatusInternalServerError )
@@ -397,22 +417,27 @@ func (amw *AuthnMiddleware) tryAuthnHandlers(ctlr *Controller) mux.MiddlewareFun
397
417
return
398
418
}
399
419
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 )
401
423
})
402
424
}
403
425
}
404
426
405
427
func bearerAuthHandler (ctlr * Controller ) mux.MiddlewareFunc {
428
+ // Get auth config safely
429
+ authConfig := ctlr .Config .GetAuthConfig ()
430
+
406
431
// although the configuration option is called 'cert', this function will also parse a public key directly
407
432
// 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 )
409
434
if err != nil {
410
435
ctlr .Log .Panic ().Err (err ).Msg ("failed to load public key for bearer authentication" )
411
436
}
412
437
413
438
authorizer := NewBearerAuthorizer (
414
- ctlr . Config . HTTP . Auth .Bearer .Realm ,
415
- ctlr . Config . HTTP . Auth .Bearer .Service ,
439
+ authConfig .Bearer .Realm ,
440
+ authConfig .Bearer .Service ,
416
441
publicKey ,
417
442
)
418
443
@@ -509,7 +534,12 @@ func noPasswdAuth(ctlr *Controller) mux.MiddlewareFunc {
509
534
}
510
535
511
536
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 )
513
543
514
544
return
515
545
}
0 commit comments