Skip to content

Commit 1e9069c

Browse files
pedro-psbdralley
authored andcommitted
Add url validaton for Rpm and Uln remote creation
Closes: #3520
1 parent 6053dcb commit 1e9069c

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-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: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
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
31+
from urllib.parse import urlparse
4332

4433

4534
class RpmRepositorySerializer(RepositorySerializer):
@@ -270,6 +259,15 @@ class RpmRemoteSerializer(RpmBaseRemoteSerializer):
270259
allow_null=True,
271260
)
272261

262+
def validate_url(self, value):
263+
ALLOWED = ("http", "https", "file")
264+
protocol = urlparse(value).scheme
265+
if protocol not in ALLOWED:
266+
raise serializers.ValidationError(
267+
f"The url {repr(value)} is not valid. It must start with: {ALLOWED}."
268+
)
269+
return value
270+
273271
class Meta:
274272
fields = RemoteSerializer.Meta.fields + ("sles_auth_token",)
275273
model = RpmRemote
@@ -309,6 +307,15 @@ class UlnRemoteSerializer(RpmBaseRemoteSerializer):
309307
allow_null=True,
310308
)
311309

310+
def validate_url(self, value):
311+
ALLOWED = ("uln",)
312+
protocol = urlparse(value).scheme
313+
if protocol not in ALLOWED:
314+
raise serializers.ValidationError(
315+
f"The url {repr(value)} is not valid. It must start with: {ALLOWED}."
316+
)
317+
return value
318+
312319
class Meta:
313320
fields = RemoteSerializer.Meta.fields + ("uln_server_base_url",)
314321
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=" is not valid. It 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=" is not valid. It 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)