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: concurrency issue in getDerivedPrimitive #2480

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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: 8 additions & 11 deletions internal/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"strings"
"sync"

"github.com/alecthomas/types/optional"
awsv1kms "github.com/aws/aws-sdk-go/service/kms"
Expand All @@ -17,6 +16,8 @@ import (
"github.com/tink-crypto/tink-go/v2/prf"
"github.com/tink-crypto/tink-go/v2/testing/fakekms"
"github.com/tink-crypto/tink-go/v2/tink"

"github.com/TBD54566975/ftl/internal/mutex"
)

// Encrypted is an interface for values that contain encrypted data.
Expand Down Expand Up @@ -100,8 +101,7 @@ type KMSEncryptor struct {
root keyset.Handle
kekAEAD tink.AEAD
encryptedKeyset []byte
cachedDerivedMu sync.RWMutex
cachedDerived map[SubKey]tink.AEAD
cachedDerived *mutex.Mutex[map[SubKey]tink.AEAD]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I def read this tink.AEAD as "think ahead"

}

var _ DataEncryptor = &KMSEncryptor{}
Expand Down Expand Up @@ -185,7 +185,7 @@ func NewKMSEncryptorWithKMS(uri string, v1client *awsv1kms.KMS, encryptedKeyset
root: *handle,
kekAEAD: kekAEAD,
encryptedKeyset: encryptedKeyset,
cachedDerived: make(map[SubKey]tink.AEAD),
cachedDerived: mutex.New(map[SubKey]tink.AEAD{}),
}, nil
}

Expand All @@ -208,9 +208,9 @@ func deriveKeyset(root keyset.Handle, salt []byte) (*keyset.Handle, error) {
}

func (k *KMSEncryptor) getDerivedPrimitive(subKey SubKey) (tink.AEAD, error) {
k.cachedDerivedMu.RLock()
primitive, ok := k.cachedDerived[subKey]
k.cachedDerivedMu.RUnlock()
cachedDerived := k.cachedDerived.Lock()
defer k.cachedDerived.Unlock()
primitive, ok := cachedDerived[subKey]
if ok {
return primitive, nil
}
Expand All @@ -225,10 +225,7 @@ func (k *KMSEncryptor) getDerivedPrimitive(subKey SubKey) (tink.AEAD, error) {
return nil, fmt.Errorf("failed to create primitive: %w", err)
}

k.cachedDerivedMu.Lock()
k.cachedDerived[subKey] = primitive
k.cachedDerivedMu.Unlock()

cachedDerived[subKey] = primitive
return primitive, nil
}

Expand Down
33 changes: 33 additions & 0 deletions internal/mutex/mutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mutex

import "sync"

// Mutex is a simple mutex that can be used to protect a value.
//
// The zero value is safe to use if the zero value of T is safe to use.
//
// Example:
//
// var m mutex.Mutex[*string]
// s := m.Lock()
// defer m.Unlock()
// *s = "hello"
type Mutex[T any] struct {
Comment on lines +11 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, this seems handy! I was going to ask why no use xsync, but this is more generic it seems.

m sync.Mutex
v T
}

func New[T any](v T) *Mutex[T] {
return &Mutex[T]{v: v}
}

// Lock the Mutex and return its protected value.
func (l *Mutex[T]) Lock() T {
l.m.Lock()
return l.v
}

// Unlock the Mutex. The value returned by Lock is no longer valid.
func (l *Mutex[T]) Unlock() {
l.m.Unlock()
}
Loading