Skip to content

Commit 87a3c5f

Browse files
committed
Add url validaton for Rpm and Uln remote creation
Closes: pulp#3520
1 parent 6053dcb commit 87a3c5f

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

CHANGES/3520.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added url validation for Rpm and Uln Remotes creation.

pulp_rpm/app/serializers/repository.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
1-
from gettext import gettext as _
2-
31
import logging
2+
from gettext import gettext as _
43

54
from django.conf import settings
65
from jsonschema import Draft7Validator
7-
from rest_framework import serializers
8-
9-
from pulpcore.plugin.util import get_domain
10-
from pulpcore.plugin.models import (
11-
AsciiArmoredDetachedSigningService,
12-
Remote,
13-
Publication,
14-
)
6+
from pulpcore.plugin.models import AsciiArmoredDetachedSigningService, Publication, Remote
157
from pulpcore.plugin.serializers import (
16-
RelatedField,
178
DetailRelatedField,
189
DistributionSerializer,
1910
PublicationSerializer,
11+
RelatedField,
2012
RemoteSerializer,
2113
RepositorySerializer,
2214
RepositorySyncURLSerializer,
2315
ValidateFieldsMixin,
2416
)
17+
from pulpcore.plugin.util import get_domain
18+
from rest_framework import serializers
2519

2620
from pulp_rpm.app.constants import (
2721
ALLOWED_CHECKSUM_ERROR_MSG,
28-
ALLOWED_PUBLISH_CHECKSUMS,
2922
ALLOWED_PUBLISH_CHECKSUM_ERROR_MSG,
23+
ALLOWED_PUBLISH_CHECKSUMS,
3024
CHECKSUM_CHOICES,
25+
COMPRESSION_CHOICES,
3126
SKIP_TYPES,
3227
SYNC_POLICY_CHOICES,
33-
COMPRESSION_CHOICES,
34-
)
35-
from pulp_rpm.app.models import (
36-
RpmDistribution,
37-
RpmRemote,
38-
RpmRepository,
39-
RpmPublication,
40-
UlnRemote,
4128
)
29+
from pulp_rpm.app.models import RpmDistribution, RpmPublication, RpmRemote, RpmRepository, UlnRemote
4230
from pulp_rpm.app.schema import COPY_CONFIG_SCHEMA
4331

4432

@@ -270,6 +258,15 @@ class RpmRemoteSerializer(RpmBaseRemoteSerializer):
270258
allow_null=True,
271259
)
272260

261+
def validate_url(self, value):
262+
protocol = value.strip(":")[0]
263+
allowed = ("http", "https", "file")
264+
if protocol not in allowed:
265+
raise serializers.ValidationError(
266+
f"The url for RPM Remotes must start with: {allowed}."
267+
)
268+
return value
269+
273270
class Meta:
274271
fields = RemoteSerializer.Meta.fields + ("sles_auth_token",)
275272
model = RpmRemote
@@ -309,6 +306,11 @@ class UlnRemoteSerializer(RpmBaseRemoteSerializer):
309306
allow_null=True,
310307
)
311308

309+
def validate_url(self, value):
310+
if not value.startswith("uln"):
311+
raise serializers.ValidationError("The url for ULN Remotes must start with: uln.")
312+
return value
313+
312314
class Meta:
313315
fields = RemoteSerializer.Meta.fields + ("uln_server_base_url",)
314316
model = UlnRemote

pulp_rpm/tests/functional/api/test_crud_remotes.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""Tests that CRUD rpm remotes."""
22

3-
import pytest
4-
53
from random import choice
64
from uuid import uuid4
75

6+
import pytest
7+
from pulpcore.client.pulp_rpm.exceptions import ApiException
8+
89
from pulp_rpm.tests.functional.constants import DOWNLOAD_POLICIES
910
from pulp_rpm.tests.functional.utils import gen_rpm_remote
1011

11-
from pulpcore.client.pulp_rpm.exceptions import ApiException
12-
1312

1413
@pytest.mark.parallel
1514
def test_basic_crud_remote(rpm_rpmremote_api, rpm_rpmremote_factory, monitor_task):
@@ -127,6 +126,24 @@ def test_policy_update_changes(rpm_rpmremote_api, rpm_rpmremote_factory, monitor
127126
rpm_rpmremote_api.partial_update(remote["pulp_href"], {"policy": str(uuid4())})
128127

129128

129+
def test_raise_on_invalid_remote_url(
130+
rpm_rpmremote_api, rpm_ulnremote_api, gen_object_with_cleanup, monitor_task
131+
):
132+
# Cant create invalid RpmRemote
133+
for protocol in ("uln", "sftp", "grpc"):
134+
with pytest.raises(ApiException, match="The url for RPM Remotes must start with"):
135+
body = _gen_verbose_remote()
136+
body["url"] = f"{protocol}://some/rpm/remote"
137+
gen_object_with_cleanup(rpm_rpmremote_api, body)
138+
139+
# Cant create invalid UlnRemote
140+
for protocol in ("http", "https", "file"):
141+
with pytest.raises(ApiException, match="The url for ULN Remotes must start with"):
142+
body = _gen_verbose_remote()
143+
body["url"] = f"{protocol}://some/uln/remote"
144+
gen_object_with_cleanup(rpm_ulnremote_api, body)
145+
146+
130147
def _gen_verbose_remote():
131148
"""Return a semi-random dict for use in defining a remote.
132149

0 commit comments

Comments
 (0)