Skip to content

Commit c5225ae

Browse files
committed
Begin writing unit test for external proxy
1 parent dc5af55 commit c5225ae

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

enterprise/wsproxy/proxy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Options struct {
5555

5656
APIRateLimit int
5757
SecureAuthCookie bool
58+
59+
ProxySessionToken string
5860
}
5961

6062
// Server is an external workspace proxy server. This server can communicate
@@ -97,7 +99,7 @@ func New(opts *Options) (*Server, error) {
9799
// Ideally we reuse the same client as the cli, but this can be changed.
98100
// If the auth fails, we need some logic to retry and make sure this client
99101
// is always authenticated and usable.
100-
err := client.SetSessionToken("fake-token")
102+
err := client.SetSessionToken(opts.ProxySessionToken)
101103
if err != nil {
102104
return nil, xerrors.Errorf("set client token: %w", err)
103105
}

enterprise/wsproxy/proxy_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)