Skip to content

Commit 674f60f

Browse files
authored
fix(cli): replace $SESSION_TOKEN placeholder for external apps (#17048)
Fixes an oversight in #17032 The FE has logic to replace the string `$SESSION_TOKEN` with a newly-minted session token. This adds corresponding logic to the `coder open app` command.
1 parent 77fe10e commit 674f60f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cli/open.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ func (r *RootCmd) openApp() *serpent.Command {
301301
pathAppURL := strings.TrimPrefix(region.PathAppURL, baseURL.String())
302302
appURL := buildAppLinkURL(baseURL, ws, agt, foundApp, region.WildcardHostname, pathAppURL)
303303

304+
if foundApp.External {
305+
appURL = replacePlaceholderExternalSessionTokenString(client, appURL)
306+
}
307+
304308
// Check if we're inside a workspace. Generally, we know
305309
// that if we're inside a workspace, `open` can't be used.
306310
insideAWorkspace := inv.Environ.Get("CODER") == "true"
@@ -314,7 +318,7 @@ func (r *RootCmd) openApp() *serpent.Command {
314318
if !testOpenError {
315319
err = open.Run(appURL)
316320
} else {
317-
err = xerrors.New("test.open-error")
321+
err = xerrors.New("test.open-error: " + appURL)
318322
}
319323
return err
320324
},
@@ -511,3 +515,15 @@ func buildAppLinkURL(baseURL *url.URL, workspace codersdk.Workspace, agent coder
511515
}
512516
return u.String()
513517
}
518+
519+
// replacePlaceholderExternalSessionTokenString replaces any $SESSION_TOKEN
520+
// strings in the URL with the actual session token.
521+
// This is consistent behavior with the frontend. See: site/src/modules/resources/AppLink/AppLink.tsx
522+
func replacePlaceholderExternalSessionTokenString(client *codersdk.Client, appURL string) string {
523+
if !strings.Contains(appURL, "$SESSION_TOKEN") {
524+
return appURL
525+
}
526+
527+
// We will just re-use the existing session token we're already using.
528+
return strings.ReplaceAll(appURL, "$SESSION_TOKEN", client.SessionToken())
529+
}

cli/open_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,29 @@ func TestOpenApp(t *testing.T) {
381381
w.RequireError()
382382
w.RequireContains("region not found")
383383
})
384+
385+
t.Run("ExternalAppSessionToken", func(t *testing.T) {
386+
t.Parallel()
387+
388+
client, ws, _ := setupWorkspaceForAgent(t, func(agents []*proto.Agent) []*proto.Agent {
389+
agents[0].Apps = []*proto.App{
390+
{
391+
Slug: "app1",
392+
Url: "https://example.com/app1?token=$SESSION_TOKEN",
393+
External: true,
394+
},
395+
}
396+
return agents
397+
})
398+
inv, root := clitest.New(t, "open", "app", ws.Name, "app1", "--test.open-error")
399+
clitest.SetupConfig(t, client, root)
400+
pty := ptytest.New(t)
401+
inv.Stdin = pty.Input()
402+
inv.Stdout = pty.Output()
403+
404+
w := clitest.StartWithWaiter(t, inv)
405+
w.RequireError()
406+
w.RequireContains("test.open-error")
407+
w.RequireContains(client.SessionToken())
408+
})
384409
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy