-
Notifications
You must be signed in to change notification settings - Fork 590
/
parser_test.go
5789 lines (5654 loc) · 144 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parser
import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
"time"
"github.com/hbagdi/go-kong/kong"
configurationv1 "github.com/kong/kubernetes-ingress-controller/internal/apis/configuration/v1"
configurationv1beta1 "github.com/kong/kubernetes-ingress-controller/internal/apis/configuration/v1beta1"
"github.com/kong/kubernetes-ingress-controller/internal/ingress/store"
"github.com/kong/kubernetes-ingress-controller/internal/ingress/utils"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
knative "knative.dev/serving/pkg/apis/networking/v1alpha1"
)
type TLSPair struct {
Key, Cert string
}
var (
tlsPairs = []TLSPair{
{
Cert: `-----BEGIN CERTIFICATE-----
MIIC2DCCAcACCQC32eFOsWpKojANBgkqhkiG9w0BAQsFADAuMRcwFQYDVQQDDA5z
ZWN1cmUtZm9vLWJhcjETMBEGA1UECgwKa29uZ2hxLm9yZzAeFw0xODEyMTgyMTI4
MDBaFw0xOTEyMTgyMTI4MDBaMC4xFzAVBgNVBAMMDnNlY3VyZS1mb28tYmFyMRMw
EQYDVQQKDAprb25naHEub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAqhl/HSwV6PbMv+cMFU9X+HuM7QbNNPh39GKa4pkxzFgiAnuuJ4jw9V/bzsEy
S+ZIyjzo+QKB1LzmgdcX4vkdI22BjxUd9HPHdZxtv3XilbNmSk9UOl2Hh1fORJoS
7YH+VbvVwiz5lo7qKRepbg/jcKkbs6AUE0YWFygtDLTvhP2qkphQkxZ0m8qroW91
CWgI73Ar6U2W/YQBRI3+LwtsKo0p2ASDijvqxElQBgBIiyGIr0RZc5pkCJ1eQdDB
2F6XaMfpeEyBj0MxypNL4S9HHfchOt55J1KOzYnUPkQnSoxp6oEjef4Q/ZCj5BRL
EGZnTb3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BZ+eBgUj8WB5g/8jGDAI0qzYnfAgiahjGwlEUtP
hJiPG/YFADw0m5b/8OMCZ6AXNhxjdweHniDxY2HE734Nwm9mG/7UbkdvhR05tqFh
G4KCViLH0cXt/TgW1sYB2o9Z
-----END CERTIFICATE-----`
)
func TestGlobalPlugin(t *testing.T) {
assert := assert.New(t)
t.Run("global plugins are processed correctly", func(t *testing.T) {
store, err := store.NewFakeStore(store.FakeObjects{
KongPlugins: []*configurationv1.KongPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-plugin",
Namespace: "default",
Labels: map[string]string{
"global": "true",
},
},
PluginName: "key-auth",
Protocols: []string{"grpc"},
Config: configurationv1.Configuration{
"foo": "bar",
},
},
},
KongClusterPlugins: []*configurationv1.KongClusterPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-plugin",
Labels: map[string]string{
"global": "true",
},
},
Protocols: []string{"http"},
PluginName: "basic-auth",
Config: configurationv1.Configuration{
"foo1": "bar1",
},
},
},
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(2, len(state.Plugins),
"expected one plugin to be rendered")
sort.SliceStable(state.Plugins, func(i, j int) bool {
return strings.Compare(*state.Plugins[i].Name, *state.Plugins[j].Name) > 0
})
assert.Equal("key-auth", *state.Plugins[0].Name)
assert.Equal(1, len(state.Plugins[0].Protocols))
assert.Equal(kong.Configuration{"foo": "bar"}, state.Plugins[0].Config)
assert.Equal("basic-auth", *state.Plugins[1].Name)
assert.Equal(kong.Configuration{"foo1": "bar1"}, state.Plugins[1].Config)
})
}
func TestSecretConfigurationPlugin(t *testing.T) {
jwtPluginConfig := `{"run_on_preflight": false}` // JSON
basicAuthPluginConfig := "hide_credentials: true" // YAML
assert := assert.New(t)
stock := store.FakeObjects{
Services: []*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-svc",
Namespace: "default",
Annotations: map[string]string{},
},
},
},
Ingresses: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Annotations: map[string]string{
"plugins.konghq.com": "foo-plugin",
},
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.com",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
Backend: networking.IngressBackend{
ServiceName: "foo-svc",
ServicePort: intstr.FromInt(80),
},
},
},
},
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "default",
Annotations: map[string]string{
"plugins.konghq.com": "bar-plugin",
},
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.net",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
Backend: networking.IngressBackend{
ServiceName: "foo-svc",
ServicePort: intstr.FromInt(80),
},
},
},
},
},
},
},
},
},
}}
t.Run("plugins with secret configuration are processed correctly",
func(t *testing.T) {
objects := stock
objects.KongPlugins = []*configurationv1.KongPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-foo-plugin",
Namespace: "default",
Labels: map[string]string{
"global": "true",
},
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "jwt-config",
Secret: "conf-secret",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-plugin",
Namespace: "default",
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "jwt-config",
Secret: "conf-secret",
},
},
},
}
objects.KongClusterPlugins = []*configurationv1.KongClusterPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-bar-plugin",
Labels: map[string]string{
"global": "true",
},
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "conf-secret",
Namespace: "default",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-plugin",
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "conf-secret",
Namespace: "default",
},
},
},
}
objects.Secrets = []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "conf-secret",
Namespace: "default",
},
Data: map[string][]byte{
"jwt-config": []byte(jwtPluginConfig),
"basic-auth-config": []byte(basicAuthPluginConfig),
},
},
}
store, err := store.NewFakeStore(objects)
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(4, len(state.Plugins),
"expected four plugins to be rendered")
sort.SliceStable(state.Plugins, func(i, j int) bool {
return strings.Compare(*state.Plugins[i].Name,
*state.Plugins[j].Name) > 0
})
assert.Equal("jwt", *state.Plugins[0].Name)
assert.Equal(kong.Configuration{"run_on_preflight": false},
state.Plugins[0].Config)
assert.Equal("jwt", *state.Plugins[1].Name)
assert.Equal(kong.Configuration{"run_on_preflight": false},
state.Plugins[1].Config)
assert.Equal("basic-auth", *state.Plugins[2].Name)
assert.Equal(kong.Configuration{"hide_credentials": true},
state.Plugins[2].Config)
assert.Equal("basic-auth", *state.Plugins[3].Name)
assert.Equal(kong.Configuration{"hide_credentials": true},
state.Plugins[3].Config)
})
t.Run("plugins with missing secrets or keys are not constructed",
func(t *testing.T) {
objects := stock
objects.KongPlugins = []*configurationv1.KongPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-foo-plugin",
Namespace: "default",
Labels: map[string]string{
"global": "true",
},
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "missing-key",
Secret: "conf-secret",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-plugin",
Namespace: "default",
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "missing-key",
Secret: "conf-secret",
},
},
},
}
objects.KongClusterPlugins = []*configurationv1.KongClusterPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-bar-plugin",
Labels: map[string]string{
"global": "true",
},
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "missing-secret",
Namespace: "default",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-plugin",
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "missing-secret",
Namespace: "default",
},
},
},
}
objects.Secrets = []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "conf-secret",
Namespace: "default",
},
Data: map[string][]byte{
"jwt-config": []byte(jwtPluginConfig),
"basic-auth-config": []byte(basicAuthPluginConfig),
},
},
}
store, err := store.NewFakeStore(objects)
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(0, len(state.Plugins),
"expected no plugins to be rendered")
})
t.Run("plugins with both config and configFrom are not constructed",
func(t *testing.T) {
objects := stock
objects.KongPlugins = []*configurationv1.KongPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-foo-plugin",
Namespace: "default",
Labels: map[string]string{
"global": "true",
},
},
PluginName: "jwt",
Config: configurationv1.Configuration{"fake": true},
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "jwt-config",
Secret: "conf-secret",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-plugin",
Namespace: "default",
},
PluginName: "jwt",
Config: configurationv1.Configuration{"fake": true},
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "jwt-config",
Secret: "conf-secret",
},
},
},
}
objects.KongClusterPlugins = []*configurationv1.KongClusterPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-bar-plugin",
Labels: map[string]string{
"global": "true",
},
},
Protocols: []string{"http"},
PluginName: "basic-auth",
Config: configurationv1.Configuration{"fake": true},
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "conf-secret",
Namespace: "default",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-plugin",
},
Protocols: []string{"http"},
PluginName: "basic-auth",
Config: configurationv1.Configuration{"fake": true},
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "conf-secret",
Namespace: "default",
},
},
},
}
objects.Secrets = []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "conf-secret",
Namespace: "default",
},
Data: map[string][]byte{
"jwt-config": []byte(jwtPluginConfig),
"basic-auth-config": []byte(basicAuthPluginConfig),
},
},
}
store, err := store.NewFakeStore(objects)
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(0, len(state.Plugins),
"expected no plugins to be rendered")
})
t.Run("secretToConfiguration handles valid configuration and "+
"discards invalid configuration", func(t *testing.T) {
objects := stock
jwtPluginConfig := `{"run_on_preflight": false}` // JSON
basicAuthPluginConfig := "hide_credentials: true" // YAML
badJwtPluginConfig := "22222" // not JSON
badBasicAuthPluginConfig := "111111" // not YAML
objects.Secrets = []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "conf-secret",
Namespace: "default",
},
Data: map[string][]byte{
"jwt-config": []byte(jwtPluginConfig),
"basic-auth-config": []byte(basicAuthPluginConfig),
"bad-jwt-config": []byte(badJwtPluginConfig),
"bad-basic-auth-config": []byte(badBasicAuthPluginConfig),
},
},
}
references := []*configurationv1.SecretValueFromSource{
{
Secret: "conf-secret",
Key: "jwt-config",
},
{
Secret: "conf-secret",
Key: "basic-auth-config",
},
}
badReferences := []*configurationv1.SecretValueFromSource{
{
Secret: "conf-secret",
Key: "bad-basic-auth-config",
},
{
Secret: "conf-secret",
Key: "bad-jwt-config",
},
}
store, err := store.NewFakeStore(objects)
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
for _, testcase := range references {
config, err := parser.secretToConfiguration(*testcase, "default")
assert.NotEmpty(config)
assert.Nil(err)
}
for _, testcase := range badReferences {
config, err := parser.secretToConfiguration(*testcase, "default")
assert.Empty(config)
assert.NotEmpty(err)
}
})
t.Run("plugins with unparsable configuration are not constructed",
func(t *testing.T) {
jwtPluginConfig := "22222" // not JSON
basicAuthPluginConfig := "111111" // not YAML
objects := stock
objects.KongPlugins = []*configurationv1.KongPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-foo-plugin",
Namespace: "default",
Labels: map[string]string{
"global": "true",
},
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "missing-key",
Secret: "conf-secret",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-plugin",
Namespace: "default",
},
PluginName: "jwt",
ConfigFrom: configurationv1.ConfigSource{
SecretValue: configurationv1.SecretValueFromSource{
Key: "missing-key",
Secret: "conf-secret",
},
},
},
}
objects.KongClusterPlugins = []*configurationv1.KongClusterPlugin{
{
ObjectMeta: metav1.ObjectMeta{
Name: "global-bar-plugin",
Labels: map[string]string{
"global": "true",
},
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "missing-secret",
Namespace: "default",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-plugin",
},
Protocols: []string{"http"},
PluginName: "basic-auth",
ConfigFrom: configurationv1.NamespacedConfigSource{
SecretValue: configurationv1.NamespacedSecretValueFromSource{
Key: "basic-auth-config",
Secret: "missing-secret",
Namespace: "default",
},
},
},
}
objects.Secrets = []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "conf-secret",
Namespace: "default",
},
Data: map[string][]byte{
"jwt-config": []byte(jwtPluginConfig),
"basic-auth-config": []byte(basicAuthPluginConfig),
},
},
}
store, err := store.NewFakeStore(objects)
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(0, len(state.Plugins),
"expected no plugins to be rendered")
})
}
func TestCACertificate(t *testing.T) {
assert := assert.New(t)
t.Run("valid CACertificte is processed", func(t *testing.T) {
secrets := []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
"id": []byte("8214a145-a328-4c56-ab72-2973a56d4eae"),
"cert": []byte(caCert1),
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
Secrets: secrets,
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(1, len(state.CACertificates))
assert.Equal(kong.CACertificate{
ID: kong.String("8214a145-a328-4c56-ab72-2973a56d4eae"),
Cert: kong.String(caCert1),
}, state.CACertificates[0])
})
t.Run("multiple CACertifictes are processed", func(t *testing.T) {
secrets := []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
"id": []byte("8214a145-a328-4c56-ab72-2973a56d4eae"),
"cert": []byte(caCert1),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "non-default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
"id": []byte("570c28aa-e784-43c1-8ec7-ae7f4ce40189"),
"cert": []byte(caCert2),
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
Secrets: secrets,
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(2, len(state.CACertificates))
})
t.Run("invalid CACertifictes are ignored", func(t *testing.T) {
secrets := []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
"id": []byte("8214a145-a328-4c56-ab72-2973a56d4eae"),
"cert": []byte(caCert1),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "non-default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
"id": []byte("570c28aa-e784-43c1-8ec7-ae7f4ce40189"),
// cert is missing
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "non-default",
Labels: map[string]string{
"konghq.com/ca-cert": "true",
},
},
Data: map[string][]byte{
// id is missing
"cert": []byte(caCert2),
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
Secrets: secrets,
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(1, len(state.CACertificates))
assert.Equal(kong.CACertificate{
ID: kong.String("8214a145-a328-4c56-ab72-2973a56d4eae"),
Cert: kong.String(caCert1),
}, state.CACertificates[0])
})
}
func TestServiceClientCertificate(t *testing.T) {
assert := assert.New(t)
t.Run("valid client-cert annotation", func(t *testing.T) {
ingresses := []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.com",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
Backend: networking.IngressBackend{
ServiceName: "foo-svc",
ServicePort: intstr.FromInt(80),
},
},
},
},
},
},
},
TLS: []networking.IngressTLS{
{
SecretName: "secret1",
Hosts: []string{"foo.com"},
},
},
},
},
}
secrets := []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("7428fb98-180b-4702-a91f-61351a33c6e4"),
Name: "secret1",
Namespace: "default",
},
Data: map[string][]byte{
"tls.crt": []byte(tlsPairs[0].Cert),
"tls.key": []byte(tlsPairs[0].Key),
},
},
}
services := []*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-svc",
Namespace: "default",
Annotations: map[string]string{
"configuration.konghq.com/client-cert": "secret1",
},
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
Ingresses: ingresses,
Secrets: secrets,
Services: services,
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(1, len(state.Certificates),
"expected one certificates to be rendered")
assert.Equal("7428fb98-180b-4702-a91f-61351a33c6e4",
*state.Certificates[0].ID)
assert.Equal(1, len(state.Services))
assert.Equal("7428fb98-180b-4702-a91f-61351a33c6e4",
*state.Services[0].ClientCertificate.ID)
})
t.Run("client-cert secret doesn't exist", func(t *testing.T) {
ingresses := []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.com",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
Backend: networking.IngressBackend{
ServiceName: "foo-svc",
ServicePort: intstr.FromInt(80),
},
},
},
},
},
},
},
TLS: []networking.IngressTLS{
{
SecretName: "secret1",
Hosts: []string{"foo.com"},
},
},
},
},
}
services := []*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-svc",
Namespace: "default",
Annotations: map[string]string{
"configuration.konghq.com/client-cert": "secret1",
},
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
Ingresses: ingresses,
Services: services,
})
assert.Nil(err)
parser := New(store)
state, err := parser.Build()
assert.Nil(err)
assert.NotNil(state)
assert.Equal(0, len(state.Certificates),
"expected no certificates to be rendered")
assert.Equal(1, len(state.Services))
assert.Nil(state.Services[0].ClientCertificate)
})
}
func TestKongRouteAnnotations(t *testing.T) {
assert := assert.New(t)
t.Run("strip-path annotation is correctly processed (true)", func(t *testing.T) {
ingresses := []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "default",