From d4e3127fb461c64f4451a3564605a62abe7063c4 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Fri, 12 Oct 2018 12:29:46 -0400 Subject: [PATCH] properly handle AnsibleVaultEncryptedUnicode objects in the callback --- awx/main/queue.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/awx/main/queue.py b/awx/main/queue.py index 8f5b680f84..57a784321e 100644 --- a/awx/main/queue.py +++ b/awx/main/queue.py @@ -2,6 +2,7 @@ # All Rights Reserved. # Python +import json import logging import os @@ -12,10 +13,31 @@ from django.conf import settings # Kombu from kombu import Connection, Exchange, Producer +from kombu.serialization import registry __all__ = ['CallbackQueueDispatcher'] +# use a custom JSON serializer so we can properly handle !unsafe and !vault +# objects that may exist in events emitted by the callback plugin +# see: https://github.com/ansible/ansible/pull/38759 +class AnsibleJSONEncoder(json.JSONEncoder): + + def default(self, o): + if getattr(o, 'yaml_tag') == '!vault': + return o.data + return super(AnsibleJSONEncoder, self).default(o) + + +registry.register( + 'json-ansible', + lambda obj: json.dumps(obj, cls=AnsibleJSONEncoder), + lambda obj: json.loads(obj), + content_type='application/json', + content_encoding='utf-8' +) + + class CallbackQueueDispatcher(object): def __init__(self): @@ -41,7 +63,7 @@ class CallbackQueueDispatcher(object): producer = Producer(self.connection) producer.publish(obj, - serializer='json', + serializer='json-ansible', compression='bzip2', exchange=self.exchange, declare=[self.exchange],