Skip to content

Commit

Permalink
refactor(jwk): aquire read lock unless generating
Browse files Browse the repository at this point in the history
  • Loading branch information
awill1988 committed Oct 28, 2024
1 parent 9cc5f28 commit c0f8901
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ func EnsureAsymmetricKeypairExists(ctx context.Context, r InternalRegistry, alg,
}

func GetOrGenerateKeys(ctx context.Context, r InternalRegistry, m Manager, set, kid, alg string) (private *jose.JSONWebKey, err error) {
getLock(set).Lock()
defer getLock(set).Unlock()

getLock(set).RLock()
keys, err := m.GetKeySet(ctx, set)
getLock(set).RUnlock()

if errors.Is(err, x.ErrNotFound) || keys != nil && len(keys.Keys) == 0 {
r.Logger().Warnf("JSON Web Key Set \"%s\" does not exist yet, generating new key pair...", set)

getLock(set).Lock()
defer getLock(set).Unlock()

keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
if err != nil {
return nil, err
Expand All @@ -64,6 +68,9 @@ func GetOrGenerateKeys(ctx context.Context, r InternalRegistry, m Manager, set,
} else {
r.Logger().WithField("jwks", set).Warnf("JSON Web Key not found in JSON Web Key Set %s, generating new key pair...", set)

getLock(set).Lock()
defer getLock(set).Unlock()

keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
if err != nil {
return nil, err
Expand Down

0 comments on commit c0f8901

Please sign in to comment.