Skip to content

Commit 9c4f49f

Browse files
[CDRIVER-4382] Test cases for automatic AWS credentials (#1064)
1 parent ab03aa8 commit 9c4f49f

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/libmongoc/src/mongoc/mongoc-crypt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ _try_add_aws_from_env (bson_t *out, bson_error_t *error)
662662
&& bson_append_document_end (out, &aws);
663663
BSON_ASSERT (okay && "Failed to build aws credentials document");
664664
// Good!
665+
_mongoc_aws_credentials_cleanup (&creds);
665666
return true;
666667
}
667668

src/libmongoc/tests/test-mongoc-client-side-encryption.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
/* _mongoc_host_list_from_string_with_err */
2323
#include "mongoc/mongoc-host-list-private.h"
24+
#include "mongoc/mongoc-cluster-aws-private.h"
2425

2526
/* MONGOC_SERVER_ERR_NS_NOT_FOUND */
2627
#include "mongoc/mongoc-error-private.h"
@@ -4969,6 +4970,101 @@ test_kms_callback (void *unused)
49694970
mongoc_client_destroy (cl);
49704971
}
49714972

4973+
static void
4974+
_test_auto_aws (bool should_succeed)
4975+
{
4976+
// Datakey options for AWS
4977+
mongoc_client_encryption_datakey_opts_t *dk_opts =
4978+
mongoc_client_encryption_datakey_opts_new ();
4979+
mongoc_client_encryption_datakey_opts_set_masterkey (
4980+
dk_opts,
4981+
tmp_bson ("{ 'region': 'us-east-1', 'key': "
4982+
"'arn:aws:kms:us-east-1:579766882180:key/"
4983+
"89fcc2c4-08b0-4bd9-9f25-e30687b580d0' }"));
4984+
4985+
// Create a client encryption object
4986+
mongoc_client_encryption_opts_t *opts = mongoc_client_encryption_opts_new ();
4987+
mongoc_client_t *cl = test_framework_new_default_client ();
4988+
mongoc_client_encryption_opts_set_keyvault_client (opts, cl);
4989+
4990+
// Given it an on-demand 'aws' provider
4991+
bson_t *empty_aws = tmp_bson ("{'aws': {}}");
4992+
mongoc_client_encryption_opts_set_kms_providers (opts, empty_aws);
4993+
mongoc_client_encryption_opts_set_keyvault_namespace (
4994+
opts, "testing", "testing");
4995+
4996+
{
4997+
// Attempting to create a key from 'aws' will require credentials in the
4998+
// environment immediately. Create a client encryption object for it.
4999+
bson_error_t error;
5000+
mongoc_client_encryption_t *enc =
5001+
mongoc_client_encryption_new (opts, &error);
5002+
ASSERT_OR_PRINT (enc, error);
5003+
5004+
bson_value_t keyid;
5005+
mongoc_client_encryption_create_datakey (
5006+
enc, "aws", dk_opts, &keyid, &error);
5007+
mongoc_client_encryption_destroy (enc);
5008+
5009+
if (should_succeed) {
5010+
bson_value_destroy (&keyid);
5011+
ASSERT_OR_PRINT (error.code == 0, error);
5012+
} else {
5013+
// We should encounter an error while attempting to connect to the EC2
5014+
// metadata server.
5015+
ASSERT_ERROR_CONTAINS (error,
5016+
MONGOC_ERROR_CLIENT,
5017+
MONGOC_ERROR_CLIENT_AUTHENTICATE,
5018+
"");
5019+
}
5020+
}
5021+
5022+
mongoc_client_encryption_datakey_opts_destroy (dk_opts);
5023+
mongoc_client_encryption_opts_destroy (opts);
5024+
mongoc_client_destroy (cl);
5025+
}
5026+
5027+
static void
5028+
test_auto_aws_fail (void *unused)
5029+
{
5030+
_test_auto_aws (false);
5031+
}
5032+
5033+
static void
5034+
test_auto_aws_succeed (void *unused)
5035+
{
5036+
_test_auto_aws (true);
5037+
}
5038+
5039+
static int
5040+
_have_aws_creds_env (void *unused)
5041+
{
5042+
// State variable:
5043+
// Zero: Haven't checked yet
5044+
// One: We have AWS creds
5045+
// Two = We do not have AWS creds
5046+
static int creds_check_state = 0;
5047+
if (creds_check_state == 0) {
5048+
// We need to do a check
5049+
_mongoc_aws_credentials_t creds = {0};
5050+
bson_error_t error;
5051+
bool got_creds = _mongoc_aws_credentials_obtain (NULL, &creds, &error);
5052+
_mongoc_aws_credentials_cleanup (&creds);
5053+
if (got_creds) {
5054+
creds_check_state = 1;
5055+
} else {
5056+
creds_check_state = 2;
5057+
}
5058+
}
5059+
return creds_check_state == 1;
5060+
}
5061+
5062+
static int
5063+
_not_have_aws_creds_env (void *unused)
5064+
{
5065+
return !_have_aws_creds_env (unused);
5066+
}
5067+
49725068
void
49735069
test_client_side_encryption_install (TestSuite *suite)
49745070
{
@@ -5226,4 +5322,22 @@ test_client_side_encryption_install (TestSuite *suite)
52265322
NULL, // ctx
52275323
test_framework_skip_if_no_client_side_encryption,
52285324
test_framework_skip_if_max_wire_version_less_than_8);
5325+
5326+
TestSuite_AddFull (suite,
5327+
"/client_side_encryption/kms/auto-aws/fail",
5328+
test_auto_aws_fail,
5329+
NULL,
5330+
NULL,
5331+
test_framework_skip_if_no_client_side_encryption,
5332+
test_framework_skip_if_max_wire_version_less_than_8,
5333+
_not_have_aws_creds_env);
5334+
5335+
TestSuite_AddFull (suite,
5336+
"/client_side_encryption/kms/auto-aws/succeed",
5337+
test_auto_aws_succeed,
5338+
NULL,
5339+
NULL,
5340+
test_framework_skip_if_no_client_side_encryption,
5341+
test_framework_skip_if_max_wire_version_less_than_8,
5342+
_have_aws_creds_env);
52295343
}

0 commit comments

Comments
 (0)