diff --git a/solum/api/handlers/language_pack_handler.py b/solum/api/handlers/language_pack_handler.py index 17a668a93..6172a0b13 100644 --- a/solum/api/handlers/language_pack_handler.py +++ b/solum/api/handlers/language_pack_handler.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import re import uuid from oslo_config import cfg @@ -73,6 +74,17 @@ class LanguagePackHandler(handler.Handler): else: return False + def check_lp_url(self, data): + + # try to use a correct git uri + pt = re.compile(r'github\.com[:/](.+?)/(.+?)($|/.*$|\.git$|\.git/.*$)') + match = pt.search(data['source_uri']) + if not match: + msg = ("Bad git url. Provide git url in the following format: \n" + "Public repo: https://github.com//.git\n" + "Private repo: git@github.com:/.git\n") + raise exc.BadRequest(reason=msg) + def get(self, id): """Return a languagepack.""" return objects.registry.Image.get_lp_by_name_or_uuid( @@ -84,6 +96,9 @@ class LanguagePackHandler(handler.Handler): def create(self, data, lp_metadata): """Create a new languagepack.""" + + self.check_lp_url(data) + try: # Check if an LP with the same name exists. objects.registry.Image.get_lp_by_name_or_uuid( diff --git a/solum/tests/api/handlers/test_lp_handler.py b/solum/tests/api/handlers/test_lp_handler.py index 26f84cb42..d5ff731eb 100644 --- a/solum/tests/api/handlers/test_lp_handler.py +++ b/solum/tests/api/handlers/test_lp_handler.py @@ -47,7 +47,7 @@ class TestLanguagePackHandler(base.BaseTestCase): 'LanguagePackHandler._start_build') def test_languagepack_create(self, mock_lp_build, mock_img): data = {'name': 'new app', - 'source_uri': 'git://example.com/foo'} + 'source_uri': 'git://github.com/foo/foo.git'} fi = fakes.FakeImage() mock_img.get_lp_by_name_or_uuid.side_effect = exc.ResourceNotFound() mock_img.return_value = fi @@ -57,6 +57,16 @@ class TestLanguagePackHandler(base.BaseTestCase): fi.update.assert_called_once_with(data) fi.create.assert_called_once_with(self.ctx) + def test_lp_create_bad_git_url(self, mock_img): + data = {'name': 'new app', + 'source_uri': 'git://123'} + handler = language_pack_handler.LanguagePackHandler(self.ctx) + try: + handler.create(data, lp_metadata=None) + self.assertTrue(False) + except exc.BadRequest: + self.assertTrue(True) + @mock.patch('solum.common.solum_swiftclient.SwiftClient.delete_object') @mock.patch('solum.api.handlers.userlog_handler.UserlogHandler') @mock.patch('solum.objects.registry.PlanList')