From adad5f32c082f658548643a7938aca5a595648e7 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Fri, 23 Dec 2022 12:29:59 -0600 Subject: [PATCH 1/3] Update server to account for exit_code from agent --- app/objects/secondclass/c_result.py | 4 +++- app/service/contact_svc.py | 6 ++++-- app/service/file_svc.py | 4 ++-- tests/services/test_contact_svc.py | 6 ++++++ tests/services/test_file_svc.py | 9 +++++---- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/objects/secondclass/c_result.py b/app/objects/secondclass/c_result.py index 86e9b718a..562c81a97 100644 --- a/app/objects/secondclass/c_result.py +++ b/app/objects/secondclass/c_result.py @@ -7,6 +7,7 @@ class ResultSchema(ma.Schema): id = ma.fields.String() output = ma.fields.String() stderr = ma.fields.String() + exit_code = ma.fields.String() pid = ma.fields.String() status = ma.fields.String() agent_reported_time = ma.fields.DateTime(format=BaseObject.TIME_FORMAT, missing=None) @@ -26,11 +27,12 @@ class Result(BaseObject): schema = ResultSchema() - def __init__(self, id, output, stderr="", pid=0, status=0, agent_reported_time=None): + def __init__(self, id, output, stderr="", exit_code="", pid=0, status=0, agent_reported_time=None): super().__init__() self.id = id self.output = output self.stderr = stderr + self.exit_code = exit_code self.pid = pid self.status = status self.agent_reported_time = agent_reported_time diff --git a/app/service/contact_svc.py b/app/service/contact_svc.py index eeb81ded9..b005210db 100644 --- a/app/service/contact_svc.py +++ b/app/service/contact_svc.py @@ -126,7 +126,8 @@ async def _save(self, result): result.output = await self._postprocess_link_result(result.output, link) command_results = json.dumps(dict( stdout=self.decode_bytes(result.output, strip_newlines=False), - stderr=self.decode_bytes(result.stderr, strip_newlines=False))) + stderr=self.decode_bytes(result.stderr, strip_newlines=False), + exit_code=result.exit_code)) encoded_command_results = self.encode_string(command_results) self.get_service('file_svc').write_result_file(result.id, encoded_command_results) operation = await self.get_service('app_svc').find_op_with_link(result.id) @@ -145,7 +146,8 @@ async def _save(self, result): else: command_results = json.dumps(dict( stdout=self.decode_bytes(result.output, strip_newlines=False), - stderr=self.decode_bytes(result.stderr, strip_newlines=False))) + stderr=self.decode_bytes(result.stderr, strip_newlines=False), + exit_code=result.exit_code)) encoded_command_results = self.encode_string(command_results) self.get_service('file_svc').write_result_file(result.id, encoded_command_results) except Exception as e: diff --git a/app/service/file_svc.py b/app/service/file_svc.py index ee9b315a9..31ff1aba8 100644 --- a/app/service/file_svc.py +++ b/app/service/file_svc.py @@ -126,10 +126,10 @@ def read_result_file(self, link_id, location='data/results'): return decoded_buf except json.JSONDecodeError: results = json.dumps(dict( - stdout=self.decode_bytes(decoded_buf, strip_newlines=False), stderr='')) + stdout=self.decode_bytes(decoded_buf, strip_newlines=False), stderr='', exit_code='')) return self.encode_string(str(results)) except binascii.Error: - results = json.dumps(dict(stdout=decoded_buf, stderr='')) + results = json.dumps(dict(stdout=decoded_buf, stderr='', exit_code='')) return self.encode_string(str(results)) def write_result_file(self, link_id, output, location='data/results'): diff --git a/tests/services/test_contact_svc.py b/tests/services/test_contact_svc.py index 4ccd6cb8c..ee56d5833 100644 --- a/tests/services/test_contact_svc.py +++ b/tests/services/test_contact_svc.py @@ -45,12 +45,14 @@ class TestContactSvc: async def test_save_ability_hooks(self, setup_contact_service, contact_svc, event_svc): test_string = b'test_string' err_string = b'err_string' + test_exit_code = "-1" link = setup_contact_service rest_svc = RestService() result = dict( id=link.id, output=str(base64.b64encode(base64.b64encode(test_string)), 'utf-8'), stderr=str(base64.b64encode(err_string), 'utf-8'), + exit_code=test_exit_code, pid=0, status=0 ) @@ -60,6 +62,7 @@ async def test_save_ability_hooks(self, setup_contact_service, contact_svc, even assert result_dict['stdout'] == test_string.decode() assert result_dict['stderr'] == err_string.decode() + assert result_dict['exit_code'] == test_exit_code # cleanup test try: @@ -70,11 +73,13 @@ async def test_save_ability_hooks(self, setup_contact_service, contact_svc, even async def test_save_ability_hooks_with_no_link(self, setup_contact_service, contact_svc, event_svc, file_svc): test_string = b'test_string' err_string = b'err_string' + test_exit_code = "0" # Send version with link for comparison result = dict( id="12345", output=str(base64.b64encode(test_string), 'utf-8'), stderr=str(base64.b64encode(err_string), 'utf-8'), + exit_code=test_exit_code, pid=0, status=0 ) @@ -85,6 +90,7 @@ async def test_save_ability_hooks_with_no_link(self, setup_contact_service, cont result_dict = json.loads(base64.b64decode(result)) assert result_dict['stdout'] == test_string.decode() assert result_dict['stderr'] == err_string.decode() + assert result_dict['exit_code'] == test_exit_code # cleanup test try: diff --git a/tests/services/test_file_svc.py b/tests/services/test_file_svc.py index f8ba69aec..bf007269b 100644 --- a/tests/services/test_file_svc.py +++ b/tests/services/test_file_svc.py @@ -54,10 +54,11 @@ def test_read_write_result_file(self, tmpdir, file_svc): link_id = '12345' output = 'output testing unit' error = 'error testing unit' - output_encoded = str(b64encode(json.dumps(dict(stdout=output, stderr=error)).encode()), 'utf-8') + test_exit_code = '0' + output_encoded = str(b64encode(json.dumps(dict(stdout=output, stderr=error, exit_code=test_exit_code)).encode()), 'utf-8') file_svc.write_result_file(link_id=link_id, output=output_encoded, location=tmpdir) - expected_output = dict(stdout=output, stderr=error) + expected_output = dict(stdout=output, stderr=error, exit_code=test_exit_code) output_data = file_svc.read_result_file(link_id=link_id, location=tmpdir) decoded_output_data = json.loads(base64.b64decode(output_data)) assert decoded_output_data == expected_output @@ -68,7 +69,7 @@ def test_read_write_result_file_no_dict(self, tmpdir, file_svc): output_encoded = str(b64encode(output.encode()), 'utf-8') file_svc.write_result_file(link_id=link_id, output=output_encoded, location=tmpdir) - expected_output = {'stdout': output, 'stderr': ''} + expected_output = {'stdout': output, 'stderr': '', 'exit_code': ''} output_data = file_svc.read_result_file(link_id=link_id, location=tmpdir) decoded_output_data = json.loads(base64.b64decode(output_data)) assert decoded_output_data == expected_output @@ -78,7 +79,7 @@ def test_read_write_result_file_no_base64(self, tmpdir, file_svc): output = 'output testing unit' file_svc.write_result_file(link_id=link_id, output=output, location=tmpdir) - expected_output = {'stdout': output, 'stderr': ''} + expected_output = {'stdout': output, 'stderr': '', 'exit_code': ''} output_data = file_svc.read_result_file(link_id=link_id, location=tmpdir) decoded_output_data = json.loads(base64.b64decode(output_data)) assert decoded_output_data == expected_output From b0353080746fd3aa54508f5e04c44fd203e3c7e4 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Fri, 13 Jan 2023 13:51:27 -0600 Subject: [PATCH 2/3] Updated Operations UI to include exit codes --- templates/operations.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/operations.html b/templates/operations.html index 9d8825d0d..697b7119e 100644 --- a/templates/operations.html +++ b/templates/operations.html @@ -427,13 +427,14 @@

Operations

-

+

+

-

+

From d18d0c1abd4f153fafa8b21d08561cf59e9bd264 Mon Sep 17 00:00:00 2001 From: mkouremetis Date: Thu, 9 Feb 2023 16:28:10 -0500 Subject: [PATCH 3/3] styling --- app/objects/secondclass/c_result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/objects/secondclass/c_result.py b/app/objects/secondclass/c_result.py index 562c81a97..b0790e42b 100644 --- a/app/objects/secondclass/c_result.py +++ b/app/objects/secondclass/c_result.py @@ -27,7 +27,7 @@ class Result(BaseObject): schema = ResultSchema() - def __init__(self, id, output, stderr="", exit_code="", pid=0, status=0, agent_reported_time=None): + def __init__(self, id, output, stderr='', exit_code='', pid=0, status=0, agent_reported_time=None): super().__init__() self.id = id self.output = output