|
| 1 | +package coderdenttest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/url" |
| 11 | + "regexp" |
| 12 | + "sync" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/coder/coder/codersdk" |
| 16 | + "github.com/moby/moby/pkg/namesgenerator" |
| 17 | + |
| 18 | + "github.com/coder/coder/enterprise/coderd" |
| 19 | + |
| 20 | + "github.com/coder/coder/enterprise/wsproxy" |
| 21 | + |
| 22 | + "github.com/coder/coder/coderd/httpapi" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/coder/coder/coderd/coderdtest" |
| 26 | + |
| 27 | + "github.com/coder/coder/coderd/rbac" |
| 28 | + "github.com/prometheus/client_golang/prometheus" |
| 29 | + |
| 30 | + "cdr.dev/slog" |
| 31 | + "cdr.dev/slog/sloggers/slogtest" |
| 32 | + "github.com/coder/coder/coderd/database" |
| 33 | + "github.com/coder/coder/coderd/database/dbauthz" |
| 34 | + "github.com/coder/coder/coderd/database/dbtestutil" |
| 35 | +) |
| 36 | + |
| 37 | +type ProxyOptions struct { |
| 38 | + Name string |
| 39 | + |
| 40 | + Database database.Store |
| 41 | + Pubsub database.Pubsub |
| 42 | + Authorizer rbac.Authorizer |
| 43 | + TLSCertificates []tls.Certificate |
| 44 | + ProxyURL *url.URL |
| 45 | + AppHostname string |
| 46 | +} |
| 47 | + |
| 48 | +// NewWorkspaceProxy will configure a wsproxy.Server with the given options. |
| 49 | +// The new wsproxy will register itself with the given coderd.API instance. |
| 50 | +// The first user owner client is required to create the wsproxy on the coderd |
| 51 | +// api server. |
| 52 | +func NewWorkspaceProxy(t *testing.T, coderd *coderd.API, owner *codersdk.Client, options *ProxyOptions) *wsproxy.Server { |
| 53 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 54 | + t.Cleanup(cancelFunc) |
| 55 | + |
| 56 | + if options == nil { |
| 57 | + options = &ProxyOptions{} |
| 58 | + } |
| 59 | + |
| 60 | + if options.Authorizer == nil { |
| 61 | + options.Authorizer = &coderdtest.RecordingAuthorizer{ |
| 62 | + Wrapped: rbac.NewCachingAuthorizer(prometheus.NewRegistry()), |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if options.Database == nil { |
| 67 | + options.Database, options.Pubsub = dbtestutil.NewDB(t) |
| 68 | + options.Database = dbauthz.New(options.Database, options.Authorizer, slogtest.Make(t, nil).Leveled(slog.LevelDebug)) |
| 69 | + } |
| 70 | + |
| 71 | + // HTTP Server |
| 72 | + var mutex sync.RWMutex |
| 73 | + var handler http.Handler |
| 74 | + srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 75 | + mutex.RLock() |
| 76 | + defer mutex.RUnlock() |
| 77 | + if handler != nil { |
| 78 | + handler.ServeHTTP(w, r) |
| 79 | + } |
| 80 | + })) |
| 81 | + srv.Config.BaseContext = func(_ net.Listener) context.Context { |
| 82 | + return ctx |
| 83 | + } |
| 84 | + if options.TLSCertificates != nil { |
| 85 | + srv.TLS = &tls.Config{ |
| 86 | + Certificates: options.TLSCertificates, |
| 87 | + MinVersion: tls.VersionTLS12, |
| 88 | + } |
| 89 | + srv.StartTLS() |
| 90 | + } else { |
| 91 | + srv.Start() |
| 92 | + } |
| 93 | + t.Cleanup(srv.Close) |
| 94 | + |
| 95 | + tcpAddr, ok := srv.Listener.Addr().(*net.TCPAddr) |
| 96 | + require.True(t, ok) |
| 97 | + |
| 98 | + serverURL, err := url.Parse(srv.URL) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + serverURL.Host = fmt.Sprintf("localhost:%d", tcpAddr.Port) |
| 102 | + |
| 103 | + accessURL := options.ProxyURL |
| 104 | + if accessURL == nil { |
| 105 | + accessURL = serverURL |
| 106 | + } |
| 107 | + |
| 108 | + // TODO: Stun and derp stuff |
| 109 | + //derpPort, err := strconv.Atoi(serverURL.Port()) |
| 110 | + //require.NoError(t, err) |
| 111 | + // |
| 112 | + //stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{}) |
| 113 | + //t.Cleanup(stunCleanup) |
| 114 | + // |
| 115 | + //derpServer := derp.NewServer(key.NewNode(), tailnet.Logger(slogtest.Make(t, nil).Named("derp").Leveled(slog.LevelDebug))) |
| 116 | + //derpServer.SetMeshKey("test-key") |
| 117 | + |
| 118 | + var appHostnameRegex *regexp.Regexp |
| 119 | + if options.AppHostname != "" { |
| 120 | + var err error |
| 121 | + appHostnameRegex, err = httpapi.CompileHostnamePattern(options.AppHostname) |
| 122 | + require.NoError(t, err) |
| 123 | + } |
| 124 | + |
| 125 | + if options.Name == "" { |
| 126 | + options.Name = namesgenerator.GetRandomName(1) |
| 127 | + } |
| 128 | + |
| 129 | + proxyRes, err := owner.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{ |
| 130 | + Name: options.Name, |
| 131 | + Icon: "/emojis/flag.png", |
| 132 | + URL: accessURL.String(), |
| 133 | + WildcardHostname: options.AppHostname, |
| 134 | + }) |
| 135 | + |
| 136 | + wssrv, err := wsproxy.New(&wsproxy.Options{ |
| 137 | + Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug), |
| 138 | + PrimaryAccessURL: coderd.AccessURL, |
| 139 | + AccessURL: options.ProxyURL, |
| 140 | + AppHostname: options.AppHostname, |
| 141 | + AppHostnameRegex: appHostnameRegex, |
| 142 | + RealIPConfig: coderd.RealIPConfig, |
| 143 | + AppSecurityKey: coderd.AppSecurityKey, |
| 144 | + Tracing: coderd.TracerProvider, |
| 145 | + APIRateLimit: coderd.APIRateLimit, |
| 146 | + SecureAuthCookie: coderd.SecureAuthCookie, |
| 147 | + ProxySessionToken: proxyRes.ProxyToken, |
| 148 | + // We need a new registry to not conflict with the coderd internal |
| 149 | + // proxy metrics. |
| 150 | + PrometheusRegistry: prometheus.NewRegistry(), |
| 151 | + }) |
| 152 | + require.NoError(t, err) |
| 153 | + return wssrv |
| 154 | +} |
0 commit comments