From 8ae03f5811eac34efad9acbbb769af3b036779ac Mon Sep 17 00:00:00 2001 From: bom Date: Tue, 18 Jul 2023 12:18:57 +0200 Subject: [PATCH] Implement execute_secure Allows us to sanitize commands involving sensitive information like passwords, before throwing exceptions --- .../ddadevops/infrastructure/infrastructure.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 1565064..4cacdec 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -101,9 +101,15 @@ class ExecutionApi: raise exc return output - # TODO: check for exception handling - # TODO: can we return the output here also? - # TODO: should we also print stderr? + def execute_secure(self, command: str, sanitized_command: str, dry_run=False, shell=True, check=True): + try: + output = self.execute(command, dry_run, shell, check) + return output + except CalledProcessError as exc: + sanitized_exc = exc + sanitized_exc.cmd = sanitized_command + raise sanitized_exc + def execute_live(self, command: str, dry_run=False, shell=True): if dry_run: print(command)