From f7402cbf2769fd95d541426b37815f6aacf08ced Mon Sep 17 00:00:00 2001 From: Raffaela Cunha Date: Fri, 16 Sep 2022 14:30:31 +0000 Subject: [PATCH] Fixes share server manager match of identifiers Previsously, when trying to manage a share server with a given identifier, the db API checked if the identifier is a substring of any existing identifier and if it is True, it raised an InvalidInput Exception. Now, the InvalidInput expception is raised only if the identifier is a suffix of any existing identifier and it is ignored when it is just a preffix. Some queries from the sqlalchemy API share_server_search_by_identifier method were edited to implement this change. Depends-On: I14df0c9c7326ac49274699dc0ef1adb99570784b Closes-bug: #1982429 Change-Id: I4b340ef116625f28e431b069f399771fd29a8de9 --- manila/db/sqlalchemy/api.py | 8 ++++---- manila/tests/db/sqlalchemy/test_api.py | 8 +++++--- ...ix_share_server_manager_match_id-276202295539dc0f.yaml | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/fix_share_server_manager_match_id-276202295539dc0f.yaml diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py index b1e9036a2e..d6e981950a 100644 --- a/manila/db/sqlalchemy/api.py +++ b/manila/db/sqlalchemy/api.py @@ -5110,20 +5110,20 @@ def share_server_search_by_identifier(context, identifier): identifier_field = models.ShareServer.identifier - # try if given identifier is a substring of existing entry's identifier + # try if given identifier is a suffix of existing entry's identifier result = (_share_server_get_query(context).filter( - identifier_field.like('%{}%'.format(identifier))).all()) + identifier_field.like('%{}'.format(identifier))).all()) if not result: # repeat it with underscores instead of hyphens result = (_share_server_get_query(context).filter( - identifier_field.like('%{}%'.format( + identifier_field.like('%{}'.format( identifier.replace("-", "_")))).all()) if not result: # repeat it with hypens instead of underscores result = (_share_server_get_query(context).filter( - identifier_field.like('%{}%'.format( + identifier_field.like('%{}'.format( identifier.replace("_", "-")))).all()) if not result: diff --git a/manila/tests/db/sqlalchemy/test_api.py b/manila/tests/db/sqlalchemy/test_api.py index 3c174bcb8e..2191414fe7 100644 --- a/manila/tests/db/sqlalchemy/test_api.py +++ b/manila/tests/db/sqlalchemy/test_api.py @@ -3731,8 +3731,9 @@ class ShareServerDatabaseAPITestCase(test.TestCase): self.assertEqual(num_records - 1, len(db_api.share_server_get_all(self.ctxt))) - @ddt.data('fake', '-fake-', 'foo_some_fake_identifier_bar', - 'foo-some-fake-identifier-bar', 'foobar') + @ddt.data('foobar', 'fake', '-fake-', 'some_fake_', 'some-fake-', + '-fake-identifier', '_fake_identifier', 'some-fake-identifier', + 'some_fake_identifier') def test_share_server_search_by_identifier(self, identifier): server = { @@ -3744,7 +3745,8 @@ class ShareServerDatabaseAPITestCase(test.TestCase): } server = db_utils.create_share_server(**server) - if identifier == 'foobar': + if identifier in ('foobar', 'fake', '-fake-', 'some_fake_', + 'some-fake-'): self.assertRaises(exception.ShareServerNotFound, db_api.share_server_search_by_identifier, self.ctxt, identifier) diff --git a/releasenotes/notes/fix_share_server_manager_match_id-276202295539dc0f.yaml b/releasenotes/notes/fix_share_server_manager_match_id-276202295539dc0f.yaml new file mode 100644 index 0000000000..c5086030f6 --- /dev/null +++ b/releasenotes/notes/fix_share_server_manager_match_id-276202295539dc0f.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fix share server manager API match of identifiers by preffix. "Invalid + input received Error" now raises only if the identifiers suffixes matches. + For more details, please refer to + `launchpad bug #1982429 `_