Skip to content

Commit 790e6e9

Browse files
committed
WL#16754: The host wildcard no longer applies to localhost
The unittests are updated to reflect on the fact that the host wildcard match behaviour of schema and object level grants are not going to be applicable to localhost anymore. Change-Id: I2cce0a2b7bd266dc0dde84cc03274ad610112476
1 parent ed9e248 commit 790e6e9

File tree

7 files changed

+72
-52
lines changed

7 files changed

+72
-52
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Full release notes:
1111
v9.3.0
1212
======
1313

14+
- WL#16754: The host wildcard no longer applies to localhost
1415
- WL#16752: Deprecate class methods to access instance data or to know instance internal state
1516
- WL#16327: Remove Cursors Prepared Raw and Named Tuple
1617
- BUG#37541353: (Contribution) Fix typing annotation of MySQLConnectionAbstract's close function

mysql-connector-python/tests/otel/context_propagation/sanity_check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023, 2024, Oracle and/or its affiliates.
1+
# Copyright (c) 2023, 2025, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -209,12 +209,12 @@ def __init__(self, **config) -> None:
209209
self._config = config.copy()
210210
self._tracer = None
211211
self._mysql_config = {
212+
"host": config["mysql_host"],
212213
"username": config["mysql_user"],
213214
"password": config["mysql_password"],
214215
"port": config["mysql_port"],
215216
"use_pure": config["cpy_use_pure"],
216217
}
217-
218218
self._init_otel()
219219

220220
def _init_otel(self):
@@ -250,10 +250,10 @@ def test_simple_stmts(
250250
new_user_password = "s3cr3t"
251251
new_database = "colors"
252252
new_user_stmt = (
253-
f"CREATE USER '{new_user_name}'@'%' IDENTIFIED BY '{new_user_password}'"
253+
f"CREATE USER '{new_user_name}'@'{self._mysql_config['host']}' IDENTIFIED BY '{new_user_password}'"
254254
)
255255
grant_stmt = (
256-
f"GRANT ALL PRIVILEGES ON *.* TO '{new_user_name}'@'%' WITH GRANT OPTION"
256+
f"GRANT ALL PRIVILEGES ON *.* TO '{new_user_name}'@'{self._mysql_config['host']}' WITH GRANT OPTION"
257257
)
258258
table_name = "employees"
259259
create_stmt = f"""CREATE TABLE {table_name} (

mysql-connector-python/tests/otel/test_instrumentation.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,10 @@ class PythonWithGlobalInstSpanTests(tests.MySQLConnectorTests):
371371
new_user_password = "s3cr3t"
372372
new_database = "colors"
373373
new_user_stmt = (
374-
f"CREATE USER '{new_user_name}'@'%' IDENTIFIED BY '{new_user_password}'"
374+
"CREATE USER '{new_user_name}'@'{server_host}' IDENTIFIED BY '{new_user_password}'"
375375
)
376376
grant_stmt = (
377-
f"GRANT ALL PRIVILEGES ON *.* TO '{new_user_name}'@'%' WITH GRANT OPTION"
377+
"GRANT ALL PRIVILEGES ON *.* TO '{new_user_name}'@'{server_host}' WITH GRANT OPTION"
378378
)
379379
table_name = "employees"
380380
create_stmt = f"""CREATE TABLE {table_name} (
@@ -412,6 +412,7 @@ def setUp(self) -> None:
412412
self.provider.add_span_processor(processor)
413413
self.tracer = trace.get_tracer(__name__, tracer_provider=self.provider)
414414
self.cnx_config = self.get_clean_mysql_config()
415+
self.server_host = self.cnx_config['host']
415416
self.cnx_config["use_pure"] = self.pure_python
416417

417418
# Instrumentor
@@ -569,9 +570,20 @@ def _run_client_app1(
569570
cur.execute(f"DROP TABLE IF EXISTS {self.table_name}")
570571

571572
# create a new user
572-
cur.execute(f"DROP USER IF EXISTS '{self.new_user_name}'")
573-
cur.execute(self.new_user_stmt)
574-
cur.execute(self.grant_stmt)
573+
cur.execute(f"DROP USER IF EXISTS '{self.new_user_name}'@'{self.server_host}'")
574+
cur.execute(
575+
self.new_user_stmt.format(
576+
new_user_name=self.new_user_name,
577+
server_host=self.server_host,
578+
new_user_password=self.new_user_password,
579+
)
580+
)
581+
cur.execute(
582+
self.grant_stmt.format(
583+
new_user_name=self.new_user_name,
584+
server_host=self.server_host,
585+
)
586+
)
575587
cur.execute("FLUSH PRIVILEGES")
576588
cur.execute(f"CREATE DATABASE IF NOT EXISTS {self.new_database}")
577589

mysql-connector-python/tests/qa/test_qa_caching_sha2_password.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021, 2024, Oracle and/or its affiliates.
1+
# Copyright (c) 2021, 2025, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -50,14 +50,15 @@ def test_caching_sha2_password_test1(self):
5050
for use_pure in self.use_pure_options:
5151
config = self.get_clean_mysql_config()
5252
config["use_pure"] = use_pure
53+
server_host = config["host"]
5354

5455
with mysql.connector.connect(**config) as cnx:
55-
cnx.cmd_query("DROP USER IF EXISTS 'sham'@'%'")
56+
cnx.cmd_query(f"DROP USER IF EXISTS 'sham'@'{server_host}'")
5657
cnx.cmd_query(
57-
"CREATE USER 'sham'@'%' IDENTIFIED "
58+
f"CREATE USER 'sham'@'{server_host}' IDENTIFIED "
5859
"WITH caching_sha2_password BY 'shapass'"
5960
)
60-
cnx.cmd_query("GRANT ALL ON *.* TO 'sham'@'%'")
61+
cnx.cmd_query(f"GRANT ALL ON *.* TO 'sham'@'{server_host}'")
6162

6263
config["user"] = "sham"
6364
config["password"] = "shapass"
@@ -79,22 +80,23 @@ def test_caching_sha2_password_test1(self):
7980
cur.execute("SELECT * FROM t1")
8081
self.assertEqual(1, len(cur.fetchone()))
8182
cur.execute("DROP TABLE IF EXISTS t1")
82-
cnx.cmd_query("DROP USER IF EXISTS 'sham'@'%'")
83+
cnx.cmd_query(f"DROP USER IF EXISTS 'sham'@'{server_host}'")
8384

8485
def test_caching_sha2_password_test3(self):
8586
"""Test full authentication with SSL after create user,
8687
flushing privileges, altering user, setting new password."""
8788
for use_pure in self.use_pure_options:
8889
config = self.get_clean_mysql_config()
8990
config["use_pure"] = use_pure
91+
server_host = config['host']
9092

9193
with mysql.connector.connect(**config) as cnx:
92-
cnx.cmd_query("DROP USER IF EXISTS 'sham'@'%'")
94+
cnx.cmd_query(f"DROP USER IF EXISTS 'sham'@'{server_host}'")
9395
cnx.cmd_query(
94-
"CREATE USER 'sham'@'%' IDENTIFIED "
96+
f"CREATE USER 'sham'@'{server_host}' IDENTIFIED "
9597
"WITH caching_sha2_password BY 'shapass'"
9698
)
97-
cnx.cmd_query("GRANT ALL ON *.* TO 'sham'@'%'")
99+
cnx.cmd_query(f"GRANT ALL ON *.* TO 'sham'@'{server_host}'")
98100
cnx.cmd_query("FLUSH PRIVILEGES")
99101

100102
config["user"] = "sham"
@@ -117,7 +119,7 @@ def test_caching_sha2_password_test3(self):
117119
cur.execute("SELECT * FROM t2")
118120
self.assertEqual(1, len(cur.fetchone()))
119121
cur.execute("DROP TABLE IF EXISTS t2")
120-
cur.execute("SET PASSWORD FOR 'sham'@'%'='newshapass'")
122+
cur.execute(f"SET PASSWORD FOR 'sham'@'{server_host}'='newshapass'")
121123
config["password"] = "newshapass"
122124
mysql.connector.connect(**config)
123-
cnx.cmd_query("DROP USER IF EXISTS 'sham'@'%'")
125+
cnx.cmd_query(f"DROP USER IF EXISTS 'sham'@'{server_host}'")

mysql-connector-python/tests/test_aio_authentication.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023, 2024, Oracle and/or its affiliates.
1+
# Copyright (c) 2023, 2025, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -1342,6 +1342,7 @@ class MySQLOpenIDConnectAuthPluginTests(MySQLConnectorAioTestCase):
13421342
@classmethod
13431343
def setUpClass(cls):
13441344
config = tests.get_mysql_config()
1345+
server_host = config['host']
13451346
plugin_ext = "dll" if os.name == "nt" else "so"
13461347
with mysql.connector.connect(**config) as cnx:
13471348
# Install the auth plugin `authentication_openid_connect`
@@ -1375,18 +1376,18 @@ def setUpClass(cls):
13751376
f"SET GLOBAL authentication_openid_connect_configuration = '{jwk}'"
13761377
)
13771378
# Create the user configured with OpenID connect
1378-
cnx.cmd_query("DROP USER IF EXISTS 'openid-test'@'%'")
1379+
cnx.cmd_query(f"DROP USER IF EXISTS 'openid-test'@'{server_host}'")
13791380
cnx.cmd_query(
1380-
"""CREATE USER 'openid-test'@'%' IDENTIFIED WITH 'authentication_openid_connect' AS
1381-
'{"identity_provider" : "myissuer", "user" : "mysubj"}'"""
1381+
f"""CREATE USER 'openid-test'@'{server_host}' IDENTIFIED WITH 'authentication_openid_connect' AS
1382+
'{{"identity_provider" : "myissuer", "user" : "mysubj"}}'"""
13821383
)
1383-
cnx.cmd_query("GRANT ALL ON *.* TO 'openid-test'")
1384+
cnx.cmd_query(f"GRANT ALL ON *.* TO 'openid-test'@'{server_host}'")
13841385

13851386
@classmethod
13861387
def tearDownClass(cls):
13871388
config = tests.get_mysql_config()
13881389
with mysql.connector.connect(**config) as cnx:
1389-
cnx.cmd_query("DROP USER IF EXISTS 'openid-test'@'%'")
1390+
cnx.cmd_query(f"DROP USER IF EXISTS 'openid-test'@'{config['host']}'")
13901391
try:
13911392
cnx.cmd_query("UNINSTALL PLUGIN authentication_openid_connect")
13921393
except ProgrammingError:

mysql-connector-python/tests/test_authentication.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2014, 2024, Oracle and/or its affiliates.
3+
# Copyright (c) 2014, 2025, Oracle and/or its affiliates.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -1373,12 +1373,13 @@ class MySQLOpenIDConnectAuthPluginTests(tests.MySQLConnectorTests):
13731373
"""
13741374

13751375
skip_reason = None
1376+
config = tests.get_mysql_config()
1377+
server_host = config['host']
13761378

13771379
@classmethod
13781380
def setUpClass(cls):
1379-
config = tests.get_mysql_config()
13801381
plugin_ext = "dll" if os.name == "nt" else "so"
1381-
with mysql.connector.connect(**config) as cnx:
1382+
with mysql.connector.connect(**cls.config) as cnx:
13821383
# Install the auth plugin `authentication_openid_connect`
13831384
try:
13841385
cnx.cmd_query(
@@ -1410,18 +1411,17 @@ def setUpClass(cls):
14101411
f"SET GLOBAL authentication_openid_connect_configuration = '{jwk}'"
14111412
)
14121413
# Create the user configured with OpenID connect
1413-
cnx.cmd_query("DROP USER IF EXISTS 'openid-test'@'%'")
1414+
cnx.cmd_query(f"DROP USER IF EXISTS 'openid-test'@'{cls.server_host}'")
14141415
cnx.cmd_query(
1415-
"""CREATE USER 'openid-test'@'%' IDENTIFIED WITH 'authentication_openid_connect' AS
1416-
'{"identity_provider" : "myissuer", "user" : "mysubj"}'"""
1416+
f"""CREATE USER 'openid-test'@'{cls.server_host}' IDENTIFIED WITH 'authentication_openid_connect' AS
1417+
'{{"identity_provider" : "myissuer", "user" : "mysubj"}}'"""
14171418
)
1418-
cnx.cmd_query("GRANT ALL ON *.* TO 'openid-test'")
1419+
cnx.cmd_query(f"GRANT ALL ON *.* TO 'openid-test'@'{cls.server_host}'")
14191420

14201421
@classmethod
14211422
def tearDownClass(cls):
1422-
config = tests.get_mysql_config()
1423-
with mysql.connector.connect(**config) as cnx:
1424-
cnx.cmd_query("DROP USER IF EXISTS 'openid-test'@'%'")
1423+
with mysql.connector.connect(**cls.config) as cnx:
1424+
cnx.cmd_query(f"DROP USER IF EXISTS 'openid-test'@'{cls.server_host}'")
14251425
try:
14261426
cnx.cmd_query("UNINSTALL PLUGIN authentication_openid_connect")
14271427
except ProgrammingError:

mysqlx-connector-python/tests/test_mysqlx_connection.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3+
# Copyright (c) 2016, 2025, Oracle and/or its affiliates.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -503,6 +503,7 @@ def send_auth_ok(self):
503503
class MySQLxSessionTests(tests.MySQLxTests):
504504
def setUp(self):
505505
self.connect_kwargs = tests.get_mysqlx_config()
506+
self.server_host = self.connect_kwargs['host']
506507
self.schema_name = self.connect_kwargs["schema"]
507508
try:
508509
self.session = mysqlx.get_session(self.connect_kwargs)
@@ -668,11 +669,11 @@ def test_auth(self):
668669
sess = mysqlx.get_session(self.connect_kwargs)
669670

670671
try:
671-
sess.sql("DROP USER 'native'@'%'").execute()
672+
sess.sql(f"DROP USER 'native'@'{self.server_host}'").execute()
672673
except mysqlx.errors.OperationalError:
673674
pass
674675
try:
675-
sess.sql("DROP USER 'sha256'@'%'").execute()
676+
sess.sql(f"DROP USER 'sha256'@'{self.server_host}'").execute()
676677
except mysqlx.errors.OperationalError:
677678
pass
678679

@@ -682,14 +683,14 @@ def test_auth(self):
682683
else "caching_sha2_password"
683684
)
684685
sess.sql(
685-
f"CREATE USER 'native'@'%' IDENTIFIED WITH {native_auth} BY 'test'"
686+
f"CREATE USER 'native'@'{self.server_host}' IDENTIFIED WITH {native_auth} BY 'test'"
686687
).execute()
687688
sess.sql(
688-
"CREATE USER 'sha256'@'%' IDENTIFIED WITH sha256_password BY 'sha256'"
689+
f"CREATE USER 'sha256'@'{self.server_host}' IDENTIFIED WITH sha256_password BY 'sha256'"
689690
).execute()
690691

691692
config = {
692-
"host": self.connect_kwargs["host"],
693+
"host": self.server_host,
693694
"port": self.connect_kwargs["port"],
694695
}
695696

@@ -711,8 +712,8 @@ def test_auth(self):
711712
config["auth"] = "mysql41"
712713
self.assertRaises(InterfaceError, mysqlx.get_session, config)
713714

714-
sess.sql("DROP USER 'native'@'%'").execute()
715-
sess.sql("DROP USER 'sha256'@'%'").execute()
715+
sess.sql(f"DROP USER 'native'@'{self.server_host}'").execute()
716+
sess.sql(f"DROP USER 'sha256'@'{self.server_host}'").execute()
716717
sess.close()
717718

718719
@unittest.skipIf(
@@ -722,7 +723,7 @@ def test_auth(self):
722723
def test_auth_sha265_memory(self):
723724
sess = mysqlx.get_session(self.connect_kwargs)
724725
sess.sql(
725-
"CREATE USER 'caching'@'%' IDENTIFIED WITH "
726+
f"CREATE USER 'caching'@'{self.server_host}' IDENTIFIED WITH "
726727
"caching_sha2_password BY 'caching'"
727728
).execute()
728729
config = {
@@ -750,7 +751,7 @@ def test_auth_sha265_memory(self):
750751
config["auth"] = mysqlx.Auth.SHA256_MEMORY
751752
mysqlx.get_session(config)
752753

753-
sess.sql("DROP USER 'caching'@'%'").execute()
754+
sess.sql(f"DROP USER 'caching'@'{self.server_host}'").execute()
754755
sess.close()
755756

756757
@unittest.skipIf(
@@ -1061,9 +1062,9 @@ def test_get_default_schema(self):
10611062

10621063
# Test BUG#28942938: 'ACCESS DENIED' error for unauthorized user tries
10631064
# to use the default schema if not exists at get_session
1064-
self.session.sql("DROP USER IF EXISTS 'def_schema'@'%'").execute()
1065+
self.session.sql(f"DROP USER IF EXISTS 'def_schema'@'{self.server_host}'").execute()
10651066
self.session.sql(
1066-
"CREATE USER 'def_schema'@'%' IDENTIFIED WITH "
1067+
f"CREATE USER 'def_schema'@'{self.server_host}' IDENTIFIED WITH "
10671068
"caching_sha2_password BY 'test'"
10681069
).execute()
10691070
settings = self.connect_kwargs.copy()
@@ -1078,7 +1079,7 @@ def test_get_default_schema(self):
10781079

10791080
# Grant privilege to one unrelated schema
10801081
self.session.sql(
1081-
"GRANT ALL PRIVILEGES ON nonexistent.* TO 'def_schema'@'%'"
1082+
f"GRANT ALL PRIVILEGES ON nonexistent.* TO 'def_schema'@'{self.server_host}'"
10821083
).execute()
10831084
with self.assertRaises(InterfaceError) as context:
10841085
_ = mysqlx.get_session(settings)
@@ -2604,16 +2605,19 @@ class WL14852(tests.MySQLxTests):
26042605
"""WL#14852: Align TLS and SSL options checking and behavior"""
26052606

26062607
def setUp(self):
2607-
session = mysqlx.get_session(tests.get_mysqlx_config())
2608+
config = tests.get_mysqlx_config()
2609+
session = mysqlx.get_session(config)
2610+
self.server_host = config['host']
2611+
26082612
session.sql(
2609-
"CREATE USER 'native'@'%' IDENTIFIED WITH "
2613+
f"CREATE USER 'native'@'{self.server_host}' IDENTIFIED WITH "
26102614
"mysql_native_password BY 'test'"
26112615
).execute()
2612-
session.sql("grant all privileges on *.* to 'native'@'%'").execute()
2616+
session.sql(f"grant all privileges on *.* to 'native'@'{self.server_host}'").execute()
26132617

26142618
def tearDown(self):
26152619
session = mysqlx.get_session(tests.get_mysqlx_config())
2616-
session.sql("DROP USER IF EXISTS 'native'@'%'").execute()
2620+
session.sql(f"DROP USER IF EXISTS 'native'@'{self.server_host}'").execute()
26172621

26182622
def _test_giving_ssl_disable_does_not_raise_error(self, use_uri=False):
26192623
"""Verify no error with ssl-mode=disable and other TLS or SSL options."""

0 commit comments

Comments
 (0)