Skip to content

Commit

Permalink
Use format_message on exceptions instead of str()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
djipko committed Mar 27, 2013
1 parent b936df8 commit 8603194
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/admin_actions.py
Expand Up @@ -286,7 +286,7 @@ def _migrate_live(self, req, id, body):
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"
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/agents.py
Expand Up @@ -109,7 +109,7 @@ def update(self, req, id, body):
'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}}
Expand All @@ -122,7 +122,7 @@ def delete(self, req, id):
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."""
Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/console_output.py
Expand Up @@ -66,7 +66,7 @@ def get_console_output(self, req, id, body):
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]')
Expand Down
6 changes: 3 additions & 3 deletions nova/api/openstack/compute/contrib/consoles.py
Expand Up @@ -47,7 +47,7 @@ def get_vnc_console(self, req, id, body):
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'))
Expand All @@ -69,9 +69,9 @@ def get_spice_console(self, req, id, body):
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']}}

Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/deferred_delete.py
Expand Up @@ -44,8 +44,8 @@ def _restore(self, req, id, body):
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')
Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/fixed_ips.py
Expand Up @@ -34,7 +34,7 @@ def show(self, req, id):
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:
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/flavor_access.py
Expand Up @@ -175,7 +175,7 @@ def _addTenantAccess(self, req, id, body):
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)

Expand All @@ -192,7 +192,7 @@ def _removeTenantAccess(self, req, id, body):
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)

Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/flavorextraspecs.py
Expand Up @@ -76,7 +76,7 @@ def create(self, req, flavor_id, body):
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)
Expand All @@ -95,7 +95,7 @@ def update(self, req, flavor_id, id, body):
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)
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/flavormanage.py
Expand Up @@ -45,7 +45,7 @@ def _delete(self, req, id):
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'])

Expand Down Expand Up @@ -75,7 +75,7 @@ def _create(self, req, body):
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)

Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/floating_ip_dns.py
Expand Up @@ -189,7 +189,7 @@ def delete(self, req, id):
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)

Expand Down Expand Up @@ -274,7 +274,7 @@ def delete(self, req, domain_id, id):
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)

Expand Down
6 changes: 3 additions & 3 deletions nova/api/openstack/compute/contrib/floating_ips_bulk.py
Expand Up @@ -98,12 +98,12 @@ def create(self, req, body):
'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,
Expand All @@ -126,7 +126,7 @@ def update(self, req, id, body):
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}
Expand Down
8 changes: 4 additions & 4 deletions nova/api/openstack/compute/contrib/hosts.py
Expand Up @@ -210,7 +210,7 @@ def _set_host_maintenance(self, context, host_name, mode=True):
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
Expand All @@ -230,7 +230,7 @@ def _set_enabled_status(self, context, host_name, enabled):
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
Expand All @@ -246,7 +246,7 @@ def _host_power_action(self, req, host_name, action):
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)
Expand Down Expand Up @@ -327,7 +327,7 @@ def show(self, req, id):
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)
Expand Down
3 changes: 2 additions & 1 deletion nova/api/openstack/compute/contrib/rescue.py
Expand Up @@ -64,7 +64,8 @@ def _rescue(self, req, id, body):
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}

Expand Down
8 changes: 4 additions & 4 deletions nova/api/openstack/compute/contrib/security_groups.py
Expand Up @@ -387,7 +387,7 @@ def index(self, req, server_id):
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)
Expand Down Expand Up @@ -429,11 +429,11 @@ def _invoke(self, method, context, id, group_name):
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)

Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/server_password.py
Expand Up @@ -47,7 +47,7 @@ def _get_instance(self, context, server_id):
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):
Expand Down
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/server_start_stop.py
Expand Up @@ -47,7 +47,7 @@ def _start_server(self, req, id, body):
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')
Expand All @@ -59,7 +59,7 @@ def _stop_server(self, req, id, body):
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)


Expand Down
10 changes: 6 additions & 4 deletions nova/api/openstack/compute/server_metadata.py
Expand Up @@ -127,14 +127,16 @@ def _update_instance_metadata(self, context, server_id, metadata,
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,
Expand Down
32 changes: 18 additions & 14 deletions nova/api/openstack/compute/servers.py
Expand Up @@ -897,33 +897,35 @@ def create(self, req, body):
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)
except exception.KeypairNotFound as error:
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}
Expand Down Expand Up @@ -1288,16 +1290,18 @@ def _action_rebuild(self, req, id, body):
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)

Expand Down
2 changes: 1 addition & 1 deletion nova/compute/api.py
Expand Up @@ -2888,7 +2888,7 @@ def get(self, context, name=None, id=None, map_exception=False):
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
Expand Down

0 comments on commit 8603194

Please sign in to comment.