Merge pull request #3639 from weaveworks/allowegress
lazily add iptables rule to drop egress traffic from the pods only if network policies are applied
This commit is contained in:
2 files changed
+37
-4
No files matched your search
+37
-3
@@ -1,6 +1,7 @@
|
|||||||
package npc
|
package npc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -35,8 +36,9 @@ type controller struct {
|
|||||||
ipt iptables.Interface
|
ipt iptables.Interface
|
||||||
ips ipset.Interface
|
ips ipset.Interface
|
||||||
|
|
||||||
nss map[string]*ns // ns name -> ns struct
|
nss map[string]*ns // ns name -> ns struct
|
||||||
nsSelectors *selectorSet // selector string -> nsSelector
|
nsSelectors *selectorSet // selector string -> nsSelector
|
||||||
|
defaultEgressDrop bool // flag to track if base iptable rule to drop egress traffic is added or not
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(nodeName string, ipt iptables.Interface, ips ipset.Interface) NetworkPolicyController {
|
func New(nodeName string, ipt iptables.Interface, ips ipset.Interface) NetworkPolicyController {
|
||||||
@@ -121,11 +123,26 @@ func (npc *controller) AddNetworkPolicy(obj interface{}) error {
|
|||||||
npc.Lock()
|
npc.Lock()
|
||||||
defer npc.Unlock()
|
defer npc.Unlock()
|
||||||
|
|
||||||
|
// lazily add default rule to drop egress traffic only when network policies are applied
|
||||||
|
if !npc.defaultEgressDrop {
|
||||||
|
egressNetworkPolicy, err := isEgressNetworkPolicy(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if egressNetworkPolicy {
|
||||||
|
npc.defaultEgressDrop = true
|
||||||
|
if err := npc.ipt.Append(TableFilter, EgressChain,
|
||||||
|
"-m", "mark", "!", "--mark", EgressMark, "-j", "DROP"); err != nil {
|
||||||
|
npc.defaultEgressDrop = false
|
||||||
|
return fmt.Errorf("Failed to add iptable rule to drop egress traffic from the pods by default due to %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
nsName, err := nsName(obj)
|
nsName, err := nsName(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
common.Log.Infof("EVENT AddNetworkPolicy %s", js(obj))
|
common.Log.Infof("EVENT AddNetworkPolicy %s", js(obj))
|
||||||
return npc.withNS(nsName, func(ns *ns) error {
|
return npc.withNS(nsName, func(ns *ns) error {
|
||||||
return errors.Wrap(ns.addNetworkPolicy(obj), "add network policy")
|
return errors.Wrap(ns.addNetworkPolicy(obj), "add network policy")
|
||||||
@@ -202,3 +219,20 @@ func nsName(obj interface{}) (string, error) {
|
|||||||
|
|
||||||
return "", errInvalidNetworkPolicyObjType
|
return "", errInvalidNetworkPolicyObjType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isEgressNetworkPolicy(obj interface{}) (bool, error) {
|
||||||
|
if policy, ok := obj.(*networkingv1.NetworkPolicy); ok {
|
||||||
|
if len(policy.Spec.PolicyTypes) > 0 {
|
||||||
|
for _, policyType := range policy.Spec.PolicyTypes {
|
||||||
|
if policyType == networkingv1.PolicyTypeEgress {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if policy.Spec.Egress != nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, errInvalidNetworkPolicyObjType
|
||||||
|
}
|
||||||
@@ -183,7 +183,6 @@ func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
|
|||||||
{"-m", "state", "--state", "NEW", "-j", string(npc.EgressDefaultChain)},
|
{"-m", "state", "--state", "NEW", "-j", string(npc.EgressDefaultChain)},
|
||||||
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", string(npc.EgressCustomChain)},
|
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", string(npc.EgressCustomChain)},
|
||||||
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", "NFLOG", "--nflog-group", "86"},
|
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", "NFLOG", "--nflog-group", "86"},
|
||||||
{"-m", "mark", "!", "--mark", npc.EgressMark, "-j", "DROP"},
|
|
||||||
}...)
|
}...)
|
||||||
if err := net.AddChainWithRules(ipt, npc.TableFilter, npc.EgressChain, ruleSpecs); err != nil {
|
if err := net.AddChainWithRules(ipt, npc.TableFilter, npc.EgressChain, ruleSpecs); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in new issue
Block a user