diff --git a/coderd/httpapi/url.go b/coderd/httpapi/url.go index b23d28bfd3990..1085281a8fb6f 100644 --- a/coderd/httpapi/url.go +++ b/coderd/httpapi/url.go @@ -2,6 +2,7 @@ package httpapi import ( "fmt" + "hash/crc32" "net" "regexp" "strings" @@ -18,6 +19,8 @@ var ( nameRegex)) validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`) + + crcTable = crc32.MakeTable(crc32.IEEE) ) // ApplicationURL is a parsed application URL hostname. @@ -39,7 +42,12 @@ func (a ApplicationURL) String() string { _, _ = appURL.WriteString(a.WorkspaceName) _, _ = appURL.WriteString("--") _, _ = appURL.WriteString(a.Username) - return appURL.String() + hostname := appURL.String() + + if len(hostname) < 64 { // max length for the subdomain level + return hostname + } + return fmt.Sprintf("app-%08x", crc32.Checksum([]byte(hostname), crcTable)) } // ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If diff --git a/coderd/httpapi/url_test.go b/coderd/httpapi/url_test.go index 8c1dfd8995b94..2372fe23b52ac 100644 --- a/coderd/httpapi/url_test.go +++ b/coderd/httpapi/url_test.go @@ -42,6 +42,16 @@ func TestApplicationURLString(t *testing.T) { }, Expected: "8080--agent--workspace--user", }, + { + Name: "LongAppName", + URL: httpapi.ApplicationURL{ + AppSlugOrPort: "0123456789012345678901234567890123456789", + AgentName: "agent", + WorkspaceName: "workspace", + Username: "user", + }, + Expected: "app-90667f72", + }, } for _, c := range testCases { diff --git a/coderd/workspaceapps/apptest/apptest.go b/coderd/workspaceapps/apptest/apptest.go index 09e734fa92e8c..05486695bd09a 100644 --- a/coderd/workspaceapps/apptest/apptest.go +++ b/coderd/workspaceapps/apptest/apptest.go @@ -1366,7 +1366,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) { }, testutil.WaitLong, testutil.IntervalFast, "stats not reported") assert.Equal(t, workspaceapps.AccessMethodPath, stats[0].AccessMethod) - assert.Equal(t, "test-app-owner", stats[0].SlugOrPort) + assert.Equal(t, proxyTestAppNameOwner, stats[0].SlugOrPort) assert.Equal(t, 1, stats[0].Requests) }) } diff --git a/coderd/workspaceapps/apptest/setup.go b/coderd/workspaceapps/apptest/setup.go index 3175f311dbccd..b1f420dedf5e9 100644 --- a/coderd/workspaceapps/apptest/setup.go +++ b/coderd/workspaceapps/apptest/setup.go @@ -32,10 +32,10 @@ import ( const ( proxyTestAgentName = "agent-name" - proxyTestAppNameFake = "test-app-fake" - proxyTestAppNameOwner = "test-app-owner" - proxyTestAppNameAuthenticated = "test-app-authenticated" - proxyTestAppNamePublic = "test-app-public" + proxyTestAppNameFake = "taf" + proxyTestAppNameOwner = "tao" + proxyTestAppNameAuthenticated = "taa" + proxyTestAppNamePublic = "tap" proxyTestAppQuery = "query=true" proxyTestAppBody = "hello world from apps test"