Decode the metadata response before sending it to the VM

The metadata server response can be encoded. The metadata proxy decodes
it before crafting the HTTP message that will be delivered to the
virtual machine.

It requires that the HTTP message sent by the metadata server contains
the proper "Context-Encoding" header, defining the encoding type used.
This header is not provided by default (the content is not encoded)
or is empty.

NOTE: unless we provide a method to encode the Nova metadata server
content, it would not be possible to properly test this patch.

Closes-Bug: #2120723
Signed-off-by: Rodolfo Alonso Hernandez <ralonsoh@redhat.com>
Change-Id: I747872f031cc5a1a87ced69bb0af645c088143f3
This commit is contained in:
Rodolfo Alonso Hernandez
2025-09-04 16:52:41 +00:00
parent 84f606a9bc
commit c031b59ec1

View File

@@ -33,6 +33,8 @@ LOG = logging.getLogger(__name__)
PROXY_SERVICE_NAME = 'haproxy'
PROXY_SERVICE_CMD = 'haproxy'
CONTENT_ENCODERS = ('gzip', 'deflate')
class InvalidUserOrGroupException(Exception):
pass
@@ -163,6 +165,12 @@ class MetadataProxyHandlerBaseSocketServer(
status=http_response.status_code,
content_type=http_response.headers['content-type'],
charset=http_response.encoding)
# The content of the response is decoded depending on the
# "Context-Enconding" header, if present. The operation is limited to
# ("gzip", "deflate"), as is in the ``webob.response.Response`` class.
if _res.content_encoding in CONTENT_ENCODERS:
_res.decode_content()
# NOTE(ralonsoh): there should be a better way to format the HTTP
# response, adding the HTTP version to the ``webob.Response``
# output string.