@@ -13,6 +13,7 @@ import (
1313
1414 "github.com/google/go-cmp/cmp"
1515 "github.com/google/go-cmp/cmp/cmpopts"
16+ "github.com/nginx/kubernetes-ingress/internal/configs/version1"
1617 "github.com/nginx/kubernetes-ingress/internal/configs/version2"
1718 "github.com/nginx/kubernetes-ingress/internal/k8s/secrets"
1819 nl "github.com/nginx/kubernetes-ingress/internal/logger"
@@ -24,6 +25,7 @@ import (
2425 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526 "k8s.io/apimachinery/pkg/runtime"
2627 "k8s.io/apimachinery/pkg/util/intstr"
28+ "k8s.io/client-go/tools/record"
2729)
2830
2931func createPointerFromBool(b bool) *bool {
@@ -21963,6 +21965,105 @@ func TestGenerateTimeWithDefault(t *testing.T) {
2196321965 }
2196421966}
2196521967
21968+ // TestOIDCTimeoutInMainConfig verifies that OIDC timeout values from ConfigMap appear in generated nginx.conf
21969+ func TestOIDCTimeoutInMainConfig(t *testing.T) {
21970+ t.Parallel()
21971+
21972+ tests := []struct {
21973+ name string
21974+ configMapData map[string]string
21975+ expectedTimeouts map[string]string
21976+ expectedDirectives []string
21977+ }{
21978+ {
21979+ name: "default timeouts",
21980+ configMapData: map[string]string{
21981+ "zone-sync": "true",
21982+ },
21983+ expectedTimeouts: map[string]string{
21984+ "PKCETimeout": "90s",
21985+ "IDTokenTimeout": "1h",
21986+ "AccessTimeout": "1h",
21987+ "RefreshTimeout": "8h",
21988+ "SIDSTimeout": "8h",
21989+ },
21990+ expectedDirectives: []string{
21991+ "keyval_zone zone=oidc_pkce:128K timeout=90s sync;",
21992+ "keyval_zone zone=oidc_id_tokens:1M timeout=1h sync;",
21993+ "keyval_zone zone=oidc_access_tokens:1M timeout=1h sync;",
21994+ "keyval_zone zone=refresh_tokens:1M timeout=8h sync;",
21995+ "keyval_zone zone=oidc_sids:1M timeout=8h sync;",
21996+ "include oidc/oidc_common.conf;",
21997+ },
21998+ },
21999+ {
22000+ name: "custom timeouts",
22001+ configMapData: map[string]string{
22002+ "zone-sync": "true",
22003+ "oidc-pkce-timeout": "2m",
22004+ "oidc-id-tokens-timeout": "2h",
22005+ "oidc-access-tokens-timeout": "30m",
22006+ "oidc-refresh-tokens-timeout": "1h",
22007+ "oidc-sids-timeout": "120s",
22008+ },
22009+ expectedTimeouts: map[string]string{
22010+ "PKCETimeout": "2m",
22011+ "IDTokenTimeout": "2h",
22012+ "AccessTimeout": "30m",
22013+ "RefreshTimeout": "1h",
22014+ "SIDSTimeout": "120s",
22015+ },
22016+ expectedDirectives: []string{
22017+ "keyval_zone zone=oidc_pkce:128K timeout=2m sync;",
22018+ "keyval_zone zone=oidc_id_tokens:1M timeout=2h sync;",
22019+ "keyval_zone zone=oidc_access_tokens:1M timeout=30m sync;",
22020+ "keyval_zone zone=refresh_tokens:1M timeout=1h sync;",
22021+ "keyval_zone zone=oidc_sids:1M timeout=120s sync;",
22022+ "include oidc/oidc_common.conf;",
22023+ },
22024+ },
22025+ }
22026+
22027+ for _, test := range tests {
22028+ t.Run(test.name, func(t *testing.T) {
22029+ // Parse ConfigMap
22030+ configMap := &api_v1.ConfigMap{Data: test.configMapData}
22031+ configParams, configOk := ParseConfigMap(context.Background(), configMap, true, false, false, false, false, record.NewFakeRecorder(1024))
22032+ if !configOk {
22033+ t.Error("expected configOk true, got configOk false ")
22034+ }
22035+
22036+ vsc := newVirtualServerConfigurator(&baseCfgParams, false, false, &StaticConfigParams{}, false, &fakeBV)
22037+ _, warnings := vsc.GenerateVirtualServerConfig(&virtualServerExWithOIDCTimeout, nil, nil)
22038+ if len(warnings) != 0 {
22039+ t.Errorf("Unexpected warnings: %v", warnings)
22040+ }
22041+
22042+ mainConfig := GenerateNginxMainConfig(&StaticConfigParams{}, configParams, &MGMTConfigParams{})
22043+ mainConfig.OIDC.Enable = true
22044+
22045+ templateExecutor, err := version1.NewTemplateExecutor("version1/nginx-plus.tmpl", "version1/nginx-plus.ingress.tmpl")
22046+ if err != nil {
22047+ t.Fatalf("Failed to create template executor: %v", err)
22048+ }
22049+
22050+ nginxConfigContent, err := templateExecutor.ExecuteMainConfigTemplate(mainConfig)
22051+ if err != nil {
22052+ t.Fatalf("Failed to execute template: %v", err)
22053+ }
22054+
22055+ configString := string(nginxConfigContent)
22056+ t.Logf("Generated nginx.conf snippet:\n%s", configString)
22057+
22058+ for _, directive := range test.expectedDirectives {
22059+ if !strings.Contains(configString, directive) {
22060+ t.Errorf("Expected directive not found: %s", directive)
22061+ }
22062+ }
22063+ })
22064+ }
22065+ }
22066+
2196622067var (
2196722068 l = slog.New(nic_glog.New(io.Discard, &nic_glog.Options{Level: levels.LevelInfo}))
2196822069 ctx = nl.ContextWithLogger(context.Background(), l)
@@ -21977,6 +22078,73 @@ var (
2197722078 RealIPRecursive: true,
2197822079 }
2197922080
22081+ virtualServerExWithOIDCTimeout = VirtualServerEx{
22082+ VirtualServer: &conf_v1.VirtualServer{
22083+ ObjectMeta: meta_v1.ObjectMeta{
22084+ Name: "oidc-app",
22085+ Namespace: "default",
22086+ },
22087+ Spec: conf_v1.VirtualServerSpec{
22088+ Host: "app.example.com",
22089+ Upstreams: []conf_v1.Upstream{
22090+ {
22091+ Name: "app",
22092+ Service: "app-svc",
22093+ Port: 80,
22094+ },
22095+ },
22096+ Routes: []conf_v1.Route{
22097+ {
22098+ Path: "/",
22099+ Policies: []conf_v1.PolicyReference{
22100+ {
22101+ Name: "oidc-policy",
22102+ Namespace: "default",
22103+ },
22104+ },
22105+ Action: &conf_v1.Action{
22106+ Pass: "app",
22107+ },
22108+ },
22109+ },
22110+ },
22111+ },
22112+ Policies: map[string]*conf_v1.Policy{
22113+ "default/oidc-policy": {
22114+ ObjectMeta: meta_v1.ObjectMeta{
22115+ Name: "oidc-policy",
22116+ Namespace: "default",
22117+ },
22118+ Spec: conf_v1.PolicySpec{
22119+ OIDC: &conf_v1.OIDC{
22120+ AuthEndpoint: "https://auth.example.com/auth",
22121+ TokenEndpoint: "https://auth.example.com/token",
22122+ JWKSURI: "https://auth.example.com/jwks",
22123+ ClientID: "test-client-id",
22124+ ClientSecret: "oidc-secret",
22125+ Scope: "openid profile email",
22126+ RedirectURI: "/redirect",
22127+ ZoneSyncLeeway: createPointerFromInt(20),
22128+ AccessTokenEnable: true,
22129+ EndSessionEndpoint: "https://auth.example.com/logout",
22130+ PostLogoutRedirectURI: "/_logout",
22131+ },
22132+ },
22133+ },
22134+ },
22135+ SecretRefs: map[string]*secrets.SecretReference{
22136+ "default/oidc-secret": {
22137+ Secret: &api_v1.Secret{
22138+ Type: secrets.SecretTypeOIDC,
22139+ Data: map[string][]byte{
22140+ "client-secret": []byte("super-secret-value"),
22141+ },
22142+ },
22143+ Path: "/etc/nginx/secrets/default_oidc-secret",
22144+ },
22145+ },
22146+ }
22147+
2198022148 virtualServerExWithGunzipOn = VirtualServerEx{
2198122149 VirtualServer: &conf_v1.VirtualServer{
2198222150 ObjectMeta: meta_v1.ObjectMeta{
0 commit comments