Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UTF-8 data in the JSON response for ansible 2.18< #693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions plugins/module_utils/mongodb_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from ansible.module_utils.basic import missing_required_lib # pylint: disable=unused-import:
from ansible.module_utils.six.moves import configparser
from ansible.module_utils._text import to_native
from bson.binary import Binary
import base64
from uuid import UUID
import traceback
import os
import ssl as ssl_lib
Expand Down Expand Up @@ -33,7 +36,7 @@
pymongo_found = False

try:
TYPES_NEED_TO_CONVERT = (Timestamp, ObjectId)
TYPES_NEED_TO_CONVERT = (Timestamp, ObjectId, Binary, bytes)
except NameError:
pass # sanity tests

Expand Down Expand Up @@ -466,7 +469,17 @@ def convert_to_supported(val):
return str(val)
elif isinstance(val, ObjectId):
return str(val)

# This is for replicasets in the output of general.signature.hash segment
# This is intended to solve userId output of
elif isinstance(val, bytes):
if len(val) == 16:
return str(UUID(bytes=val))
elif len(val) == 20:
# Signature hash
return str(base64.b64encode(val).decode('utf-8'))
else:
# We dont know what this is but we try to handle it to avoid errors.
return val.decode('utf-8'))
return val # By default returns the same value


Expand All @@ -484,4 +497,4 @@ def convert_bson_values_recur(mydict):
mydict[key] = convert_to_supported(value)
else:
mydict[key] = value
return mydict
return mydict
Loading