Skip to content

Commit 58107e8

Browse files
Fix incorrect netrc format assumption
According to the netrc specification (see [1] and [2]), the `machine` part should not be a full URL, but only a host name. Before, using the correct netrc format with only a host name did not work for authentication purposes in Python Inspector. Fix this by using urllib.parse to find the matching host name. [1]: https://www.ibm.com/docs/en/aix/7.2.0?topic=formats-netrc-file-format-tcpip [2]: https://docs.python.org/3/library/netrc.html#netrc.netrc.hosts Resolves: #176. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent af339f8 commit 58107e8

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/python_inspector/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
from typing import NamedTuple
1717
from typing import Optional
1818

19+
from urllib.parse import urlparse
20+
1921
import aiohttp
2022
import requests
2123

2224

2325
def get_netrc_auth(url, netrc):
2426
"""
25-
Return login and password if url is in netrc
27+
Return login and password if the hostname is in netrc
2628
else return login and password as None
2729
"""
30+
hostname = urlparse(url).hostname
2831
hosts = netrc.hosts
29-
if url in hosts:
30-
url_auth = hosts.get(url)
32+
if hostname in hosts:
33+
url_auth = hosts.get(hostname)
3134
# netrc returns a tuple of (login, account, password)
3235
return (url_auth[0], url_auth[2])
3336
return (None, None)

tests/data/test-commented.netrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
machine https://pyp2.org/simple login test password test123
2-
# machine https://pyp1.org/simple login test password test123
1+
machine pyp2.org login test password test123
2+
# machine pyp1.org login test password test123

tests/data/test.netrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
machine https://pyp1.org/simple login test password test123
1+
machine pyp1.org login test password test123
2+
machine subdomain.example.com login subdomain-user password subdomain-secret

tests/test_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ def test_get_netrc_auth():
3535
netrc_file = test_env.get_test_loc("test.netrc")
3636
parsed_netrc = netrc(netrc_file)
3737
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=parsed_netrc) == ("test", "test123")
38+
assert get_netrc_auth(url="https://pyp1.org/different/path", netrc=parsed_netrc) == (
39+
"test",
40+
"test123",
41+
)
42+
assert get_netrc_auth(url="https://pyp1.org", netrc=parsed_netrc) == ("test", "test123")
43+
44+
45+
def test_get_netrc_auth_with_ports_and_schemes():
46+
netrc_file = test_env.get_test_loc("test.netrc")
47+
parsed_netrc = netrc(netrc_file)
48+
49+
assert get_netrc_auth(url="https://pyp1.org:443/path", netrc=parsed_netrc) == (
50+
"test",
51+
"test123",
52+
)
53+
assert get_netrc_auth(url="http://pyp1.org:80/simple", netrc=parsed_netrc) == (
54+
"test",
55+
"test123",
56+
)
3857

3958

4059
def test_get_commented_netrc_auth():
@@ -49,6 +68,20 @@ def test_get_netrc_auth_with_no_matching_url():
4968
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=parsed_netrc) == (None, None)
5069

5170

71+
def test_get_netrc_auth_with_with_subdomains():
72+
netrc_file = test_env.get_test_loc("test.netrc")
73+
parsed_netrc = netrc(netrc_file)
74+
75+
assert get_netrc_auth(url="https://subdomain.example.com/simple", netrc=parsed_netrc) == (
76+
"subdomain-user",
77+
"subdomain-secret",
78+
)
79+
assert get_netrc_auth(url="https://another.example.com/simple", netrc=parsed_netrc) == (
80+
None,
81+
None,
82+
)
83+
84+
5285
@pytest.mark.asyncio
5386
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
5487
@mock.patch("python_inspector.utils_pypi.CACHE.get")

0 commit comments

Comments
 (0)