Skip to content
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

[TOOL-95] Add whois info to the host string. #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"
pytest = ">=3.6.3"
ipwhois = "~=1.2.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For libraries meant to be reused by many applications, strict dependencies are discouraged since they lock every dependent app to use that specific version and block them from upgrading to 1.4.0 for example.

We should be as lax as possible here, and as strict as possible in shiphero_app.
For example here ipwhois = ">= 1.0.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This article is a good reference to understand why. setup.py here would be our pyproject.toml

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done. Good catch.


[tool.poetry.dev-dependencies]
coverage = "^7.0"
Expand Down
10 changes: 9 additions & 1 deletion pytest_socket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ipaddress
import socket
from ipwhois import IPWhois
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to declare this as a new dependency in pyproject.toml

from ipwhois.exceptions import HTTPLookupError, IPDefinedError

import pytest

Expand All @@ -16,9 +18,15 @@ class SocketConnectBlockedError(RuntimeError):
def __init__(self, allowed, host, *_args, **_kwargs):
if allowed:
allowed = ",".join(allowed)
try:
ip_whois = IPWhois(host).lookup_rdap(depth=1)
host_description = ip_whois.get("asn_description", None)
except (HTTPLookupError, IPDefinedError):
host_description = None
host_text = f"{host} ({host_description})" if host_description else host
super().__init__(
"A test tried to use socket.socket.connect() "
f'with host "{host}" (allowed: "{allowed}").'
f'with host "{host_text}" (allowed: "{allowed}").'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't it break any test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_host_blocked and test_parametrize_with_socket_enabled_and_allow_hosts are matching with A test tried to use socket.socket.connect() with host*, so no tests seem to be broken.

)


Expand Down