Use format_message on exceptions instead of str()

This patch changes all usages of unicode() and str() in the API layer
(and most other reasonable places) into a new format_message method.
This is done in order to avoid leaking the stack trace back to the
user through the Remote excepitons.

This patch deliberately avoids the EC2 api as the way how the EC2 API
handles responses is being worked on as part of another blueprint.

Fixes bug 1159755

Change-Id: I04a74682c4fac9a66ba25b52fd5c7af5e10e81e8
This commit is contained in:
Nikola Dipanov 2013-03-26 14:38:09 +01:00
parent b936df8c1e
commit 86031940b3
19 changed files with 59 additions and 52 deletions

View File

@ -286,7 +286,7 @@ class AdminActionsController(wsgi.Controller):
exception.InvalidHypervisorType,
exception.UnableToMigrateToSelf,
exception.DestinationHypervisorTooOld) as ex:
raise exc.HTTPBadRequest(explanation=str(ex))
raise exc.HTTPBadRequest(explanation=ex.format_message())
except Exception:
if host is None:
msg = _("Live migration of instance %(id)s to another host"

View File

@ -109,7 +109,7 @@ class AgentController(object):
'url': url,
'md5hash': md5hash})
except exception.AgentBuildNotFound as ex:
raise webob.exc.HTTPNotFound(explanation=str(ex))
raise webob.exc.HTTPNotFound(explanation=ex.format_message())
return {"agent": {'agent_id': id, 'version': version,
'url': url, 'md5hash': md5hash}}
@ -122,7 +122,7 @@ class AgentController(object):
try:
db.agent_build_destroy(context, id)
except exception.AgentBuildNotFound as ex:
raise webob.exc.HTTPNotFound(explanation=str(ex))
raise webob.exc.HTTPNotFound(explanation=ex.format_message())
def create(self, req, body):
"""Creates a new agent build."""

View File

@ -66,7 +66,7 @@ class ConsoleOutputController(wsgi.Controller):
except exception.NotFound:
raise webob.exc.HTTPNotFound(_('Unable to get console'))
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=unicode(e))
raise webob.exc.HTTPConflict(explanation=e.format_message())
# XML output is not correctly escaped, so remove invalid characters
remove_re = re.compile('[\x00-\x08\x0B-\x0C\x0E-\x1F-\x0D]')

View File

@ -47,7 +47,7 @@ class ConsolesController(wsgi.Controller):
instance,
console_type)
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(
explanation=_('Instance not yet ready'))
@ -69,9 +69,9 @@ class ConsolesController(wsgi.Controller):
instance,
console_type)
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=unicode(e))
raise webob.exc.HTTPConflict(explanation=e.format_message())
return {'console': {'type': console_type, 'url': output['url']}}

View File

@ -44,8 +44,8 @@ class DeferredDeleteController(wsgi.Controller):
self.compute_api.restore(context, instance)
except exception.QuotaError as error:
raise webob.exc.HTTPRequestEntityTooLarge(
explanation=unicode(error),
headers={'Retry-After': 0})
explanation=error.format_message(),
headers={'Retry-After': 0})
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'restore')

View File

@ -34,7 +34,7 @@ class FixedIPController(object):
try:
fixed_ip = db.fixed_ip_get_by_address_detailed(context, id)
except exception.FixedIpNotFoundForAddress as ex:
raise webob.exc.HTTPNotFound(explanation=str(ex))
raise webob.exc.HTTPNotFound(explanation=ex.format_message())
fixed_ip_info = {"fixed_ip": {}}
if fixed_ip[1] is None:

View File

@ -175,7 +175,7 @@ class FlavorActionController(wsgi.Controller):
try:
instance_types.add_instance_type_access(id, tenant, context)
except exception.FlavorAccessExists as err:
raise webob.exc.HTTPConflict(explanation=str(err))
raise webob.exc.HTTPConflict(explanation=err.format_message())
return _marshall_flavor_access(id)
@ -192,7 +192,7 @@ class FlavorActionController(wsgi.Controller):
try:
instance_types.remove_instance_type_access(id, tenant, context)
except exception.FlavorAccessNotFound, e:
raise webob.exc.HTTPNotFound(explanation=str(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
return _marshall_flavor_access(id)

View File

@ -76,7 +76,7 @@ class FlavorExtraSpecsController(object):
flavor_id,
specs)
except exception.MetadataLimitExceeded as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
return body
@wsgi.serializers(xml=ExtraSpecTemplate)
@ -95,7 +95,7 @@ class FlavorExtraSpecsController(object):
flavor_id,
body)
except exception.MetadataLimitExceeded as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
return body
@wsgi.serializers(xml=ExtraSpecTemplate)

View File

@ -45,7 +45,7 @@ class FlavorManageController(wsgi.Controller):
flavor = instance_types.get_instance_type_by_flavor_id(
id, read_deleted="no")
except exception.NotFound, e:
raise webob.exc.HTTPNotFound(explanation=str(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
instance_types.destroy(flavor['name'])
@ -75,7 +75,7 @@ class FlavorManageController(wsgi.Controller):
req.cache_db_flavor(flavor)
except (exception.InstanceTypeExists,
exception.InstanceTypeIdExists) as err:
raise webob.exc.HTTPConflict(explanation=str(err))
raise webob.exc.HTTPConflict(explanation=err.format_message())
return self._view_builder.show(req, flavor)

View File

@ -189,7 +189,7 @@ class FloatingIPDNSDomainController(object):
try:
self.network_api.delete_dns_domain(context, domain)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
return webob.Response(status_int=202)
@ -274,7 +274,7 @@ class FloatingIPDNSEntryController(object):
try:
self.network_api.delete_dns_entry(context, name, domain)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
return webob.Response(status_int=202)

View File

@ -98,12 +98,12 @@ class FloatingIPBulkController(object):
'interface': interface}
for address in self._address_to_hosts(ip_range))
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=str(exc))
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try:
db.floating_ip_bulk_create(context, ips)
except exception.FloatingIpExists as exc:
raise webob.exc.HTTPBadRequest(explanation=str(exc))
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
return {"floating_ips_bulk_create": {"ip_range": ip_range,
"pool": pool,
@ -126,7 +126,7 @@ class FloatingIPBulkController(object):
ips = ({'address': str(address)}
for address in self._address_to_hosts(ip_range))
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=str(exc))
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
db.floating_ip_bulk_destroy(context, ips)
return {"floating_ips_bulk_delete": ip_range}

View File

@ -210,7 +210,7 @@ class HostController(object):
msg = _("Virt driver does not implement host maintenance mode.")
raise webob.exc.HTTPNotImplemented(explanation=msg)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
if result not in ("on_maintenance", "off_maintenance"):
raise webob.exc.HTTPBadRequest(explanation=result)
return result
@ -230,7 +230,7 @@ class HostController(object):
msg = _("Virt driver does not implement host disabled status.")
raise webob.exc.HTTPNotImplemented(explanation=msg)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
if result not in ("enabled", "disabled"):
raise webob.exc.HTTPBadRequest(explanation=result)
return result
@ -246,7 +246,7 @@ class HostController(object):
msg = _("Virt driver does not implement host power management.")
raise webob.exc.HTTPNotImplemented(explanation=msg)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
return {"host": host_name, "power_action": result}
@wsgi.serializers(xml=HostActionTemplate)
@ -327,7 +327,7 @@ class HostController(object):
try:
service = self.api.service_get_by_compute_host(context, host_name)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e))
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.AdminRequired:
msg = _("Describe-resource is admin only functionality")
raise webob.exc.HTTPForbidden(explanation=msg)

View File

@ -64,7 +64,8 @@ class RescueController(wsgi.Controller):
common.raise_http_conflict_for_instance_invalid_state(state_error,
'rescue')
except exception.InstanceNotRescuable as non_rescuable:
raise exc.HTTPBadRequest(explanation=unicode(non_rescuable))
raise exc.HTTPBadRequest(
explanation=non_rescuable.format_message())
return {'adminPass': password}

View File

@ -387,7 +387,7 @@ class ServerSecurityGroupController(SecurityGroupControllerBase):
try:
instance = self.compute_api.get(context, server_id)
except exception.InstanceNotFound as exp:
raise exc.HTTPNotFound(explanation=unicode(exp))
raise exc.HTTPNotFound(explanation=exp.format_message())
groups = self.security_group_api.get_instance_security_groups(
req, instance['id'], instance['uuid'], True)
@ -429,11 +429,11 @@ class SecurityGroupActionController(wsgi.Controller):
instance = self.compute_api.get(context, id)
method(context, instance, group_name)
except exception.SecurityGroupNotFound as exp:
raise exc.HTTPNotFound(explanation=unicode(exp))
raise exc.HTTPNotFound(explanation=exp.format_message())
except exception.InstanceNotFound as exp:
raise exc.HTTPNotFound(explanation=unicode(exp))
raise exc.HTTPNotFound(explanation=exp.format_message())
except exception.Invalid as exp:
raise exc.HTTPBadRequest(explanation=unicode(exp))
raise exc.HTTPBadRequest(explanation=exp.format_message())
return webob.Response(status_int=202)

View File

@ -47,7 +47,7 @@ class ServerPasswordController(object):
try:
return self.compute_api.get(context, server_id)
except exception.InstanceNotFound as exp:
raise webob.exc.HTTPNotFound(explanation=unicode(exp))
raise webob.exc.HTTPNotFound(explanation=exp.format_message())
@wsgi.serializers(xml=ServerPasswordTemplate)
def index(self, req, server_id):

View File

@ -47,7 +47,7 @@ class ServerStartStopActionController(wsgi.Controller):
try:
self.compute_api.start(context, instance)
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=unicode(e))
raise webob.exc.HTTPConflict(explanation=e.format_message())
return webob.Response(status_int=202)
@wsgi.action('os-stop')
@ -59,7 +59,7 @@ class ServerStartStopActionController(wsgi.Controller):
try:
self.compute_api.stop(context, instance)
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=unicode(e))
raise webob.exc.HTTPConflict(explanation=e.format_message())
return webob.Response(status_int=202)

View File

@ -127,14 +127,16 @@ class Controller(object):
raise exc.HTTPBadRequest(explanation=msg)
except exception.InvalidMetadata as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.InvalidMetadataSize as error:
raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message())
except exception.QuotaError as error:
raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error),
headers={'Retry-After': 0})
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message(),
headers={'Retry-After': 0})
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,

View File

@ -897,25 +897,27 @@ class Controller(wsgi.Controller):
auto_disk_config=auto_disk_config,
scheduler_hints=scheduler_hints)
except exception.QuotaError as error:
raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error),
headers={'Retry-After': 0})
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message(),
headers={'Retry-After': 0})
except exception.InstanceTypeMemoryTooSmall as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.InstanceTypeNotFound as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error)
except exception.InstanceTypeDiskTooSmall as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.InvalidMetadata as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.InvalidMetadataSize as error:
raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message())
except exception.InvalidRequest as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.ImageNotFound as error:
msg = _("Can not find requested image")
raise exc.HTTPBadRequest(explanation=msg)
except exception.ImageNotActive as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.FlavorNotFound as error:
msg = _("Invalid flavorRef provided.")
raise exc.HTTPBadRequest(explanation=msg)
@ -923,7 +925,7 @@ class Controller(wsgi.Controller):
msg = _("Invalid key_name provided.")
raise exc.HTTPBadRequest(explanation=msg)
except exception.SecurityGroupNotFound as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except rpc_common.RemoteError as err:
msg = "%(err_type)s: %(err_msg)s" % {'err_type': err.exc_type,
'err_msg': err.value}
@ -1288,16 +1290,18 @@ class Controller(wsgi.Controller):
msg = _("Instance could not be found")
raise exc.HTTPNotFound(explanation=msg)
except exception.InvalidMetadata as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(
explanation=error.format_message())
except exception.InvalidMetadataSize as error:
raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message())
except exception.ImageNotFound:
msg = _("Cannot find image for rebuild")
raise exc.HTTPBadRequest(explanation=msg)
except exception.InstanceTypeMemoryTooSmall as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.InstanceTypeDiskTooSmall as error:
raise exc.HTTPBadRequest(explanation=unicode(error))
raise exc.HTTPBadRequest(explanation=error.format_message())
instance = self._get_server(context, req, id)

View File

@ -2888,7 +2888,7 @@ class SecurityGroupAPI(base.Base, security_group_base.SecurityGroupBase):
return self.db.security_group_get(context, id)
except exception.NotFound as exp:
if map_exception:
msg = unicode(exp)
msg = exp.format_message()
self.raise_not_found(msg)
else:
raise