@@ -11,6 +11,7 @@ import (
11
11
v1 "k8s.io/api/core/v1"
12
12
networking "k8s.io/api/networking/v1beta1"
13
13
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14
15
"k8s.io/apimachinery/pkg/util/intstr"
15
16
16
17
"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
@@ -20,10 +21,11 @@ func TestGenerateNginxCfg(t *testing.T) {
20
21
cafeIngressEx := createCafeIngressEx ()
21
22
configParams := NewDefaultConfigParams ()
22
23
23
- expected := createExpectedConfigForCafeIngressEx ()
24
+ isPlus := false
25
+ expected := createExpectedConfigForCafeIngressEx (isPlus )
24
26
25
27
apRes := make (map [string ]string )
26
- result , warnings := generateNginxCfg (& cafeIngressEx , apRes , false , configParams , false , false , & StaticConfigParams {}, false )
28
+ result , warnings := generateNginxCfg (& cafeIngressEx , apRes , false , configParams , isPlus , false , & StaticConfigParams {}, false )
27
29
28
30
if diff := cmp .Diff (expected , result ); diff != "" {
29
31
t .Errorf ("generateNginxCfg() returned unexpected result (-want +got):\n %s" , diff )
@@ -48,7 +50,9 @@ func TestGenerateNginxCfgForJWT(t *testing.T) {
48
50
49
51
configParams := NewDefaultConfigParams ()
50
52
51
- expected := createExpectedConfigForCafeIngressEx ()
53
+ isPlus := true
54
+
55
+ expected := createExpectedConfigForCafeIngressEx (isPlus )
52
56
expected .Servers [0 ].JWTAuth = & version1.JWTAuth {
53
57
Key : "/etc/nginx/secrets/default-cafe-jwk" ,
54
58
Realm : "Cafe App" ,
@@ -63,7 +67,7 @@ func TestGenerateNginxCfgForJWT(t *testing.T) {
63
67
}
64
68
65
69
apRes := make (map [string ]string )
66
- result , warnings := generateNginxCfg (& cafeIngressEx , apRes , false , configParams , true , false , & StaticConfigParams {}, false )
70
+ result , warnings := generateNginxCfg (& cafeIngressEx , apRes , false , configParams , isPlus , false , & StaticConfigParams {}, false )
67
71
68
72
if ! reflect .DeepEqual (result .Servers [0 ].JWTAuth , expected .Servers [0 ].JWTAuth ) {
69
73
t .Errorf ("generateNginxCfg returned \n %v, but expected \n %v" , result .Servers [0 ].JWTAuth , expected .Servers [0 ].JWTAuth )
@@ -173,7 +177,7 @@ func TestGenerateIngressPath(t *testing.T) {
173
177
}
174
178
}
175
179
176
- func createExpectedConfigForCafeIngressEx () version1.IngressNginxConfig {
180
+ func createExpectedConfigForCafeIngressEx (isPlus bool ) version1.IngressNginxConfig {
177
181
coffeeUpstream := version1.Upstream {
178
182
Name : "default-cafe-ingress-cafe.example.com-coffee-svc-80" ,
179
183
LBMethod : "random two least_conn" ,
@@ -188,6 +192,15 @@ func createExpectedConfigForCafeIngressEx() version1.IngressNginxConfig {
188
192
},
189
193
},
190
194
}
195
+ if isPlus {
196
+ coffeeUpstream .UpstreamLabels = version1.UpstreamLabels {
197
+ Service : "coffee-svc" ,
198
+ ResourceType : "ingress" ,
199
+ ResourceName : "cafe-ingress" ,
200
+ ResourceNamespace : "default" ,
201
+ }
202
+ }
203
+
191
204
teaUpstream := version1.Upstream {
192
205
Name : "default-cafe-ingress-cafe.example.com-tea-svc-80" ,
193
206
LBMethod : "random two least_conn" ,
@@ -202,6 +215,15 @@ func createExpectedConfigForCafeIngressEx() version1.IngressNginxConfig {
202
215
},
203
216
},
204
217
}
218
+ if isPlus {
219
+ teaUpstream .UpstreamLabels = version1.UpstreamLabels {
220
+ Service : "tea-svc" ,
221
+ ResourceType : "ingress" ,
222
+ ResourceName : "cafe-ingress" ,
223
+ ResourceNamespace : "default" ,
224
+ }
225
+ }
226
+
205
227
expected := version1.IngressNginxConfig {
206
228
Upstreams : []version1.Upstream {
207
229
coffeeUpstream ,
@@ -324,12 +346,14 @@ func createCafeIngressEx() IngressEx {
324
346
325
347
func TestGenerateNginxCfgForMergeableIngresses (t * testing.T ) {
326
348
mergeableIngresses := createMergeableCafeIngress ()
327
- expected := createExpectedConfigForMergeableCafeIngress ()
349
+
350
+ isPlus := false
351
+ expected := createExpectedConfigForMergeableCafeIngress (isPlus )
328
352
329
353
configParams := NewDefaultConfigParams ()
330
354
331
355
masterApRes := make (map [string ]string )
332
- result , warnings := generateNginxCfgForMergeableIngresses (mergeableIngresses , masterApRes , configParams , false , false , & StaticConfigParams {}, false )
356
+ result , warnings := generateNginxCfgForMergeableIngresses (mergeableIngresses , masterApRes , configParams , isPlus , false , & StaticConfigParams {}, false )
333
357
334
358
if diff := cmp .Diff (expected , result ); diff != "" {
335
359
t .Errorf ("generateNginxCfgForMergeableIngresses() returned unexpected result (-want +got):\n %s" , diff )
@@ -388,7 +412,9 @@ func TestGenerateNginxCfgForMergeableIngressesForJWT(t *testing.T) {
388
412
Path : "/etc/nginx/secrets/default-coffee-jwk" ,
389
413
}
390
414
391
- expected := createExpectedConfigForMergeableCafeIngress ()
415
+ isPlus := true
416
+
417
+ expected := createExpectedConfigForMergeableCafeIngress (isPlus )
392
418
expected .Servers [0 ].JWTAuth = & version1.JWTAuth {
393
419
Key : "/etc/nginx/secrets/default-cafe-jwk" ,
394
420
Realm : "Cafe" ,
@@ -415,7 +441,6 @@ func TestGenerateNginxCfgForMergeableIngressesForJWT(t *testing.T) {
415
441
minionJwtKeyFileNames := make (map [string ]string )
416
442
minionJwtKeyFileNames [objectMetaToFileName (& mergeableIngresses .Minions [0 ].Ingress .ObjectMeta )] = "/etc/nginx/secrets/default-coffee-jwk"
417
443
configParams := NewDefaultConfigParams ()
418
- isPlus := true
419
444
420
445
masterApRes := make (map [string ]string )
421
446
result , warnings := generateNginxCfgForMergeableIngresses (mergeableIngresses , masterApRes , configParams , isPlus , false , & StaticConfigParams {}, false )
@@ -578,7 +603,7 @@ func createMergeableCafeIngress() *MergeableIngresses {
578
603
return mergeableIngresses
579
604
}
580
605
581
- func createExpectedConfigForMergeableCafeIngress () version1.IngressNginxConfig {
606
+ func createExpectedConfigForMergeableCafeIngress (isPlus bool ) version1.IngressNginxConfig {
582
607
coffeeUpstream := version1.Upstream {
583
608
9E72
Name : "default-cafe-ingress-coffee-minion-cafe.example.com-coffee-svc-80" ,
584
609
LBMethod : "random two least_conn" ,
@@ -593,6 +618,15 @@ func createExpectedConfigForMergeableCafeIngress() version1.IngressNginxConfig {
593
618
},
594
619
},
595
620
}
621
+ if isPlus {
622
+ coffeeUpstream .UpstreamLabels = version1.UpstreamLabels {
623
+ Service : "coffee-svc" ,
624
+ ResourceType : "ingress" ,
625
+ ResourceName : "cafe-ingress-coffee-minion" ,
626
+ ResourceNamespace : "default" ,
627
+ }
628
+ }
629
+
596
630
teaUpstream := version1.Upstream {
597
631
Name : "default-cafe-ingress-tea-minion-cafe.example.com-tea-svc-80" ,
598
632
LBMethod : "random two least_conn" ,
@@ -607,6 +641,15 @@ func createExpectedConfigForMergeableCafeIngress() version1.IngressNginxConfig {
607
641
},
608
642
},
609
643
}
644
+ if isPlus {
645
+ teaUpstream .UpstreamLabels = version1.UpstreamLabels {
646
+ Service : "tea-svc" ,
647
+ ResourceType : "ingress" ,
648
+ ResourceName : "cafe-ingress-tea-minion" ,
649
+ ResourceNamespace : "default" ,
650
+ }
651
+ }
652
+
610
653
expected := version1.IngressNginxConfig {
611
654
Upstreams : []version1.Upstream {
612
655
coffeeUpstream ,
@@ -786,14 +829,16 @@ func TestGenerateNginxCfgForSpiffe(t *testing.T) {
786
829
cafeIngressEx := createCafeIngressEx ()
787
830
configParams := NewDefaultConfigParams ()
788
831
789
- expected := createExpectedConfigForCafeIngressEx ()
832
+ isPlus := false
833
+
834
+ expected := createExpectedConfigForCafeIngressEx (isPlus )
790
835
expected .SpiffeClientCerts = true
791
836
for i := range expected .Servers [0 ].Locations {
792
837
expected .Servers [0 ].Locations [i ].SSL = true
793
838
}
794
839
795
840
apResources := make (map [string ]string )
796
- result , warnings := generateNginxCfg (& cafeIngressEx , apResources , false , configParams , false , false ,
841
+ result , warnings := generateNginxCfg (& cafeIngressEx , apResources , false , configParams , isPlus , false ,
797
842
& StaticConfigParams {NginxServiceMesh : true }, false )
798
843
799
844
if diff := cmp .Diff (expected , result ); diff != "" {
@@ -810,12 +855,14 @@ func TestGenerateNginxCfgForInternalRoute(t *testing.T) {
810
855
cafeIngressEx .Ingress .Annotations [internalRouteAnnotation ] = "true"
811
856
configParams := NewDefaultConfigParams ()
812
857
813
- expected := createExpectedConfigForCafeIngressEx ()
858
+ isPlus := false
859
+
860
+ expected := createExpectedConfigForCafeIngressEx (isPlus )
814
861
expected .Servers [0 ].SpiffeCerts = true
815
862
expected .Ingress .Annotations [internalRouteAnnotation ] = "true"
816
863
817
864
apResources := make (map [string ]string )
818
- result , warnings := generateNginxCfg (& cafeIngressEx , apResources , false , configParams , false , false ,
865
+ result , warnings := generateNginxCfg (& cafeIngressEx , apResources , false , configParams , isPlus , false ,
819
866
& StaticConfigParams {NginxServiceMesh : true , EnableInternalRoutes : true }, false )
820
867
821
868
if diff := cmp .Diff (expected , result ); diff != "" {
@@ -1251,3 +1298,99 @@ func TestGenerateJWTConfig(t *testing.T) {
1251
1298
}
1252
1299
}
1253
1300
}
1301
+
1302
+ func TestGenerateNginxCfgForAppProtect (t * testing.T ) {
1303
+ cafeIngressEx := createCafeIngressEx ()
1304
+ cafeIngressEx .Ingress .Annotations ["appprotect.f5.com/app-protect-enable" ] = "True"
1305
+ cafeIngressEx .Ingress .Annotations ["appprotect.f5.com/app-protect-security-log-enable" ] = "True"
1306
+ cafeIngressEx .AppProtectPolicy = & unstructured.Unstructured {
1307
+ Object : map [string ]interface {}{
1308
+ "metadata" : map [string ]interface {}{
1309
+ "namespace" : "default" ,
1310
+ "name" : "dataguard-alarm" ,
1311
+ },
1312
+ },
1313
+ }
1314
+ cafeIngressEx .AppProtectLogConf = & unstructured.Unstructured {
1315
+ Object : map [string ]interface {}{
1316
+ "metadata" : map [string ]interface {}{
1317
+ "namespace" : "default" ,
1318
+ "name" : "logconf" ,
1319
+ },
1320
+ },
1321
+ }
1322
+
1323
+ configParams := NewDefaultConfigParams ()
1324
+ apRes := map [string ]string {
1325
+ appProtectPolicyKey : "/etc/nginx/waf/nac-policies/default_dataguard-alarm" ,
1326
+ appProtectLogConfKey : "/etc/nginx/waf/nac-logconfs/default_logconf syslog:server=127.0.0.1:514" ,
1327
+ }
1328
+ staticCfgParams := & StaticConfigParams {
1329
+ MainAppProtectLoadModule : true ,
1330
+ }
1331
+
1332
+ isPlus := true
1333
+
1334
+ expected := createExpectedConfigForCafeIngressEx (isPlus )
1335
+ expected .Servers [0 ].AppProtectEnable = "on"
1336
+ expected .Servers [0 ].AppProtectPolicy = "/etc/nginx/waf/nac-policies/default_dataguard-alarm"
1337
+ expected .Servers [0 ].AppProtectLogConf = "/etc/nginx/waf/nac-logconfs/default_logconf syslog:server=127.0.0.1:514"
1338
+ expected .Servers [0 ].AppProtectLogEnable = "on"
1339
+ expected .Ingress .Annotations = cafeIngressEx .Ingress .Annotations
1340
+
1341
+ result , warnings := generateNginxCfg (& cafeIngressEx , apRes , false , configParams , isPlus , false , staticCfgParams , false )
1342
+ if diff := cmp .Diff (expected , result ); diff != "" {
1343
+ t .Errorf ("generateNginxCfg() returned unexpected result (-want +got):\n %s" , diff )
1344
+ }
1345
+ if len (warnings ) != 0 {
1346
+ t .Errorf ("generateNginxCfg() returned warnings: %v" , warnings )
1347
+ }
1348
+ }
1349
+
1350
+ func TestGenerateNginxCfgForMergeableIngressesForAppProtect (t * testing.T ) {
1351
+ mergeableIngresses := createMergeableCafeIngress ()
1352
+ mergeableIngresses .Master .Ingress .Annotations ["appprotect.f5.com/app-protect-enable" ] = "True"
1353
+ mergeableIngresses .Master .Ingress .Annotations ["appprotect.f5.com/app-protect-security-log-enable" ] = "True"
1354
+ mergeableIngresses .Master .AppProtectPolicy = & unstructured.Unstructured {
1355
+ Object : map [string ]interface {}{
1356
+ "metadata" : map [string ]interface {}{
1357
+ "namespace" : "default" ,
1358
+ "name" : "dataguard-alarm" ,
1359
+ },
1360
+ },
1361
+ }
1362
+ mergeableIngresses .Master .AppProtectLogConf = & unstructured.Unstructured {
1363
+ Object : map [string ]interface {}{
1364
+ "metadata" : map [string ]interface {}{
1365
+ "namespace" : "default" ,
1366
+ "name" : "logconf" ,
1367
+ },
1368
+ },
1369
+ }
1370
+
1371
+ configParams := NewDefaultConfigParams ()
1372
+ apRes := map [string ]string {
1373
+ appProtectPolicyKey : "/etc/nginx/waf/nac-policies/default_dataguard-alarm" ,
1374
+ appProtectLogConfKey : "/etc/nginx/waf/nac-logconfs/default_logconf syslog:server=127.0.0.1:514" ,
1375
+ }
1376
+ staticCfgParams := & StaticConfigParams {
1377
+ MainAppProtectLoadModule : true ,
1378
+ }
1379
+
1380
+ isPlus := true
1381
+
1382
+ expected := createExpectedConfigForMergeableCafeIngress (isPlus )
1383
+ expected .Servers [0 ].AppProtectEnable = "on"
1384
+ expected .Servers [0 ].AppProtectPolicy = "/etc/nginx/waf/nac-policies/default_dataguard-alarm"
1385
+ expected .Servers [0 ].AppProtectLogConf = "/etc/nginx/waf/nac-logconfs/default_logconf syslog:server=127.0.0.1:514"
1386
+ expected .Servers [0 ].AppProtectLogEnable = "on"
1387
+ expected .Ingress .Annotations = mergeableIngresses .Master .Ingress .Annotations
1388
+
1389
+ result , warnings := generateNginxCfgForMergeableIngresses (mergeableIngresses , apRes , configParams , isPlus , false , staticCfgParams , false )
1390
+ if diff := cmp .Diff (expected , result ); diff != "" {
1391
+ t .Errorf ("generateNginxCfgForMergeableIngresses() returned unexpected result (-want +got):\n %s" , diff )
1392
+ }
1393
+ if len (warnings ) != 0 {
1394
+ t .Errorf ("generateNginxCfgForMergeableIngresses() returned warnings: %v" , warnings )
1395
+ }
1396
+ }
0 commit comments