From: Melissa Li
Date: Tue, 16 Mar 2021 05:07:31 +0000 (-0400)
Subject: python-common: Validate characters in service_id for container names
X-Git-Tag: v16.2.0~73^2~3
X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ca9dd0d1affe95de404d5bb0349ecfc9464649fe;p=ceph.git
python-common: Validate characters in service_id for container names
Service_ids need to be valid docker and podman container names.
Fixes: https://tracker.ceph.com/issues/46497
Signed-off-by: Melissa Li
(cherry picked from commit 8dd2bf85e759072b4af6546e93ef3768ef9b2db8)
---
diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py
index d44eb2d076b7..08d12523dbcf 100644
--- a/src/python-common/ceph/deployment/service_spec.py
+++ b/src/python-common/ceph/deployment/service_spec.py
@@ -609,6 +609,9 @@ class ServiceSpec(object):
if self.service_type in self.REQUIRES_SERVICE_ID:
if not self.service_id:
raise ServiceSpecValidationError('Cannot add Service: id required')
+ if not re.match('^[a-zA-Z0-9_.-]+$', self.service_id):
+ raise ServiceSpecValidationError('Service id contains invalid characters, '
+ 'only [a-zA-Z0-9_.-] allowed')
elif self.service_id:
raise ServiceSpecValidationError(
f'Service of type \'{self.service_type}\' should not contain a service id')
diff --git a/src/python-common/ceph/tests/test_service_spec.py b/src/python-common/ceph/tests/test_service_spec.py
index aa4e36dbb85f..035018958356 100644
--- a/src/python-common/ceph/tests/test_service_spec.py
+++ b/src/python-common/ceph/tests/test_service_spec.py
@@ -309,3 +309,18 @@ def test_service_name(s_type, s_id, s_name):
spec = ServiceSpec.from_json(_get_dict_spec(s_type, s_id))
spec.validate()
assert spec.service_name() == s_name
+
+@pytest.mark.parametrize(
+ 's_type,s_id',
+ [
+ ('mds', 's:id'),
+ ('rgw', '*s_id'),
+ ('nfs', 's/id'),
+ ('iscsi', 's@id'),
+ ('osd', 's;id'),
+ ])
+
+def test_service_id_raises_invalid_char(s_type, s_id):
+ with pytest.raises(ServiceSpecValidationError):
+ spec = ServiceSpec.from_json(_get_dict_spec(s_type, s_id))
+ spec.validate()