Skip to content

fix: keep auth tokens, secret values and statement text out of debug logs, errors and reprs - #1340

Merged
JuanLeee merged 1 commit into
mainfrom
fix/keep-secrets-out-of-logs-and-errors
Nov 12, 2026
Merged

JuanLeee merged 1 commit into
mainfrom
fix/keep-secrets-out-of-logs-and-errors

Conversation

@JuanLeee

@JuanLeee JuanLeee commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Description

With DEBUG logging on, the wrapper writes the RDS auth token that iam, federated_auth and okta connect with into its own log lines, and the Secrets Manager plugin's fetch lets botocore log the whole GetSecretValue response, password included. Two error paths leak as well: the old-connection AwsWrapperError quotes a mysql-connector cursor's last statement with its values, and Properties prints the raw password in repr(), tracebacks and pytest reports.

This PR removes each value where it enters the text and leaves the rest of what is logged or raised as it was.

  1. IamAuthUtils.generate_authentication_token, IamAuthPlugin._connect, FederatedAuthPlugin._connect, OktaAuthPlugin._connect and AsyncFederatedAuthPlugin._resolve_credentials passed the token to logger.debug, and four message texts printed it with = {}. The token works as the database password until it expires, so the DEBUG log held a working credential. The calls now pass no argument and the texts drop = {}, so the lines still say that a token was generated or reused. Reproduced on Aurora PostgreSQL and Aurora MySQL with IAM database authentication: the token was in the DEBUG records before this change and in none after.
  2. botocore logs every response body at DEBUG on the botocore.parsers logger, and a GetSecretValue body holds SecretString. A new helper, _get_secret_value in aws_secrets_manager_plugin.py, now makes the call for both AwsSecretsManagerPlugin and AsyncAwsSecretsManagerPlugin. While the call runs, a filter on botocore.parsers drops records on the calling thread only, so records from other threads and records after the call are kept, and an application's own boto3 calls still log. The filter is added on each call and never removed, because addFilter skips a filter already present and removing it could race with another thread's logging. Reproduced with LocalStack Secrets Manager and with an RDS-managed master secret on Aurora, sync and async.
  3. PluginManager.execute and AbstractReadWriteSplittingPlugin.execute formatted the driver object into the PluginManager.MethodInvokedAgainstOldConnection message. A mysql-connector cursor's str() is its class name plus the first 40 characters of its last statement with the values filled in, so literals and bound parameters reached error text, logs and error reporters, and a psycopg cursor's showed the host, user and database. Both sites now pass type(target).__name__, so the message names the class ('CMySQLCursor'). Reproduced with mysql-connector's C extension and pure builds through srw, on MySQL 8 and on Aurora MySQL.
  4. Properties is a dict subclass with no __repr__, so repr(), str(), f-strings, traceback with capture_locals and pytest's failure reports showed every value, password included, although the wrapper's logs already mask it with PropertiesUtils.mask_properties. Properties.__repr__ now returns the dict repr of the masked copy, and the stored values don't change. On PostgreSQL 16 and MySQL 8, a failed connect's traceback had 19 and 17 Properties locals showing the password before this change and none after.

Behaviour changes

  1. With DEBUG on, the "Generated new authentication token" and "Used cached ... token" lines end without the token.
  2. The wrapper's own GetSecretValue calls leave no botocore.parsers records on their thread, headers included.
  3. The old-connection message names a class instead of the object, which also takes psycopg's host, user and database out of the sync message. The async check in aio/plugin_manager.py is unchanged, because psycopg's async cursor text holds no statement and aiomysql's cursor has no text of its own, so the sync and async messages now differ.
  4. repr(), str() and f-strings of a Properties show '***' for every key containing password or passwd, as the logs already do.

Not in scope

Each of these needs a filter on a third-party logger, so they're left for a separate change.

  1. At DEBUG on the root logger or botocore.auth, botocore's CanonicalRequest and Signature records hold every token parameter and the signature, which is enough to rebuild the token.
  2. For federated_auth and okta, botocore can log the STS AssumeRoleWithSAML response, which holds temporary AWS credentials (from reading botocore/parsers.py; not reproduced).
  3. mysql-connector-python 9.7.0's pure build logs the password it sends at DEBUG when the server switches the auth plugin to sha256_password or mysql_clear_password. With iam that password is the token. It does this without the wrapper too, and logging.getLogger("mysql.connector").setLevel(logging.INFO) avoids it.

The drivers' own frames in a traceback still hold their unmasked dicts.

Tests

The 14 new unit test cases (7 for the token lines, 2 for Secrets Manager, 3 for the old-connection message, 2 for Properties) fail on main and pass with this change. The token tests also check that no Logger.debug call receives the token, and a guard checks that another thread's botocore.parsers record and one logged after the fetch still appear. pytest tests/unit -Werror passes 2458 tests on Python 3.12 and 3.10, with flake8, isort and mypy clean. Live checks outside this PR fail on main and pass with this change: 27 cases against PostgreSQL 16, MySQL 8 and LocalStack Secrets Manager, and 15 on Aurora PostgreSQL and Aurora MySQL (Serverless v2, IAM database authentication, RDS-managed secrets). A differential run of 783 cases across plugin chains found only the expected old-connection message change (6 cases), and 51 cases on Aurora found none. The integration test suite wasn't run. No CHANGELOG entry, message key or docs change.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@JuanLeee
JuanLeee merged commit 68a5a34 into main Nov 12, 2026
9 checks passed
@JuanLeee
JuanLeee deleted the fix/keep-secrets-out-of-logs-and-errors branch November 12, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants