|
21 | 21 |
|
22 | 22 | /* _mongoc_host_list_from_string_with_err */
|
23 | 23 | #include "mongoc/mongoc-host-list-private.h"
|
| 24 | +#include "mongoc/mongoc-cluster-aws-private.h" |
24 | 25 |
|
25 | 26 | /* MONGOC_SERVER_ERR_NS_NOT_FOUND */
|
26 | 27 | #include "mongoc/mongoc-error-private.h"
|
@@ -4969,6 +4970,101 @@ test_kms_callback (void *unused)
|
4969 | 4970 | mongoc_client_destroy (cl);
|
4970 | 4971 | }
|
4971 | 4972 |
|
| 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 | + |
4972 | 5068 | void
|
4973 | 5069 | test_client_side_encryption_install (TestSuite *suite)
|
4974 | 5070 | {
|
@@ -5226,4 +5322,22 @@ test_client_side_encryption_install (TestSuite *suite)
|
5226 | 5322 | NULL, // ctx
|
5227 | 5323 | test_framework_skip_if_no_client_side_encryption,
|
5228 | 5324 | 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); |
5229 | 5343 | }
|
0 commit comments