diff --git a/gateway/api/tasks/providers.py b/gateway/api/tasks/providers.py index 8a0536503..dff35202a 100644 --- a/gateway/api/tasks/providers.py +++ b/gateway/api/tasks/providers.py @@ -20,25 +20,31 @@ def assign_admin_group(): """ try: providers_configuration = json.loads(settings.PROVIDERS_CONFIGURATION) - except json.JSONDecodeError as e: - logger.error("Assign admin group JSON malformed: %s", e) + except json.JSONDecodeError: + logger.error( + "Assign admin group JSON malformed in settings.PROVIDERS_CONFIGURATION" + ) return - except Exception as e: # pylint: disable=broad-exception-caught - logger.error("Assign admin group unexpected error: %s", e) + except Exception: # pylint: disable=broad-exception-caught + logger.error("Assign admin group unexpected error") return - for provider_name, admin_group_name in providers_configuration.items(): + for provider_name, provider_attributes in providers_configuration.items(): + admin_group_name = provider_attributes["admin_group"] + registry = provider_attributes["registry"] group = Group.objects.filter(name=admin_group_name).first() if group is None: logger.warning("Group [%s] does not exist", admin_group_name) else: provider, created = Provider.objects.update_or_create( - name=provider_name, defaults={"admin_group": group} + name=provider_name, + defaults={"admin_group": group, "registry": registry}, ) if created: - logger.info( - "Provider [%s] created for admin [%s]", + logger.info( # pylint: disable=logging-too-many-args + "Provider [%s] created for admin [%s] with registrt [%s]", provider.name, admin_group_name, + registry, ) diff --git a/gateway/api/v1/serializers.py b/gateway/api/v1/serializers.py index ea9155b07..c6de0ea65 100644 --- a/gateway/api/v1/serializers.py +++ b/gateway/api/v1/serializers.py @@ -4,6 +4,7 @@ from rest_framework.serializers import ValidationError from api import serializers +from api.models import Provider class ProgramSerializer(serializers.ProgramSerializer): @@ -54,6 +55,19 @@ def validate(self, attrs): raise ValidationError( "Custom images are only available if you are a provider." ) + if not provider: + provider = title_split[0] + provider_instance = Provider.objects.filter(name=provider).first() + if provider_instance is None: + raise ValidationError( + "Custom images are only available a valid provider." + ) + if not provider_instance.registry or not image.startswith( + provider_instance.registry + ): + raise ValidationError( + f"Custom images must be in {provider_instance.registry}." + ) return super().validate(attrs) class Meta(serializers.UploadProgramSerializer.Meta): diff --git a/gateway/tests/api/tasks/test_tasks.py b/gateway/tests/api/tasks/test_tasks.py index 10b97d7e8..f0b3d2dac 100644 --- a/gateway/tests/api/tasks/test_tasks.py +++ b/gateway/tests/api/tasks/test_tasks.py @@ -11,7 +11,9 @@ class TestProgramApi(APITestCase): fixtures = ["tests/fixtures/tasks_fixtures.json"] - @override_settings(PROVIDERS_CONFIGURATION='{"test_provider": "runner"}') + @override_settings( + PROVIDERS_CONFIGURATION='{"test_provider": {"admin_group": "runner", "registry": "docker.io/"}}' + ) def test_assign_admin_group(self): """This test will check assign admin group task""" @@ -20,7 +22,9 @@ def test_assign_admin_group(self): provider = Provider.objects.get(name="test_provider") self.assertEqual(provider.admin_group.name, "runner") - @override_settings(PROVIDERS_CONFIGURATION='{"test_provider": "runner"}') + @override_settings( + PROVIDERS_CONFIGURATION='{"test_provider": {"admin_group": "runner", "registry": "docker.io/"}}' + ) @override_settings( FUNCTIONS_PERMISSIONS='{"function_provider": {"provider": "test_provider", "instances": ["runner"]}}' ) @@ -36,7 +40,7 @@ def test_assign_run_permission(self): "title": "function_provider", "dependencies": "[]", "env_vars": "{}", - "image": "icr.io/awesome-namespace/awesome-title", + "image": "docker.io/awesome-namespace/awesome-title", "provider": "test_provider", }, ) diff --git a/gateway/tests/api/test_v1_program.py b/gateway/tests/api/test_v1_program.py index ee13f2389..4dfe0ffb1 100644 --- a/gateway/tests/api/test_v1_program.py +++ b/gateway/tests/api/test_v1_program.py @@ -216,8 +216,8 @@ def test_upload_custom_image_without_access_to_the_provider(self): "title": "Private function", "dependencies": "[]", "env_vars": env_vars, - "image": "icr.io/awesome-namespace/awesome-title", - "provider": "default", + "image": "docker.io/awesome-namespace/awesome-title", + "provider": "ibm", }, ) self.assertEqual(programs_response.status_code, status.HTTP_404_NOT_FOUND) @@ -225,10 +225,10 @@ def test_upload_custom_image_without_access_to_the_provider(self): programs_response = self.client.post( "/api/v1/programs/upload/", data={ - "title": "default/Private function", + "title": "ibm/Private function", "dependencies": "[]", "env_vars": env_vars, - "image": "icr.io/awesome-namespace/awesome-title", + "image": "docker.io/awesome-namespace/awesome-title", }, ) self.assertEqual(programs_response.status_code, status.HTTP_404_NOT_FOUND) diff --git a/gateway/tests/api/test_v1_serializers.py b/gateway/tests/api/test_v1_serializers.py index a34a15817..48e44d940 100644 --- a/gateway/tests/api/test_v1_serializers.py +++ b/gateway/tests/api/test_v1_serializers.py @@ -122,7 +122,7 @@ def test_upload_program_with_custom_image_and_provider(self): arguments = {} dependencies = "[]" image = "docker.io/awesome/awesome-image:latest" - provider = "ibm" + provider = "default" data = {} data["title"] = title @@ -138,7 +138,7 @@ def test_upload_program_with_custom_image_and_provider(self): def test_upload_program_with_custom_image_and_title_provider(self): """Tests image upload serializer.""" - title = "ibm/Hello world" + title = "default/Hello world" entrypoint = "main.py" arguments = {} dependencies = "[]" diff --git a/gateway/tests/fixtures/fixtures.json b/gateway/tests/fixtures/fixtures.json index ca8182d4f..21716c37e 100644 --- a/gateway/tests/fixtures/fixtures.json +++ b/gateway/tests/fixtures/fixtures.json @@ -174,7 +174,18 @@ "fields": { "name": "default", "created": "2023-02-01T15:30:43.281796Z", - "admin_group": 105 + "admin_group": 105, + "registry": "docker.io/awesome" + } + }, + { + "model": "api.provider", + "pk": "bfe8aa6a-2127-4123-bf57-5b547293cbeb", + "fields": { + "name": "ibm", + "created": "2023-02-01T15:30:43.281796Z", + "admin_group": 101, + "registry": "docker.io/awesome" } } ]