Skip to content

Commit fd669cf

Browse files
fix: don't crash when mariadb connection string doesn't include port or user [backport 1.18] (#6689)
Backport 6a6e688 from #6682 to 1.18. This change fixes #6203 by supplying defaults for the accesses of `port` and `user` fields from the MariaDB connection info object. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Emmett Butler <[email protected]>
1 parent f49bd41 commit fd669cf

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

ddtrace/contrib/mariadb/patch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def unpatch():
3939
def _connect(func, instance, args, kwargs):
4040
conn = func(*args, **kwargs)
4141
tags = {
42-
net.TARGET_HOST: kwargs["host"],
43-
net.TARGET_PORT: kwargs["port"],
44-
db.USER: kwargs["user"],
45-
db.NAME: kwargs["database"],
42+
net.TARGET_HOST: kwargs.get("host", "127.0.0.1"),
43+
net.TARGET_PORT: kwargs.get("port", 3306),
44+
db.USER: kwargs.get("user", "test"),
45+
db.NAME: kwargs.get("database", "test"),
4646
db.SYSTEM: "mariadb",
4747
}
4848

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
mariadb: This fix resolves an issue where MariaDB connection information objects not including the user
5+
or port caused exceptions to be raised.

tests/contrib/mariadb/test_mariadb.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ def connection(tracer):
5151
yield connection
5252

5353

54+
def test_connection_no_port_or_user_does_not_raise():
55+
conf = MARIADB_CONFIG.copy()
56+
del conf["port"]
57+
del conf["user"]
58+
try:
59+
mariadb.connect(**conf)
60+
except mariadb.OperationalError as exc:
61+
# this error is expected because mariadb defaults user to root when not given
62+
if "Access denied for user 'root'" not in str(exc):
63+
raise exc
64+
65+
5466
def test_simple_query(connection, tracer):
5567
cursor = connection.cursor()
5668
cursor.execute("SELECT 1")

0 commit comments

Comments
 (0)