Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change pod network policy checks #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 28 additions & 60 deletions internal/lint/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package lint
import (
"context"
"fmt"
"net"
"sort"
"strings"

Expand Down Expand Up @@ -134,64 +133,18 @@ func (s *Pod) checkNPs(ctx context.Context, pod *v1.Pod) {
}
}

type Labels map[string]string

func (s *Pod) isPodTargeted(pod *v1.Pod, nsSel, podSel *metav1.LabelSelector, b *netv1.IPBlock) (bool, error) {
nn, err := s.db.FindNSNameBySel(nsSel)
if err != nil {
return false, err
}
if len(nn) == 0 && b == nil {
if podSel == nil {
return false, nil
}
return labelsMatch(podSel, pod.Labels), nil
}
for _, sns := range nn {
if sns != pod.Namespace {
continue
}
if podSel != nil && podSel.Size() > 0 {
if labelsMatch(podSel, pod.Labels) {
return true, nil
}
}
}

if b == nil {
return false, nil
}
_, ipnet, err := net.ParseCIDR(b.CIDR)
if err != nil {
return false, err
}
for _, ip := range pod.Status.PodIPs {
if ipnet.Contains(net.ParseIP(ip.IP)) {
return true, nil
}
}

return false, nil
}

func (s *Pod) checkIngresses(ctx context.Context, pod *v1.Pod, rr []netv1.NetworkPolicyIngressRule) bool {
var match int
if rr == nil {
return false
}
var match int
for _, r := range rr {
if r.From == nil {
return true
}
for _, from := range r.From {
ok, err := s.isPodTargeted(pod, from.NamespaceSelector, from.PodSelector, from.IPBlock)
if err != nil {
s.AddErr(ctx, err)
return true
}
if ok {
match++
}

if checkTargetPeers(r.From) && checkTargetPorts(r.Ports) {
match++
}
}

Expand All @@ -207,21 +160,36 @@ func (s *Pod) checkEgresses(ctx context.Context, pod *v1.Pod, rr []netv1.Network
if r.To == nil {
return true
}
for _, to := range r.To {
ok, err := s.isPodTargeted(pod, to.NamespaceSelector, to.PodSelector, to.IPBlock)
if err != nil {
s.AddErr(ctx, err)
return true
}
if ok {
match++
}

if checkTargetPeers(r.To) && checkTargetPorts(r.Ports) {
match++
}
}

return match > 0
}

func checkTargetPeers(polPeers []netv1.NetworkPolicyPeer) bool {
var validPeer bool
for _, polPeer := range polPeers {
if polPeer.NamespaceSelector.Size() > 0 || polPeer.PodSelector.Size() > 0 || polPeer.IPBlock.Size() > 0 {
validPeer = true
}
}
return validPeer
}

func checkTargetPorts(polPorts []netv1.NetworkPolicyPort) bool {
var validPort bool
for _, polPort := range polPorts {
if polPort.Size() > 0 {
validPort = true
}
}

return validPort
}

func labelsMatch(sel *metav1.LabelSelector, ll map[string]string) bool {
if sel == nil || sel.Size() == 0 {
return true
Expand Down
Loading