Skip to content

Commit

Permalink
Revert "Handle nsfilter/fromsource filter from a common func (#532)"
Browse files Browse the repository at this point in the history
This reverts commit 24ed1cf.
  • Loading branch information
seswarrajan authored and wazir-ahmed committed Aug 25, 2022
1 parent 24ed1cf commit e68c767
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
26 changes: 26 additions & 0 deletions src/networkpolicy/networkPolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/accuknox/auto-policy-discovery/src/cluster"
"github.com/accuknox/auto-policy-discovery/src/config"
cfg "github.com/accuknox/auto-policy-discovery/src/config"
fc "github.com/accuknox/auto-policy-discovery/src/feedconsumer"
"github.com/accuknox/auto-policy-discovery/src/libs"
Expand Down Expand Up @@ -2008,6 +2009,28 @@ func isVM(podName string, pods []types.Pod) bool {
return libs.ContainsElement(getLabelsFromPod(podName, pods), ReservedHost)
}

func applyPolicyFilter(discoveredPolicies map[string][]types.KnoxNetworkPolicy) map[string][]types.KnoxNetworkPolicy {

nsFilter := config.CurrentCfg.ConfigNetPolicy.NsFilter
nsNotFilter := config.CurrentCfg.ConfigNetPolicy.NsNotFilter

if len(nsFilter) > 0 {
for ns := range discoveredPolicies {
if !libs.ContainsElement(nsFilter, ns) {
delete(discoveredPolicies, ns)
}
}
} else if len(nsNotFilter) > 0 {
for ns := range discoveredPolicies {
if libs.ContainsElement(nsNotFilter, ns) {
delete(discoveredPolicies, ns)
}
}
}

return discoveredPolicies
}

func PopulateNetworkPoliciesFromNetworkLogs(networkLogs []types.KnoxNetworkLog) map[string][]types.KnoxNetworkPolicy {

discoveredNetworkPolicies := map[string][]types.KnoxNetworkPolicy{}
Expand Down Expand Up @@ -2070,6 +2093,9 @@ func PopulateNetworkPoliciesFromNetworkLogs(networkLogs []types.KnoxNetworkLog)
}
}

// filter discovered policies
discoveredNetworkPolicies = applyPolicyFilter(discoveredNetworkPolicies)

// iterate each namespace
for _, namespace := range namespaces {
discoveredPolicies := discoveredNetworkPolicies[namespace]
Expand Down
11 changes: 0 additions & 11 deletions src/plugin/cilium.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,6 @@ func StartHubbleRelay(StopChan chan struct{}, cfg types.ConfigCiliumHubble) {
},
}

nsFilter := config.CurrentCfg.ConfigNetPolicy.NsFilter
nsNotFilter := config.CurrentCfg.ConfigSysPolicy.NsNotFilter

stream, err := client.GetFlows(context.Background(), req)
if err != nil {
log.Error().Msg("Unable to stream network flow: " + err.Error())
Expand All @@ -867,14 +864,6 @@ func StartHubbleRelay(StopChan chan struct{}, cfg types.ConfigCiliumHubble) {
case *observer.GetFlowsResponse_Flow:
flow := r.Flow

if IgnoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, flow.Destination.Namespace) {
continue
}

if IgnoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, flow.Source.Namespace) {
continue
}

CiliumFlowsMutex.Lock()
CiliumFlows = append(CiliumFlows, flow)
CiliumFlowsMutex.Unlock()
Expand Down
29 changes: 0 additions & 29 deletions src/plugin/common.go

This file was deleted.

34 changes: 30 additions & 4 deletions src/plugin/kubearmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,32 @@ func GetSystemAlertsFromKubeArmorRelay(trigger int) []*pb.Log {
return results
}

func ignoreLogFromRelayWithSource(filter []string, log *pb.Log) bool {
for _, srcFilter := range filter {
if strings.Contains(log.Source, srcFilter) {
return true
}
}
return false
}

func ignoreLogFromRelayWithNamespace(nsFilter, nsNotFilter []string, log *pb.Log) bool {
if len(nsFilter) > 0 {
for _, ns := range nsFilter {
if !strings.Contains(log.NamespaceName, ns) {
return true
}
}
} else if len(nsNotFilter) > 0 {
for _, notns := range nsNotFilter {
if strings.Contains(log.NamespaceName, notns) {
return true
}
}
}
return false
}

var KubeArmorRelayStarted = false

func StartKubeArmorRelay(StopChan chan struct{}, cfg types.ConfigKubeArmorRelay) {
Expand Down Expand Up @@ -387,11 +413,11 @@ func StartKubeArmorRelay(StopChan chan struct{}, cfg types.ConfigKubeArmorRelay)
return
}

if IgnoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, res.NamespaceName) {
if ignoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, res) {
continue
}

if IgnoreLogFromRelayWithSource(fromSourceFilter, res.Source) {
if ignoreLogFromRelayWithSource(fromSourceFilter, res) {
continue
}

Expand Down Expand Up @@ -444,11 +470,11 @@ func StartKubeArmorRelay(StopChan chan struct{}, cfg types.ConfigKubeArmorRelay)
Type: res.Type,
}

if IgnoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, log.NamespaceName) {
if ignoreLogFromRelayWithNamespace(nsFilter, nsNotFilter, &log) {
continue
}

if IgnoreLogFromRelayWithSource(fromSourceFilter, log.Source) {
if ignoreLogFromRelayWithSource(fromSourceFilter, &log) {
continue
}

Expand Down

0 comments on commit e68c767

Please sign in to comment.