Skip to content

Commit 04a3d29

Browse files
ck319huntergregory
andauthored
fix: [NPM] add check for valid IPV4 addresses in TranslatePolicy (#1738)
* add check for ipv6 addresses in translatePolicy * fix static error lint issue * updated static error name and moved IPV4 logic * moved check to ipBlockRule * updated UT --------- Co-authored-by: Hunter Gregory <[email protected]>
1 parent da56b37 commit 04a3d29

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

npm/pkg/controlplane/controllers/v2/networkPolicyController.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
295295
}
296296

297297
klog.Errorf("Failed to translate podSelector in NetworkPolicy %s in namespace %s: %s", netPolObj.ObjectMeta.Name, netPolObj.ObjectMeta.Namespace, err.Error())
298-
// The exec time isn't relevant here, so consider a no-op.
299-
return metrics.NoOp, errNetPolTranslationFailure
298+
// The exec time isn't relevant here, so consider a no-op. Returning nil to prevent re-queuing since this is not a transient error.
299+
return metrics.NoOp, nil
300300
}
301301

302302
_, policyExisted := c.rawNpSpecMap[netpolKey]

npm/pkg/controlplane/translation/translatePolicy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var (
3232
ErrInvalidMatchExpressionValues = errors.New(
3333
"matchExpression label values must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character",
3434
)
35+
// ErrUnsupportedIPAddress is returned when an unsupported IP address, such as IPV6, is used
36+
ErrUnsupportedIPAddress = errors.New("unsupported IP address")
3537
)
3638

3739
type podSelectorResult struct {
@@ -225,6 +227,10 @@ func ipBlockRule(policyName, ns string, direction policies.Direction, matchType
225227
return nil, policies.SetInfo{}, nil
226228
}
227229

230+
if !util.IsIPV4(ipBlockRule.CIDR) {
231+
return nil, policies.SetInfo{}, ErrUnsupportedIPAddress
232+
}
233+
228234
ipBlockIPSet, err := ipBlockIPSet(policyName, ns, direction, ipBlockSetIndex, ipBlockPeerIndex, ipBlockRule)
229235
if err != nil {
230236
return nil, policies.SetInfo{}, err
@@ -563,8 +569,8 @@ func egressPolicy(npmNetPol *policies.NPMNetworkPolicy, netPolName string, egres
563569
return nil
564570
}
565571

566-
// TranslatePolicy traslates networkpolicy object to NPMNetworkPolicy object
567-
// and return the NPMNetworkPolicy object.
572+
// TranslatePolicy translates networkpolicy object to NPMNetworkPolicy object
573+
// and returns the NPMNetworkPolicy object.
568574
func TranslatePolicy(npObj *networkingv1.NetworkPolicy) (*policies.NPMNetworkPolicy, error) {
569575
netPolName := npObj.Name
570576
npmNetPol := policies.NewNPMNetworkPolicy(netPolName, npObj.Namespace)

npm/pkg/controlplane/translation/translatePolicy_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ func TestIPBlockRule(t *testing.T) {
491491
translatedIPSet *ipsets.TranslatedIPSet
492492
setInfo policies.SetInfo
493493
skipWindows bool
494+
wantErr bool
494495
}{
495496
{
496497
name: "empty ipblock rule ",
@@ -540,6 +541,26 @@ func TestIPBlockRule(t *testing.T) {
540541
setInfo: policies.NewSetInfo("test-network-policy-in-ns-default-0-0IN", ipsets.CIDRBlocks, included, policies.SrcMatch),
541542
skipWindows: true,
542543
},
544+
{
545+
name: "invalid ipv6",
546+
ipBlockInfo: createIPBlockInfo("test", defaultNS, policies.Ingress, policies.SrcMatch, 0, 0),
547+
ipBlockRule: &networkingv1.IPBlock{
548+
CIDR: "2002::1234:abcd:ffff:c0a8:101/64",
549+
},
550+
translatedIPSet: nil,
551+
setInfo: policies.SetInfo{},
552+
wantErr: true,
553+
},
554+
{
555+
name: "invalid cidr",
556+
ipBlockInfo: createIPBlockInfo("test", defaultNS, policies.Ingress, policies.SrcMatch, 0, 0),
557+
ipBlockRule: &networkingv1.IPBlock{
558+
CIDR: "10.0.0.1/33",
559+
},
560+
translatedIPSet: nil,
561+
setInfo: policies.SetInfo{},
562+
wantErr: true,
563+
},
543564
}
544565

545566
for _, tt := range tests {
@@ -550,9 +571,15 @@ func TestIPBlockRule(t *testing.T) {
550571
if tt.skipWindows && util.IsWindowsDP() {
551572
require.Error(t, err)
552573
} else {
553-
require.NoError(t, err)
554-
require.Equal(t, tt.translatedIPSet, translatedIPSet)
555-
require.Equal(t, tt.setInfo, setInfo)
574+
if tt.wantErr {
575+
require.Error(t, err)
576+
require.Equal(t, tt.translatedIPSet, translatedIPSet)
577+
require.Equal(t, tt.setInfo, setInfo)
578+
} else {
579+
require.NoError(t, err)
580+
require.Equal(t, tt.translatedIPSet, translatedIPSet)
581+
require.Equal(t, tt.setInfo, setInfo)
582+
}
556583
}
557584
})
558585
}

0 commit comments

Comments
 (0)