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

MONGOCRYPT-749 Retry rewrapManyDataKey encrypt requests #922

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/mongocrypt-ctx-rewrap-many-datakey.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ static mongocrypt_kms_ctx_t *_next_kms_ctx_encrypt(mongocrypt_ctx_t *ctx) {
mongocrypt_ctx_t *dkctx = NULL;

BSON_ASSERT_PARAM(ctx);
/* Check if any need retry */
{
_mongocrypt_ctx_rmd_datakey_t *it = rmdctx->datakeys;
while (it != NULL) {
_mongocrypt_ctx_datakey_t *dkctx = (_mongocrypt_ctx_datakey_t *)it->dkctx;
if (dkctx->kms.should_retry) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (dkctx->kms.should_retry) {
if (dkctx->kms.should_retry) {
dkctx->kms.should_retry = false; // Reset retry state.

Similar to other contexts (example), reset should_retry before returning.

return &dkctx->kms;
}
it = it->next;
}
}

/* No more datakey contexts requiring KMS. */
if (!rmdctx->datakeys_iter) {
Expand Down
22 changes: 22 additions & 0 deletions test/test-mongocrypt-ctx-rewrap-many-datakey.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,28 @@ static void _test_rewrap_many_datakey_need_kms_retry(_mongocrypt_tester_t *teste
ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS); // To encrypt.
mongocrypt_ctx_destroy(ctx);

/* Clear key cache. */
mongocrypt_destroy(crypt);
crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);

/* Ensure KMS encrypt requests retry for network errors */
ctx = mongocrypt_ctx_new(crypt);
ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS); // To decrypt.
ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-a.txt")), kms);
ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS); // To encrypt.
ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
ASSERT(mongocrypt_kms_ctx_fail(kms)); // Simulate driver-side network failure for an encrypt request.
ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx))); // Assert fails. Expected KMS request to retry but did not.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx))); // Assert fails. Expected KMS request to retry but did not.
ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx))); // Expect KMS request to retry.


Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-a.txt")), kms);
ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);

Suggest testing feeding the retried KMS context. This catches the missing should_retry = false.

mongocrypt_ctx_destroy(ctx);
mongocrypt_destroy(crypt);
}
Expand Down
Loading