From ac37b6dd4e2a6a459c162852d4096e5e43a549b3 Mon Sep 17 00:00:00 2001 From: "Dave St.Germain" Date: Wed, 26 Dec 2018 13:09:12 -0500 Subject: [PATCH] Check for file permissions --- edx_proctoring/apps.py | 13 +++++++++---- edx_proctoring/tests/test_workerconfig.py | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/edx_proctoring/apps.py b/edx_proctoring/apps.py index ba6e840b7be..916e6d23c9c 100644 --- a/edx_proctoring/apps.py +++ b/edx_proctoring/apps.py @@ -40,10 +40,15 @@ def make_worker_config(backends, out='/tmp/workers.json'): except KeyError: warnings.warn('%r does not contain a `main` entry' % package_file) if config: - with open(out, 'wb') as outfp: - json.dump(config, outfp) - os.chmod(out, 0o664) - return True + try: + with open(out, 'wb+') as outfp: + json.dump(config, outfp) + except IOError: + warnings.warn("Could not write worker config to %s" % out) + else: + # make sure that this file is group writable, because it may be written by different users + os.chmod(out, 0o664) + return True return False diff --git a/edx_proctoring/tests/test_workerconfig.py b/edx_proctoring/tests/test_workerconfig.py index be4add6fd3b..54fab9b5769 100644 --- a/edx_proctoring/tests/test_workerconfig.py +++ b/edx_proctoring/tests/test_workerconfig.py @@ -80,6 +80,13 @@ def test_no_module(self): self.assertFalse(make_worker_config([backend], self.outfile)) self._check_outfile(None) + def test_no_permission(self): + self.outfile = '/etc/workers-test.json' + backend = TestBackendProvider() + backend.npm_module = self._make_npm_module('test-1234', 'foo/bar/baz.js') + self.assertFalse(make_worker_config([backend], self.outfile)) + self._check_outfile(None) + @patch('django.conf.settings.NODE_MODULES_ROOT', None) def test_no_setting(self): backend = TestBackendProvider()