def test_script_run(self): msg = str(uuid.uuid4()) _, tmpfilepath = tempfile.mkstemp() bash_script = """#!/bin/bash echo $1 > %s """ % tmpfilepath sha256 = hashlib.sha256() sha256.update(bash_script.encode()) actual_checksum = sha256.hexdigest() b64_script = base64.b64encode(bash_script.encode()).decode() arguments = {'b64script': b64_script, 'checksum': actual_checksum, 'arguments': [msg]} plugin = run_script_plugin.load_plugin( self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) result = plugin.run() result = result.get_reply_doc() self.assertEqual(result['return_code'], 0) self.assertTrue(os.path.exists(tmpfilepath)) with open(tmpfilepath, "r") as fptr: data = fptr.read().strip() self.assertEqual(data, msg)
def test_script_run(self): msg = str(uuid.uuid4()) _, tmpfilepath = tempfile.mkstemp() bash_script = """#!/bin/bash echo $1 > %s """ % tmpfilepath sha256 = hashlib.sha256() sha256.update(bash_script.encode()) actual_checksum = sha256.hexdigest() b64_script = base64.b64encode(bash_script.encode()).decode() arguments = { 'b64script': b64_script, 'checksum': actual_checksum, 'arguments': [msg] } plugin = run_script_plugin.load_plugin(self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) result = plugin.run() result = result.get_reply_doc() self.assertEqual(result['return_code'], 0) self.assertTrue(os.path.exists(tmpfilepath)) with open(tmpfilepath, "r") as fptr: data = fptr.read().strip() self.assertEqual(data, msg)
def test_zipped_python_run(self): py_script = """import sys print(sys.executable) """ compressed_script = zlib.compress(py_script.encode()) b64_py = base64.b64encode(compressed_script).decode() sha256 = hashlib.sha256() # fake a checksum for failure sha256.update(py_script.encode()) actual_checksum = sha256.hexdigest() arguments = { 'b64script': b64_py, 'inpython': True, 'compression': 'gzip', 'checksum': actual_checksum } plugin = run_script_plugin.load_plugin(self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) result = plugin.run() result = result.get_reply_doc() self.assertEqual(result['return_code'], 0) self.assertEqual(sys.executable, result['message'].strip())
def test_script_run_bad_checksum(self): msg = str(uuid.uuid4()) _, tmpfilepath = tempfile.mkstemp() bash_script = """#!/bin/bash echo $1 > %s """ % tmpfilepath sha256 = hashlib.sha256() sha256.update(str(uuid.uuid4()).encode()) actual_checksum = sha256.hexdigest() b64_script = base64.b64encode(bash_script.encode()).decode() arguments = {'b64script': b64_script, 'checksum': actual_checksum, 'arguments': [msg]} plugin = run_script_plugin.load_plugin( self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) self.assertRaises(AgentPluginOperationException, plugin.run)
def test_python_run(self): py_script = """import sys print(sys.executable) """ b64_py = base64.b64encode(py_script.encode()).decode() sha256 = hashlib.sha256() # fake a checksum for failure sha256.update(py_script.encode()) actual_checksum = sha256.hexdigest() arguments = {'b64script': b64_py, 'inpython': True, 'checksum': actual_checksum} plugin = run_script_plugin.load_plugin( self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) result = plugin.run() result = result.get_reply_doc() self.assertEqual(result['return_code'], 0) self.assertEqual(sys.executable, result['message'].strip())
def test_script_run_bad_checksum(self): msg = str(uuid.uuid4()) _, tmpfilepath = tempfile.mkstemp() bash_script = """#!/bin/bash echo $1 > %s """ % tmpfilepath sha256 = hashlib.sha256() sha256.update(str(uuid.uuid4()).encode()) actual_checksum = sha256.hexdigest() b64_script = base64.b64encode(bash_script.encode()).decode() arguments = { 'b64script': b64_script, 'checksum': actual_checksum, 'arguments': [msg] } plugin = run_script_plugin.load_plugin(self.conf_obj, str(uuid.uuid4()), {}, "run_script", arguments) self.assertRaises(AgentPluginOperationException, plugin.run)