Skip to content

Commit

Permalink
update NeedKms logic
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Nov 20, 2024
1 parent 12a1530 commit 6ecb7c2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion etc/install-libmongocrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This script installs libmongocrypt into an "install" directory.
set -eux

LIBMONGOCRYPT_TAG="1.11.0"
LIBMONGOCRYPT_TAG="1.12.0"

# Install libmongocrypt based on OS.
if [ "Windows_NT" = "${OS:-}" ]; then
Expand Down
2 changes: 2 additions & 0 deletions mongo/client_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ func (ce *ClientEncryption) CreateDataKey(
}

// create data key document
fmt.Println("CreateDataKey")
dataKeyDoc, err := ce.crypt.CreateDataKey(ctx, kmsProvider, co)
if err != nil {
return bson.Binary{}, err
}

// insert key into key vault
fmt.Println("InsertOne")
_, err = ce.keyVaultColl.InsertOne(ctx, dataKeyDoc)
if err != nil {
return bson.Binary{}, err
Expand Down
6 changes: 6 additions & 0 deletions x/mongo/driver/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func (c *crypt) retrieveKeys(ctx context.Context, cryptCtx *mongocrypt.Context)
}

func (c *crypt) decryptKeys(cryptCtx *mongocrypt.Context) error {
c.mongoCrypt.EnableRetry()
for {
kmsCtx := cryptCtx.NextKmsContext()
if kmsCtx == nil {
Expand Down Expand Up @@ -400,6 +401,11 @@ func (c *crypt) decryptKey(kmsCtx *mongocrypt.KmsContext) error {
res := make([]byte, bytesNeeded)
bytesRead, err := conn.Read(res)
if err != nil && !errors.Is(err, io.EOF) {
fail := kmsCtx.Fail()
fmt.Println("conn read", err, fail)
if fail {
continue
}
return err
}

Expand Down
5 changes: 5 additions & 0 deletions x/mongo/driver/mongocrypt/mongocrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,8 @@ func (m *MongoCrypt) GetKmsProviders(ctx context.Context) (bsoncore.Document, er
}
return builder.Build(), nil
}

// EnableRetry enables retry.
func (m *MongoCrypt) EnableRetry() {
_ = C.mongocrypt_setopt_retry_kms(m.wrapped, true)
}
8 changes: 8 additions & 0 deletions x/mongo/driver/mongocrypt/mongocrypt_kms_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package mongocrypt

// #include <mongocrypt.h>
import "C"
import "time"

// KmsContext represents a mongocrypt_kms_ctx_t handle.
type KmsContext struct {
Expand Down Expand Up @@ -41,6 +42,8 @@ func (kc *KmsContext) KMSProvider() string {

// Message returns the message to send to the KMS.
func (kc *KmsContext) Message() ([]byte, error) {
time.Sleep(C.mongocrypt_kms_ctx_usleep(kc.wrapped) * time.Microsecond)

msgBinary := newBinary()
defer msgBinary.close()

Expand Down Expand Up @@ -74,3 +77,8 @@ func (kc *KmsContext) createErrorFromStatus() error {
C.mongocrypt_kms_ctx_status(kc.wrapped, status)
return errorFromStatus(status)
}

// Fail returns a boolean indicating whether the failed request may be retried.
func (kc *KmsContext) Fail() bool {
return C.mongocrypt_kms_ctx_fail(kc.wrapped)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ func (kc *KmsContext) BytesNeeded() int32 {
func (kc *KmsContext) FeedResponse([]byte) error {
panic(cseNotSupportedMsg)
}

// Fail returns a boolean indicating whether the failed request may be retried.
func (kc *KmsContext) Fail() bool {
panic(cseNotSupportedMsg)
}
5 changes: 5 additions & 0 deletions x/mongo/driver/mongocrypt/mongocrypt_not_enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ func (m *MongoCrypt) Close() {
func (m *MongoCrypt) GetKmsProviders(context.Context) (bsoncore.Document, error) {
panic(cseNotSupportedMsg)
}

// EnableRetry enables retry.
func (m *MongoCrypt) EnableRetry() {
panic(cseNotSupportedMsg)
}

0 comments on commit 6ecb7c2

Please sign in to comment.