summaryrefslogtreecommitdiffstats
path: root/custodia/httpd/server.py
blob: c2bed22f3f7410d010bd338c832c194ca588e0e1 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (C) 2015  Custodia Project Contributors - see LICENSE file

try:
    # pylint: disable=import-error
    from BaseHTTPServer import BaseHTTPRequestHandler
    from SocketServer import ForkingMixIn, UnixStreamServer
    from urlparse import urlparse, parse_qs
except ImportError:
    # pylint: disable=import-error,no-name-in-module
    from http.server import BaseHTTPRequestHandler
    from socketserver import ForkingMixIn, UnixStreamServer
    from urllib.parse import urlparse, parse_qs
import io
import os
import shutil
import six
import socket
import struct
import sys
import traceback

SO_PEERCRED = 17


class HTTPError(Exception):

    def __init__(self, code=None, message=None):
        self.code = code if code is not None else 500
        self.mesg = message
        super(HTTPError, self).__init__('%d: %s' % (self.code, self.mesg))


def stacktrace():
    with io.BytesIO() as f:
        _, _, tb = sys.exc_info()
        traceback.print_tb(tb, None, file=f)
        del tb
        return f.getvalue()


class ForkingLocalHTTPServer(ForkingMixIn, UnixStreamServer):

    """
    A forking HTTP Server.
    Each request runs into a forked server so that the whole environment
    is clean and isolated, and parallel requests cannot unintentionally
    influence one another.

    When a request is received it is parsed by the handler_class provided
    at server initialization. The hanlder is suppoed to call the pipeline()
    function provided by the server to handle requests after parsing.

    The pipeline() function handles authentication and invocation of the
    correct consumer based on the server configuration, that is provided
    at initialization time.

    When authentication is performed the request dictionary will have
    a 'valid_auth' boolean member set to True if authentication was
    successful. Additional attributes may be set by authentication plugins.

    Once authentication is successful the pipeline will parse the path
    component and find the consumer plugin that handles the provided path
    walking up the path component by component until a consumer is found.

    Paths are walked up from the leaf to the root, so if two consumers hang
    on the same tree, the one closer to the leaf will be used. If there is
    a trailing path when the conumer is selected then it will be stored in
    the request dicstionary named 'trail'. The 'trail' is an ordered list
    of the path components below the consumer entry point.
    """

    server_string = "Custodia/0.1"
    allow_reuse_address = True
    socket_file = None

    def __init__(self, server_address, handler_class, config):
        UnixStreamServer.__init__(self, server_address, handler_class)
        if 'consumers' not in config:
            raise ValueError('Configuration does not provide any consumer')
        self.config = config
        if 'server_string' in self.config:
            self.server_string = self.config['server_string']

    def server_bind(self):
        UnixStreamServer.server_bind(self)
        self.socket_file = self.socket.getsockname()

    def pipeline(self, request):

        # auth framework here
        authers = self.config.get('authenticators')
        if authers is None:
            raise HTTPError(403)
        for auth in authers:
            authers[auth].handle(request)
        if 'valid_auth' not in request or request['valid_auth'] is not True:
            raise HTTPError(403)

        # Select consumer
        path = request.get('path', '')
        if not os.path.isabs(path):
            raise HTTPError(400)

        trail = []
        while path != '':
            if path in self.config['consumers']:
                con = self.config['consumers'][path]
                if len(trail) != 0:
                    request['trail'] = trail
                return con.handle(request)
            if path == '/':
                path = ''
            else:
                head, tail = os.path.split(path)
                trail.insert(0, tail)
                path = head

        raise HTTPError(404)


class LocalHTTPRequestHandler(BaseHTTPRequestHandler):

    """
    This request handler is a slight modification of BaseHTTPRequestHandler
    where the per-request handler is replaced.

    When a request comes in it is parsed and the 'request' dictionary is
    populated accordingly. Additionally a 'creds' structure is added to the
    request.

    The 'creds' structure contains the data retrieved via a call to
    getsockopt with the SO_PEERCRED option. This retrieves via kernel assist
    the uid,gid and pid of the process on the other side of the unix socket
    on which the request has been made. This can be used for authentication
    and/or authorization purposes.

    after the request is parsed the server's pipeline() function is invoked
    in order to handle it. The pipeline() should return a response object,
    where te return 'code', the 'output' and 'headers' may be found.

    If no 'code' is present the request is assumed to be successful and a
    '200 OK' status code will be sent back to the client.

    The 'output' parameter can be a string or a file like object.

    The 'headers' objct must be a dictionary where keys are headers names.

    By default we assume HTTP1.0
    """

    protocol_version = "HTTP/1.0"

    def __init__(self, *args, **kwargs):
        BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
        self.requestline = ''
        self.request_version = ''
        self.command = ''
        self.raw_requestline = None
        self.close_connection = 0
        self.path = None
        self.query = None
        self.url = None

    def version_string(self):
        return self.server.server_string

    @property
    def peer_creds(self):

        creds = self.request.getsockopt(socket.SOL_SOCKET, SO_PEERCRED,
                                        struct.calcsize('3i'))
        pid, uid, gid = struct.unpack('3i', creds)
        return {'pid': pid, 'uid': uid, 'gid': gid}

    def parse_request(self, *args, **kwargs):
        if not BaseHTTPRequestHandler.parse_request(self, *args, **kwargs):
            return False

        # after basic parsing also use urlparse to retrieve individual
        # elements of a request.
        url = urlparse(self.path)

        # Yes, override path with the path part only
        self.path = url.path

        # Create dict out of query
        self.query = parse_qs(url.query)

        # keep the rest into the 'url' element in case someone needs it
        self.url = url

        return True

    def handle_one_request(self):
        # Set a fake client address to make log functions happy
        self.client_address = ['127.0.0.1', 0]
        try:
            if not self.server.pipeline:
                self.close_connection = 1
                return
            self.raw_requestline = self.rfile.readline(65537)
            if not self.raw_requestline:
                self.close_connection = 1
                return
            if len(self.raw_requestline) > 65536:
                self.requestline = ''
                self.request_version = ''
                self.command = ''
                self.send_error(414)
                self.wfile.flush()
                return
            if not self.parse_request():
                self.close_connection = 1
                return
            request = {'creds': self.peer_creds,
                       'command': self.command,
                       'path': self.path,
                       'query': self.query,
                       'url': self.url,
                       'version': self.request_version,
                       'headers': self.headers}
            try:
                response = self.server.pipeline(request)
                if response is None:
                    raise HTTPError(500)
            except HTTPError as e:
                self.send_error(e.code, e.mesg)
                self.wfile.flush()
                return
            except socket.timeout as e:
                self.log_error("Request timed out: %r", e)
                self.close_connection = 1
                return
            except Exception as e:  # pylint: disable=broad-except
                self.log_error("Handler failed: %r", e)
                self.log_traceback()
                self.send_error(500)
                self.wfile.flush()
                return
            self.send_response(response.get('code', 200))
            for header, value in six.iteritems(response.get('headers', {})):
                self.send_header(header, value)
            self.end_headers()
            output = response.get('output', None)
            if hasattr(output, 'read'):
                shutil.copyfileobj(output, self.wfile)
                output.close()
            elif output is not None:
                self.wfile.write(str(output).encode('utf-8'))
            else:
                self.close_connection = 1
            self.wfile.flush()
            return
        except socket.timeout as e:
            self.log_error("Request timed out: %r", e)
            self.close_connection = 1
            return

    def log_traceback(self):
        self.log_error('Traceback:\n%s' % stacktrace())


class LocalHTTPServer(object):

    def __init__(self, address, config):
        if address[0] != '/':
            raise ValueError('Must use absolute unix socket name')
        if os.path.exists(address):
            os.remove(address)
        self.httpd = ForkingLocalHTTPServer(address, LocalHTTPRequestHandler,
                                            config)

    def get_socket(self):
        return (self.httpd.socket, self.httpd.socket_file)

    def serve(self):
        return self.httpd.serve_forever()