-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
proxy_test.go
1405 lines (1209 loc) · 57.4 KB
/
proxy_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package dnsproxy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/netip"
"os"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/cilium/dns"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"sigs.k8s.io/yaml"
datapath "github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/fqdn/restore"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/ipcache"
v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/labels"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/policy/api"
"github.com/cilium/cilium/pkg/source"
"github.com/cilium/cilium/pkg/testutils"
testidentity "github.com/cilium/cilium/pkg/testutils/identity"
testipcache "github.com/cilium/cilium/pkg/testutils/ipcache"
"github.com/cilium/cilium/pkg/u8proto"
)
type DNSProxyTestSuite struct {
repo *policy.Repository
dnsTCPClient *dns.Client
dnsServer *dns.Server
proxy *DNSProxy
restoring bool
}
func setupDNSProxyTestSuite(tb testing.TB) *DNSProxyTestSuite {
testutils.PrivilegedTest(tb)
s := &DNSProxyTestSuite{}
// Add these identities
wg := &sync.WaitGroup{}
testSelectorCache.UpdateIdentities(identity.IdentityMap{
dstID1: labels.Labels{"Dst1": labels.NewLabel("Dst1", "test", labels.LabelSourceK8s)}.LabelArray(),
dstID2: labels.Labels{"Dst2": labels.NewLabel("Dst2", "test", labels.LabelSourceK8s)}.LabelArray(),
dstID3: labels.Labels{"Dst3": labels.NewLabel("Dst3", "test", labels.LabelSourceK8s)}.LabelArray(),
dstID4: labels.Labels{"Dst4": labels.NewLabel("Dst4", "test", labels.LabelSourceK8s)}.LabelArray(),
}, nil, wg)
wg.Wait()
s.repo = policy.NewPolicyRepository(nil, nil, nil)
s.dnsTCPClient = &dns.Client{Net: "tcp", Timeout: time.Second, SingleInflight: true}
s.dnsServer = setupServer(tb)
require.NotNil(tb, s.dnsServer, "unable to setup DNS server")
dnsProxyConfig := DNSProxyConfig{
Address: "",
Port: 0,
IPv4: true,
IPv6: true,
EnableDNSCompression: true,
MaxRestoreDNSIPs: 1000,
ConcurrencyLimit: 0,
ConcurrencyGracePeriod: 0,
}
proxy, err := StartDNSProxy(dnsProxyConfig, // any address, any port, enable ipv4, enable ipv6, enable compression, max 1000 restore IPs
// LookupEPByIP
func(ip netip.Addr) (*endpoint.Endpoint, error) {
if s.restoring {
return nil, fmt.Errorf("No EPs available when restoring")
}
return endpoint.NewTestEndpointWithState(tb, s, s, testipcache.NewMockIPCache(), &endpoint.FakeEndpointProxy{}, testidentity.NewMockIdentityAllocator(nil), uint16(epID1), endpoint.StateReady), nil
},
// LookupSecIDByIP
func(ip netip.Addr) (ipcache.Identity, bool) {
DNSServerListenerAddr := (s.dnsServer.Listener.Addr()).(*net.TCPAddr)
switch {
case ip.String() == DNSServerListenerAddr.IP.String():
ident := ipcache.Identity{
ID: dstID1,
Source: source.Unspec,
}
return ident, true
default:
ident := ipcache.Identity{
ID: dstID2,
Source: source.Unspec,
}
return ident, true
}
},
// LookupIPsBySecID
func(nid identity.NumericIdentity) []string {
DNSServerListenerAddr := (s.dnsServer.Listener.Addr()).(*net.TCPAddr)
switch nid {
case dstID1:
return []string{DNSServerListenerAddr.IP.String()}
case dstID2:
return []string{"127.0.0.1", "127.0.0.2"}
default:
return nil
}
},
// NotifyOnDNSMsg
func(lookupTime time.Time, ep *endpoint.Endpoint, epIPPort string, serverID identity.NumericIdentity, dstAddr string, msg *dns.Msg, protocol string, allowed bool, stat *ProxyRequestContext) error {
return nil
},
)
require.Nil(tb, err, "error starting DNS Proxy")
s.proxy = proxy
// This is here because Listener or Listeer.Addr() was nil. The
// lookupTargetDNSServer function doesn't need to change the target.
require.NotNil(tb, s.dnsServer.Listener, "DNS server missing a Listener")
DNSServerListenerAddr := (s.dnsServer.Listener.Addr()).(*net.TCPAddr)
require.NotNil(tb, DNSServerListenerAddr, "DNS server missing a Listener address")
s.proxy.lookupTargetDNSServer = func(w dns.ResponseWriter) (serverIP net.IP, serverPortProto restore.PortProto, addrStr string, err error) {
return DNSServerListenerAddr.IP, restore.MakeV2PortProto(uint16(DNSServerListenerAddr.Port), uint8(u8proto.UDP)), DNSServerListenerAddr.String(), nil
}
dstPortProto = restore.MakeV2PortProto(uint16(DNSServerListenerAddr.Port), udpProto)
tb.Cleanup(func() {
for epID := range s.proxy.allowed {
for pp := range s.proxy.allowed[epID] {
s.proxy.UpdateAllowed(epID, pp, nil)
}
}
for epID := range s.proxy.restored {
s.proxy.RemoveRestoredRules(uint16(epID))
}
if len(s.proxy.cache) > 0 {
tb.Error("cache not fully empty after removing all rules. Possible memory leak found.")
}
s.proxy.SetRejectReply(option.FQDNProxyDenyWithRefused)
s.dnsServer.Listener.Close()
for _, s := range s.proxy.DNSServers {
s.Shutdown()
}
})
return s
}
func (s *DNSProxyTestSuite) GetPolicyRepository() *policy.Repository {
return s.repo
}
func (s *DNSProxyTestSuite) GetProxyPort(string) (uint16, error) {
return 0, nil
}
func (s *DNSProxyTestSuite) QueueEndpointBuild(ctx context.Context, epID uint64) (func(), error) {
return nil, nil
}
func (s *DNSProxyTestSuite) GetCompilationLock() datapath.CompilationLock {
return nil
}
func (s *DNSProxyTestSuite) GetCIDRPrefixLengths() (s6, s4 []int) {
return nil, nil
}
func (s *DNSProxyTestSuite) SendNotification(msg monitorAPI.AgentNotifyMessage) error {
return nil
}
func (s *DNSProxyTestSuite) Datapath() datapath.Datapath {
return nil
}
func (s *DNSProxyTestSuite) GetDNSRules(epID uint16) restore.DNSRules {
return nil
}
func (s *DNSProxyTestSuite) RemoveRestoredDNSRules(epID uint16) {
}
func setupServer(tb testing.TB) (dnsServer *dns.Server) {
waitOnListen := make(chan struct{})
dnsServer = &dns.Server{Addr: ":0", Net: "tcp", NotifyStartedFunc: func() { close(waitOnListen) }}
go dnsServer.ListenAndServe()
dns.HandleFunc(".", serveDNS)
select {
case <-waitOnListen:
return dnsServer
case <-time.After(10 * time.Second):
tb.Error("DNS server did not start listening")
}
return nil
}
func serveDNS(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
retARR, err := dns.NewRR(m.Question[0].Name + " 60 IN A 1.1.1.1")
if err != nil {
panic(err)
}
m.Answer = append(m.Answer, retARR)
w.WriteMsg(m)
}
type DummySelectorCacheUser struct{}
func (d *DummySelectorCacheUser) IdentitySelectionUpdated(selector policy.CachedSelector, added, deleted []identity.NumericIdentity) {
}
// Setup identities, ports and endpoint IDs we will need
var (
cacheAllocator = cache.NewCachingIdentityAllocator(&testidentity.IdentityAllocatorOwnerMock{})
testSelectorCache = policy.NewSelectorCache(cacheAllocator.GetIdentityCache())
dummySelectorCacheUser = &DummySelectorCacheUser{}
DstID1Selector = api.NewESFromLabels(labels.ParseSelectLabel("k8s:Dst1=test"))
cachedDstID1Selector, _ = testSelectorCache.AddIdentitySelector(dummySelectorCacheUser, nil, DstID1Selector)
DstID2Selector = api.NewESFromLabels(labels.ParseSelectLabel("k8s:Dst2=test"))
cachedDstID2Selector, _ = testSelectorCache.AddIdentitySelector(dummySelectorCacheUser, nil, DstID2Selector)
DstID3Selector = api.NewESFromLabels(labels.ParseSelectLabel("k8s:Dst3=test"))
cachedDstID3Selector, _ = testSelectorCache.AddIdentitySelector(dummySelectorCacheUser, nil, DstID3Selector)
DstID4Selector = api.NewESFromLabels(labels.ParseSelectLabel("k8s:Dst4=test"))
cachedDstID4Selector, _ = testSelectorCache.AddIdentitySelector(dummySelectorCacheUser, nil, DstID4Selector)
cachedWildcardSelector, _ = testSelectorCache.AddIdentitySelector(dummySelectorCacheUser, nil, api.WildcardEndpointSelector)
epID1 = uint64(111)
epID2 = uint64(222)
epID3 = uint64(333)
dstID1 = identity.NumericIdentity(1001)
dstID2 = identity.NumericIdentity(2002)
dstID3 = identity.NumericIdentity(3003)
dstID4 = identity.NumericIdentity(4004)
dstPortProto = restore.MakeV2PortProto(53, udpProto) // Set below when we setup the server!
udpProtoPort53 = dstPortProto
udpProtoPort54 = restore.MakeV2PortProto(54, udpProto)
udpProtoPort8053 = restore.MakeV2PortProto(8053, udpProto)
tcpProtoPort53 = restore.MakeV2PortProto(53, tcpProto)
)
func TestRejectFromDifferentEndpoint(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := name
// Reject a query from not endpoint 1
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.NoError(t, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID2, dstPortProto, dstID1, nil, query)
require.NoError(t, err, "Error when checking allowed")
require.False(t, allowed, "request was not rejected when it should be blocked")
}
func TestAcceptFromMatchingEndpoint(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := name
// accept a query that matches from endpoint1
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.NoError(t, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.NoError(t, err, "Error when checking allowed")
require.True(t, allowed, "request was rejected when it should be allowed")
}
func TestAcceptNonRegex(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "simple.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := name
// accept a query that matches from endpoint1
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
}
func TestRejectNonRegex(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := "ciliumXio."
// reject a query for a non-regex where a . is different (i.e. ensure simple FQDNs treat . as .)
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was not rejected when it should be blocked")
}
func (s *DNSProxyTestSuite) requestRejectNonMatchingRefusedResponse(t *testing.T) *dns.Msg {
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := "notcilium.io."
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was not rejected when it should be blocked")
request := new(dns.Msg)
request.SetQuestion(query, dns.TypeA)
return request
}
func TestRejectNonMatchingRefusedResponseWithNameError(t *testing.T) {
s := setupDNSProxyTestSuite(t)
request := s.requestRejectNonMatchingRefusedResponse(t)
// reject a query with NXDomain
s.proxy.SetRejectReply(option.FQDNProxyDenyWithNameError)
response, _, err := s.dnsTCPClient.Exchange(request, s.proxy.DNSServers[0].Listener.Addr().String())
require.NoError(t, err, "DNS request from test client failed when it should succeed")
require.Equal(t, dns.RcodeNameError, response.Rcode, "DNS request from test client was not rejected when it should be blocked")
}
func TestRejectNonMatchingRefusedResponseWithRefused(t *testing.T) {
s := setupDNSProxyTestSuite(t)
request := s.requestRejectNonMatchingRefusedResponse(t)
// reject a query with Refused
s.proxy.SetRejectReply(option.FQDNProxyDenyWithRefused)
response, _, err := s.dnsTCPClient.Exchange(request, s.proxy.DNSServers[0].Listener.Addr().String())
require.NoError(t, err, "DNS request from test client failed when it should succeed")
require.Equal(t, dns.RcodeRefused, response.Rcode, "DNS request from test client was not rejected when it should be blocked")
}
func TestRespondViaCorrectProtocol(t *testing.T) {
s := setupDNSProxyTestSuite(t)
// Respond with an actual answer for the query. This also tests that the
// connection was forwarded via the correct protocol (tcp/udp) because we
// connet with TCP, and the server only listens on TCP.
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := name
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
request := new(dns.Msg)
request.SetQuestion(query, dns.TypeA)
response, rtt, err := s.dnsTCPClient.Exchange(request, s.proxy.DNSServers[0].Listener.Addr().String())
require.NoErrorf(t, err, "DNS request from test client failed when it should succeed (RTT: %v)", rtt)
require.Equal(t, 1, len(response.Answer), "Proxy returned incorrect number of answer RRs %s", response)
require.Equal(t, "cilium.io.\t60\tIN\tA\t1.1.1.1", response.Answer[0].String(), "Proxy returned incorrect RRs")
}
func TestRespondMixedCaseInRequestResponse(t *testing.T) {
s := setupDNSProxyTestSuite(t)
// Test that mixed case query is allowed out and then back in to support
// high-order-bit query uniqueing schemes (and a data exfiltration
// vector :( )
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := "CILIUM.io."
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
request := new(dns.Msg)
request.SetQuestion(query, dns.TypeA)
response, _, err := s.dnsTCPClient.Exchange(request, s.proxy.DNSServers[0].Listener.Addr().String())
require.NoError(t, err, "DNS request from test client failed when it should succeed")
require.Equal(t, 1, len(response.Answer), "Proxy returned incorrect number of answer RRs %s", response)
require.Equal(t, "CILIUM.io.\t60\tIN\tA\t1.1.1.1", response.Answer[0].String(), "Proxy returned incorrect RRs")
request.SetQuestion("ciliuM.io.", dns.TypeA)
response, _, err = s.dnsTCPClient.Exchange(request, s.proxy.DNSServers[0].Listener.Addr().String())
require.NoError(t, err, "DNS request from test client failed when it should succeed")
require.Equal(t, 1, len(response.Answer), "Proxy returned incorrect number of answer RRs %+v", response.Answer)
require.Equal(t, "ciliuM.io.\t60\tIN\tA\t1.1.1.1", response.Answer[0].String(), "Proxy returned incorrect RRs")
}
func TestCheckNoRules(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{},
},
}
query := name
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Error when inserting rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
l7map = policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{},
},
},
}
err = s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Error when inserting rules")
allowed, err = s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
}
func TestCheckAllowedTwiceRemovedOnce(t *testing.T) {
s := setupDNSProxyTestSuite(t)
name := "cilium.io."
l7map := policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{{MatchName: name}},
},
},
}
query := name
// Add the rule twice
err := s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
err = s.proxy.UpdateAllowed(epID1, dstPortProto, l7map)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err := s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Delete once, it should reject
err = s.proxy.UpdateAllowed(epID1, dstPortProto, nil)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err = s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Delete once, it should reject and not crash
err = s.proxy.UpdateAllowed(epID1, dstPortProto, nil)
require.Equal(t, nil, err, "Could not update with rules")
allowed, err = s.proxy.CheckAllowed(epID1, dstPortProto, dstID1, nil, query)
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
}
func TestFullPathDependence(t *testing.T) {
s := setupDNSProxyTestSuite(t)
// Test that we consider each of endpoint ID, destination SecID (via the
// selector in L7DataMap), destination port (set in the redirect itself) and
// the DNS name.
// The rules approximate:
// +------+--------+---------+----------+----------------+
// | From | To | DstPort | Protocol | DNSNames |
// +======+========+=========+===========================+
// | EP1 | DstID1 | 53 | UDP | *.ubuntu.com |
// | EP1 | DstID1 | 53 | TCP | sub.ubuntu.com |
// | EP1 | DstID1 | 53 | UDP | aws.amazon.com |
// | EP1 | DstID2 | 53 | UDP | cilium.io |
// | EP1 | * | 54 | UDP | example.com |
// | EP3 | DstID1 | 53 | UDP | example.com |
// | EP3 | DstID3 | 53 | UDP | * |
// | EP3 | DstID3 | 53 | TCP | example.com |
// | EP3 | DstID4 | 53 | UDP | nil |
// +------+--------+---------+---------------------------+
//
// Cases:
// +------+-------+--------+------+---------------------------+----------+----------------------------------------------------------------+
// | Case | From | To | Port | Protocol | Query | Outcome | Reason |
// +------+-------+--------+------+----------+----------------+----------+----------------------------------------------------------------+
// | 1 | EPID1 | DstID1 | 53 | UDP | www.ubuntu.com | Allowed | |
// | 2 | EPID1 | DstID1 | 53 | TCP | www.ubuntu.com | Rejected | Protocol TCP only allows "sub.ubuntu.com" |
// | 3 | EPID1 | DstID1 | 53 | TCP | sub.ubuntu.com | Allowed | |
// | 4 | EPID1 | DstID1 | 53 | UDP | sub.ubuntu.com | Allowed | |
// | 5 | EPID1 | DstID1 | 54 | UDP | cilium.io | Rejected | Port 54 only allows example.com |
// | 6 | EPID1 | DstID2 | 53 | UDP | cilium.io | Allowed | |
// | 7 | EPID1 | DstID2 | 53 | UDP | aws.amazon.com | Rejected | Only cilium.io is allowed with DstID2 |
// | 8 | EPID1 | DstID1 | 54 | UDP | example.com | Allowed | |
// | 9 | EPID2 | DstID1 | 53 | UDP | cilium.io | Rejected | EPID2 is not allowed as a source by any policy |
// | 10 | EPID3 | DstID1 | 53 | UDP | example.com | Allowed | |
// | 11 | EPID3 | DstID1 | 53 | UDP | aws.amazon.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com |
// | 12 | EPID3 | DstID1 | 54 | UDP | example.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com |
// | 13 | EPID3 | DstID2 | 53 | UDP | example.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com |
// | 14 | EPID3 | DstID3 | 53 | UDP | example.com | Allowed | Allowed due to wildcard match pattern |
// | 15 | EPID3 | DstID3 | 53 | TCP | example.com | Allowed | |
// | 16 | EPID3 | DstID3 | 53 | TCP | amazon.com | Rejected | TCP protocol only allows "example.com" |
// | 17 | EPID3 | DstID4 | 53 | TCP | example.com | Rejected | "example.com" only allowed for DstID3 |
// | 18 | EPID3 | DstID4 | 53 | UDP | example.com | Allowed | Allowed due to a nil rule |
// +------+-------+--------+------+----------------+----------+----------+----------------------------------------------------------------+
// Setup rules
// | EP1 | DstID1 | 53 | UDP | *.ubuntu.com |
// | EP1 | DstID1 | 53 | UDP | aws.amazon.com |
// | EP1 | DstID2 | 53 | UDP | cilium.io |
err := s.proxy.UpdateAllowed(epID1, udpProtoPort53, policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "*.ubuntu.com."},
{MatchPattern: "aws.amazon.com."},
},
},
},
cachedDstID2Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "cilium.io."},
},
},
},
})
require.Equal(t, nil, err, "Could not update with port 53 rules")
// | EP1 | DstID1 | 53 | TCP | sub.ubuntu.com |
err = s.proxy.UpdateAllowed(epID1, tcpProtoPort53, policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "sub.ubuntu.com."},
},
},
},
})
require.Equal(t, nil, err, "Could not update with rules")
// | EP1 | DstID1 | 54 | UDP | example.com |
err = s.proxy.UpdateAllowed(epID1, udpProtoPort54, policy.L7DataMap{
cachedWildcardSelector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "example.com."},
},
},
},
})
require.Equal(t, nil, err, "Could not update with rules")
// | EP3 | DstID1 | 53 | UDP | example.com |
// | EP3 | DstID3 | 53 | UDP | * |
// | EP3 | DstID4 | 53 | UDP | nil |
err = s.proxy.UpdateAllowed(epID3, udpProtoPort53, policy.L7DataMap{
cachedDstID1Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "example.com."},
},
},
},
cachedDstID3Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "*"},
},
},
},
cachedDstID4Selector: nil,
})
require.Equal(t, nil, err, "Could not update with rules")
// | EP3 | DstID3 | 53 | TCP | example.com |
err = s.proxy.UpdateAllowed(epID3, tcpProtoPort53, policy.L7DataMap{
cachedDstID3Selector: &policy.PerSelectorPolicy{
L7Rules: api.L7Rules{
DNS: []api.PortRuleDNS{
{MatchPattern: "example.com"},
},
},
},
})
require.Equal(t, nil, err, "Could not update with rules")
// Test cases
// Case 1 | EPID1 | DstID1 | 53 | UDP | www.ubuntu.com | Allowed
allowed, err := s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID1, nil, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 2 | EPID1 | DstID1 | 53 | TCP | www.ubuntu.com | Rejected | Protocol TCP only allows "sub.ubuntu.com"
allowed, err = s.proxy.CheckAllowed(epID1, tcpProtoPort53, dstID1, nil, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 3 | EPID1 | DstID1 | 53 | TCP | sub.ubuntu.com | Allowed
allowed, err = s.proxy.CheckAllowed(epID1, tcpProtoPort53, dstID1, nil, "sub.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 4 | EPID1 | DstID1 | 53 | UDP | sub.ubuntu.com | Allowed
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID1, nil, "sub.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 5 | EPID1 | DstID1 | 54 | UDP | cilium.io | Rejected | Port 54 only allows example.com
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, nil, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 6 | EPID1 | DstID2 | 53 | UDP | cilium.io | Allowed
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, nil, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 7 | EPID1 | DstID2 | 53 | UDP | aws.amazon.com | Rejected | Only cilium.io is allowed with DstID2
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, nil, "aws.amazon.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 8 | EPID1 | DstID1 | 54 | UDP | example.com | Allowed
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 9 | EPID2 | DstID1 | 53 | UDP | cilium.io | Rejected | EPID2 is not allowed as a source by any policy
allowed, err = s.proxy.CheckAllowed(epID2, udpProtoPort53, dstID1, nil, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 10 | EPID3 | DstID1 | 53 | UDP | example.com | Allowed
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort53, dstID1, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 11 | EPID3 | DstID1 | 53 | UDP | aws.amazon.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort53, dstID1, nil, "aws.amazon.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 12 | EPID3 | DstID1 | 54 | UDP | example.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort54, dstID1, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 13 | EPID3 | DstID2 | 53 | UDP | example.com | Rejected | EPID3 is only allowed to ask DstID1 on Port 53 for example.com
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort53, dstID2, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 14 | EPID3 | DstID3 | 53 | UDP | example.com | Allowed due to wildcard match pattern
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort53, dstID3, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 15 | EPID3 | DstID3 | 53 | TCP | example.com | Allowed
allowed, err = s.proxy.CheckAllowed(epID3, tcpProtoPort53, dstID3, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 16 | EPID3 | DstID3 | 53 | TCP | amazon.com | Rejected | TCP protocol only allows "example.com"
allowed, err = s.proxy.CheckAllowed(epID3, tcpProtoPort53, dstID3, nil, "amazon.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 17 | EPID3 | DstID4 | 53 | TCP | example.com | Rejected | "example.com" only allowed for DstID3
allowed, err = s.proxy.CheckAllowed(epID3, tcpProtoPort53, dstID4, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 18 | EPID3 | DstID4 | 53 | UDP | example.com | Allowed due to a nil rule
allowed, err = s.proxy.CheckAllowed(epID3, udpProtoPort53, dstID4, nil, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Get rules for restoration
expected1 := restore.DNSRules{
udpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][udpProtoPort53][cachedDstID1Selector], map[string]struct{}{"::": {}}),
asIPRule(s.proxy.allowed[epID1][udpProtoPort53][cachedDstID2Selector], map[string]struct{}{"127.0.0.1": {}, "127.0.0.2": {}}),
}.Sort(nil),
udpProtoPort54: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][udpProtoPort54][cachedWildcardSelector], nil),
},
tcpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][tcpProtoPort53][cachedDstID1Selector], map[string]struct{}{"::": {}}),
},
}
restored1, _ := s.proxy.GetRules(uint16(epID1))
restored1.Sort(nil)
require.EqualValues(t, expected1, restored1)
expected2 := restore.DNSRules{}
restored2, _ := s.proxy.GetRules(uint16(epID2))
restored2.Sort(nil)
require.EqualValues(t, expected2, restored2)
expected3 := restore.DNSRules{
udpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID3][udpProtoPort53][cachedDstID1Selector], map[string]struct{}{"::": {}}),
asIPRule(s.proxy.allowed[epID3][udpProtoPort53][cachedDstID3Selector], map[string]struct{}{}),
asIPRule(s.proxy.allowed[epID3][udpProtoPort53][cachedDstID4Selector], map[string]struct{}{}),
}.Sort(nil),
tcpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID3][tcpProtoPort53][cachedDstID3Selector], map[string]struct{}{}),
},
}
restored3, _ := s.proxy.GetRules(uint16(epID3))
restored3.Sort(nil)
require.EqualValues(t, expected3, restored3)
// Test with limited set of allowed IPs
oldUsed := s.proxy.usedServers
s.proxy.usedServers = map[string]struct{}{"127.0.0.2": {}}
expected1b := restore.DNSRules{
udpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][udpProtoPort53][cachedDstID1Selector], map[string]struct{}{}),
asIPRule(s.proxy.allowed[epID1][udpProtoPort53][cachedDstID2Selector], map[string]struct{}{"127.0.0.2": {}}),
}.Sort(nil),
udpProtoPort54: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][udpProtoPort54][cachedWildcardSelector], nil),
},
tcpProtoPort53: restore.IPRules{
asIPRule(s.proxy.allowed[epID1][tcpProtoPort53][cachedDstID1Selector], map[string]struct{}{}),
},
}
restored1b, _ := s.proxy.GetRules(uint16(epID1))
restored1b.Sort(nil)
require.EqualValues(t, expected1b, restored1b)
// unlimited again
s.proxy.usedServers = oldUsed
s.proxy.UpdateAllowed(epID1, udpProtoPort53, nil)
s.proxy.UpdateAllowed(epID1, udpProtoPort54, nil)
s.proxy.UpdateAllowed(epID1, tcpProtoPort53, nil)
_, exists := s.proxy.allowed[epID1]
require.Equal(t, false, exists)
_, exists = s.proxy.allowed[epID2]
require.Equal(t, false, exists)
s.proxy.UpdateAllowed(epID3, udpProtoPort53, nil)
s.proxy.UpdateAllowed(epID3, tcpProtoPort53, nil)
_, exists = s.proxy.allowed[epID3]
require.Equal(t, false, exists)
dstIP1 := (s.dnsServer.Listener.Addr()).(*net.TCPAddr).IP
dstIP2a := net.ParseIP("127.0.0.1")
dstIP2b := net.ParseIP("127.0.0.2")
dstIPrandom := net.ParseIP("127.0.0.42")
// Before restore: all rules removed above, everything is dropped
// Case 1 | EPID1 | DstID1 | 53 | UDP | www.ubuntu.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID1, dstIP1, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 2 | EPID1 | DstID1 | 54 | UDP | cilium.io | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, dstIP1, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 3 | EPID1 | DstID2 | 53 | UDP | cilium.io | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, dstIP2a, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 4 | EPID1 | DstID2 | 53 | UDP | aws.amazon.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, dstIP2b, "aws.amazon.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 5 | EPID1 | DstID1 | 54 | UDP | example.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, dstIP1, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Restore rules
ep1 := endpoint.NewTestEndpointWithState(t, s, s, testipcache.NewMockIPCache(), &endpoint.FakeEndpointProxy{}, testidentity.NewMockIdentityAllocator(nil), uint16(epID1), endpoint.StateReady)
ep1.DNSRulesV2 = restored1
s.proxy.RestoreRules(ep1)
_, exists = s.proxy.restored[epID1]
require.Equal(t, true, exists)
// Same tests with 2 (WORLD) dstID to make sure it is not used, but with correct destination IP
// Case 1 | EPID1 | dstIP1 | 53 | UDP | www.ubuntu.com | Allowed due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, 2, dstIP1, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 2 | EPID1 | dstIP1 | 54 | UDP | cilium.io | Rejected due to restored rules | Port 54 only allows example.com
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, 2, dstIP1, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 3 | EPID1 | dstIP2a | 53 | UDP | cilium.io | Allowed due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, 2, dstIP2a, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 4 | EPID1 | dstIP2b | 53 | UDP | aws.amazon.com | Rejected due to restored rules | Only cilium.io is allowed with DstID2
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, 2, dstIP2b, "aws.amazon.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 5 | EPID1 | dstIP1 | 54 | UDP | example.com | Allowed due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, 2, dstIP1, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// make sure random IP is not allowed
// Case 5 | EPID1 | random IP | 53 | UDP | example.com | Rejected due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, 2, dstIPrandom, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// make sure random destination IP is allowed in a wildcard selector
// Case 5 | EPID1 | random IP | 54 | UDP | example.com | Allowed due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, 2, dstIPrandom, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Restore rules for epID3
ep3 := endpoint.NewTestEndpointWithState(t, s, s, testipcache.NewMockIPCache(), &endpoint.FakeEndpointProxy{}, testidentity.NewMockIdentityAllocator(nil), uint16(epID3), endpoint.StateReady)
ep3.DNSRulesV2 = restored3
s.proxy.RestoreRules(ep3)
_, exists = s.proxy.restored[epID3]
require.Equal(t, true, exists)
// Set empty ruleset, check that restored rules were deleted in epID3
err = s.proxy.UpdateAllowed(epID3, udpProtoPort53, nil)
require.Equal(t, nil, err, "Could not update with rules")
_, exists = s.proxy.restored[epID3]
require.Equal(t, false, exists)
// epID1 still has restored rules
_, exists = s.proxy.restored[epID1]
require.Equal(t, true, exists)
// Marshal restored rules to JSON
jsn, err := json.Marshal(s.proxy.restored[epID1])
require.Equal(t, nil, err, "Could not marshal restored rules to json")
expected := `
{
"` + restore.MakeV2PortProto(53, tcpProto).String() + `":[{
"Re":"^(?:sub[.]ubuntu[.]com[.])$",
"IPs":{"::":{}}
}],
"` + restore.MakeV2PortProto(53, udpProto).String() + `":[{
"Re":"^(?:[-a-zA-Z0-9_]*[.]ubuntu[.]com[.]|aws[.]amazon[.]com[.])$",
"IPs":{"::":{}}
},{
"Re":"^(?:cilium[.]io[.])$",
"IPs":{"127.0.0.1":{},"127.0.0.2":{}}
}],
"` + restore.MakeV2PortProto(54, udpProto).String() + `":[{
"Re":"^(?:example[.]com[.])$",
"IPs":null
}]
}`
pretty := new(bytes.Buffer)
err = json.Compact(pretty, []byte(expected))
require.Equal(t, nil, err, "Could not compact expected json")
require.Equal(t, pretty.String(), string(jsn))
s.proxy.RemoveRestoredRules(uint16(epID1))
_, exists = s.proxy.restored[epID1]
require.Equal(t, false, exists)
// Before restore after marshal: previous restored rules are removed, everything is dropped
// Case 1 | EPID1 | DstID1 | 53 | UDP | www.ubuntu.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID1, dstIP1, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 2 | EPID1 | DstID1 | 54 | UDP | cilium.io | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, dstIP1, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 3 | EPID1 | DstID2 | 53 | UDP | cilium.io | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, dstIP2a, "cilium.io")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 4 | EPID1 | DstID2 | 53 | UDP | aws.amazon.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, dstID2, dstIP2b, "aws.amazon.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 5 | EPID1 | DstID1 | 54 | UDP | example.com | Rejected | No rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, dstID1, dstIP1, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Case 5 | EPID1 | random IP | 54 | UDP | example.com | Rejected
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort54, 2, dstIPrandom, "example.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, false, allowed, "request was allowed when it should be rejected")
// Restore Unmarshaled rules
var rules restore.DNSRules
err = json.Unmarshal(jsn, &rules)
rules = rules.Sort(nil)
require.Equal(t, nil, err, "Could not unmarshal restored rules from json")
require.EqualValues(t, expected1, rules)
// Marshal again & compare
// Marshal restored rules to JSON
jsn2, err := json.Marshal(rules)
require.Equal(t, nil, err, "Could not marshal restored rules to json")
require.Equal(t, pretty.String(), string(jsn2))
ep1.DNSRulesV2 = rules
s.proxy.RestoreRules(ep1)
_, exists = s.proxy.restored[epID1]
require.Equal(t, true, exists)
// After restoration of JSON marshaled/unmarshaled rules
// Case 1 | EPID1 | dstIP1 | 53 | UDP | www.ubuntu.com | Allowed due to restored rules
allowed, err = s.proxy.CheckAllowed(epID1, udpProtoPort53, 2, dstIP1, "www.ubuntu.com")
require.Equal(t, nil, err, "Error when checking allowed")
require.Equal(t, true, allowed, "request was rejected when it should be allowed")
// Case 2 | EPID1 | dstIP1 | 54 | UDP | cilium.io | Rejected due to restored rules | Port 54 only allows example.com