forked from Unleash/unleash-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteAddress.py
68 lines (58 loc) · 2.23 KB
/
RemoteAddress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# pylint: disable=invalid-name
import ipaddress
from UnleashClient.strategies.Strategy import Strategy
from UnleashClient.utils import LOGGER
class RemoteAddress(Strategy):
def load_provisioning(self) -> list:
parsed_ips = []
for address in self.parameters["IPs"].split(","):
if "/" in address:
try:
parsed_ips.append(ipaddress.ip_network(address.strip(), strict=True)) # type: ignore
except (
ipaddress.AddressValueError,
ipaddress.NetmaskValueError,
ValueError,
) as parsing_error:
LOGGER.warning("Error parsing IP range: %s", parsing_error)
else:
try:
parsed_ips.append(ipaddress.ip_address(address.strip())) # type: ignore
except (
ipaddress.AddressValueError,
ipaddress.NetmaskValueError,
ValueError,
) as parsing_error:
LOGGER.warning("Error parsing IP : %s", parsing_error)
return parsed_ips
def apply(self, context: dict = None) -> bool:
"""
Returns true if IP is in list of IPs
:return:
"""
return_value = False
try:
context_ip = ipaddress.ip_address(context["remoteAddress"])
except (
ipaddress.AddressValueError,
ipaddress.NetmaskValueError,
ValueError,
) as parsing_error:
LOGGER.warning("Error parsing IP : %s", parsing_error)
context_ip = None
if context_ip:
for addr_or_range in [
value
for value in self.parsed_provisioning
if value.version == context_ip.version
]:
if isinstance(
addr_or_range, (ipaddress.IPv4Address, ipaddress.IPv6Address)
):
if context_ip == addr_or_range:
return_value = True
break
elif context_ip in addr_or_range: # noqa: PLR5501
return_value = True
break
return return_value