forked from coreos/go-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
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 coreos#17
- Loading branch information
1 parent
562ae81
commit 58dee99
Showing
4 changed files
with
122 additions
and
47 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,61 @@ | ||
// 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) | ||
} | ||
|
||
// If 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 | ||
|
||
cliCfg := ClientConfig{ | ||
HTTPClient: http.DefaultClient, | ||
ProviderConfig: ProviderConfig{Issuer: s.URL}, | ||
} | ||
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{}{} | ||
}() | ||
|
||
// Iterate serveral times to increase the likelyhood of triggering the race detector. | ||
for i := 0; i < 100; 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