|
| 1 | +package wsproxy_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/coder/coder/coderd/httpapi" |
| 9 | + "github.com/coder/coder/enterprise/wsproxy" |
| 10 | + |
| 11 | + "github.com/moby/moby/pkg/namesgenerator" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/coder/coder/enterprise/coderd/license" |
| 15 | + |
| 16 | + "github.com/coder/coder/codersdk" |
| 17 | + |
| 18 | + "github.com/coder/coder/enterprise/coderd/coderdenttest" |
| 19 | + |
| 20 | + "github.com/coder/coder/cli/clibase" |
| 21 | + "github.com/coder/coder/coderd/coderdtest" |
| 22 | + "github.com/coder/coder/coderd/httpmw" |
| 23 | + "github.com/coder/coder/coderd/workspaceapps/apptest" |
| 24 | +) |
| 25 | + |
| 26 | +func TestExternalProxyWorkspaceApps(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + apptest.Run(t, func(t *testing.T, opts *apptest.DeploymentOptions) *apptest.Deployment { |
| 30 | + deploymentValues := coderdtest.DeploymentValues(t) |
| 31 | + deploymentValues.DisablePathApps = clibase.Bool(opts.DisablePathApps) |
| 32 | + deploymentValues.Dangerous.AllowPathAppSharing = clibase.Bool(opts.DangerousAllowPathAppSharing) |
| 33 | + deploymentValues.Dangerous.AllowPathAppSiteOwnerAccess = clibase.Bool(opts.DangerousAllowPathAppSiteOwnerAccess) |
| 34 | + deploymentValues.Experiments = []string{ |
| 35 | + string(codersdk.ExperimentMoons), |
| 36 | + "*", |
| 37 | + } |
| 38 | + |
| 39 | + client, _, api := coderdenttest.NewWithAPI(t, &coderdenttest.Options{ |
| 40 | + Options: &coderdtest.Options{ |
| 41 | + DeploymentValues: deploymentValues, |
| 42 | + // TODO: @emyrk Should we give a hostname here too? |
| 43 | + AppHostname: "", |
| 44 | + IncludeProvisionerDaemon: true, |
| 45 | + RealIPConfig: &httpmw.RealIPConfig{ |
| 46 | + TrustedOrigins: []*net.IPNet{{ |
| 47 | + IP: net.ParseIP("127.0.0.1"), |
| 48 | + Mask: net.CIDRMask(8, 32), |
| 49 | + }}, |
| 50 | + TrustedHeaders: []string{ |
| 51 | + "CF-Connecting-IP", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | + user := coderdtest.CreateFirstUser(t, client) |
| 58 | + _ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{ |
| 59 | + Features: license.Features{ |
| 60 | + codersdk.FeatureWorkspaceProxy: 1, |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + // Create the external proxy |
| 65 | + // TODO: @emyrk this code will probably change as we create a better |
| 66 | + // method of creating external proxies. |
| 67 | + ctx := context.Background() |
| 68 | + proxyRes, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{ |
| 69 | + Name: namesgenerator.GetRandomName(1), |
| 70 | + Icon: "/emojis/flag.png", |
| 71 | + URL: "https://" + namesgenerator.GetRandomName(1) + ".com", |
| 72 | + WildcardHostname: opts.AppHost, |
| 73 | + }) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + appHostRegex, err := httpapi.CompileHostnamePattern(opts.AppHost) |
| 77 | + require.NoError(t, err, "app host regex should compile") |
| 78 | + |
| 79 | + // Make the external proxy service |
| 80 | + proxy, err := wsproxy.New(&wsproxy.Options{ |
| 81 | + Logger: api.Logger, |
| 82 | + PrimaryAccessURL: api.AccessURL, |
| 83 | + // TODO: @emyrk give this an access url |
| 84 | + AccessURL: nil, |
| 85 | + AppHostname: opts.AppHost, |
| 86 | + AppHostnameRegex: appHostRegex, |
| 87 | + RealIPConfig: api.RealIPConfig, |
| 88 | + AppSecurityKey: api.AppSecurityKey, |
| 89 | + Tracing: api.TracerProvider, |
| 90 | + PrometheusRegistry: api.PrometheusRegistry, |
| 91 | + APIRateLimit: api.APIRateLimit, |
| 92 | + SecureAuthCookie: api.SecureAuthCookie, |
| 93 | + ProxySessionToken: proxyRes.ProxyToken, |
| 94 | + }) |
| 95 | + require.NoError(t, err, "wsproxy should be created") |
| 96 | + |
| 97 | + // TODO: Run the wsproxy, http.Serve |
| 98 | + _ = proxy |
| 99 | + |
| 100 | + return &apptest.Deployment{ |
| 101 | + Options: opts, |
| 102 | + Client: client, |
| 103 | + FirstUser: user, |
| 104 | + PathAppBaseURL: client.URL, |
| 105 | + } |
| 106 | + }) |
| 107 | +} |
0 commit comments