Skip to content

Commit

Permalink
feat(cli): get image repository host on login
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski committed Mar 24, 2023
1 parent 2108f65 commit f7fc673
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
19 changes: 19 additions & 0 deletions renku/core/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions renku/core/session/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
16 changes: 16 additions & 0 deletions tests/cli/fixtures/cli_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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
4 changes: 4 additions & 0 deletions tests/cli/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand All @@ -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")


Expand Down
18 changes: 18 additions & 0 deletions tests/core/models/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit f7fc673

Please sign in to comment.