Skip to content

Commit

Permalink
lock public JWKS generation and re-check cache (#27929)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: kpcraig <3031348+kpcraig@users.noreply.github.com>
  • Loading branch information
bhowe34 and kpcraig authored Sep 9, 2024
1 parent cbbe573 commit 5934294
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/27929.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
identity/oidc: prevent JWKS from being generated by multiple concurrent requests
```
14 changes: 14 additions & 0 deletions vault/identity_store_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,20 @@ func (i *IdentityStore) generatePublicJWKS(ctx context.Context, s logical.Storag
return jwksRaw.(*jose.JSONWebKeySet), nil
}

i.generateJWKSLock.Lock()
defer i.generateJWKSLock.Unlock()

// Check the cache again incase another requset acquired the lock
// before this request.
jwksRaw, ok, err = i.oidcCache.Get(ns, "jwks")
if err != nil {
return nil, err
}

if ok {
return jwksRaw.(*jose.JSONWebKeySet), nil
}

if _, err := i.expireOIDCPublicKeys(ctx, s); err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions vault/identity_store_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ type IdentityStore struct {
db *memdb.MemDB

// locks to make sure things are consistent
lock sync.RWMutex
oidcLock sync.RWMutex
lock sync.RWMutex
oidcLock sync.RWMutex
generateJWKSLock sync.Mutex

// groupLock is used to protect modifications to group entries
groupLock sync.RWMutex
Expand Down

0 comments on commit 5934294

Please sign in to comment.