Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only lock for key generation #3866

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ 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 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 @@ -61,20 +61,22 @@ func GetOrGenerateKeys(ctx context.Context, r InternalRegistry, m Manager, set,
privKey, privKeyErr := FindPrivateKey(keys)
if privKeyErr == nil {
return privKey, nil
} else {
r.Logger().WithField("jwks", set).Warnf("JSON Web Key not found in JSON Web Key Set %s, generating new key pair...", set)
}

keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
if err != nil {
return nil, err
}
r.Logger().WithField("jwks", set).Warnf("JSON Web Key not found in JSON Web Key Set %s, generating new key pair...", set)

privKey, err := FindPrivateKey(keys)
if err != nil {
return nil, err
}
return privKey, nil
getLock(set).Lock()
keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
getLock(set).Unlock()
if err != nil {
return nil, err
}

privKey, err = FindPrivateKey(keys)
if err != nil {
return nil, err
}
return privKey, nil
}

func First(keys []jose.JSONWebKey) *jose.JSONWebKey {
Expand Down
Loading