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

WIP: Fix .netrc credentials taking precedence over poetry config [#8443] #9738

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ def request(
credential = self.get_credentials_for_url(url)

if credential.username is not None or credential.password is not None:
request = requests.auth.HTTPBasicAuth(
request.auth = requests.auth.HTTPBasicAuth(
credential.username or "", credential.password or ""
)(request)
)
request.auth(request)

session = self.get_session(url=url)
prepared_request = session.prepare_request(request)
Expand Down
29 changes: 29 additions & 0 deletions tests/utils/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,35 @@ def test_authenticator_uses_env_provided_credentials(

request = http.last_request()

basic_auth = base64.b64encode(b"bar:baz").decode()

assert request.headers["Authorization"] == f"Basic {basic_auth}"


def test_authenticator_uses_env_provided_credentials_over_netrc(
config: Config,
repo: dict[str, dict[str, str]],
environ: None,
mock_remote: type[httpretty.httpretty],
http: type[httpretty.httpretty],
monkeypatch: MonkeyPatch,
tmp_path: Path
) -> None:
# Create .netrc file inside the temporary directory
temp_file = tmp_path / ".netrc"
temp_file.write_text("machine foo.bar\nlogin foor\npassword fooz")

monkeypatch.setenv("NETRC", str(temp_file))
monkeypatch.setenv("POETRY_HTTP_BASIC_FOO_USERNAME", "bar")
monkeypatch.setenv("POETRY_HTTP_BASIC_FOO_PASSWORD", "baz")

config.merge({"repositories": repo})

authenticator = Authenticator(config, NullIO())
authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz")

request = http.last_request()

basic_auth = base64.b64encode(b"bar:baz").decode()
assert request.headers["Authorization"] == f"Basic {basic_auth}"

Expand Down
Loading