Skip to content

Fix issues with authenticated indexes #231

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions src/python_inspector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@
from typing import NamedTuple
from typing import Optional

from urllib.parse import urlparse

import aiohttp
import requests


def get_netrc_auth(url, netrc):
"""
Return login and password if url is in netrc
Return login and password if either the hostname is in netrc or a default is set in netrc
else return login and password as None
"""
hostname = urlparse(url).hostname
hosts = netrc.hosts
if url in hosts:
url_auth = hosts.get(url)
if hostname in hosts:
url_auth = hosts.get(hostname)
# netrc returns a tuple of (login, account, password)
return (url_auth[0], url_auth[2])

if "default" in hosts:
default_auth = hosts.get("default")
return (default_auth[0], default_auth[2])

return (None, None)


Expand Down
9 changes: 8 additions & 1 deletion src/python_inspector/utils_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,10 @@ async def fetch_links(
name using the `index_url` of this repository.
"""
package_url = f"{self.index_url}/{normalized_name}"

if not package_url.endswith("/"):
package_url += "/"

text, _ = await CACHE.get(
path_or_url=package_url,
credentials=self.credentials,
Expand Down Expand Up @@ -1797,7 +1801,10 @@ async def get_remote_file_content(

auth = None
if credentials:
auth = (credentials.get("login"), credentials.get("password"))
login = credentials.get("login")
password = credentials.get("password")
if login and password:
auth = aiohttp.BasicAuth(login, password)

async with aiohttp.ClientSession() as session:
async with session.get(url, allow_redirects=True, headers=headers, auth=auth) as response:
Expand Down
4 changes: 2 additions & 2 deletions tests/data/test-commented.netrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
machine https://pyp2.org/simple login test password test123
# machine https://pyp1.org/simple login test password test123
machine pyp2.org login test password test123
# machine pyp1.org login test password test123
2 changes: 2 additions & 0 deletions tests/data/test-default.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
machine example.com login test password test123
default login defaultuser password defaultpass
3 changes: 2 additions & 1 deletion tests/data/test.netrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
machine https://pyp1.org/simple login test password test123
machine pyp1.org login test password test123
machine subdomain.example.com login subdomain-user password subdomain-secret
47 changes: 47 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ def test_get_netrc_auth():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=parsed_netrc) == ("test", "test123")
assert get_netrc_auth(url="https://pyp1.org/different/path", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="https://pyp1.org", netrc=parsed_netrc) == ("test", "test123")


def test_get_netrc_auth_with_ports_and_schemes():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)

assert get_netrc_auth(url="https://pyp1.org:443/path", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="http://pyp1.org:80/simple", netrc=parsed_netrc) == (
"test",
"test123",
)


def test_get_commented_netrc_auth():
Expand All @@ -49,6 +68,34 @@ def test_get_netrc_auth_with_no_matching_url():
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=parsed_netrc) == (None, None)


def test_get_netrc_auth_with_with_subdomains():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)

assert get_netrc_auth(url="https://subdomain.example.com/simple", netrc=parsed_netrc) == (
"subdomain-user",
"subdomain-secret",
)
assert get_netrc_auth(url="https://another.example.com/simple", netrc=parsed_netrc) == (
None,
None,
)


def test_get_netrc_auth_with_default():
netrc_file = test_env.get_test_loc("test-default.netrc")
parsed_netrc = netrc(netrc_file)

assert get_netrc_auth(url="https://example.com/simple", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="https://non-existing.org/simple", netrc=parsed_netrc) == (
"defaultuser",
"defaultpass",
)


@pytest.mark.asyncio
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
@mock.patch("python_inspector.utils_pypi.CACHE.get")
Expand Down
Loading