Skip to content

Commit

Permalink
All URLs now point to mitmproxy URLs
Browse files Browse the repository at this point in the history
Previously, Go clients created in Deployment would point directly to the underlying
HS. This is usually fine because the Go clients are used to set up / configure the
test rather than run the test, but we have numerous helpers which convert Go clients
into actual clients-under-test. If the `.BaseURL` was not set when this happened, it
would cause the client-under-test to not be pointing to a mitmproxy'd URL.

This commit removes this footgun.
  • Loading branch information
kegsay committed Mar 4, 2024
1 parent 1105bfc commit da10f98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
30 changes: 26 additions & 4 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"github.com/docker/go-connections/nat"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement-crypto/internal/api"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/ct"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/must"
testcontainers "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
Expand All @@ -38,7 +41,7 @@ type SlidingSyncDeployment struct {
slidingSyncURL string
mitmClient *http.Client
ControllerURL string
proxyURLToHS map[string]string
proxyHSToURL map[string]string
mu sync.RWMutex
tcpdump *exec.Cmd
}
Expand Down Expand Up @@ -113,10 +116,29 @@ func (d *SlidingSyncDeployment) unlockOptions(t *testing.T, lockID []byte) {
must.Equal(t, res.StatusCode, 200, "controller returned wrong HTTP status")
}

func (d *SlidingSyncDeployment) ReverseProxyURLForHS(hsName string) string {
func (d *SlidingSyncDeployment) UnauthenticatedClient(t ct.TestLike, serverName string) *client.CSAPI {
return d.withReverseProxyURL(serverName, d.Deployment.UnauthenticatedClient(t, serverName))
}

func (d *SlidingSyncDeployment) Register(t ct.TestLike, hsName string, opts helpers.RegistrationOpts) *client.CSAPI {
return d.withReverseProxyURL(hsName, d.Deployment.Register(t, hsName, opts))
}

func (d *SlidingSyncDeployment) Login(t ct.TestLike, hsName string, existing *client.CSAPI, opts helpers.LoginOpts) *client.CSAPI {
return d.withReverseProxyURL(hsName, d.Deployment.Login(t, hsName, existing, opts))
}

func (d *SlidingSyncDeployment) AppServiceUser(t ct.TestLike, hsName, appServiceUserID string) *client.CSAPI {
return d.withReverseProxyURL(hsName, d.Deployment.AppServiceUser(t, hsName, appServiceUserID))
}

// Replace the actual HS URL with a mitmproxy reverse proxy URL so we can sniff/intercept/modify traffic.
func (d *SlidingSyncDeployment) withReverseProxyURL(hsName string, c *client.CSAPI) *client.CSAPI {
d.mu.RLock()
defer d.mu.RUnlock()
return d.proxyURLToHS[hsName]
proxyURL := d.proxyHSToURL[hsName]
c.BaseURL = proxyURL
return c
}

func (d *SlidingSyncDeployment) Teardown(writeLogs bool) {
Expand Down Expand Up @@ -343,7 +365,7 @@ func RunNewDeployment(t *testing.T, mitmProxyAddonsDir string, shouldTCPDump boo
Proxy: http.ProxyURL(proxyURL),
},
},
proxyURLToHS: map[string]string{
proxyHSToURL: map[string]string{
"hs1": rpHS1URL,
"hs2": rpHS2URL,
},
Expand Down
1 change: 0 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ func (c *TestContext) MustRegisterNewDevice(t *testing.T, cli *client.CSAPI, hsN
func (c *TestContext) MustCreateClient(t *testing.T, cli *client.CSAPI, clientType api.ClientType, options ...func(*api.ClientCreationOpts)) api.Client {
t.Helper()
cfg := api.NewClientCreationOpts(cli)
cfg.BaseURL = c.Deployment.ReverseProxyURLForHS(clientType.HS)
for _, opt := range options {
opt(&cfg)
}
Expand Down
1 change: 0 additions & 1 deletion tests/state_synchronisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func testSigkillBeforeKeysUploadResponseRust(t *testing.T, clientType api.Client
},
}, func() {
cfg := api.NewClientCreationOpts(tc.Alice)
cfg.BaseURL = tc.Deployment.ReverseProxyURLForHS(clientType.HS)
cfg.PersistentStorage = true
// run some code in a separate process so we can kill it later
cmd, close := templates.PrepareGoScript(t, "testSigkillBeforeKeysUploadResponseRust/test.go",
Expand Down

0 comments on commit da10f98

Please sign in to comment.