Skip to content

Commit

Permalink
fix: only lock for key generation
Browse files Browse the repository at this point in the history
Closes #3865
Closes #3863
  • Loading branch information
aeneasr committed Oct 29, 2024
1 parent 9cc5f28 commit 9b0a824
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ 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()

keys, err := m.GetKeySet(ctx, set)
if err == nil && keys != nil && len(keys.Keys) > 0 {
return FindPrivateKey(keys)
}

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()
keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
getLock(set).Unlock()

if err != nil {
return nil, err
}
Expand All @@ -64,7 +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()
keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
getLock(set).Unlock()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9b0a824

Please sign in to comment.