diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 3aaae69fac..f469809647 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -8,6 +8,7 @@ import hmac import logging import time import json +import base64 from urlparse import urljoin # Django @@ -705,10 +706,10 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin): return '{}'.format(self.inventory.id) def memcached_fact_host_key(self, host_name): - return '{}-{}'.format(self.inventory.id, host_name) + return '{}-{}'.format(self.inventory.id, base64.b64encode(host_name)) def memcached_fact_modified_key(self, host_name): - return '{}-{}-modified'.format(self.inventory.id, host_name) + return '{}-{}-modified'.format(self.inventory.id, base64.b64encode(host_name)) def _get_inventory_hosts(self, only=['name', 'ansible_facts', 'modified',]): return self.inventory.hosts.only(*only) diff --git a/awx/main/tests/unit/models/test_jobs.py b/awx/main/tests/unit/models/test_jobs.py index 9b2e3a60d3..0e9113cf7a 100644 --- a/awx/main/tests/unit/models/test_jobs.py +++ b/awx/main/tests/unit/models/test_jobs.py @@ -8,6 +8,7 @@ from awx.main.models import ( import datetime import json +import base64 from dateutil.tz import tzutc @@ -89,8 +90,8 @@ def test_start_job_fact_cache(hosts, job, inventory, mocker): job._get_memcache_connection().set.assert_any_call('5', [h.name for h in hosts]) for host in hosts: - job._get_memcache_connection().set.assert_any_call('{}-{}'.format(5, host.name), json.dumps(host.ansible_facts)) - job._get_memcache_connection().set.assert_any_call('{}-{}-modified'.format(5, host.name), host.ansible_facts_modified.isoformat()) + job._get_memcache_connection().set.assert_any_call('{}-{}'.format(5, base64.b64encode(host.name)), json.dumps(host.ansible_facts)) + job._get_memcache_connection().set.assert_any_call('{}-{}-modified'.format(5, base64.b64encode(host.name)), host.ansible_facts_modified.isoformat()) def test_start_job_fact_cache_existing_host(hosts, hosts2, job, job2, inventory, mocker): @@ -98,15 +99,15 @@ def test_start_job_fact_cache_existing_host(hosts, hosts2, job, job2, inventory, job.start_job_fact_cache() for host in hosts: - job._get_memcache_connection().set.assert_any_call('{}-{}'.format(5, host.name), json.dumps(host.ansible_facts)) - job._get_memcache_connection().set.assert_any_call('{}-{}-modified'.format(5, host.name), host.ansible_facts_modified.isoformat()) + job._get_memcache_connection().set.assert_any_call('{}-{}'.format(5, base64.b64encode(host.name)), json.dumps(host.ansible_facts)) + job._get_memcache_connection().set.assert_any_call('{}-{}-modified'.format(5, base64.b64encode(host.name)), host.ansible_facts_modified.isoformat()) job._get_memcache_connection().set.reset_mock() job2.start_job_fact_cache() # Ensure hosts2 ansible_facts didn't overwrite hosts ansible_facts - ansible_facts_cached = job._get_memcache_connection().get('{}-{}'.format(5, hosts2[0].name)) + ansible_facts_cached = job._get_memcache_connection().get('{}-{}'.format(5, base64.b64encode(hosts2[0].name))) assert ansible_facts_cached == json.dumps(hosts[1].ansible_facts) diff --git a/awx/plugins/fact_caching/tower.py b/awx/plugins/fact_caching/tower.py index a6f8852362..86624f75da 100755 --- a/awx/plugins/fact_caching/tower.py +++ b/awx/plugins/fact_caching/tower.py @@ -33,6 +33,7 @@ import os import memcache import json import datetime +import base64 from dateutil import parser from dateutil.tz import tzutc @@ -56,10 +57,10 @@ class CacheModule(BaseCacheModule): return '{}'.format(self._inventory_id) def translate_host_key(self, host_name): - return '{}-{}'.format(self._inventory_id, host_name) + return '{}-{}'.format(self._inventory_id, base64.b64encode(host_name)) def translate_modified_key(self, host_name): - return '{}-{}-modified'.format(self._inventory_id, host_name) + return '{}-{}-modified'.format(self._inventory_id, base64.b64encode(host_name)) def get(self, key): host_key = self.translate_host_key(key)