summaryrefslogtreecommitdiff
path: root/turnip/pack/http.py
blob: f8b25e571ccfc3e9b1bfe3f4262a50d609843c4f (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# Copyright 2015 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import base64
import io
import json
import os.path
import tempfile
import textwrap
import time
import uuid
import zlib
from urllib.parse import urlencode

import paste.auth.cookie
import six
from openid.consumer import consumer
from openid.extensions.sreg import SRegRequest, SRegResponse
from paste.auth.cookie import AuthCookieSigner
from paste.auth.cookie import decode as decode_cookie
from paste.auth.cookie import encode as encode_cookie
from twisted.internet import defer, error, protocol
from twisted.internet import reactor as default_reactor
from twisted.python import compat, log
from twisted.web import http, resource, server, static, twcgi, xmlrpc

from turnip.helpers import compose_path
from turnip.pack.git import ERROR_PREFIX, VIRT_ERROR_PREFIX, PackProtocol
from turnip.pack.helpers import (
    TurnipFaultCode,
    encode_packet,
    encode_request,
    get_capabilities_advertisement,
    translate_xmlrpc_fault,
)

try:
    from turnip.version_info import version_info
except ImportError:
    version_info = {}


def fail_request(request, message, code=http.INTERNAL_SERVER_ERROR):
    if not request.startedWriting:
        request.setResponseCode(code)
        request.setHeader(b"Content-Type", b"text/plain; charset=UTF-8")
        if not isinstance(message, bytes):
            message = message.encode("UTF-8")
        request.write(message)
    request.unregisterProducer()
    request.finish()
    # Some callsites want to be able to return from render_*, so make
    # that possible.
    return b""


def get_protocol_version_from_request(request):
    version_header = request.requestHeaders.getRawHeaders(
        b"git-protocol", [b"version=0"]
    )[0]
    try:
        return six.ensure_binary(version_header).split(b"version=", 1)[1]
    except IndexError:
        pass
    return b"0"


class HTTPPackClientProtocol(PackProtocol):
    """Abstract bridge between a Git pack connection and a smart HTTP request.

    The transport must be a connection to a Git pack server.
    factory.http_request is a Git smart HTTP client request.

    Upon backend connection, a factory-defined request is sent, followed
    by the client's request body. If the immediate response is a known
    error, it is mapped to an HTTP response and the connection is
    terminated. Otherwise all data is forwarded from backend to client.

    Concrete implementations must override backendConnected to prepare
    the HTTP response for the backend's reply.
    """

    user_error_possible = True

    def startGoodResponse(self):
        """Prepare the HTTP response for forwarding from the backend."""
        self.factory.http_request.write(
            get_capabilities_advertisement(self.getProtocolVersion())
        )

    def getProtocolVersion(self):
        return get_protocol_version_from_request(self.factory.http_request)

    def backendConnected(self):
        """Called when the backend is connected and has sent a good packet."""
        self.startGoodResponse()

    def backendConnectionFailed(self, msg):
        """Called when the backend fails to connect or returns an error."""
        # stateless-rpc doesn't send a greeting, so we can't always tell if a
        # backend failed to start at all or rejected some user input
        # afterwards. But we can make educated guesses.
        error_code = None
        if msg.startswith(VIRT_ERROR_PREFIX):
            error_name, msg = msg[len(VIRT_ERROR_PREFIX) :].split(b" ", 1)
            if error_name == b"NOT_FOUND":
                error_code = http.NOT_FOUND
            elif error_name == b"FORBIDDEN":
                error_code = http.FORBIDDEN
            elif error_name == b"UNAUTHORIZED":
                error_code = http.UNAUTHORIZED
                self.factory.http_request.setHeader(
                    b"WWW-Authenticate", b"Basic realm=turnip"
                )
            elif error_name == b"GATEWAY_TIMEOUT":
                error_code = http.GATEWAY_TIMEOUT
            else:
                error_code = http.INTERNAL_SERVER_ERROR
        elif msg.startswith(b"Repository is read-only"):
            error_code = http.FORBIDDEN
        elif not self.user_error_possible:
            error_code = http.INTERNAL_SERVER_ERROR

        if error_code is not None:
            # This is probably a system error (bad auth, not found,
            # repository corruption, etc.), so fail the request.
            fail_request(self.factory.http_request, msg, error_code)
            return True
        # We don't know it was a system error, so just send it back to
        # the client as a remote error and proceed to forward data
        # regardless.
        return False

    def _finish(self, result):
        # Ensure the backend dies if the client disconnects.
        if self.transport is not None:
            self.transport.stopProducing()

    def connectionMade(self):
        """Forward the request and the client's payload to the backend."""
        self.factory.deferred.callback(None)
        self.factory.http_request.notifyFinish().addBoth(self._finish)
        self.factory.http_request.registerProducer(self.transport, True)
        self.sendPacket(
            encode_request(
                self.factory.command,
                self.factory.pathname,
                self.factory.params,
            )
        )
        self.sendRawData(self.factory.body.read())
        if hasattr(self.transport, "loseWriteConnection"):
            self.transport.loseWriteConnection()

    def packetReceived(self, data):
        """Check and forward the first packet from the backend.

        Assume that any non-error packet indicates a success response,
        so we can just forward raw data afterward.
        """
        self.raw = True
        if data is not None and data.startswith(ERROR_PREFIX):
            # Handle the error nicely if it's known (eg. 404 on
            # nonexistent repo). If it's unknown, just forward the error
            # along to the client and forward as normal.
            virt_error = self.backendConnectionFailed(
                data[len(ERROR_PREFIX) :]
            )
        else:
            virt_error = False
        if not virt_error:
            self.backendConnected()
            self.rawDataReceived(encode_packet(data))

    def rawDataReceived(self, data):
        if not self.factory.http_request.finished:
            self.factory.http_request.write(data)

    def connectionLost(self, reason):
        if not self.factory.http_request.finished:
            self.factory.http_request.unregisterProducer()
            if reason.check(error.ConnectionDone):
                # We assume that the backend will have sent an error if
                # necessary; otherwise an empty response is permitted (and
                # needed by git's probe_rpc mechanism).
                if not self.paused and not self.raw:
                    self.startGoodResponse()
                self.factory.http_request.finish()
            else:
                fail_request(
                    self.factory.http_request, b"Backend connection lost."
                )


class HTTPPackClientRefsProtocol(HTTPPackClientProtocol):
    # The only user input is the request line, which the virt proxy should
    # cause to always be valid. Any unrecognised error is probably a backend
    # failure from repository corruption or similar.
    user_error_possible = False

    def startGoodResponse(self):
        """Prepare the HTTP response for forwarding from the backend."""
        self.factory.http_request.setResponseCode(http.OK)
        self.factory.http_request.setHeader(
            b"Content-Type",
            b"application/x-%s-advertisement" % self.factory.command,
        )
        super().startGoodResponse()

    def backendConnected(self):
        HTTPPackClientProtocol.backendConnected(self)
        self.rawDataReceived(
            encode_packet(b"# service=%s\n" % self.factory.command)
        )
        self.rawDataReceived(encode_packet(None))


class HTTPPackClientCommandProtocol(HTTPPackClientProtocol):
    def startGoodResponse(self):
        """Prepare the HTTP response for forwarding from the backend."""
        if self.getProtocolVersion() != b"2":
            self.factory.http_request.setResponseCode(http.OK)
            self.factory.http_request.setHeader(
                b"Content-Type",
                b"application/x-%s-result" % self.factory.command,
            )


class HTTPPackClientFactory(protocol.ClientFactory):
    def __init__(self, command, pathname, params, body, http_request, d):
        self.command = command
        self.pathname = pathname
        self.params = params
        self.body = body
        self.http_request = http_request
        self.deferred = d

    def clientConnectionFailed(self, connector, reason):
        self.deferred.errback(reason)


class HTTPPackClientCommandFactory(HTTPPackClientFactory):
    protocol = HTTPPackClientCommandProtocol


class HTTPPackClientRefsFactory(HTTPPackClientFactory):
    protocol = HTTPPackClientRefsProtocol


class BaseSmartHTTPResource(resource.Resource):
    """Base HTTP resource for Git smart HTTP auth and error handling."""

    extra_params = {}

    def errback(self, failure, request, msg):
        """Handle a Twisted failure by returning an HTTP error."""
        log.err(failure, msg)
        if request.finished:
            return
        fail_request(request, msg)

    @defer.inlineCallbacks
    def authenticateUser(self, request):
        """Attempt authentication of the request with the virt service."""
        if request.getUser() or request.getPassword():
            params = yield self.root.authenticateWithPassword(
                request.getUser(), request.getPassword()
            )
            return params
        return {}

    @defer.inlineCallbacks
    def connectToBackend(self, factory, service, path, content, request):
        """Establish a pack connection to the backend.

        The turnip-authenticated-* parameters are set to the values returned
        by the virt service, if any.
        """
        params = {
            b"turnip-can-authenticate": b"yes",
            b"turnip-request-id": str(uuid.uuid4()),
            b"version": six.ensure_binary(
                get_protocol_version_from_request(request)
            ),
        }
        authenticated_params = yield self.authenticateUser(request)
        for key, value in authenticated_params.items():
            encoded_key = "turnip-authenticated-" + six.ensure_str(key)
            params[encoded_key] = str(value)
        params.update(self.extra_params)
        d = defer.Deferred()
        client_factory = factory(service, path, params, content, request, d)
        self.root.connectToBackend(client_factory)
        yield d


class SmartHTTPRefsResource(BaseSmartHTTPResource):
    """HTTP resource for Git smart HTTP ref discovery requests."""

    isLeaf = True

    extra_params = {
        b"turnip-stateless-rpc": b"yes",
        b"turnip-advertise-refs": b"yes",
    }

    def __init__(self, root, path):
        self.root = root
        self.path = path

    def render_GET(self, request):
        try:
            service = request.args[b"service"][0]
        except (KeyError, IndexError):
            return fail_request(
                request,
                b"Only git smart HTTP clients are supported.",
                code=http.NOT_FOUND,
            )

        if service not in self.root.allowed_services:
            return fail_request(
                request, b"Unsupported service.", code=http.FORBIDDEN
            )

        d = self.connectToBackend(
            HTTPPackClientRefsFactory,
            service,
            self.path,
            request.content,
            request,
        )
        d.addErrback(self.errback, request, b"Backend connection failed")
        return server.NOT_DONE_YET


class SmartHTTPCommandResource(BaseSmartHTTPResource):
    """HTTP resource for Git smart HTTP command requests."""

    isLeaf = True

    extra_params = {b"turnip-stateless-rpc": b"yes"}

    def __init__(self, root, service, path):
        self.root = root
        self.service = service
        self.path = path

    def render_POST(self, request):
        content_type = request.requestHeaders.getRawHeaders(b"Content-Type")
        if content_type != [b"application/x-%s-request" % self.service]:
            return fail_request(
                request,
                b"Invalid Content-Type for service.",
                code=http.BAD_REQUEST,
            )

        content = request.content
        # XXX: We really need to hack twisted.web to stream the request
        # body, and decode it in a less hacky manner (git always uses
        # C-E: gzip without negotiating).
        content_encoding = request.requestHeaders.getRawHeaders(
            b"Content-Encoding"
        )
        if content_encoding == [b"gzip"]:
            content = io.BytesIO(
                zlib.decompress(request.content.read(), 16 + zlib.MAX_WBITS)
            )
        d = self.connectToBackend(
            HTTPPackClientCommandFactory,
            self.service,
            self.path,
            content,
            request,
        )
        d.addErrback(self.errback, request, b"Backend connection failed")
        return server.NOT_DONE_YET


class SmartHTTPRootResource(resource.Resource):
    """HTTP resource to handle operations on the root path."""

    def __init__(self, root):
        self.root = root

    def render_GET(self, request):
        if "revision_id" in version_info:
            request.setHeader(
                b"X-Turnip-Revision",
                version_info["revision_id"].encode("UTF-8"),
            )
        request.redirect(self.root.main_site_root)
        return b""

    def render_OPTIONS(self, request):
        # Trivially respond to OPTIONS / for the sake of haproxy.
        return b""


class DirectoryWithoutListings(static.File):
    """A static directory resource without support for directory listings."""

    def directoryListing(self):
        return self.childNotFound


class RobotsResource(static.Data):
    """HTTP resource to serve our robots.txt."""

    robots_txt = textwrap.dedent(
        """\
        User-agent: *
        Disallow: /
        """
    ).encode("US-ASCII")

    def __init__(self):
        static.Data.__init__(self, self.robots_txt, "text/plain")


class CGitScriptResource(twcgi.CGIScript):
    """HTTP resource to run cgit."""

    def __init__(self, root, repo_url, repo_path, trailing, private):
        twcgi.CGIScript.__init__(self, root.cgit_exec_path)
        self.root = root
        self.repo_url = repo_url
        self.repo_path = repo_path
        self.trailing = trailing
        self.private = private
        self.cgit_config = None

    def _finished(self, ignored):
        if self.cgit_config is not None:
            self.cgit_config.close()

    def runProcess(self, env, request, *args, **kwargs):
        request.notifyFinish().addBoth(self._finished)
        self.cgit_config = tempfile.NamedTemporaryFile(
            mode="w+", prefix="turnip-cgit-"
        )
        os.chmod(self.cgit_config.name, 0o644)
        fmt = {
            "repo_url": six.ensure_text(self.repo_url),
            "repo_path": six.ensure_text(self.repo_path),
        }
        if self.root.site_name is not None:
            prefixes = " ".join(
                f"{scheme}://{self.root.site_name}"
                for scheme in ("git", "git+ssh", "https")
            )
            print(f"clone-prefix={prefixes}", file=self.cgit_config)
        if self.private:
            fmt["css"] = "/static/cgit-private.css"
        else:
            fmt["css"] = "/static/cgit-public.css"
        print(
            textwrap.dedent(
                """\
            css={css}
            enable-http-clone=0
            enable-index-owner=0
            logo=/static/launchpad-logo.png
            """
            ).format(**fmt),
            end="",
            file=self.cgit_config,
        )
        if self.private:
            top = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
            print(
                f"header={top}/static/private-banner.html",
                file=self.cgit_config,
            )
        print(file=self.cgit_config)
        print(
            textwrap.dedent(
                """\
            repo.url={repo_url}
            repo.path={repo_path}
            """
            ).format(**fmt),
            end="",
            file=self.cgit_config,
        )
        self.cgit_config.flush()
        env["CGIT_CONFIG"] = self.cgit_config.name
        env["PATH_INFO"] = f"/{self.repo_url}{self.trailing}"
        env["SCRIPT_NAME"] = "/"
        env["QUERY_STRING"] = six.ensure_text(env["QUERY_STRING"])
        twcgi.CGIScript.runProcess(self, env, request, *args, **kwargs)


class TurnipAuthCookieSigner(AuthCookieSigner):
    def auth(self, cookie):
        """
        Authenticate the cooke using the signature, verify that it
        has not expired; and return the cookie's content.

        This was moved from the original AuthCookieSigner to make it python3
        compatible, since paste.auth.* is marked to be deprecated according to
        https://pythonpaste.readthedocs.io/en/latest/future.html#to-deprecate.
        """
        _signature_size = paste.auth.cookie._signature_size
        _header_size = paste.auth.cookie._header_size
        sha1 = paste.auth.cookie.sha1
        hmac = paste.auth.cookie.hmac
        make_time = paste.auth.cookie.make_time
        decode = base64.decodebytes(
            cookie.replace(b"_", b"/").replace(b"~", b"=")
        )
        signature = decode[:_signature_size]
        expires = decode[_signature_size:_header_size]
        content = decode[_header_size:]
        if signature == hmac.new(self.secret, content, sha1).digest():
            if int(expires) > int(make_time(time.time())):
                return content
            else:
                # This is the normal case of an expired cookie; just
                # don't bother doing anything here.
                pass
        else:
            # This case can happen if the server is restarted with a
            # different secret; or if the user's IP address changed
            # due to a proxy.  However, it could also be a break-in
            # attempt -- so should it be reported?
            pass


class BaseHTTPAuthResource(resource.Resource):
    """Base HTTP resource for OpenID authentication handling."""

    session_var = "turnip.session"
    cookie_name = b"TURNIP_COOKIE"
    anonymous_id = "+launchpad-anonymous"

    def __init__(self, root):
        resource.Resource.__init__(self)
        self.root = root
        if root.cgit_secret is not None:
            self.signer = TurnipAuthCookieSigner(root.cgit_secret)
        else:
            self.signer = None

    def _getSession(self, request):
        if self.signer is not None:
            cookie = request.getCookie(self.cookie_name)
            if cookie is not None:
                content = self.signer.auth(cookie)
                if content:
                    return json.loads(decode_cookie(six.ensure_text(content)))
        return {}

    def _putSession(self, request, session):
        if self.signer is not None:
            content = self.signer.sign(encode_cookie(json.dumps(session)))
            cookie = b"%s=%s; Path=/; secure;" % (self.cookie_name, content)
            request.setHeader(b"Set-Cookie", cookie)

    def _setErrorCode(self, request, code=http.INTERNAL_SERVER_ERROR):
        request.setResponseCode(code)
        request.setHeader(b"Content-Type", b"text/plain")

    def _makeConsumer(self, session):
        """Build an OpenID `Consumer` object with standard arguments."""
        # Multiple instances need to share a store or not use one at all (in
        # which case they will use check_authentication).  Using no store is
        # easier, and check_authentication is cheap.
        return consumer.Consumer(session, None)


class HTTPAuthLoginResource(BaseHTTPAuthResource):
    """HTTP resource to complete OpenID authentication."""

    isLeaf = True

    def render_GET(self, request):
        """Complete the OpenID authentication process.

        Here we handle the result of the OpenID process.  If the process
        succeeded, we record the identity URL and username in the session
        and redirect the user to the page they were trying to view that
        triggered the login attempt.  In the various failure cases we return
        a 401 Unauthorized response with a brief explanation of what went
        wrong.
        """
        session = self._getSession(request)
        query = {
            six.ensure_text(k): six.ensure_text(v[-1])
            for k, v in request.args.items()
        }
        response = self._makeConsumer(session).complete(
            query, query["openid.return_to"]
        )
        if response.status == consumer.SUCCESS:
            log.msg("OpenID response: SUCCESS")
            sreg_info = SRegResponse.fromSuccessResponse(response)
            if not sreg_info:
                log.msg("sreg_info is None")
                self._setErrorCode(request, http.UNAUTHORIZED)
                return (
                    b"You don't have a Launchpad account.  Check that you're "
                    b"logged in as the right user, or log into Launchpad and "
                    b"try again."
                )
            else:
                session["identity_url"] = response.identity_url
                session["user"] = sreg_info["nickname"]
                self._putSession(request, session)
                request.redirect(six.ensure_binary(query["back_to"]))
                return b""
        elif response.status == consumer.FAILURE:
            log.msg("OpenID response: FAILURE: %s" % response.message)
            self._setErrorCode(request, http.UNAUTHORIZED)
            return response.message.encode("UTF-8")
        elif response.status == consumer.CANCEL:
            log.msg("OpenID response: CANCEL")
            self._setErrorCode(request, http.UNAUTHORIZED)
            return b"Authentication cancelled."
        else:
            log.msg("OpenID response: UNKNOWN")
            self._setErrorCode(request, http.UNAUTHORIZED)
            return b"Unknown OpenID response."


class HTTPAuthLogoutResource(BaseHTTPAuthResource):
    """HTTP resource to log out of OpenID authentication."""

    isLeaf = True

    def render_GET(self, request):
        """Log out of turnip.

        Clear the cookie and redirect to `next_to`.
        """
        self._putSession(request, {})
        if "next_to" in request.args:
            next_url = request.args["next_to"][-1]
        else:
            next_url = self.root.main_site_root
        request.redirect(next_url)
        return b""


class HTTPAuthRootResource(BaseHTTPAuthResource):
    """HTTP resource to translate a path and authenticate if necessary.

    Requests that require further authentication are denied or sent through
    OpenID redirection, as appropriate.  Properly-authenticated requests are
    passed on to cgit.
    """

    isLeaf = True

    def _beginLogin(self, request, session):
        """Start the process of authenticating with OpenID.

        We redirect the user to Launchpad to identify themselves.  Launchpad
        will then redirect them to our +login page with enough information
        that we can then redirect them again to the page they were looking
        at, with a cookie that gives us the identity URL and username.
        """
        base_url = "https://%s" % compat.nativeString(
            request.getRequestHostname()
        )
        back_to = six.ensure_binary(base_url) + request.uri
        realm = base_url + "/"
        return_to = base_url + "/+login/?" + urlencode({"back_to": back_to})

        openid_request = self._makeConsumer(session).begin(
            self.root.openid_provider_root
        )
        openid_request.addExtension(SRegRequest(required=["nickname"]))
        target = openid_request.redirectURL(realm, return_to)
        request.redirect(target.encode("UTF-8"))
        request.finish()

    def _translatePathCallback(self, translated, request):
        if "path" not in translated:
            return fail_request(
                request, "translatePath response did not include path"
            )
        repo_url = six.ensure_text(request.path.rstrip(b"/"))
        # cgit simply parses configuration values up to the end of a line
        # following the first '=', so almost anything is safe, but
        # double-check that there are no newlines to confuse things.
        if "\n" in repo_url:
            return fail_request(
                request, "repository URL may not contain newlines"
            )
        try:
            repo_path = compose_path(
                self.root.repo_store, six.ensure_binary(translated["path"])
            )
        except ValueError as e:
            return fail_request(request, str(e))
        trailing = translated.get("trailing")
        if trailing:
            if not trailing.startswith("/"):
                trailing = "/" + trailing
            if not repo_url.endswith(trailing):
                return fail_request(
                    request,
                    "translatePath returned inconsistent response: "
                    '"%s" does not end with "%s"' % (repo_url, trailing),
                )
            repo_url = repo_url[: -len(trailing)]
        repo_url = six.ensure_text(repo_url.strip("/"))
        cgit_resource = CGitScriptResource(
            self.root, repo_url, repo_path, trailing, translated["private"]
        )
        # XXX pappacena 2020-12-15: CGIScript.render assumes postpath items
        # are str, and fail if any item is bytes.
        request.postpath = [six.ensure_text(i) for i in request.postpath]
        request.render(cgit_resource)

    def _translatePathErrback(self, failure, request, session):
        if failure.check(defer.TimeoutError) is not None:
            code = TurnipFaultCode.GATEWAY_TIMEOUT
            message = "Path translation timed out."
        elif failure.check(xmlrpc.Fault) is not None:
            code = translate_xmlrpc_fault(failure.value.faultCode)
            message = failure.value.faultString
        else:
            code, message = None, "Unexpected error in translatePath."
        if code == TurnipFaultCode.NOT_FOUND:
            error_code = http.NOT_FOUND
        elif code == TurnipFaultCode.FORBIDDEN:
            error_code = http.FORBIDDEN
        elif code == TurnipFaultCode.UNAUTHORIZED:
            if "user" in session:
                error_code = http.FORBIDDEN
                message = (
                    "You are logged in as %s, but do not have access to this "
                    "repository." % session["user"]
                )
            elif self.signer is None:
                error_code = http.FORBIDDEN
                message = "Server does not support OpenID authentication."
            else:
                self._beginLogin(request, session)
                return
        elif code == TurnipFaultCode.GATEWAY_TIMEOUT:
            error_code = http.GATEWAY_TIMEOUT
        else:
            log.err(failure, "Unexpected error in translatePath")
            error_code = http.INTERNAL_SERVER_ERROR
        fail_request(request, message, code=error_code)

    def render_GET(self, request):
        session = self._getSession(request)
        identity_url = session.get("identity_url", self.anonymous_id)
        proxy = xmlrpc.Proxy(self.root.virtinfo_endpoint, allowNone=True)
        d = proxy.callRemote(
            "translatePath",
            six.ensure_text(request.path),
            "read",
            {"uid": identity_url, "can-authenticate": True},
        )
        d.addTimeout(self.root.virtinfo_timeout, self.root.reactor)
        d.addCallback(self._translatePathCallback, request)
        d.addErrback(self._translatePathErrback, request, session)
        return server.NOT_DONE_YET


class HTTPAuthResource(resource.Resource):
    """Container for the various HTTP authentication resources."""

    def __init__(self, root):
        resource.Resource.__init__(self)
        self.root = root
        self.putChild(b"+login", HTTPAuthLoginResource(root))
        self.putChild(b"+logout", HTTPAuthLogoutResource(root))

    def getChild(self, path, request):
        # Delegate to a child resource without consuming a path element.
        request.postpath.insert(0, path)
        request.prepath.pop()
        return HTTPAuthRootResource(self.root)


class SmartHTTPFrontendResource(resource.Resource):
    """HTTP resource to translate Git smart HTTP requests to pack protocol."""

    allowed_services = frozenset(
        (b"git-upload-pack", b"git-receive-pack", b"turnip-set-symbolic-ref")
    )

    def __init__(self, config, reactor=None):
        resource.Resource.__init__(self)
        self.backend_host = config.get("pack_virt_host")
        self.backend_port = int(config.get("pack_virt_port"))
        self.virtinfo_endpoint = six.ensure_binary(
            config.get("virtinfo_endpoint")
        )
        self.virtinfo_timeout = int(config.get("virtinfo_timeout"))
        self.reactor = reactor or default_reactor
        # XXX cjwatson 2015-03-30: Knowing about the store path here
        # violates turnip's layering and may cause scaling problems later,
        # but for now cgit needs direct filesystem access.
        self.repo_store = config.get("repo_store")
        self.cgit_exec_path = config.get("cgit_exec_path")
        self.openid_provider_root = config.get("openid_provider_root")
        self.site_name = config.get("site_name")
        self.main_site_root = config.get("main_site_root")
        self.putChild(b"", SmartHTTPRootResource(self))
        cgit_data_path = config.get("cgit_data_path")
        if cgit_data_path is not None:
            static_resource = DirectoryWithoutListings(
                cgit_data_path, defaultType="text/plain"
            )
            top = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
            stdir = os.path.join(top, "static")
            for name in ("launchpad-logo.png", "notification-private.png"):
                path = os.path.join(stdir, name)
                static_resource.putChild(
                    name.encode("UTF-8"), static.File(path)
                )
            with open(os.path.join(cgit_data_path, "cgit.css"), "rb") as f:
                css = f.read()
            with open(os.path.join(stdir, "ubuntu-webfonts.css"), "rb") as f:
                css += b"\n" + f.read()
            with open(os.path.join(stdir, "global.css"), "rb") as f:
                css += b"\n" + f.read()
            with open(os.path.join(stdir, "private.css"), "rb") as f:
                private_css = css + b"\n" + f.read()
            static_resource.putChild(
                b"cgit-public.css", static.Data(css, "text/css")
            )
            static_resource.putChild(
                b"cgit-private.css", static.Data(private_css, "text/css")
            )
            self.putChild(b"static", static_resource)
            favicon = os.path.join(stdir, "launchpad.png")
            self.putChild(b"favicon.ico", static.File(favicon))
            self.putChild(b"robots.txt", RobotsResource())
        cgit_secret_path = config.get("cgit_secret_path")
        if cgit_secret_path:
            with open(cgit_secret_path, "rb") as cgit_secret_file:
                self.cgit_secret = cgit_secret_file.read()
        else:
            self.cgit_secret = None

    @staticmethod
    def _isGitRequest(request):
        if request.path.endswith(b"/info/refs"):
            service = request.args.get(b"service", [])
            if service and service[0].startswith(b"git-"):
                return True
        content_type = request.getHeader(b"Content-Type")
        if content_type is None:
            return False
        return content_type.startswith(
            b"application/x-git-"
        ) or content_type.startswith(b"application/x-turnip-")

    def getChild(self, path, request):
        if self._isGitRequest(request):
            if request.path.endswith(b"/info/refs"):
                # /PATH/TO/REPO/info/refs
                return SmartHTTPRefsResource(
                    self, request.path[: -len(b"/info/refs")]
                )
            try:
                # /PATH/TO/REPO/SERVICE
                path, service = request.path.rsplit(b"/", 1)
            except ValueError:
                path = request.path
                service = None
            if service in self.allowed_services:
                return SmartHTTPCommandResource(self, service, path)
        elif self.cgit_exec_path is not None:
            # Delegate to a child resource without consuming a path element.
            request.postpath.insert(0, path)
            request.prepath.pop()
            return HTTPAuthResource(self)
        return resource.NoResource(b"No such resource")

    def connectToBackend(self, client_factory):
        self.reactor.connectTCP(
            self.backend_host, self.backend_port, client_factory
        )

    @defer.inlineCallbacks
    def authenticateWithPassword(self, user, password):
        proxy = xmlrpc.Proxy(self.virtinfo_endpoint)
        try:
            translated = yield proxy.callRemote(
                "authenticateWithPassword",
                six.ensure_str(user),
                six.ensure_str(password),
            )
        except xmlrpc.Fault as e:
            code = translate_xmlrpc_fault(e.faultCode)
            if code == TurnipFaultCode.UNAUTHORIZED:
                return {}
            else:
                raise
        return translated