Skip to content

Commit a137a14

Browse files
committed
feat: do not call keyring when username in config and password in env
1 parent 0ea82c5 commit a137a14

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/poetry/utils/password_manager.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,13 @@ def delete_pypi_token(self, name: str) -> None:
193193
self.keyring.delete_password(name, "__token__")
194194

195195
def get_http_auth(self, name: str) -> dict[str, str | None] | None:
196-
auth = self._config.get(f"http-basic.{name}")
197-
if not auth:
198-
username = self._config.get(f"http-basic.{name}.username")
199-
password = self._config.get(f"http-basic.{name}.password")
200-
if not username and not password:
201-
return None
202-
else:
203-
username, password = auth["username"], auth.get("password")
204-
if password is None:
205-
password = self.keyring.get_password(name, username)
196+
username = self._config.get(f"http-basic.{name}.username")
197+
password = self._config.get(f"http-basic.{name}.password")
198+
if not username and not password:
199+
return None
200+
201+
if not password:
202+
password = self.keyring.get_password(name, username)
206203

207204
return {
208205
"username": username,

tests/utils/test_password_manager.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44

55
from typing import TYPE_CHECKING
6+
from unittest.mock import MagicMock
67

78
import pytest
89

@@ -222,18 +223,47 @@ def test_fail_keyring_should_be_unavailable(
222223

223224

224225
def test_get_http_auth_from_environment_variables(
225-
environ: None, config: Config, with_simple_keyring: None
226+
environ: None, config: Config
226227
) -> None:
227228
os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar"
228229
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
229230

230231
manager = PasswordManager(config)
231232

232233
auth = manager.get_http_auth("foo")
233-
assert auth is not None
234+
assert auth == {"username": "bar", "password": "baz"}
234235

235-
assert auth["username"] == "bar"
236-
assert auth["password"] == "baz"
236+
237+
def test_get_http_auth_does_not_call_keyring_when_credentials_in_environment_variables(
238+
environ: None, config: Config
239+
) -> None:
240+
os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar"
241+
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
242+
243+
manager = PasswordManager(config)
244+
manager._keyring = MagicMock()
245+
246+
auth = manager.get_http_auth("foo")
247+
assert auth == {"username": "bar", "password": "baz"}
248+
manager._keyring.get_password.assert_not_called()
249+
250+
251+
def test_get_http_auth_does_not_call_keyring_when_password_in_environment_variables(
252+
environ: None, config: Config
253+
) -> None:
254+
config.merge(
255+
{
256+
"http-basic": {"foo": {"username": "bar"}},
257+
}
258+
)
259+
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
260+
261+
manager = PasswordManager(config)
262+
manager._keyring = MagicMock()
263+
264+
auth = manager.get_http_auth("foo")
265+
assert auth == {"username": "bar", "password": "baz"}
266+
manager._keyring.get_password.assert_not_called()
237267

238268

239269
def test_get_pypi_token_with_env_var_positive(

0 commit comments

Comments
 (0)