Skip to content

Commit c8e48b8

Browse files
pippolo84christarazi
authored andcommitted
policy: Reject CNP when ExceptCIDRs is used with CIDRGroupRef
Since the usage of ExceptCIDRs is not supported in conjunction with CIDRGroupRef, scan each CIDR rule in the policy and reject it in case a rule specifying the unallowed combination is found. Signed-off-by: Fabio Falzoi <[email protected]>
1 parent e82db73 commit c8e48b8

File tree

3 files changed

+341
-27
lines changed

3 files changed

+341
-27
lines changed

pkg/k8s/watchers/cilium_cidr_group.go

+44
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,47 @@ func translateSpec(spec *api.Rule, cidrsSets map[string][]api.CIDR) {
249249
spec.Ingress[i].FromCIDRSet = append(oldRules, newRules...)
250250
}
251251
}
252+
253+
func validateCIDRRules(cnp *types.SlimCNP) error {
254+
for _, spec := range append(cnp.Specs, cnp.Spec) {
255+
if spec == nil {
256+
continue
257+
}
258+
for _, ingress := range spec.Ingress {
259+
for _, rule := range ingress.FromCIDRSet {
260+
if err := validateCIDRRule(rule); err != nil {
261+
return err
262+
}
263+
}
264+
}
265+
for _, ingress := range spec.IngressDeny {
266+
for _, rule := range ingress.FromCIDRSet {
267+
if err := validateCIDRRule(rule); err != nil {
268+
return err
269+
}
270+
}
271+
}
272+
for _, ingress := range spec.Egress {
273+
for _, rule := range ingress.ToCIDRSet {
274+
if err := validateCIDRRule(rule); err != nil {
275+
return err
276+
}
277+
}
278+
}
279+
for _, ingress := range spec.EgressDeny {
280+
for _, rule := range ingress.ToCIDRSet {
281+
if err := validateCIDRRule(rule); err != nil {
282+
return err
283+
}
284+
}
285+
}
286+
}
287+
return nil
288+
}
289+
290+
func validateCIDRRule(rule api.CIDRRule) error {
291+
if rule.CIDRGroupRef != "" && len(rule.ExceptCIDRs) > 0 {
292+
return errors.New("ExceptCIDRs cannot be used in combination with CIDRGroupRef")
293+
}
294+
return nil
295+
}

0 commit comments

Comments
 (0)