fix: use CRC32 to shorten app subdomain (#9645)

This commit is contained in:
Marcin Tojek
2023-09-12 17:11:45 +02:00
committed by GitHub
parent 898971b329
commit 0e28397c82
4 changed files with 24 additions and 6 deletions
+9 -1
View File
@@ -2,6 +2,7 @@ package httpapi
import ( import (
"fmt" "fmt"
"hash/crc32"
"net" "net"
"regexp" "regexp"
"strings" "strings"
@@ -18,6 +19,8 @@ var (
nameRegex)) nameRegex))
validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`) validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
crcTable = crc32.MakeTable(crc32.IEEE)
) )
// ApplicationURL is a parsed application URL hostname. // ApplicationURL is a parsed application URL hostname.
@@ -39,7 +42,12 @@ func (a ApplicationURL) String() string {
_, _ = appURL.WriteString(a.WorkspaceName) _, _ = appURL.WriteString(a.WorkspaceName)
_, _ = appURL.WriteString("--") _, _ = appURL.WriteString("--")
_, _ = appURL.WriteString(a.Username) _, _ = 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 // ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If
+10
View File
@@ -42,6 +42,16 @@ func TestApplicationURLString(t *testing.T) {
}, },
Expected: "8080--agent--workspace--user", 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 { for _, c := range testCases {
+1 -1
View File
@@ -1366,7 +1366,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
}, testutil.WaitLong, testutil.IntervalFast, "stats not reported") }, testutil.WaitLong, testutil.IntervalFast, "stats not reported")
assert.Equal(t, workspaceapps.AccessMethodPath, stats[0].AccessMethod) 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) assert.Equal(t, 1, stats[0].Requests)
}) })
} }
+4 -4
View File
@@ -32,10 +32,10 @@ import (
const ( const (
proxyTestAgentName = "agent-name" proxyTestAgentName = "agent-name"
proxyTestAppNameFake = "test-app-fake" proxyTestAppNameFake = "taf"
proxyTestAppNameOwner = "test-app-owner" proxyTestAppNameOwner = "tao"
proxyTestAppNameAuthenticated = "test-app-authenticated" proxyTestAppNameAuthenticated = "taa"
proxyTestAppNamePublic = "test-app-public" proxyTestAppNamePublic = "tap"
proxyTestAppQuery = "query=true" proxyTestAppQuery = "query=true"
proxyTestAppBody = "hello world from apps test" proxyTestAppBody = "hello world from apps test"