Skip to content
71 changes: 57 additions & 14 deletions npm/pkg/controlplane/translation/translatePolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,55 @@
return nil
}

func directPeerAndPortRule(npmNetPol *policies.NPMNetworkPolicy, direction policies.Direction, ports []networkingv1.NetworkPolicyPort, cidr string, npmLiteToggle bool) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably include Allow somewhere in this func to be extra explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated thanks

if len(ports) == 0 {
acl := policies.NewACLPolicy(policies.Allowed, direction)
// bypasses ipset creation for /32 cidrs and directly creates an acl with the cidr
if direction == policies.Ingress {
acl.SrcDirectIPs = []string{cidr}
} else {
acl.DstDirectIPs = []string{cidr}
}
npmNetPol.ACLs = append(npmNetPol.ACLs, acl)
return nil
} else {

Check failure on line 376 in npm/pkg/controlplane/translation/translatePolicy.go

View workflow job for this annotation

GitHub Actions / Lint (windows-latest)

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 376 in npm/pkg/controlplane/translation/translatePolicy.go

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest)

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
// handle each port separately
for i := range ports {
portKind, err := portType(ports[i])
if err != nil {
return err
}

err = checkForNamedPortType(portKind, npmLiteToggle)
if err != nil {
return err
}

acl := policies.NewACLPolicy(policies.Allowed, direction)

// Set direct IP based on direction
if direction == policies.Ingress {
acl.SrcDirectIPs = []string{cidr}
} else {
acl.DstDirectIPs = []string{cidr}
}

// Handle ports
if portKind == namedPortType {
return ErrUnsupportedNamedPort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also wrap this with some additional context from the npmNetPol in case you're doing a trace later on

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added thanks

}
if portKind == numericPortType {
portInfo, protocol := numericPortRule(&ports[i])
acl.DstPorts = portInfo
acl.Protocol = policies.Protocol(protocol)
}
npmNetPol.ACLs = append(npmNetPol.ACLs, acl)

}
}
return nil
}

// translateRule translates ingress or egress rules and update npmNetPol object.
func translateRule(npmNetPol *policies.NPMNetworkPolicy,
netPolName string,
Expand Down Expand Up @@ -405,6 +454,14 @@
// #2.1 Handle IPBlock and port if exist
if peer.IPBlock != nil {
if len(peer.IPBlock.CIDR) > 0 {
if npmLiteToggle {
err = directPeerAndPortRule(npmNetPol, direction, ports, peer.IPBlock.CIDR, npmLiteToggle)
if err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ error wrapping here

}
continue
}

ipBlockIPSet, ipBlockSetInfo, err := ipBlockRule(netPolName, npmNetPol.Namespace, direction, matchType, ruleIndex, peerIdx, peer.IPBlock)
if err != nil {
return err
Expand All @@ -417,12 +474,6 @@
}
}

// if npm lite is configured, check network policy only consists of CIDR blocks
err := npmLiteValidPolicy(peer, npmLiteToggle)
if err != nil {
return err
}

// Do not need to run below code to translate PodSelector and NamespaceSelector
// since IPBlock field is exclusive in NetworkPolicyPeer (i.e., peer in this code).

Expand Down Expand Up @@ -642,14 +693,6 @@
return npmNetPol, nil
}

// validates only CIDR based peer is present + no combination of CIDR with pod/namespace selectors are present
func npmLiteValidPolicy(peer networkingv1.NetworkPolicyPeer, npmLiteEnabled bool) error {
if npmLiteEnabled && (peer.PodSelector != nil || peer.NamespaceSelector != nil) {
return ErrUnsupportedNonCIDR
}
return nil
}

func checkForNamedPortType(portKind netpolPortType, npmLiteToggle bool) error {
if npmLiteToggle && portKind == namedPortType {
return ErrUnsupportedNonCIDR
Expand Down
Loading
Loading