Skip to content

Commit 18f5ec7

Browse files
committed
Add webhook check to prevent CIDR duplication when configure multi rules filtering
Signed-off-by: Mohamed Mahmoud <mmahmoud@redhat.com>
1 parent c86c4a8 commit 18f5ec7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

apis/flowcollector/v1beta2/flowcollector_validation_webhook.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,15 @@ func (r *FlowCollector) validateAgent(_ context.Context, fc *FlowCollector) (adm
107107
}
108108
var errs []error
109109
if fc.Spec.Agent.EBPF.FlowFilter != nil && fc.Spec.Agent.EBPF.FlowFilter.Enable != nil && *fc.Spec.Agent.EBPF.FlowFilter.Enable {
110+
m := make(map[string]bool)
110111
for i := range fc.Spec.Agent.EBPF.FlowFilter.FlowFilterRules {
111-
errs = append(errs, validateFilter(&fc.Spec.Agent.EBPF.FlowFilter.FlowFilterRules[i])...)
112+
rule := fc.Spec.Agent.EBPF.FlowFilter.FlowFilterRules[i]
113+
if found := m[rule.CIDR]; found {
114+
errs = append(errs, fmt.Errorf("flow filter rule CIDR %s already exists", rule.CIDR))
115+
break
116+
}
117+
m[rule.CIDR] = true
118+
errs = append(errs, validateFilter(&rule)...)
112119
}
113120
errs = append(errs, validateFilter(fc.Spec.Agent.EBPF.FlowFilter)...)
114121
}

apis/flowcollector/v1beta2/flowcollector_validation_webhook_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestValidateAgent(t *testing.T) {
4949
Action: "Accept",
5050
CIDR: "0.0.0.0/0",
5151
Direction: "Egress",
52+
Protocol: "TCP",
5253
},
5354
},
5455
},
@@ -57,6 +58,42 @@ func TestValidateAgent(t *testing.T) {
5758
},
5859
},
5960
},
61+
{
62+
name: "Invalid filter with duplicate CIDR",
63+
fc: &FlowCollector{
64+
ObjectMeta: metav1.ObjectMeta{
65+
Name: "cluster",
66+
},
67+
Spec: FlowCollectorSpec{
68+
Agent: FlowCollectorAgent{
69+
Type: AgentEBPF,
70+
EBPF: FlowCollectorEBPF{
71+
Features: []AgentFeature{DNSTracking, FlowRTT, PacketDrop},
72+
Privileged: true,
73+
Sampling: ptr.To(int32(100)),
74+
FlowFilter: &EBPFFlowFilter{
75+
Enable: ptr.To(true),
76+
FlowFilterRules: []EBPFFlowFilterRule{
77+
{
78+
Action: "Accept",
79+
CIDR: "0.0.0.0/0",
80+
Direction: "Egress",
81+
Protocol: "TCP",
82+
},
83+
{
84+
Action: "Accept",
85+
CIDR: "0.0.0.0/0",
86+
Direction: "Egress",
87+
Protocol: "UDP",
88+
},
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
95+
expectedError: "flow filter rule CIDR 0.0.0.0/0 already exists",
96+
},
6097
{
6198
name: "PacketDrop without privilege triggers warning",
6299
fc: &FlowCollector{

0 commit comments

Comments
 (0)