-
Notifications
You must be signed in to change notification settings - Fork 13
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
FIREWALL: Allow to block hosts by IP address #90
Conversation
tox failures existed before this PR. |
Hi, please rebase to get CI green. |
Perfect. Thank you. |
pytest_mh/utils/firewall.py
Outdated
ipv4 = IPv4Address(hostname) | ||
if type == "A": | ||
return [hostname] | ||
except ValueError: | ||
ipv4 = None | ||
|
||
try: | ||
ipv6 = IPv6Address(hostname) | ||
if type == "AAAA": | ||
return [hostname] | ||
except ValueError: | ||
ipv6 = None | ||
|
||
if ipv4 is not None or ipv6 is not None: | ||
# In this case we were given an IP address but of the wrong family. We must not consider it a hostname. | ||
return [] | ||
else: | ||
result = self.firewall.host.conn.exec( | ||
["dig", "+short", "-t", type, hostname], log_level=ProcessLogLevel.Error | ||
) | ||
return result.stdout_lines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about simplifying it to:
addrs = []
try:
ip = ipaddress.ip_address(hostname)
ip_type = ipaddress.IPv4Address if type == "A" else ipaddress.IPv6Address
if isinstance(ip, ip_type):
addrs = [hostname]
except ValueError:
result = self.firewall.host.conn.exec(
["dig", "+short", "-t", type, hostname], log_level=ProcessLogLevel.Error
)
addrs = result.stdout_lines
return addrs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I will use it.
When blocking a host, its hostname is resolved using the `dig` command. If an IP address is provided, `dig` returns nothing. Check whether it is an IP address before launching `dig`. No matter whether the correct family is requested, do not call `dig` when an IP address is provided, in case the caller has already blocked the DNS host.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack. Thank you. Since I contributed to the solution, somebody else should review it as well.
When blocking a host, its hostname is resolved using the
dig
command. If an IP address is provided,dig
returns nothing and it is not blocked.We need to check whether it is an IP address before launching
dig
. No matter whether the correct family is requested, do not calldig
when an IP address is provided, in case the caller has already blocked the DNS host.