Skip to content

Commit

Permalink
check regstry location of provider (#1412)
Browse files Browse the repository at this point in the history
* check regstry location of provider
  • Loading branch information
akihikokuroda authored Jul 17, 2024
1 parent 646dfcb commit 6b1003d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
22 changes: 14 additions & 8 deletions gateway/api/tasks/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
14 changes: 14 additions & 0 deletions gateway/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from rest_framework.serializers import ValidationError
from api import serializers
from api.models import Provider


class ProgramSerializer(serializers.ProgramSerializer):
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions gateway/tests/api/tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand All @@ -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"]}}'
)
Expand All @@ -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",
},
)
Expand Down
8 changes: 4 additions & 4 deletions gateway/tests/api/test_v1_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ 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)

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)
Expand Down
4 changes: 2 additions & 2 deletions gateway/tests/api/test_v1_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "[]"
Expand Down
13 changes: 12 additions & 1 deletion gateway/tests/fixtures/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand Down

0 comments on commit 6b1003d

Please sign in to comment.