fix: Protect codersdk.Client SessionToken so it can be updated (#4965)

This feature is used by the coder agent to exchange a new token. By
protecting the SessionToken via mutex we ensure there are no data races
when accessing it.
This commit is contained in:
Mathias Fredriksson
2022-11-09 15:31:24 +02:00
committed by GitHub
parent 8cadb33396
commit 26ab0d37c1
25 changed files with 82 additions and 64 deletions
+3 -3
View File
@@ -97,7 +97,7 @@ func workspaceAgent() *cobra.Command {
if err != nil {
return xerrors.Errorf("CODER_AGENT_TOKEN must be set for token auth: %w", err)
}
client.SessionToken = token
client.SetSessionToken(token)
case "google-instance-identity":
// This is *only* done for testing to mock client authentication.
// This will never be set in a production scenario.
@@ -153,13 +153,13 @@ func workspaceAgent() *cobra.Command {
Logger: logger,
ExchangeToken: func(ctx context.Context) (string, error) {
if exchangeToken == nil {
return client.SessionToken, nil
return client.SessionToken(), nil
}
resp, err := exchangeToken(ctx)
if err != nil {
return "", err
}
client.SessionToken = resp.SessionToken
client.SetSessionToken(resp.SessionToken)
return resp.SessionToken, nil
},
EnvironmentVariables: map[string]string{
+1 -1
View File
@@ -43,7 +43,7 @@ func NewWithSubcommands(
// SetupConfig applies the URL and SessionToken of the client to the config.
func SetupConfig(t *testing.T, client *codersdk.Client, root config.Root) {
err := root.Session().Write(client.SessionToken)
err := root.Session().Write(client.SessionToken())
require.NoError(t, err)
err = root.URL().Write(client.URL.String())
require.NoError(t, err)
+1 -1
View File
@@ -105,7 +105,7 @@ func TestConfigSSH(t *testing.T) {
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = authToken
agentClient.SetSessionToken(authToken)
agentCloser := agent.New(agent.Options{
Client: agentClient,
Logger: slogtest.Make(t, nil).Named("agent"),
+2 -2
View File
@@ -214,7 +214,7 @@ func login() *cobra.Command {
Text: "Paste your token here:",
Secret: true,
Validate: func(token string) error {
client.SessionToken = token
client.SetSessionToken(token)
_, err := client.User(cmd.Context(), codersdk.Me)
if err != nil {
return xerrors.New("That's not a valid token!")
@@ -228,7 +228,7 @@ func login() *cobra.Command {
}
// Login to get user data - verify it is OK before persisting
client.SessionToken = sessionToken
client.SetSessionToken(sessionToken)
resp, err := client.User(cmd.Context(), codersdk.Me)
if err != nil {
return xerrors.Errorf("get user: %w", err)
+3 -3
View File
@@ -148,7 +148,7 @@ func TestLogin(t *testing.T) {
}()
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken)
pty.WriteLine(client.SessionToken())
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
@@ -183,11 +183,11 @@ func TestLogin(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken)
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken())
err := root.Execute()
require.NoError(t, err)
sessionFile, err := cfg.Session().Read()
require.NoError(t, err)
require.Equal(t, client.SessionToken, sessionFile)
require.Equal(t, client.SessionToken(), sessionFile)
})
}
+1 -1
View File
@@ -209,7 +209,7 @@ func login(t *testing.T, pty *ptytest.PTY) config.Root {
}()
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken)
pty.WriteLine(client.SessionToken())
pty.ExpectMatch("Welcome to Coder")
<-doneChan
+2 -2
View File
@@ -306,7 +306,7 @@ func CreateClient(cmd *cobra.Command) (*codersdk.Client, error) {
if err != nil {
return nil, err
}
client.SessionToken = token
client.SetSessionToken(token)
return client, nil
}
@@ -347,7 +347,7 @@ func createAgentClient(cmd *cobra.Command) (*codersdk.Client, error) {
return nil, err
}
client := codersdk.New(serverURL)
client.SessionToken = token
client.SetSessionToken(token)
return client, nil
}
+1 -1
View File
@@ -22,7 +22,7 @@ func TestSpeedtest(t *testing.T) {
}
client, workspace, agentToken := setupWorkspaceForAgent(t)
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentClient.SetSessionToken(agentToken)
agentCloser := agent.New(agent.Options{
Client: agentClient,
Logger: slogtest.Make(t, nil).Named("agent"),
+3 -3
View File
@@ -87,7 +87,7 @@ func TestSSH(t *testing.T) {
pty.ExpectMatch("Waiting")
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentClient.SetSessionToken(agentToken)
agentCloser := agent.New(agent.Options{
Client: agentClient,
Logger: slogtest.Make(t, nil).Named("agent"),
@@ -107,7 +107,7 @@ func TestSSH(t *testing.T) {
// Run this async so the SSH command has to wait for
// the build and agent to connect!
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentClient.SetSessionToken(agentToken)
agentCloser := agent.New(agent.Options{
Client: agentClient,
Logger: slogtest.Make(t, nil).Named("agent"),
@@ -174,7 +174,7 @@ func TestSSH(t *testing.T) {
client, workspace, agentToken := setupWorkspaceForAgent(t)
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentClient.SetSessionToken(agentToken)
agentCloser := agent.New(agent.Options{
Client: agentClient,
Logger: slogtest.Make(t, nil).Named("agent"),