Skip to content

Commit 9caece8

Browse files
authored
Merge pull request kubernetes#86020 from enj/enj/i/oidc_cache/79546
kubectl oidc auth-provider: include cluster address in cache key
2 parents 6dbd521 + 96fe76a commit 9caece8

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,25 @@ func newClientCache() *clientCache {
7676
}
7777

7878
type cacheKey struct {
79+
clusterAddress string
7980
// Canonical issuer URL string of the provider.
8081
issuerURL string
8182
clientID string
8283
}
8384

84-
func (c *clientCache) getClient(issuer, clientID string) (*oidcAuthProvider, bool) {
85+
func (c *clientCache) getClient(clusterAddress, issuer, clientID string) (*oidcAuthProvider, bool) {
8586
c.mu.RLock()
8687
defer c.mu.RUnlock()
87-
client, ok := c.cache[cacheKey{issuer, clientID}]
88+
client, ok := c.cache[cacheKey{clusterAddress: clusterAddress, issuerURL: issuer, clientID: clientID}]
8889
return client, ok
8990
}
9091

9192
// setClient attempts to put the client in the cache but may return any clients
9293
// with the same keys set before. This is so there's only ever one client for a provider.
93-
func (c *clientCache) setClient(issuer, clientID string, client *oidcAuthProvider) *oidcAuthProvider {
94+
func (c *clientCache) setClient(clusterAddress, issuer, clientID string, client *oidcAuthProvider) *oidcAuthProvider {
9495
c.mu.Lock()
9596
defer c.mu.Unlock()
96-
key := cacheKey{issuer, clientID}
97+
key := cacheKey{clusterAddress: clusterAddress, issuerURL: issuer, clientID: clientID}
9798

9899
// If another client has already initialized a client for the given provider we want
99100
// to use that client instead of the one we're trying to set. This is so all transports
@@ -107,7 +108,7 @@ func (c *clientCache) setClient(issuer, clientID string, client *oidcAuthProvide
107108
return client
108109
}
109110

110-
func newOIDCAuthProvider(_ string, cfg map[string]string, persister restclient.AuthProviderConfigPersister) (restclient.AuthProvider, error) {
111+
func newOIDCAuthProvider(clusterAddress string, cfg map[string]string, persister restclient.AuthProviderConfigPersister) (restclient.AuthProvider, error) {
111112
issuer := cfg[cfgIssuerUrl]
112113
if issuer == "" {
113114
return nil, fmt.Errorf("Must provide %s", cfgIssuerUrl)
@@ -119,7 +120,7 @@ func newOIDCAuthProvider(_ string, cfg map[string]string, persister restclient.A
119120
}
120121

121122
// Check cache for existing provider.
122-
if provider, ok := cache.getClient(issuer, clientID); ok {
123+
if provider, ok := cache.getClient(clusterAddress, issuer, clientID); ok {
123124
return provider, nil
124125
}
125126

@@ -157,7 +158,7 @@ func newOIDCAuthProvider(_ string, cfg map[string]string, persister restclient.A
157158
persister: persister,
158159
}
159160

160-
return cache.setClient(issuer, clientID, provider), nil
161+
return cache.setClient(clusterAddress, issuer, clientID, provider), nil
161162
}
162163

163164
type oidcAuthProvider struct {

staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,40 @@ func TestExpired(t *testing.T) {
119119
func TestClientCache(t *testing.T) {
120120
cache := newClientCache()
121121

122-
if _, ok := cache.getClient("issuer1", "id1"); ok {
122+
if _, ok := cache.getClient("cluster1", "issuer1", "id1"); ok {
123123
t.Fatalf("got client before putting one in the cache")
124124
}
125+
assertCacheLen(t, cache, 0)
125126

126127
cli1 := new(oidcAuthProvider)
127128
cli2 := new(oidcAuthProvider)
129+
cli3 := new(oidcAuthProvider)
128130

129-
gotcli := cache.setClient("issuer1", "id1", cli1)
131+
gotcli := cache.setClient("cluster1", "issuer1", "id1", cli1)
130132
if cli1 != gotcli {
131133
t.Fatalf("set first client and got a different one")
132134
}
135+
assertCacheLen(t, cache, 1)
133136

134-
gotcli = cache.setClient("issuer1", "id1", cli2)
137+
gotcli = cache.setClient("cluster1", "issuer1", "id1", cli2)
135138
if cli1 != gotcli {
136139
t.Fatalf("set a second client and didn't get the first")
137140
}
141+
assertCacheLen(t, cache, 1)
142+
143+
gotcli = cache.setClient("cluster2", "issuer1", "id1", cli3)
144+
if cli1 == gotcli {
145+
t.Fatalf("set a third client and got the first")
146+
}
147+
if cli3 != gotcli {
148+
t.Fatalf("set third client and got a different one")
149+
}
150+
assertCacheLen(t, cache, 2)
151+
}
152+
153+
func assertCacheLen(t *testing.T, cache *clientCache, length int) {
154+
t.Helper()
155+
if len(cache.cache) != length {
156+
t.Errorf("expected cache length %d got %d", length, len(cache.cache))
157+
}
138158
}

0 commit comments

Comments
 (0)