Debian Codegraph @

plumbing/transport/http/proxy_test.go

raw
1 package http
2
3 import (
4 "context"
5 "crypto/tls"
6 "fmt"
7 "net"
8 "net/http"
9 "strings"
10 "sync/atomic"
11
12 "github.com/elazarl/goproxy"
13 fixtures "github.com/go-git/go-git-fixtures/v4"
14 "github.com/go-git/go-git/v5/plumbing/transport"
15 "github.com/go-git/go-git/v5/plumbing/transport/http/internal/test"
16
17 . "gopkg.in/check.v1"
18 )
19
20 type ProxySuite struct {
21 u UploadPackSuite
22 fixtures.Suite
23 }
24
25 var _ = Suite(&ProxySuite{})
26
27 var proxiedRequests int32
28
29 func (s *ProxySuite) TestAdvertisedReferences(c *C) {
30 s.u.SetUpTest(c)
31 proxy := goproxy.NewProxyHttpServer()
32 proxy.Verbose = true
33 setupHTTPProxy(proxy, &proxiedRequests)
34 httpListener, err := net.Listen("tcp", ":0")
35 c.Assert(err, IsNil)
36 defer httpListener.Close()
37
38 httpProxyAddr := fmt.Sprintf("http://localhost:%d", httpListener.Addr().(*net.TCPAddr).Port)
39 proxyServer := http.Server{
40 Addr: httpProxyAddr,
41 Handler: proxy,
42 }
43 go proxyServer.Serve(httpListener)
44 defer proxyServer.Close()
45
46 endpoint := s.u.prepareRepository(c, fixtures.Basic().One(), "basic.git")
47 endpoint.Proxy = transport.ProxyOptions{
48 URL: httpProxyAddr,
49 Username: "user",
50 Password: "pass",
51 }
52
53 s.u.Client = NewClient(nil)
54 session, err := s.u.Client.NewUploadPackSession(endpoint, nil)
55 c.Assert(err, IsNil)
56
57 ctx, cancel := context.WithCancel(context.Background())
58 defer cancel()
59 info, err := session.AdvertisedReferencesContext(ctx)
60 c.Assert(err, IsNil)
61 c.Assert(info, NotNil)
62 proxyUsed := atomic.LoadInt32(&proxiedRequests) > 0
63 c.Assert(proxyUsed, Equals, true)
64
65 atomic.StoreInt32(&proxiedRequests, 0)
66 test.SetupHTTPSProxy(proxy, &proxiedRequests)
67 httpsListener, err := net.Listen("tcp", ":0")
68 c.Assert(err, IsNil)
69 defer httpsListener.Close()
70 httpsProxyAddr := fmt.Sprintf("https://localhost:%d", httpsListener.Addr().(*net.TCPAddr).Port)
71
72 tlsProxyServer := http.Server{
73 Addr: httpsProxyAddr,
74 Handler: proxy,
75 // Due to how golang manages http/2 when provided with custom TLS config,
76 // servers and clients running in the same process leads to issues.
77 // Ref: https://github.com/golang/go/issues/21336
78 TLSConfig: &tls.Config{
79 NextProtos: []string{"http/1.1"},
80 },
81 }
82 go tlsProxyServer.ServeTLS(httpsListener, "testdata/certs/server.crt", "testdata/certs/server.key")
83 defer tlsProxyServer.Close()
84
85 endpoint, err = transport.NewEndpoint("https://github.com/git-fixtures/basic.git")
86 c.Assert(err, IsNil)
87 endpoint.Proxy = transport.ProxyOptions{
88 URL: httpsProxyAddr,
89 Username: "user",
90 Password: "pass",
91 }
92 endpoint.InsecureSkipTLS = true
93
94 session, err = s.u.Client.NewUploadPackSession(endpoint, nil)
95 c.Assert(err, IsNil)
96
97 info, err = session.AdvertisedReferencesContext(ctx)
98 c.Assert(err, IsNil)
99 c.Assert(info, NotNil)
100 proxyUsed = atomic.LoadInt32(&proxiedRequests) > 0
101 c.Assert(proxyUsed, Equals, true)
102 }
103
104 func setupHTTPProxy(proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) {
105 // The request is being forwarded to the local test git server in this handler.
106 var proxyHandler goproxy.FuncReqHandler = func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
107 if strings.Contains(req.Host, "localhost") {
108 user, pass, _ := test.ParseBasicAuth(req.Header.Get("Proxy-Authorization"))
109 if user != "user" || pass != "pass" {
110 return req, goproxy.NewResponse(req, goproxy.ContentTypeText, http.StatusUnauthorized, "")
111 }
112 atomic.AddInt32(proxiedRequests, 1)
113 return req, nil
114 }
115 // Reject if it isn't our request.
116 return req, goproxy.NewResponse(req, goproxy.ContentTypeText, http.StatusForbidden, "")
117 }
118 proxy.OnRequest().Do(proxyHandler)
119 }