From d03de31404905ad144737f1568e1dcd4e53d490b Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Tue, 19 May 2020 02:51:27 -0400 Subject: [PATCH] controlclient/direct: fix a race condition accessing auth keys. Signed-off-by: Avery Pennarun --- control/controlclient/auto_test.go | 5 +++++ control/controlclient/direct.go | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/control/controlclient/auto_test.go b/control/controlclient/auto_test.go index 59a9205cb..847336852 100644 --- a/control/controlclient/auto_test.go +++ b/control/controlclient/auto_test.go @@ -74,10 +74,12 @@ func runSub(t *testing.T, name string, fn func(t *testing.T)) { } func fatal(t *testing.T, args ...interface{}) { + t.Helper() t.Fatal("FAILED: ", fmt.Sprint(args...)) } func fatalf(t *testing.T, s string, args ...interface{}) { + t.Helper() t.Fatalf("FAILED: "+s, args...) } @@ -813,7 +815,10 @@ func TestAuthKey(t *testing.T) { c1.Login(nil, 0) c1.waitStatus(t, stateURLVisitRequired) + c1.direct.mu.Lock() c1.direct.authKey = string(key) + c1.direct.mu.Unlock() + c1.Login(nil, 0) c1.waitStatus(t, stateAuthenticated) c1.waitStatus(t, stateSynchronized) diff --git a/control/controlclient/direct.go b/control/controlclient/direct.go index 250cddf1c..7fbf522f3 100644 --- a/control/controlclient/direct.go +++ b/control/controlclient/direct.go @@ -257,6 +257,7 @@ func (c *Direct) doLogin(ctx context.Context, t *oauth2.Token, flags LoginFlags, persist := c.persist tryingNewKey := c.tryingNewKey serverKey := c.serverKey + authKey := c.authKey expired := c.expiry != nil && !c.expiry.IsZero() && c.expiry.Before(c.timeNow()) c.mu.Unlock() @@ -330,7 +331,7 @@ func (c *Direct) doLogin(ctx context.Context, t *oauth2.Token, flags LoginFlags, request.Auth.Oauth2Token = t request.Auth.Provider = persist.Provider request.Auth.LoginName = persist.LoginName - request.Auth.AuthKey = c.authKey + request.Auth.AuthKey = authKey bodyData, err := encode(request, &serverKey, &persist.PrivateMachineKey) if err != nil { return regen, url, err