summaryrefslogtreecommitdiff
path: root/master/buildbot/scripts/devproxy.py
blob: aaa64c62466c753d23c79cd07cc914b9386edb3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members


import asyncio
import json
import logging

import aiohttp  # dev-proxy command requires aiohttp! run 'pip install aiohttp'
import aiohttp.web
import jinja2

from buildbot.plugins.db import get_plugins

log = logging.getLogger(__name__)


class DevProxy:
    MAX_CONNECTIONS = 10

    def __init__(self, port, next_url, plugins, unsafe_ssl, auth_cookie):
        while next_url.endswith('/'):
            next_url = next_url[:-1]
        self.next_url = next_url
        self.app = app = aiohttp.web.Application()
        self.apps = get_plugins('www', None, load_now=True)
        self.unsafe_ssl = unsafe_ssl
        cookies = {}
        if auth_cookie:
            if "TWISTED_SESSION" in auth_cookie:  # user pasted the whole document.cookie part!
                cookies = dict(c.split("=") for c in auth_cookie.split(";"))
                auth_cookie = cookies["TWISTED_SESSION"]
            cookies = {'TWISTED_SESSION': auth_cookie}
        logging.basicConfig(level=logging.DEBUG)
        if plugins is None:
            plugins = {}
        else:
            plugins = json.loads(plugins)

        self.plugins = plugins

        app.router.add_route('*', '/ws', self.ws_handler)
        for path in ['/api', '/auth', '/sse', '/avatar']:
            app.router.add_route('*', path + '{path:.*}', self.proxy_handler)
        app.router.add_route('*', '/', self.index_handler)
        for plugin in self.apps.names:
            if plugin != 'base':
                staticdir = self.apps.get(plugin).static_dir
                app.router.add_static('/' + plugin, staticdir)
        staticdir = self.staticdir = self.apps.get('base').static_dir
        loader = jinja2.FileSystemLoader(staticdir)
        self.jinja = jinja2.Environment(loader=loader, undefined=jinja2.StrictUndefined)
        app.router.add_static('/', staticdir)
        conn = aiohttp.TCPConnector(limit=self.MAX_CONNECTIONS, verify_ssl=not self.unsafe_ssl)
        self.session = aiohttp.ClientSession(connector=conn, trust_env=True, cookies=cookies)
        self.config = None
        self.buildbotURL = f"http://localhost:{port}/"
        app.on_startup.append(self.on_startup)
        app.on_cleanup.append(self.on_cleanup)
        aiohttp.web.run_app(app, host="localhost", port=port)

    async def on_startup(self, app):
        try:
            await self.fetch_config_from_upstream()
        except aiohttp.ClientConnectionError as e:
            raise RuntimeError("Unable to connect to buildbot master" + str(e)) from e

    async def on_cleanup(self, app):
        await self.session.close()

    async def ws_handler(self, req):
        # based on https://github.com/oetiker/aio-reverse-proxy/blob/master/paraview-proxy.py
        ws_server = aiohttp.web.WebSocketResponse()
        await ws_server.prepare(req)

        async with self.session.ws_connect(self.next_url + "/ws", headers=req.headers) as ws_client:

            async def ws_forward(ws_from, ws_to):
                async for msg in ws_from:
                    if ws_to.closed:
                        await ws_to.close(code=ws_to.close_code, message=msg.extra)
                        return
                    if msg.type == aiohttp.WSMsgType.TEXT:
                        await ws_to.send_str(msg.data)
                    elif msg.type == aiohttp.WSMsgType.BINARY:
                        await ws_to.send_bytes(msg.data)
                    elif msg.type == aiohttp.WSMsgType.PING:
                        await ws_to.ping()
                    elif msg.type == aiohttp.WSMsgType.PONG:
                        await ws_to.pong()
                    else:
                        raise ValueError(f'unexpected message type: {msg}')

            # keep forwarding websocket data in both directions
            await asyncio.wait(
                [ws_forward(ws_server, ws_client), ws_forward(ws_client, ws_server)],
                return_when=asyncio.FIRST_COMPLETED,
            )
        return ws_server

    async def proxy_handler(self, req):
        method = getattr(self.session, req.method.lower())
        upstream_url = self.next_url + req.path
        headers = req.headers.copy()
        query = req.query
        try:
            # note that req.content is a StreamReader, so the data is streamed
            # and not fully loaded in memory (unlike with python-requests)
            async with method(
                upstream_url, headers=headers, params=query, allow_redirects=False, data=req.content
            ) as request:
                response = aiohttp.web.StreamResponse(
                    status=request.status, headers=request.headers
                )
                writer = await response.prepare(req)
                while True:
                    chunk = await request.content.readany()
                    if not chunk:
                        break
                    # using writer.write instead of response.write saves a few checks
                    await writer.write(chunk)
                return response
        except aiohttp.ClientConnectionError as e:
            return self.connection_error(e)

    def connection_error(self, error):
        return aiohttp.web.Response(
            text=f'Unable to connect to upstream server {self.next_url} ({error!s})', status=502
        )

    async def fetch_config_from_upstream(self):
        async with self.session.get(self.next_url) as request:
            index = await request.content.read()
            if request.status != 200:
                raise RuntimeError("Unable to fetch buildbot config: " + index.decode())
        # hack to parse the configjson from upstream buildbot config
        start_delimiter = b'angular.module("buildbot_config", []).constant("config", '
        start_index = index.index(start_delimiter)
        last_index = index.index(b')</script></html>')
        self.config = json.loads(index[start_index + len(start_delimiter) : last_index].decode())

        # keep the original config, but remove the plugins that we don't know
        for plugin in list(self.config['plugins'].keys()):
            if plugin not in self.apps:
                del self.config['plugins'][plugin]
                log.warn("warning: Missing plugin compared to original buildbot: %s", plugin)

        # add the plugins configs passed in cmdline
        for k, v in self.plugins.items():
            self.config['plugins'][k] = v

        self.config['buildbotURL'] = self.buildbotURL
        self.config['buildbotURLs'] = [self.buildbotURL, self.next_url + "/"]

    async def index_handler(self, req):
        tpl = self.jinja.get_template('index.html')
        index = tpl.render(
            configjson=json.dumps(self.config), custom_templates={}, config=self.config
        )
        return aiohttp.web.Response(body=index, content_type='text/html')


def devproxy(config):
    DevProxy(
        config['port'],
        config['buildbot_url'],
        config['plugins'],
        config['unsafe_ssl'],
        config['auth_cookie'],
    )