[BUG] binary return data causes tracebacks in many returners
Description Some returners return binary data. The default returner uses msgpack, which can handle binary data.
19/40 returners in salt/returners use salt.utils.json.dumps, which cannot handle binary data.
Maybe binary data should be b64 encoded instead of the return data being dropped?
grep 'json.dumps' salt/returners | awk -F : '{print $1}' | uniq:
cassandra_cql_return.py
couchbase_return.py
couchdb_return.py
elasticsearch_return.py
etcd_return.py
highstate_return.py
influxdb_return.py
kafka_return.py
mattermost_returner.py
memcache_return.py
mysql.py
odbc.py
postgres.py
postgres_local_cache.py
redis_return.py
slack_webhook_return.py
splunk.py
sqlite3_return.py
syslog_return.py
Maybe binary data should be b64 encoded instead of the return data being dropped?
It's probably because the returners were all written for Python 2, where str and bytes are interchangeable, and never updated.
Any state that is still returning bytes should be fixed to return str, but it wouldn't hurt to also allow the returners to handle both.
Joe was pulled off of some work in AL for other priorities, moving this to Si release cycle
@dhiltonp @xeacott @danielrobbins there are several of these issues where Python2 expected either strings or bytes and some modules were written with one or the other and likely not both, I created a label, if anyone can create PRs please do. I hope to get most of these work done in Silicon release cycle or at least planned and worked in Silicon and soon after, if there is a global approach (doubtful) we may want to explore it.
This issue still present in 3006.1 right ? Trying to use odbc returner and getting errors like [WARNING ] Returner unavailable: the JSON object must be str, bytes or bytearray, not 'Row' TypeError: the JSON object must be str, bytes or bytearray, not 'Row'
A recent salt release (I went from 3006.9 -> 3006.12) added the tok return to all minion payloads, which is encoded, that caused me problems when using the API.
I implemented a local patch on the salt-master to sanitize the data:
--- /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/json.py.dist 2025-06-16 17:08:27.484890052 +0100
+++ /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/json.py 2025-06-16 17:19:28.804754435 +0100
@@ -152,6 +152,22 @@
kwargs["ensure_ascii"] = False
return json_module.dump(obj, fp, **kwargs)
+def _sanitize_for_json(obj):
+ """
+ Recursively clean up an object to make it safe for JSON serialization.
+ Non-serializable types are converted to strings.
+ """
+ if isinstance(obj, dict):
+ return {_sanitize_for_json(k): _sanitize_for_json(v) for k, v in obj.items()}
+ elif isinstance(obj, list):
+ return [_sanitize_for_json(v) for v in obj]
+ elif isinstance(obj, tuple):
+ return tuple(_sanitize_for_json(v) for v in obj)
+ elif isinstance(obj, (str, int, float, bool)) or obj is None:
+ return obj
+ else:
+ # Convert non-serializable object to string
+ return str(obj)
def dumps(obj, **kwargs):
"""
@@ -171,4 +187,4 @@
json_module = kwargs.pop("_json_module", json)
if "ensure_ascii" not in kwargs:
kwargs["ensure_ascii"] = False
- return json_module.dumps(obj, **kwargs)
+ return json_module.dumps(_sanitize_for_json(obj), **kwargs)