From f7fc673409eb84ea048d49c969a9b55b5eec39d0 Mon Sep 17 00:00:00 2001 From: Tasko Olevski Date: Fri, 24 Mar 2023 11:09:44 +0100 Subject: [PATCH] feat(cli): get image repository host on login --- renku/core/login.py | 19 +++++++++++++++++++ renku/core/session/utils.py | 8 ++++---- tests/cli/fixtures/cli_gateway.py | 16 ++++++++++++++++ tests/cli/test_login.py | 4 ++++ tests/core/models/test_git.py | 18 ++++++++++++++++++ 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/renku/core/login.py b/renku/core/login.py index 7fb9058050..fe724775a9 100644 --- a/renku/core/login.py +++ b/renku/core/login.py @@ -135,6 +135,25 @@ def login(endpoint: Optional[str], git_login: bool, yes: bool): else: raise errors.AuthenticationError(f"Invalid status code from server: {status_code} - {response.content}") + image_regsitry_host_req_url = _get_url(parsed_endpoint, path="/api/config/imageRegistries") + image_registry_host_res = requests.get(image_regsitry_host_req_url) + if image_registry_host_res.status_code != 200: + raise errors.ConfigurationError( + f"Cannot get the image registry host from {image_regsitry_host_req_url}, " + f"got unexpected status code {image_registry_host_res.status_code}" + ) + image_registry_host = image_registry_host_res.json().get("default") + if not image_registry_host: + raise errors.ConfigurationError( + f"Cannot get the image registry host from the response {image_registry_host_res.text}" + ) + set_value( + section=CONFIG_SECTION, + key=f"{parsed_endpoint.netloc}_image_registry_host", + value=image_registry_host, + global_only=True, + ) + access_token = response.json().get("access_token") _store_token(parsed_endpoint.netloc, access_token) diff --git a/renku/core/session/utils.py b/renku/core/session/utils.py index 9604e55894..85613be266 100644 --- a/renku/core/session/utils.py +++ b/renku/core/session/utils.py @@ -19,6 +19,7 @@ import urllib from typing import Optional +from renku.core.config import get_value from renku.core.util.git import get_remote from renku.core.util.urls import parse_authentication_endpoint from renku.domain_model.project_context import project_context @@ -48,11 +49,10 @@ def get_renku_url() -> Optional[str]: def get_image_repository_host() -> Optional[str]: """Derive the hostname for the gitlab container registry.""" - # NOTE: Used to circumvent cases where the registry URL is not guessed correctly - # remove once #3301 is done if "RENKU_IMAGE_REGISTRY" in os.environ: return os.environ["RENKU_IMAGE_REGISTRY"] - renku_url = get_renku_url() + renku_url = parse_authentication_endpoint(use_remote=True) if not renku_url: return None - return "registry." + urllib.parse.urlparse(renku_url).netloc + renku_host = renku_url.netloc + return get_value("http", f"{renku_host}_image_registry_host") diff --git a/tests/cli/fixtures/cli_gateway.py b/tests/cli/fixtures/cli_gateway.py index 8f81c88f36..9b4a930181 100644 --- a/tests/cli/fixtures/cli_gateway.py +++ b/tests/cli/fixtures/cli_gateway.py @@ -26,6 +26,7 @@ ENDPOINT = "renku.deployment.ch" ACCESS_TOKEN = "jwt-token" DEVICE_CODE = "valid-device-code" +IMAGE_REGISTRY_HOST = "registry.renku.deployment.ch" @pytest.fixture(scope="module") @@ -65,6 +66,12 @@ def token_callback(request): return token_callback + def create_image_registry_host_callback(host): + def image_registry_host_callback(_): + return 200, {"Content-Type": "application/json"}, json.dumps({"default": host}) + + return image_registry_host_callback + requests_mock.add_passthru("https://pypi.org/") class RequestMockWrapper: @@ -82,6 +89,15 @@ def add_device_auth(endpoint, token): callback=create_token_callback(token), ) + @staticmethod + def add_registry_image_host(endpoint, host): + requests_mock.add_callback( + responses.GET, + f"https://{endpoint}/api/config/imageRegistries", + callback=create_image_registry_host_callback(host), + ) + RequestMockWrapper.add_device_auth(ENDPOINT, ACCESS_TOKEN) + RequestMockWrapper.add_registry_image_host(ENDPOINT, IMAGE_REGISTRY_HOST) yield RequestMockWrapper diff --git a/tests/cli/test_login.py b/tests/cli/test_login.py index 58149978bc..05ad677f53 100644 --- a/tests/cli/test_login.py +++ b/tests/cli/test_login.py @@ -20,6 +20,7 @@ from renku.core import errors from renku.core.login import read_renku_token +from renku.core.session.utils import get_image_repository_host from renku.core.util.contexts import chdir from renku.ui.cli import cli from tests.cli.fixtures.cli_gateway import ACCESS_TOKEN, ENDPOINT @@ -144,7 +145,9 @@ def test_repeated_logout(runner, project, mock_login, with_injection): def test_login_to_multiple_endpoints(runner, project_with_remote, mock_login, with_injection): """Test login to multiple endpoints changes project's remote to the first endpoint.""" second_endpoint, second_token = "second.endpoint", "second-token" + second_image_registry_host = "registry.second.endpoint" mock_login.add_device_auth(second_endpoint, second_token) + mock_login.add_registry_image_host(second_endpoint, second_image_registry_host) assert 0 == runner.invoke(cli, ["login", "--yes", ENDPOINT]).exit_code result = runner.invoke(cli, ["login", "--yes", second_endpoint]) @@ -157,6 +160,7 @@ def test_login_to_multiple_endpoints(runner, project_with_remote, mock_login, wi with with_injection(): assert ACCESS_TOKEN == read_renku_token(ENDPOINT) assert second_token == read_renku_token(second_endpoint) + assert second_image_registry_host == get_image_repository_host() assert project_with_remote.repository.remotes["origin"].url.startswith(f"https://{second_endpoint}/repo") diff --git a/tests/core/models/test_git.py b/tests/core/models/test_git.py index d9847e49d3..010ed1886d 100644 --- a/tests/core/models/test_git.py +++ b/tests/core/models/test_git.py @@ -296,6 +296,24 @@ "owner": "renku-test", "env": "https://gitlab.example.com", }, + { + "href": "https://dev.renku.ch/gitlab/group1/renku-test/test-2022-11-11-17-01-46.git", + "scheme": "https", + "hostname": "dev.renku.ch", + "name": "test-2022-11-11-17-01-46", + "path": "gitlab/group1/renku-test/test-2022-11-11-17-01-46.git", + "owner": "group1/renku-test", + "env": "https://dev.renku.ch/gitlab/", + }, + { + "href": "https://gitlab.example.com/group1/group2/renku-test/test-2022-11-11-17-01-46.git", + "scheme": "https", + "hostname": "gitlab.example.com", + "name": "test-2022-11-11-17-01-46", + "path": "group1/group2/renku-test/test-2022-11-11-17-01-46.git", + "owner": "group1/group2/renku-test", + "env": "https://gitlab.example.com", + }, ], ) def test_valid_href(fields):