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

refactor: jwk aquire lock when generating #3865

Closed
Closed
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"github.com/pkg/errors"
)

var mapLock sync.RWMutex
var locks = map[string]*sync.RWMutex{}
var mapLock sync.Mutex
var locks = map[string]*sync.Mutex{}

func getLock(set string) *sync.RWMutex {
func getLock(set string) *sync.Mutex {
mapLock.Lock()
defer mapLock.Unlock()
if _, ok := locks[set]; !ok {
locks[set] = new(sync.RWMutex)
locks[set] = new(sync.Mutex)
}
return locks[set]
}
Expand All @@ -44,12 +44,14 @@ 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)

l := getLock(set)
defer l.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you missed it, I think you meant to lock the returned lock then defer unlock. Same thing below on line 69.

At the end of this if block it may make sense to directly return FindPrivateKey(keys). If we don't it opens up the potential for deadlock if FindPrivateKey fails and we try to generate another keyset. Also I don't really think it would help to generate another keyset if the one we just generated didn't have a private key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops!


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

l := getLock(set)
defer l.Lock()

keys, err = m.GenerateAndPersistKeySet(ctx, set, kid, alg, "sig")
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"require_request_uri_registration": true,
"response_modes_supported": [
"query",
"fragment"
"fragment",
"form_post"
],
"response_types_supported": [
"code",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"require_request_uri_registration": true,
"response_modes_supported": [
"query",
"fragment"
"fragment",
"form_post"
],
"response_types_supported": [
"code",
Expand Down
Loading