From 9ad18c4967b39cf208c41a139103e2177fd0137f Mon Sep 17 00:00:00 2001 From: djp <dimsss0607@gmail.com> Date: Wed, 28 May 2025 00:35:28 +0900 Subject: [PATCH] Fix openstack image import --method web-download --uri 'invalid value' although python-openstackclient run command(image import) with invalid uri, but the request succeeds. Fixed it to throw an exception when requesting with an invalid URI. unit test added. the test cover --uri 'invalid value' Task: 52251 Story: 2011468 Closes-Bug: 2111777 Change-Id: I62cd8cdf054b6a5e07d664a543b0923ce5f20f83 --- openstackclient/image/v2/image.py | 7 +++++ .../tests/unit/image/v2/test_image.py | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 7dbd1609f9..6c7ca740ed 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -22,6 +22,7 @@ import logging import os import sys import typing as ty +import urllib.parse from openstack import exceptions as sdk_exceptions from openstack.image import image_signer @@ -1744,6 +1745,12 @@ class ImportImage(command.ShowOne): "'--method=web-download'" ) raise exceptions.CommandError(msg) + _parsed = urllib.parse.urlparse(parsed_args.uri) + if not all({_parsed.scheme, _parsed.netloc}): + msg = _("'%(uri)s' is not a valid url") + raise exceptions.CommandError( + msg % {'uri': parsed_args.uri}, + ) else: if parsed_args.uri: msg = _( diff --git a/openstackclient/tests/unit/image/v2/test_image.py b/openstackclient/tests/unit/image/v2/test_image.py index 437c8a1e53..95d384c24b 100644 --- a/openstackclient/tests/unit/image/v2/test_image.py +++ b/openstackclient/tests/unit/image/v2/test_image.py @@ -2039,6 +2039,36 @@ class TestImageImport(TestImage): self.image_client.import_image.assert_not_called() + def test_import_image__web_download_invalid_url(self): + arglist = [ + self.image.name, + '--method', + 'web-download', + '--uri', + 'invalid:1234', + ] + + verifylist = [ + ('image', self.image.name), + ('import_method', 'web-download'), + ('uri', 'invalid:1234'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args, + ) + + self.assertIn( + "'invalid:1234' is not a valid url", + str(exc), + ) + + self.image_client.import_image.assert_not_called() + def test_import_image__web_download_invalid_image_state(self): self.image.status = 'uploading' # != 'queued' arglist = [