Browse Source

filter_inNetworks: allow an IP as a network (mask = /32 or /128)

master 1.1
Y 3 years ago
parent
commit
60b7f11176
3 changed files with 14 additions and 3 deletions
  1. +2
    -2
      pyruse/base.py
  2. +4
    -1
      pyruse/filters/filter_inNetworks.py
  3. +8
    -0
      tests/filter_inNetworks.py

+ 2
- 2
pyruse/base.py View File

@ -35,7 +35,7 @@ class Filter(Step):
nextStep = self.nextStep if self.filter(entry) else self.altStep
except Exception as e:
nextStep = self.altStep
log.error("Error while executing %s: %s." % (type(self), str(e)))
log.error("Error while executing %s (%s): %s." % (type(self), self.stepName, str(e)))
return nextStep
class Action(Step):
@ -52,5 +52,5 @@ class Action(Step):
nextStep = self.nextStep
except Exception as e:
nextStep = None
log.error("Error while executing %s: %s." % (type(self), str(e)))
log.error("Error while executing %s (%s): %s." % (type(self), self.stepName, str(e)))
return nextStep

+ 4
- 1
pyruse/filters/filter_inNetworks.py View File

@ -38,7 +38,10 @@ class Filter(base.Filter):
return False
def _toNetAndMask(self, family, bits, net):
ip, mask = net.split("/")
if "/" in net:
ip, mask = net.split("/")
else:
ip, mask = net, bits
numericMask = ((1<<int(mask))-1)<<(bits-int(mask))
numericIP = self._numericIP(family, ip)
return numericIP & numericMask, numericMask


+ 8
- 0
tests/filter_inNetworks.py View File

@ -9,12 +9,18 @@ def whenIp4InNet4ThenTrue():
def whenIp4NotInNet4ThenFalse():
assert not Filter({"field": "ip", "nets": ["34.56.78.90/12"]}).filter({"ip": "34.47.255.254"})
def whenIp4ItselfThenTrue():
assert Filter({"field": "ip", "nets": ["12.34.56.78"]}).filter({"ip": "12.34.56.78"})
def whenIp6InNet6ThenTrue():
assert Filter({"field": "ip", "nets": ["2001:db8:1:1a0::/59"]}).filter({"ip": "2001:db8:1:1a0::1"})
def whenIp6NotInNet6ThenFalse():
assert not Filter({"field": "ip", "nets": ["2001:db8:1:1a0::/59"]}).filter({"ip": "2001:db8:1:19f:ffff:ffff:ffff:fffe"})
def whenIp6ItselfThenTrue():
assert Filter({"field": "ip", "nets": ["2001:db8:1:1a0::"]}).filter({"ip": "2001:db8:1:1a0::"})
def whenNumericIp6InNet4ThenFalse():
assert not Filter({"field": "ip", "nets": ["34.56.78.90/12"]}).filter({"ip": "::2230:1"})
@ -33,8 +39,10 @@ def whenNoNetworkThenFalse():
def unitTests():
whenIp4InNet4ThenTrue()
whenIp4NotInNet4ThenFalse()
whenIp4ItselfThenTrue()
whenIp6InNet6ThenTrue()
whenIp6NotInNet6ThenFalse()
whenIp6ItselfThenTrue()
whenNumericIp6InNet4ThenFalse()
whenNumericIp4InNet6ThenFalse()
whenIpInOneNetworkThenTrue()


Loading…
Cancel
Save