← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~troyanov/maas:describe-maas-url into maas:master

 

Anton Troyanov has proposed merging ~troyanov/maas:describe-maas-url into maas:master.

Commit message:
fix(api): /describe should respect maas_url
    
When MAAS is behind a load-balancer or proxy
(e.g. HAProxy with `mode tcp`), we should use address specified in
`maas_url` when returning resources information in `/describe`
    
Resolves LP:2009186


Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~troyanov/maas/+git/maas/+merge/438387

Consider the following deployment:

1. HAProxy (mode tcp) that listens on `10.37.215.16:80`
2. MAAS available on `10.81.242.188:5240 `

> cat haproxy.cfg
frontend maas
    bind    *:80
    retries 3
    option  redispatch
    option  http-server-close
    default_backend maas_tls


backend maas
    timeout server 900s
    balance source
    hash-type consistent
    server maas-api-0 10.81.242.188:5240 check



When we are making a call to `http://10.37.215.16/MAAS/api/2.0/describe/` it will return resources information with `uri` pointing to `10.81.242.188:5240` instead of `10.37.215.16`
{
  "doc": "Manage the collection of boot sources.",
  "name": "BootSourcesHandler",
  "params": [],
  "path": "/MAAS/api/2.0/boot-sources/",
  "uri": "http://10.81.242.188:5240/MAAS/api/2.0/boot-sources/";
}

Thats because current logic is to take MAAS URL from HTTP `Host` header, which is not set by HAProxy, but rather by nginx (which comes with MAAS), so there is no way for MAAS to know that it was accessed through another proxy from the Host header.

To fix this behaviour, this modification changes the logic, so we always use value from `maas_url` configuration option.

-- 
Your team MAAS Maintainers is requested to review the proposed merge of ~troyanov/maas:describe-maas-url into maas:master.
diff --git a/src/maasserver/api/doc_handler.py b/src/maasserver/api/doc_handler.py
index 3dd41c6..7487498 100644
--- a/src/maasserver/api/doc_handler.py
+++ b/src/maasserver/api/doc_handler.py
@@ -73,6 +73,7 @@ from maasserver.api.doc import (
     get_api_description,
 )
 from maasserver.api.templates import APITemplateRenderer
+from maasserver.models.config import Config
 from maasserver.utils import build_absolute_uri
 
 # Title section for the API documentation.  Matches in style, format,
@@ -231,12 +232,12 @@ def describe(request):
     # Make all URIs absolute. Clients - and the command-line client in
     # particular - expect that all handler URIs are absolute, not just paths.
     # The handler URIs returned by _describe_resource() are relative paths.
-    absolute = partial(build_absolute_uri, request)
+    maas_url = Config.objects.get_config("maas_url").rstrip("/").removesuffix("/MAAS")
     for resource in description["resources"]:
         for handler_type in ("anon", "auth"):
             handler = resource[handler_type]
             if handler is not None:
-                handler["uri"] = absolute(handler["path"])
+                handler["uri"] = f"{maas_url}{handler['path']}"
     # Return as a JSON document.
     return HttpResponse(
         json.dumps(description, sort_keys=True),

Follow ups