Skip to content

CDRIVER-4439 add AWS credential cache #1207

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

Merged
merged 24 commits into from
Feb 23, 2023
Merged

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented Feb 15, 2023

Summary

  • Add global cache for AWS credentials.
  • Add a new target, test-awsauth, to test new auth in AWS environments.

Verified with this patch build of AWS tasks.

Background & Motivation

DRIVERS-2333 describes the required behavior.

The test-awsauth target replaces mongoc-ping in AWS tests. test-awsauth includes caching tests specific to AWS authentication. test-awsauth requires access to API in private headers to modify the cache.

Cache locking

The cache is locked when making requests for credentials that may be cached.
This is consistent with the AWS SDK behavior locking around fetching.
I assume this is intended to prevent duplicate requests.

test-awsauth is intended to replace mongoc-ping in AWS tests.
test-awsauth will include caching tests specific to AWS.
There is no reason to condition on the presence of ENABLE_MONGODB_AWS_AUTH

Removing reduces duplicate definitions.
add _mongoc_aws_credentials_copy_to

add _mongoc_aws_credentials_cache_t
test-awsauth statically links the C driver
@kevinAlbs kevinAlbs marked this pull request as ready for review February 15, 2023 19:20
Comment on lines 38 to 41
// expiration is the time in milliseconds since the Epoch when these
// credentials expire. If expiration is 0, the credentials do not have a
// known expiration.
uint64_t expiration_ms;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be done with mcd_duration from mcd-time.h?

It would require a conversion of the time points:

  • Calculate the duration $D$ as the difference between the current UTC time and Expiration.
  • Create an mcd_duration d from $D$
  • Store the computed expire-time: mcd_time_point expires_at = mcd_later(mcd_now(), d)

Not sure if it's worth the trouble, but may eek out later issues with a bare integer-as-time. Also has a "never-expires" of MCD_TIME_POINT_MAX and "always-expired" MCD_TIME_POINT_MIN.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think so. Updated to use mcd_timer.

bool
_mongoc_aws_credentials_obtain (mongoc_uri_t *uri,
_mongoc_aws_credentials_t *creds,
bson_error_t *error);

void
_mongoc_aws_credentials_copy_to (const _mongoc_aws_credentials_t *src,
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it is more conventional to have the dest parameter first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I plan to keep as is. The src, dst order is consistent with all other C driver functions named *_copy_to.

Comment on lines 408 to 409
mcd_timer expiration_timer;
bool expiration_set;
Copy link
Contributor

Choose a reason for hiding this comment

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

These can be dropped and the values written directly into creds->expiration, IIUC.

Copy link
Contributor

@eramongodb eramongodb left a comment

Choose a reason for hiding this comment

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

Some minor suggestions remaining; otherwise, LGTM.

Comment on lines 1209 to 1210
// Zero creds, so callers can safely call _mongoc_aws_credentials_cleanup.
*creds = MONGOC_AWS_CREDENTIALS_INIT;
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
// Zero creds, so callers can safely call _mongoc_aws_credentials_cleanup.
*creds = MONGOC_AWS_CREDENTIALS_INIT;
_mongoc_aws_credentials_cleanup (creds);
*creds = MONGOC_AWS_CREDENTIALS_INIT;

Suggest redefining behavior to be capable of handling an already-initialized "non-zero" creds argument, and suggest removing the line "The passed creds is expected to be uninitialized or zeroed." from documentation, to avoid encouraging the "require/support an uninitialized argument for a conditionally-initializing function" pattern.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

to avoid encouraging the "require/support an uninitialized argument for a conditionally-initializing function" pattern.

Done. I agree with preferring not to support uninitialized arguments. That has been a source of confusion in C driver APIs that expect an uninitialized bson_t. CDRIVER-3368 is one example.

(_mongoc_aws_credentials_t) \
{ \
.access_key_id = NULL, .secret_access_key = NULL, .session_token = NULL, \
.expiration = {{0}}, \
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
.expiration = {{0}}, \
.expiration = {.value = {0}, .set = false}, \

Addresses the following warning:

warning: missing field 'set' initializer [-Wmissing-field-initializers]
      *creds = MONGOC_AWS_CREDENTIALS_INIT;
               ^
note: expanded from macro 'MONGOC_AWS_CREDENTIALS_INIT'
      .expiration = {{0}},                                                     \
                        ^

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for the suggested fix.

Copy link
Collaborator Author

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

*-auth tasks with latest are failing with:

[2023/02/22 21:47:29.636] /data/mci/a00f187bc87fef01fb2eecba06c8c43c/mongoc/src/libmongoc/tests/test-mongoc-scram.c:295 _check_error(): [Unable to use SCRAM-SHA-256 based authentication for user without any SCRAM-SHA-256 credentials registered] does not contain [Authentication failed]

This is failing on the waterfall, and appears unrelated to this commit. The AWS tasks still pass, proceeding with merge.

(_mongoc_aws_credentials_t) \
{ \
.access_key_id = NULL, .secret_access_key = NULL, .session_token = NULL, \
.expiration = {{0}}, \
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for the suggested fix.

Comment on lines 1209 to 1210
// Zero creds, so callers can safely call _mongoc_aws_credentials_cleanup.
*creds = MONGOC_AWS_CREDENTIALS_INIT;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

to avoid encouraging the "require/support an uninitialized argument for a conditionally-initializing function" pattern.

Done. I agree with preferring not to support uninitialized arguments. That has been a source of confusion in C driver APIs that expect an uninitialized bson_t. CDRIVER-3368 is one example.

@kevinAlbs kevinAlbs merged commit 8eaac64 into mongodb:master Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants