@@ -298,6 +298,246 @@ func TestParseConfigMapAccessLogDefault(t *testing.T) {
298298 }
299299}
300300
301+ func TestParseConfigMapOIDC (t * testing.T ) {
302+ t .Parallel ()
303+ tests := []struct {
304+ configMap * v1.ConfigMap
305+ want * OIDC
306+ msg string
307+ }{
308+ {
309+ configMap : & v1.ConfigMap {
310+ Data : map [string ]string {},
311+ },
312+ want : & OIDC {
313+ PKCETimeout : "90s" ,
314+ IDTokenTimeout : "1h" ,
315+ AccessTimeout : "1h" ,
316+ RefreshTimeout : "8h" ,
317+ SIDSTimeout : "8h" ,
318+ },
319+ msg : "default OIDC values" ,
320+ },
321+ {
322+ configMap : & v1.ConfigMap {
323+ Data : map [string ]string {
324+ "oidc-pkce-timeout" : "5m" ,
325+ "oidc-id-tokens-timeout" : "2h" ,
326+ "oidc-access-tokens-timeout" : "3h" ,
327+ "oidc-refresh-tokens-timeout" : "48h" ,
328+ "oidc-sids-timeout" : "72h" ,
329+ },
330+ },
331+ want : & OIDC {
332+ PKCETimeout : "5m" ,
333+ IDTokenTimeout : "2h" ,
334+ AccessTimeout : "3h" ,
335+ RefreshTimeout : "48h" ,
336+ SIDSTimeout : "72h" ,
337+ },
338+ msg : "all timeout values custom" ,
339+ },
340+ {
341+ configMap : & v1.ConfigMap {
342+ Data : map [string ]string {
343+ "oidc-pkce-timeout" : "15m" ,
344+ },
345+ },
346+ want : & OIDC {
347+ PKCETimeout : "15m" ,
348+ },
349+ msg : "custom PKCE timeout only" ,
350+ },
351+ {
352+ configMap : & v1.ConfigMap {
353+ Data : map [string ]string {
354+ "oidc-id-tokens-timeout" : "90m" ,
355+ },
356+ },
357+ want : & OIDC {
358+ IDTokenTimeout : "90m" ,
359+ },
360+ msg : "custom ID token timeout only" ,
361+ },
362+ {
363+ configMap : & v1.ConfigMap {
364+ Data : map [string ]string {
365+ "oidc-access-tokens-timeout" : "4h" ,
366+ },
367+ },
368+ want : & OIDC {
369+ AccessTimeout : "4h" ,
370+ },
371+ msg : "custom access token timeout only" ,
372+ },
373+ {
374+ configMap : & v1.ConfigMap {
375+ Data : map [string ]string {
376+ "oidc-refresh-tokens-timeout" : "16h" ,
377+ },
378+ },
379+ want : & OIDC {
380+ RefreshTimeout : "16h" ,
381+ },
382+ msg : "custom refresh token timeout only" ,
383+ },
384+ {
385+ configMap : & v1.ConfigMap {
386+ Data : map [string ]string {
387+ "oidc-sids-timeout" : "12h" ,
388+ },
389+ },
390+ want : & OIDC {
391+ SIDSTimeout : "12h" ,
392+ },
393+ msg : "custom SIDS timeout only" ,
394+ },
395+ }
396+
397+ nginxPlus := true
398+ hasAppProtect := false
399+ hasAppProtectDos := false
400+ hasTLSPassthrough := false
401+ directiveAutoadjustEnabled := false
402+
403+ for _ , test := range tests {
404+ t .Run (test .msg , func (t * testing.T ) {
405+ result , configOk := ParseConfigMap (context .Background (), test .configMap , nginxPlus , hasAppProtect , hasAppProtectDos , hasTLSPassthrough , directiveAutoadjustEnabled , makeEventLogger ())
406+ if ! configOk {
407+ t .Error ("want configOk true, got configOk false" )
408+ }
409+
410+ // Check only the specific fields that are set in the test expectation
411+ if test .want .PKCETimeout != "" {
412+ assert .Equal (t , test .want .PKCETimeout , result .OIDC .PKCETimeout )
413+ }
414+ if test .want .IDTokenTimeout != "" {
415+ assert .Equal (t , test .want .IDTokenTimeout , result .OIDC .IDTokenTimeout )
416+ }
417+ if test .want .AccessTimeout != "" {
418+ assert .Equal (t , test .want .AccessTimeout , result .OIDC .AccessTimeout )
419+ }
420+ if test .want .RefreshTimeout != "" {
421+ assert .Equal (t , test .want .RefreshTimeout , result .OIDC .RefreshTimeout )
422+ }
423+ if test .want .SIDSTimeout != "" {
424+ assert .Equal (t , test .want .SIDSTimeout , result .OIDC .SIDSTimeout )
425+ }
426+ })
427+ }
428+ }
429+
430+ func TestParseConfigMapOIDCErrors (t * testing.T ) {
431+ t .Parallel ()
432+ tests := []struct {
433+ configMap * v1.ConfigMap
434+ expectedErr bool
435+ msg string
436+ }{
437+ {
438+ configMap : & v1.ConfigMap {
439+ Data : map [string ]string {
440+ "oidc-pkce-timeout" : "invalid-time" ,
441+ },
442+ },
443+ expectedErr : true ,
444+ msg : "invalid PKCE timeout format" ,
445+ },
446+ {
447+ configMap : & v1.ConfigMap {
448+ Data : map [string ]string {
449+ "oidc-id-tokens-timeout" : "abc123" ,
450+ },
451+ },
452+ expectedErr : true ,
453+ msg : "invalid ID token timeout format" ,
454+ },
455+ {
456+ configMap : & v1.ConfigMap {
457+ Data : map [string ]string {
458+ "oidc-access-tokens-timeout" : "5x" ,
459+ },
460+ },
461+ expectedErr : true ,
462+ msg : "invalid access token timeout format" ,
463+ },
464+ {
465+ configMap : & v1.ConfigMap {
466+ Data : map [string ]string {
467+ "oidc-refresh-tokens-timeout" : "" ,
468+ },
469+ },
470+ expectedErr : true ,
471+ msg : "empty refresh token timeout" ,
472+ },
473+ {
474+ configMap : & v1.ConfigMap {
475+ Data : map [string ]string {
476+ "oidc-sids-timeout" : " " ,
477+ },
478+ },
479+ expectedErr : true ,
480+ msg : "whitespace-only SIDS timeout" ,
481+ },
482+ {
483+ configMap : & v1.ConfigMap {
484+ Data : map [string ]string {
485+ "oidc-pkce-timeout" : "-5m" ,
486+ },
487+ },
488+ expectedErr : true ,
489+ msg : "negative PKCE timeout" ,
490+ },
491+ {
492+ configMap : & v1.ConfigMap {
493+ Data : map [string ]string {
494+ "oidc-id-tokens-timeout" : "1.5h" ,
495+ },
496+ },
497+ expectedErr : true ,
498+ msg : "decimal in ID token timeout" ,
499+ },
500+ {
501+ configMap : & v1.ConfigMap {
502+ Data : map [string ]string {
503+ "oidc-access-tokens-timeout" : "5minutes" ,
504+ },
505+ },
506+ expectedErr : true ,
507+ msg : "invalid time unit format" ,
508+ },
509+
510+ {
511+ configMap : & v1.ConfigMap {
512+ Data : map [string ]string {
513+ "oidc-sids-timeout" : "5s 10m" ,
514+ },
515+ },
516+ expectedErr : true ,
517+ msg : "multiple time values without proper format" ,
518+ },
519+ }
520+
521+ nginxPlus := true
522+ hasAppProtect := false
523+ hasAppProtectDos := false
524+ hasTLSPassthrough := false
525+ directiveAutoadjustEnabled := false
526+
527+ for _ , test := range tests {
528+ t .Run (test .msg , func (t * testing.T ) {
529+ _ , configOk := ParseConfigMap (context .Background (), test .configMap , nginxPlus , hasAppProtect , hasAppProtectDos , hasTLSPassthrough , directiveAutoadjustEnabled , makeEventLogger ())
530+
531+ if test .expectedErr && configOk {
532+ t .Errorf ("want configOk false, got configOk true for %s" , test .msg )
533+ }
534+ if ! test .expectedErr && ! configOk {
535+ t .Errorf ("want configOk true, got configOk false for %s" , test .msg )
536+ }
537+ })
538+ }
539+ }
540+
301541func TestParseMGMTConfigMapError (t * testing.T ) {
302542 t .Parallel ()
303543 tests := []struct {
0 commit comments