From: Nick Erdmann Date: Mon, 22 Jan 2018 12:33:32 +0000 (+0000) Subject: pybind/mgr/dashboard: fix reverse proxy support X-Git-Tag: v12.2.3~105^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=91bfcc73f68a869703eb2f6a5ee5f5962b9b727b;p=ceph.git pybind/mgr/dashboard: fix reverse proxy support This fixes http redirection for reverse http proxies Fixes: http://tracker.ceph.com/issues/22557 Signed-off-by: Nick Erdmann (cherry picked from commit 95e1963cb5327f0699081c4c4b0b355d109ff0e3) --- diff --git a/src/pybind/mgr/dashboard/module.py b/src/pybind/mgr/dashboard/module.py index 1d0f8f3daee4..21310f3a46fc 100644 --- a/src/pybind/mgr/dashboard/module.py +++ b/src/pybind/mgr/dashboard/module.py @@ -26,6 +26,7 @@ import socket import cherrypy import jinja2 +import urlparse from mgr_module import MgrModule, MgrStandbyModule, CommandResult @@ -63,14 +64,22 @@ def recurse_refs(root, path): log.info("%s %d (%s)" % (path, sys.getrefcount(root), root.__class__)) def get_prefixed_url(url): - return global_instance().url_prefix + url + return global_instance().url_prefix.rstrip('/') + url +def prepare_url_prefix(url_prefix): + url_prefix = urlparse.urljoin('/', url_prefix) + if url_prefix[-1] != '/': + url_prefix = url_prefix + '/' + return url_prefix + class StandbyModule(MgrStandbyModule): def serve(self): server_addr = self.get_localized_config('server_addr', '::') server_port = self.get_localized_config('server_port', '7000') + url_prefix = prepare_url_prefix(self.get_config('url_prefix', default='')) + if server_addr is None: raise RuntimeError('no server_addr configured; try "ceph config-key set mgr/dashboard/server_addr "') log.info("server_addr: %s server_port: %s" % (server_addr, server_port)) @@ -88,16 +97,16 @@ class StandbyModule(MgrStandbyModule): class Root(object): @cherrypy.expose - def index(self): + def default(self, *args, **kwargs): active_uri = module.get_active_uri() if active_uri: - log.info("Redirecting to active '{0}'".format(active_uri)) - raise cherrypy.HTTPRedirect(active_uri) + log.info("Redirecting to active '{0}'".format(active_uri + "/".join(args))) + raise cherrypy.HTTPRedirect(active_uri + "/".join(args)) else: template = env.get_template("standby.html") return template.render(delay=5) - cherrypy.tree.mount(Root(), "/", {}) + cherrypy.tree.mount(Root(), url_prefix, {}) log.info("Starting engine...") cherrypy.engine.start() log.info("Waiting for engine...") @@ -888,15 +897,7 @@ class Module(MgrModule): ret[k1][k2] = sorted_dict return ret - url_prefix = self.get_config('url_prefix') - if url_prefix == None: - url_prefix = '' - else: - if len(url_prefix) != 0: - if url_prefix[0] != '/': - url_prefix = '/'+url_prefix - if url_prefix[-1] == '/': - url_prefix = url_prefix[:-1] + url_prefix = prepare_url_prefix(self.get_config('url_prefix', default='')) self.url_prefix = url_prefix server_addr = self.get_localized_config('server_addr', '::') @@ -915,9 +916,10 @@ class Module(MgrModule): # Publish the URI that others may use to access the service we're # about to start serving - self.set_uri("http://{0}:{1}/".format( + self.set_uri("http://{0}:{1}{2}".format( socket.getfqdn() if server_addr == "::" else server_addr, - server_port + server_port, + url_prefix )) static_dir = os.path.join(current_dir, 'static')