Skip to content

Commit 548d1fe

Browse files
committed
update NeedKms logic
1 parent 12a1530 commit 548d1fe

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

mongo/client_encryption.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ func (ce *ClientEncryption) CreateDataKey(
188188
}
189189

190190
// create data key document
191+
fmt.Println("CreateDataKey")
191192
dataKeyDoc, err := ce.crypt.CreateDataKey(ctx, kmsProvider, co)
192193
if err != nil {
193194
return bson.Binary{}, err
194195
}
195196

196197
// insert key into key vault
198+
fmt.Println("InsertOne")
197199
_, err = ce.keyVaultColl.InsertOne(ctx, dataKeyDoc)
198200
if err != nil {
199201
return bson.Binary{}, err

x/mongo/driver/crypt.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ func (c *crypt) retrieveKeys(ctx context.Context, cryptCtx *mongocrypt.Context)
341341
}
342342

343343
func (c *crypt) decryptKeys(cryptCtx *mongocrypt.Context) error {
344+
c.mongoCrypt.EnableRetry()
344345
for {
345346
kmsCtx := cryptCtx.NextKmsContext()
346347
if kmsCtx == nil {
@@ -400,6 +401,11 @@ func (c *crypt) decryptKey(kmsCtx *mongocrypt.KmsContext) error {
400401
res := make([]byte, bytesNeeded)
401402
bytesRead, err := conn.Read(res)
402403
if err != nil && !errors.Is(err, io.EOF) {
404+
fail := kmsCtx.Fail()
405+
fmt.Println("conn read", err, fail)
406+
if fail {
407+
continue
408+
}
403409
return err
404410
}
405411

x/mongo/driver/mongocrypt/mongocrypt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,8 @@ func (m *MongoCrypt) GetKmsProviders(ctx context.Context) (bsoncore.Document, er
522522
}
523523
return builder.Build(), nil
524524
}
525+
526+
// EnableRetry enables retry.
527+
func (m *MongoCrypt) EnableRetry() {
528+
_ = C.mongocrypt_setopt_retry_kms(m.wrapped, true)
529+
}

x/mongo/driver/mongocrypt/mongocrypt_kms_context.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package mongocrypt
1111

1212
// #include <mongocrypt.h>
1313
import "C"
14+
import "time"
1415

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

4243
// Message returns the message to send to the KMS.
4344
func (kc *KmsContext) Message() ([]byte, error) {
45+
time.Sleep(C.mongocrypt_kms_ctx_usleep(kc.wrapped) * time.Microsecond)
46+
4447
msgBinary := newBinary()
4548
defer msgBinary.close()
4649

@@ -74,3 +77,8 @@ func (kc *KmsContext) createErrorFromStatus() error {
7477
C.mongocrypt_kms_ctx_status(kc.wrapped, status)
7578
return errorFromStatus(status)
7679
}
80+
81+
// Fail returns a boolean indicating whether the failed request may be retried.
82+
func (kc *KmsContext) Fail() bool {
83+
return C.mongocrypt_kms_ctx_fail(kc.wrapped)
84+
}

x/mongo/driver/mongocrypt/mongocrypt_kms_context_not_enabled.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ func (kc *KmsContext) BytesNeeded() int32 {
3737
func (kc *KmsContext) FeedResponse([]byte) error {
3838
panic(cseNotSupportedMsg)
3939
}
40+
41+
// Fail returns a boolean indicating whether the failed request may be retried.
42+
func (kc *KmsContext) Fail() bool {
43+
panic(cseNotSupportedMsg)
44+
}

x/mongo/driver/mongocrypt/mongocrypt_not_enabled.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ func (m *MongoCrypt) Close() {
9595
func (m *MongoCrypt) GetKmsProviders(context.Context) (bsoncore.Document, error) {
9696
panic(cseNotSupportedMsg)
9797
}
98+
99+
// EnableRetry enables retry.
100+
func (m *MongoCrypt) EnableRetry() {
101+
panic(cseNotSupportedMsg)
102+
}

0 commit comments

Comments
 (0)