From 7d64f97cf44e63335cc3862688e18e65f602e13a Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 12 Jun 2019 11:54:52 +0200 Subject: [PATCH 1/2] Fix #856 --- IM/REST.py | 173 ++++++++++++++++++++++-------------------- IM/ServiceRequests.py | 2 +- 2 files changed, 91 insertions(+), 84 deletions(-) diff --git a/IM/REST.py b/IM/REST.py index cb0fdcf7f..1c5262c20 100644 --- a/IM/REST.py +++ b/IM/REST.py @@ -304,6 +304,13 @@ def format_output(res, default_type="text/plain", field_name=None, list_field_na return info +def get_ex_error(ex): + """ + Return a secure string with the error of the exception in Py2 and Py3 + """ + return getattr(ex, 'message', get_ex_error(ex) if len(ex.args) else repr(ex)) + + @app.route('/infrastructures/:infid', method='DELETE') def RESTDestroyInfrastructure(infid=None): try: @@ -316,14 +323,14 @@ def RESTDestroyInfrastructure(infid=None): bottle.response.content_type = "text/plain" return "" except DeletedInfrastructureException as ex: - return return_error(404, "Error Destroying Inf: %s" % ex.args[0]) + return return_error(404, "Error Destroying Inf: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Destroying Inf: %s" % ex.args[0]) + return return_error(404, "Error Destroying Inf: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Destroying Inf: %s" % ex.args[0]) + return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Destroying Inf") - return return_error(400, "Error Destroying Inf: %s" % ex.args[0]) + return return_error(400, "Error Destroying Inf: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid', method='GET') @@ -342,14 +349,14 @@ def RESTGetInfrastructureInfo(infid=None): return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: - return return_error(404, "Error Getting Inf. info: %s" % ex.args[0]) + return return_error(404, "Error Getting Inf. info: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Getting Inf. info: %s" % ex.args[0]) + return return_error(404, "Error Getting Inf. info: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Getting Inf. info: %s" % ex.args[0]) + return return_error(403, "Error Getting Inf. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. info") - return return_error(400, "Error Getting Inf. info: %s" % ex.args[0]) + return return_error(400, "Error Getting Inf. info: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/:prop', method='GET') @@ -428,14 +435,14 @@ def RESTGetInfrastructureProperty(infid=None, prop=None): return format_output(res, field_name=prop) except DeletedInfrastructureException as ex: - return return_error(404, "Error Getting Inf. prop: %s" % ex.args[0]) + return return_error(404, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Getting Inf. prop: %s" % ex.args[0]) + return return_error(404, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Getting Inf. prop: %s" % ex.args[0]) + return return_error(403, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. prop") - return return_error(400, "Error Getting Inf. prop: %s" % ex.args[0]) + return return_error(400, "Error Getting Inf. prop: %s" % get_ex_error(ex)) @app.route('/infrastructures', method='GET') @@ -454,10 +461,10 @@ def RESTGetInfrastructureList(): return format_output(res, "text/uri-list", "uri-list", "uri") except InvaliddUserException as ex: - return return_error(401, "Error Getting Inf. List: %s" % ex.args[0]) + return return_error(401, "Error Getting Inf. List: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. List") - return return_error(400, "Error Getting Inf. List: %s" % ex.args[0]) + return return_error(400, "Error Getting Inf. List: %s" % get_ex_error(ex)) @app.route('/infrastructures', method='POST') @@ -506,10 +513,10 @@ def RESTCreateInfrastructure(): return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: - return return_error(401, "Error Getting Inf. info: %s" % ex.args[0]) + return return_error(401, "Error Getting Inf. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Creating Inf.") - return return_error(400, "Error Creating Inf.: %s" % ex.args[0]) + return return_error(400, "Error Creating Inf.: %s" % get_ex_error(ex)) @app.route('/infrastructures', method='PUT') @@ -534,10 +541,10 @@ def RESTImportInfrastructure(): return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: - return return_error(401, "Error Impporting Inf.: %s" % ex.args[0]) + return return_error(401, "Error Impporting Inf.: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Impporting Inf.") - return return_error(400, "Error Impporting Inf.: %s" % ex.args[0]) + return return_error(400, "Error Impporting Inf.: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid', method='GET') @@ -551,18 +558,18 @@ def RESTGetVMInfo(infid=None, vmid=None): radl = InfrastructureManager.GetVMInfo(infid, vmid, auth) return format_output(radl, field_name="radl") except DeletedInfrastructureException as ex: - return return_error(404, "Error Getting VM. info: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Getting VM. info: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Getting VM. info: %s" % ex.args[0]) + return return_error(403, "Error Getting VM. info: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error Getting VM. info: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error Getting VM. info: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM info") - return return_error(400, "Error Getting VM info: %s" % ex.args[0]) + return return_error(400, "Error Getting VM info: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid/:prop', method='GET') @@ -646,18 +653,18 @@ def RESTGetVMProperty(infid=None, vmid=None, prop=None): else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: - return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. property: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Getting VM. property: %s" % ex.args[0]) + return return_error(403, "Error Getting VM. property: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) + return return_error(404, "Error Getting VM. property: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM property") - return return_error(400, "Error Getting VM property: %s" % ex.args[0]) + return return_error(400, "Error Getting VM property: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid', method='POST') @@ -715,14 +722,14 @@ def RESTAddResource(infid=None): return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: - return return_error(404, "Error Adding resources: %s" % ex.args[0]) + return return_error(404, "Error Adding resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Adding resources: %s" % ex.args[0]) + return return_error(404, "Error Adding resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Adding resources: %s" % ex.args[0]) + return return_error(403, "Error Adding resources: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Adding resources") - return return_error(400, "Error Adding resources: %s" % ex.args[0]) + return return_error(400, "Error Adding resources: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid', method='DELETE') @@ -747,18 +754,18 @@ def RESTRemoveResource(infid=None, vmid=None): bottle.response.content_type = "text/plain" return "" except DeletedInfrastructureException as ex: - return return_error(404, "Error Removing resources: %s" % ex.args[0]) + return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error Removing resources: %s" % ex.args[0]) + return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error Removing resources: %s" % ex.args[0]) + return return_error(403, "Error Removing resources: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error Removing resources: %s" % ex.args[0]) + return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error Removing resources: %s" % ex.args[0]) + return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Removing resources") - return return_error(400, "Error Removing resources: %s" % ex.args[0]) + return return_error(400, "Error Removing resources: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid', method='PUT') @@ -787,18 +794,18 @@ def RESTAlterVM(infid=None, vmid=None): return format_output(vm_info, field_name="radl") except DeletedInfrastructureException as ex: - return return_error(404, "Error modifying resources: %s" % ex.args[0]) + return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error modifying resources: %s" % ex.args[0]) + return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error modifying resources: %s" % ex.args[0]) + return return_error(403, "Error modifying resources: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error modifying resources: %s" % ex.args[0]) + return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error modifying resources: %s" % ex.args[0]) + return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error modifying resources") - return return_error(400, "Error modifying resources: %s" % ex.args[0]) + return return_error(400, "Error modifying resources: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/reconfigure', method='PUT') @@ -833,14 +840,14 @@ def RESTReconfigureInfrastructure(infid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.Reconfigure(infid, radl_data, auth, vm_list) except DeletedInfrastructureException as ex: - return return_error(404, "Error reconfiguring infrastructure: %s" % ex.args[0]) + return return_error(404, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error reconfiguring infrastructure: %s" % ex.args[0]) + return return_error(404, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error reconfiguring infrastructure: %s" % ex.args[0]) + return return_error(403, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error reconfiguring infrastructure") - return return_error(400, "Error reconfiguring infrastructure: %s" % ex.args[0]) + return return_error(400, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/start', method='PUT') @@ -854,14 +861,14 @@ def RESTStartInfrastructure(infid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.StartInfrastructure(infid, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error starting infrastructure: %s" % ex.args[0]) + return return_error(404, "Error starting infrastructure: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error starting infrastructure: %s" % ex.args[0]) + return return_error(404, "Error starting infrastructure: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error starting infrastructure: %s" % ex.args[0]) + return return_error(403, "Error starting infrastructure: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error starting infrastructure") - return return_error(400, "Error starting infrastructure: %s" % ex.args[0]) + return return_error(400, "Error starting infrastructure: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/stop', method='PUT') @@ -875,14 +882,14 @@ def RESTStopInfrastructure(infid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.StopInfrastructure(infid, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error stopping infrastructure: %s" % ex.args[0]) + return return_error(404, "Error stopping infrastructure: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error stopping infrastructure: %s" % ex.args[0]) + return return_error(404, "Error stopping infrastructure: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error stopping infrastructure: %s" % ex.args[0]) + return return_error(403, "Error stopping infrastructure: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error stopping infrastructure") - return return_error(400, "Error stopping infrastructure: %s" % ex.args[0]) + return return_error(400, "Error stopping infrastructure: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid/start', method='PUT') @@ -896,18 +903,18 @@ def RESTStartVM(infid=None, vmid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.StartVM(infid, vmid, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error starting VM: %s" % ex.args[0]) + return return_error(404, "Error starting VM: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error starting VM: %s" % ex.args[0]) + return return_error(404, "Error starting VM: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error starting VM: %s" % ex.args[0]) + return return_error(403, "Error starting VM: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error starting VM: %s" % ex.args[0]) + return return_error(404, "Error starting VM: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error starting VM: %s" % ex.args[0]) + return return_error(404, "Error starting VM: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error starting VM") - return return_error(400, "Error starting VM: %s" % ex.args[0]) + return return_error(400, "Error starting VM: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid/stop', method='PUT') @@ -921,18 +928,18 @@ def RESTStopVM(infid=None, vmid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.StopVM(infid, vmid, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error stopping VM: %s" % ex.args[0]) + return return_error(404, "Error stopping VM: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error stopping VM: %s" % ex.args[0]) + return return_error(404, "Error stopping VM: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error stopping VM: %s" % ex.args[0]) + return return_error(403, "Error stopping VM: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error stopping VM: %s" % ex.args[0]) + return return_error(404, "Error stopping VM: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error stopping VM: %s" % ex.args[0]) + return return_error(404, "Error stopping VM: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error stopping VM") - return return_error(400, "Error stopping VM: %s" % ex.args[0]) + return return_error(400, "Error stopping VM: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid/reboot', method='PUT') @@ -946,18 +953,18 @@ def RESTRebootVM(infid=None, vmid=None): bottle.response.content_type = "text/plain" return InfrastructureManager.RebootVM(infid, vmid, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error rebooting VM: %s" % ex.args[0]) + return return_error(404, "Error rebooting VM: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error rebooting VM: %s" % ex.args[0]) + return return_error(404, "Error rebooting VM: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error rebooting VM: %s" % ex.args[0]) + return return_error(403, "Error rebooting VM: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error rebooting VM: %s" % ex.args[0]) + return return_error(404, "Error rebooting VM: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error rebooting VM: %s" % ex.args[0]) + return return_error(404, "Error rebooting VM: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error rebooting VM") - return return_error(400, "Error rebooting VM: %s" % ex.args[0]) + return return_error(400, "Error rebooting VM: %s" % get_ex_error(ex)) @app.route('/version', method='GET') @@ -966,7 +973,7 @@ def RESTGeVersion(): from IM import __version__ as version return format_output(version, field_name="version") except Exception as ex: - return return_error(400, "Error getting IM version: %s" % ex.args[0]) + return return_error(400, "Error getting IM version: %s" % get_ex_error(ex)) @app.route('/infrastructures/:infid/vms/:vmid/disks/:disknum/snapshot', method='PUT') @@ -996,18 +1003,18 @@ def RESTCreateDiskSnapshot(infid=None, vmid=None, disknum=None): return InfrastructureManager.CreateDiskSnapshot(infid, vmid, int(disknum), image_name, auto_delete, auth) except DeletedInfrastructureException as ex: - return return_error(404, "Error creating snapshot: %s" % ex.args[0]) + return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: - return return_error(404, "Error creating snapshot: %s" % ex.args[0]) + return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: - return return_error(403, "Error creating snapshot: %s" % ex.args[0]) + return return_error(403, "Error creating snapshot: %s" % get_ex_error(ex)) except DeletedVMException as ex: - return return_error(404, "Error creating snapshot: %s" % ex.args[0]) + return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except IncorrectVMException as ex: - return return_error(404, "Error creating snapshot: %s" % ex.args[0]) + return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error creating snapshot") - return return_error(400, "Error creating snapshot: %s" % ex.args[0]) + return return_error(400, "Error creating snapshot: %s" % get_ex_error(ex)) @app.error(403) diff --git a/IM/ServiceRequests.py b/IM/ServiceRequests.py index fbd6c201e..f4b5a12e1 100644 --- a/IM/ServiceRequests.py +++ b/IM/ServiceRequests.py @@ -123,7 +123,7 @@ def _execute(self): return True except Exception as ex: logger.exception(self._error_mesage) - self.set("%s" % ex.args[0]) + self.set("%s" % getattr(ex, 'message', ex.args[0] if len(ex.args) else repr(ex))) return False From 6a902319d018d30edc378cab9b652054ba73ea45 Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 12 Jun 2019 12:03:57 +0200 Subject: [PATCH 2/2] Fix #856 --- IM/REST.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IM/REST.py b/IM/REST.py index 1c5262c20..d9739b894 100644 --- a/IM/REST.py +++ b/IM/REST.py @@ -308,7 +308,7 @@ def get_ex_error(ex): """ Return a secure string with the error of the exception in Py2 and Py3 """ - return getattr(ex, 'message', get_ex_error(ex) if len(ex.args) else repr(ex)) + return getattr(ex, 'message', ex.args[0] if len(ex.args) else repr(ex)) @app.route('/infrastructures/:infid', method='DELETE')