Return a HTTP404 message when the Metadata instance is not present
If the Metadata instance (network, router) is not present, now the Metadata proxy handler writes a HTTP 404 error message in the socket file that is received by the client (virtual machine) that requests the metadata. Closes-Bug: #2106713 Change-Id: Ic717de94f2c483c08e2aa8fe688fe2e88d17d035
This commit is contained in:
@@ -24,7 +24,6 @@ from oslo_log import log as logging
|
|||||||
from oslo_service import loopingcall
|
from oslo_service import loopingcall
|
||||||
import requests
|
import requests
|
||||||
import webob
|
import webob
|
||||||
from webob import exc as webob_exc
|
|
||||||
|
|
||||||
from neutron._i18n import _
|
from neutron._i18n import _
|
||||||
from neutron.agent.common import base_agent_rpc
|
from neutron.agent.common import base_agent_rpc
|
||||||
@@ -169,9 +168,22 @@ class MetadataProxyHandler(MetadataProxyHandlerBaseSocketServer,
|
|||||||
res = self._proxy_request(instance_id, project_id, req)
|
res = self._proxy_request(instance_id, project_id, req)
|
||||||
self.wfile.write(res)
|
self.wfile.write(res)
|
||||||
return
|
return
|
||||||
# TODO(ralonsoh): change this return to be a formatted Request
|
|
||||||
# and added to self.wfile
|
network_id, router_id = self._get_instance_id(req)
|
||||||
return webob_exc.HTTPNotFound()
|
if network_id and router_id:
|
||||||
|
title = '400 Bad Request'
|
||||||
|
msg = _(f'Both network {network_id} and router {router_id} '
|
||||||
|
f'defined.')
|
||||||
|
elif network_id:
|
||||||
|
title = '404 Not Found'
|
||||||
|
msg = _(f'Instance was not found on network {network_id}.')
|
||||||
|
LOG.warning(msg)
|
||||||
|
else:
|
||||||
|
title = '404 Not Found'
|
||||||
|
msg = _(f'Instance was not found on router {router_id}.')
|
||||||
|
LOG.warning(msg)
|
||||||
|
res = common_metadata.encode_http_reponse(title, title, msg)
|
||||||
|
self.wfile.write(res)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
LOG.exception('Error while receiving data.')
|
LOG.exception('Error while receiving data.')
|
||||||
raise exc
|
raise exc
|
||||||
|
|||||||
@@ -91,11 +91,16 @@ class MetadataProxyHandlerBase(metaclass=abc.ABCMeta):
|
|||||||
explanation = str(msg)
|
explanation = str(msg)
|
||||||
return webob.exc.HTTPInternalServerError(explanation=explanation)
|
return webob.exc.HTTPInternalServerError(explanation=explanation)
|
||||||
|
|
||||||
def _get_instance_and_project_id(self, req, skip_cache=False):
|
def _get_instance_id(self, req):
|
||||||
forwarded_for = req.headers.get('X-Forwarded-For')
|
"""Returns the network ID and the router ID from the request"""
|
||||||
network_id = req.headers.get(self.NETWORK_ID_HEADER)
|
network_id = req.headers.get(self.NETWORK_ID_HEADER)
|
||||||
router_id = (req.headers.get(self.ROUTER_ID_HEADER)
|
router_id = (req.headers.get(self.ROUTER_ID_HEADER)
|
||||||
if self.ROUTER_ID_HEADER else None)
|
if self.ROUTER_ID_HEADER else None)
|
||||||
|
return network_id, router_id
|
||||||
|
|
||||||
|
def _get_instance_and_project_id(self, req, skip_cache=False):
|
||||||
|
forwarded_for = req.headers.get('X-Forwarded-For')
|
||||||
|
network_id, router_id = self._get_instance_id(req)
|
||||||
|
|
||||||
# Only one should be given, drop since it could be spoofed
|
# Only one should be given, drop since it could be spoofed
|
||||||
if network_id and router_id:
|
if network_id and router_id:
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ from oslo_config import cfg
|
|||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
import requests
|
import requests
|
||||||
import webob
|
import webob
|
||||||
from webob import exc as webob_exc
|
|
||||||
|
|
||||||
from neutron._i18n import _
|
from neutron._i18n import _
|
||||||
from neutron.agent.linux import utils as agent_utils
|
from neutron.agent.linux import utils as agent_utils
|
||||||
@@ -145,9 +144,22 @@ class MetadataProxyHandler(MetadataProxyHandlerBaseSocketServer,
|
|||||||
res = self._proxy_request(instance_id, project_id, req)
|
res = self._proxy_request(instance_id, project_id, req)
|
||||||
self.wfile.write(res)
|
self.wfile.write(res)
|
||||||
return
|
return
|
||||||
# TODO(ralonsoh): change this return to be a formatted Request
|
|
||||||
# and added to self.wfile
|
network_id, router_id = self._get_instance_id(req)
|
||||||
return webob_exc.HTTPNotFound()
|
if network_id and router_id:
|
||||||
|
title = '400 Bad Request'
|
||||||
|
msg = _(f'Both network {network_id} and router {router_id} '
|
||||||
|
f'defined.')
|
||||||
|
elif network_id:
|
||||||
|
title = '404 Not Found'
|
||||||
|
msg = _(f'Instance was not found on network {network_id}.')
|
||||||
|
LOG.warning(msg)
|
||||||
|
else:
|
||||||
|
title = '404 Not Found'
|
||||||
|
msg = _(f'Instance was not found on router {router_id}.')
|
||||||
|
LOG.warning(msg)
|
||||||
|
res = common_metadata.encode_http_reponse(title, title, msg)
|
||||||
|
self.wfile.write(res)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
LOG.exception('Error while receiving data.')
|
LOG.exception('Error while receiving data.')
|
||||||
raise exc
|
raise exc
|
||||||
|
|||||||
Reference in New Issue
Block a user