From c604031049ed2c2ce912b27c3045808e7e426be3 Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Thu, 30 Jan 2020 15:51:26 +0000 Subject: [PATCH] Support using environment variables to configure username and password for basic auth. Closes #1871 --- poetry/utils/password_manager.py | 12 ++++++++++-- tests/utils/test_password_manager.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/poetry/utils/password_manager.py b/poetry/utils/password_manager.py index e992b0562e9..439c6a0d9e2 100644 --- a/poetry/utils/password_manager.py +++ b/poetry/utils/password_manager.py @@ -148,11 +148,19 @@ def delete_pypi_token(self, name): self._keyring.delete_password(name, "__token__") def get_http_auth(self, name): + username = self._config.get("http-basic.{}.username".format(name)) + password = self._config.get("http-basic.{}.password".format(name)) auth = self._config.get("http-basic.{}".format(name)) - if not auth: + + if not auth and not (username or password): return None - username, password = auth["username"], auth.get("password") + if not username: + username = auth['username'] + + if not password: + password = auth.get('password') + if password is None: password = self._keyring.get_password(name, username) diff --git a/tests/utils/test_password_manager.py b/tests/utils/test_password_manager.py index a5f89eb028e..a9b6f5c2630 100644 --- a/tests/utils/test_password_manager.py +++ b/tests/utils/test_password_manager.py @@ -70,6 +70,7 @@ def test_set_http_password(config, mock_available_backend, backend): assert "password" not in auth +@pytest.paramaterize def test_get_http_auth(config, mock_available_backend, backend): backend.set_password("poetry-repository-foo", "bar", "baz") config.auth_config_source.add_property("http-basic.foo", {"username": "bar"}) @@ -82,6 +83,23 @@ def test_get_http_auth(config, mock_available_backend, backend): assert "baz" == auth["password"] +@pytest.mark.parametrize("user_env,password_env,combined", ( + ("bar", "baz", {}, {"username": "bar", "password": "baz"}), + ("bar", None, {"username": "notbar", "password": "baz"}), + (None, "baz", {"username": "bar", "password": "notbaz"}), + (None, None, {"username": "bar", "password": "baz"}), +)) +def test_get_http_auth_specific_variables(config, mock_available_backend, backend, user_env, password_env, combined): + if user_env: + config.auth_config_source.add_property("http-basic.foo.username", user_env) + if password_env: + config.auth_config_source.add_property("http-basic.foo.password", password_env) + manager = PasswordManager(config) + + auth = manager.get_http_auth("foo") + assert auth == {"username": "bar", "password": "baz"} + + def test_delete_http_password(config, mock_available_backend, backend): backend.set_password("poetry-repository-foo", "bar", "baz") config.auth_config_source.add_property("http-basic.foo", {"username": "bar"})