From a735aacf7e7ead93aaa2e9fa9b66bcf8dd814185 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Mon, 8 Apr 2024 18:14:18 +0000 Subject: [PATCH 1/7] feat: link with protocol on shared ports --- site/src/modules/resources/PortForwardButton.tsx | 1 + site/src/utils/portForward.ts | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/site/src/modules/resources/PortForwardButton.tsx b/site/src/modules/resources/PortForwardButton.tsx index f9e1ccfff1afa..e445f99ea1ca3 100644 --- a/site/src/modules/resources/PortForwardButton.tsx +++ b/site/src/modules/resources/PortForwardButton.tsx @@ -393,6 +393,7 @@ export const PortForwardPopoverView: FC = ({ agent.name, workspaceName, username, + share.protocol === "https", ); const label = share.port; return ( diff --git a/site/src/utils/portForward.ts b/site/src/utils/portForward.ts index 6d2dc4cbefeb7..5614100599923 100644 --- a/site/src/utils/portForward.ts +++ b/site/src/utils/portForward.ts @@ -4,12 +4,13 @@ export const portForwardURL = ( agentName: string, workspaceName: string, username: string, + https = false, ): string => { const { location } = window; + const suffix = https ? "s" : ""; - const subdomain = `${ - isNaN(port) ? 3000 : port - }--${agentName}--${workspaceName}--${username}`; + const subdomain = `${port + }${suffix}--${agentName}--${workspaceName}--${username}`; return `${location.protocol}//${host}`.replace("*", subdomain); }; @@ -53,6 +54,7 @@ export const openMaybePortForwardedURL = ( portForwardURL( proxyHost, parseInt(url.port), + agentName, workspaceName, username, From 885bc329cb16caba623be4e6c88e5ad6cb64b55a Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Mon, 8 Apr 2024 18:16:31 +0000 Subject: [PATCH 2/7] make fmt --- site/src/utils/portForward.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/site/src/utils/portForward.ts b/site/src/utils/portForward.ts index 5614100599923..bd666823b2c23 100644 --- a/site/src/utils/portForward.ts +++ b/site/src/utils/portForward.ts @@ -9,8 +9,7 @@ export const portForwardURL = ( const { location } = window; const suffix = https ? "s" : ""; - const subdomain = `${port - }${suffix}--${agentName}--${workspaceName}--${username}`; + const subdomain = `${port}${suffix}--${agentName}--${workspaceName}--${username}`; return `${location.protocol}//${host}`.replace("*", subdomain); }; @@ -54,7 +53,6 @@ export const openMaybePortForwardedURL = ( portForwardURL( proxyHost, parseInt(url.port), - agentName, workspaceName, username, From eb430fac08a82fb270d99c77b1cd8d2de7dc6ffa Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Tue, 9 Apr 2024 16:18:17 +0000 Subject: [PATCH 3/7] remove port share authority on protocol --- coderd/workspaceapps/request.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/coderd/workspaceapps/request.go b/coderd/workspaceapps/request.go index d0fba4256cf03..4f6a6f3a64e65 100644 --- a/coderd/workspaceapps/request.go +++ b/coderd/workspaceapps/request.go @@ -350,10 +350,6 @@ func (r Request) getDatabase(ctx context.Context, db database.Store) (*databaseR } // No port share found, so we keep default to owner. } else { - if ps.Protocol == database.PortShareProtocolHttps { - // Apply HTTPS protocol if specified. - appURL = fmt.Sprintf("https://127.0.0.1:%d", portUint) - } appSharingLevel = ps.ShareLevel } } else { From aa3ef4ea6ee343c9b32157ead8a46a2f765027df Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Tue, 9 Apr 2024 16:53:59 +0000 Subject: [PATCH 4/7] fix some tests --- coderd/workspaceapps/apptest/apptest.go | 3 ++- coderd/workspaceapps/apptest/setup.go | 9 +++++++++ coderd/workspaceapps/appurl/appurl.go | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/coderd/workspaceapps/apptest/apptest.go b/coderd/workspaceapps/apptest/apptest.go index 2e91953d6709a..5ba60fbb58687 100644 --- a/coderd/workspaceapps/apptest/apptest.go +++ b/coderd/workspaceapps/apptest/apptest.go @@ -1165,6 +1165,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) { appDetails := setupProxyTest(t, &DeploymentOptions{ ServeHTTPS: true, }) + // using the fact that Apps.Port and Apps.PortHTTPS are the same port here port, err := strconv.ParseInt(appDetails.Apps.Port.AppSlugOrPort, 10, 32) require.NoError(t, err) _, err = appDetails.SDKClient.UpsertWorkspaceAgentPortShare(ctx, appDetails.Workspace.ID, codersdk.UpsertWorkspaceAgentPortShareRequest{ @@ -1178,7 +1179,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) { publicAppClient := appDetails.AppClient(t) publicAppClient.SetSessionToken("") - resp, err := requestWithRetries(ctx, t, publicAppClient, http.MethodGet, appDetails.SubdomainAppURL(appDetails.Apps.Port).String(), nil) + resp, err := requestWithRetries(ctx, t, publicAppClient, http.MethodGet, appDetails.SubdomainAppURL(appDetails.Apps.PortHTTPS).String(), nil) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) diff --git a/coderd/workspaceapps/apptest/setup.go b/coderd/workspaceapps/apptest/setup.go index 702789e4cf76f..fee00173d091f 100644 --- a/coderd/workspaceapps/apptest/setup.go +++ b/coderd/workspaceapps/apptest/setup.go @@ -91,6 +91,7 @@ type App struct { // URL with an agent name. AgentName string AppSlugOrPort string + HTTPS bool // Prefix should have ---. Prefix string @@ -116,6 +117,7 @@ type Details struct { Authenticated App Public App Port App + PortHTTPS App } } @@ -154,6 +156,7 @@ func (d *Details) SubdomainAppURL(app App) *url.URL { AgentName: app.AgentName, WorkspaceName: app.WorkspaceName, Username: app.Username, + HTTPS: app.HTTPS, } u := *d.PathAppBaseURL u.Host = strings.Replace(d.Options.AppHost, "*", appHost.String(), 1) @@ -247,6 +250,12 @@ func setupProxyTestWithFactory(t *testing.T, factory DeploymentFactory, opts *De AgentName: agnt.Name, AppSlugOrPort: strconv.Itoa(int(opts.port)), } + details.Apps.PortHTTPS = App{ + Username: me.Username, + WorkspaceName: workspace.Name, + AgentName: agnt.Name, + AppSlugOrPort: strconv.Itoa(int(opts.port)) + "s", + } return details } diff --git a/coderd/workspaceapps/appurl/appurl.go b/coderd/workspaceapps/appurl/appurl.go index 4daa05a7e3664..c518ab4ab4e0b 100644 --- a/coderd/workspaceapps/appurl/appurl.go +++ b/coderd/workspaceapps/appurl/appurl.go @@ -58,6 +58,7 @@ type ApplicationURL struct { AgentName string WorkspaceName string Username string + HTTPS bool } // String returns the application URL hostname without scheme. You will likely @@ -66,6 +67,9 @@ func (a ApplicationURL) String() string { var appURL strings.Builder _, _ = appURL.WriteString(a.Prefix) _, _ = appURL.WriteString(a.AppSlugOrPort) + if a.HTTPS { + _, _ = appURL.WriteString("s") + } _, _ = appURL.WriteString("--") _, _ = appURL.WriteString(a.AgentName) _, _ = appURL.WriteString("--") @@ -90,9 +94,10 @@ func (a ApplicationURL) Path() string { // // Subdomains should be in the form: // -// ({PREFIX}---)?{PORT/APP_SLUG}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} +// ({PREFIX}---)?{PORT{s?}/APP_SLUG}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} // e.g. // https://8080--main--dev--dean.hi.c8s.io +// https://8080s--main--dev--dean.hi.c8s.io // https://app--main--dev--dean.hi.c8s.io // https://prefix---8080--main--dev--dean.hi.c8s.io // https://prefix---app--main--dev--dean.hi.c8s.io From 8d5999c1bc51b2098a204232d1c6447f344eb7bb Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Tue, 9 Apr 2024 17:42:57 +0000 Subject: [PATCH 5/7] simplify code --- coderd/workspaceapps/apptest/setup.go | 1 - coderd/workspaceapps/appurl/appurl.go | 4 ---- 2 files changed, 5 deletions(-) diff --git a/coderd/workspaceapps/apptest/setup.go b/coderd/workspaceapps/apptest/setup.go index fee00173d091f..78a85794a9427 100644 --- a/coderd/workspaceapps/apptest/setup.go +++ b/coderd/workspaceapps/apptest/setup.go @@ -156,7 +156,6 @@ func (d *Details) SubdomainAppURL(app App) *url.URL { AgentName: app.AgentName, WorkspaceName: app.WorkspaceName, Username: app.Username, - HTTPS: app.HTTPS, } u := *d.PathAppBaseURL u.Host = strings.Replace(d.Options.AppHost, "*", appHost.String(), 1) diff --git a/coderd/workspaceapps/appurl/appurl.go b/coderd/workspaceapps/appurl/appurl.go index c518ab4ab4e0b..182ceac2a4c1a 100644 --- a/coderd/workspaceapps/appurl/appurl.go +++ b/coderd/workspaceapps/appurl/appurl.go @@ -58,7 +58,6 @@ type ApplicationURL struct { AgentName string WorkspaceName string Username string - HTTPS bool } // String returns the application URL hostname without scheme. You will likely @@ -67,9 +66,6 @@ func (a ApplicationURL) String() string { var appURL strings.Builder _, _ = appURL.WriteString(a.Prefix) _, _ = appURL.WriteString(a.AppSlugOrPort) - if a.HTTPS { - _, _ = appURL.WriteString("s") - } _, _ = appURL.WriteString("--") _, _ = appURL.WriteString(a.AgentName) _, _ = appURL.WriteString("--") From 9ccfe70749a589e7b84c9141913b81a8de84009d Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Tue, 9 Apr 2024 17:45:14 +0000 Subject: [PATCH 6/7] fix comment --- coderd/workspaceapps/appurl/appurl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/workspaceapps/appurl/appurl.go b/coderd/workspaceapps/appurl/appurl.go index 182ceac2a4c1a..8b8cfd74d36bd 100644 --- a/coderd/workspaceapps/appurl/appurl.go +++ b/coderd/workspaceapps/appurl/appurl.go @@ -93,7 +93,7 @@ func (a ApplicationURL) Path() string { // ({PREFIX}---)?{PORT{s?}/APP_SLUG}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} // e.g. // https://8080--main--dev--dean.hi.c8s.io -// https://8080s--main--dev--dean.hi.c8s.io +// https://8080s--main--dev--dean.hi.c8s.io // https://app--main--dev--dean.hi.c8s.io // https://prefix---8080--main--dev--dean.hi.c8s.io // https://prefix---app--main--dev--dean.hi.c8s.io From 2e9b9bd241241a41576fee4f70786735c1701279 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse Date: Wed, 10 Apr 2024 13:19:43 +0000 Subject: [PATCH 7/7] remove unused field --- coderd/workspaceapps/apptest/setup.go | 1 - 1 file changed, 1 deletion(-) diff --git a/coderd/workspaceapps/apptest/setup.go b/coderd/workspaceapps/apptest/setup.go index 78a85794a9427..c27032c192b91 100644 --- a/coderd/workspaceapps/apptest/setup.go +++ b/coderd/workspaceapps/apptest/setup.go @@ -91,7 +91,6 @@ type App struct { // URL with an agent name. AgentName string AppSlugOrPort string - HTTPS bool // Prefix should have ---. Prefix string