-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oidc: fix race condition in provider config syncing
Add a RWMutex around Client's providerConfig field. Syncing the provider config writes to the field, while numerous other actions read from it. Fixes #17
- Loading branch information
1 parent
562ae81
commit f086c9a
Showing
5 changed files
with
136 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// This file contains tests which depend on the race detector being enabled. | ||
// +build race | ||
|
||
package oidc | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type testProvider struct { | ||
baseURL string | ||
} | ||
|
||
func (p *testProvider) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path != discoveryConfigPath { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
cfg := ProviderConfig{ | ||
Issuer: p.baseURL, | ||
} | ||
json.NewEncoder(w).Encode(&cfg) | ||
} | ||
|
||
// This test fails by triggering the race detector, not by calling t.Error or t.Fatal. | ||
func TestProviderSyncRace(t *testing.T) { | ||
|
||
prov := &testProvider{} | ||
|
||
s := httptest.NewServer(prov) | ||
defer s.Close() | ||
prov.baseURL = s.URL | ||
|
||
prevValue := minimumProviderConfigSyncInterval | ||
defer func() { minimumProviderConfigSyncInterval = prevValue }() | ||
|
||
// Reduce the sync interval to increase the write frequencey. | ||
minimumProviderConfigSyncInterval = 5 * time.Millisecond | ||
|
||
cliCfg := ClientConfig{ | ||
HTTPClient: http.DefaultClient, | ||
ProviderConfig: ProviderConfig{ | ||
Issuer: s.URL, | ||
ExpiresAt: time.Now().Add(time.Minute), // Must expire to trigger frequent syncs. | ||
}, | ||
} | ||
cli, err := NewClient(cliCfg) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
// SyncProviderConfig beings a goroutine which writes to the client's provider config. | ||
c := cli.SyncProviderConfig(s.URL) | ||
defer func() { | ||
// stop the background process | ||
c <- struct{}{} | ||
}() | ||
|
||
for i := 0; i < 10; i++ { | ||
time.Sleep(5 * time.Millisecond) | ||
// Creating an OAuth client reads from the provider config. | ||
cli.OAuthClient() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters