-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-4489 refactor URI auth finalization by authentication mechanism #1896
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the direction. Posting initial comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactoring here is quite nice! Found a few minor things. I'm not sure if I'm missing something about '0' being a portable null pointer literal in C now, I thought that was a C++ism.
It turns out this is not spec compliant per CDRIVER-5776. Null fields (specified by
The "keys should always exist" condition is not upheld by the connection string spec test runner due to supporting both connection string, URI, and auth spec tests, which have different field requirements (hosts+auth+options: connection string and URI; credentials: auth). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I had three small comments on re-review, see the separate posts above (sorry they didn't get attached directly to this). No big deal on any of them really, but it would be nice to resolve the minor memory leak.
Summary
Precursor changes to CDRIVER-4489.
This PR does not implement any
MONGODB-OIDC
features yet. Instead, this PR updates existing code related to authentication mechanisms to better align with the current Drivers Authentication specification and better faciliate the addition ofMONGODB-OIDC
features. This includes some API breaking changes to correct behavior to conform to the current Authentication spec.MongoCredential
The Authentication spec is written in terms of a
MongoCredential
type. The mongoc library has no such type. Instead, the fields are split across data members ofmongoc_uri_t
. Comments have been added to better document the correlation between theMongoCredential
spec and how they are represented by data members ofmongoc_uri_t
.Although refactoring the data members to better reflect the
MongoCredential
type specification was considered (e.g. adding a newmongoc_credential_t
type, movingMongoCredential.source
into its own data member, etc.), this was rejected due to requiring significant breaking changes tomongoc_uri_get_credentials
and related API.Default
authSource
BehaviorThe
mongoc_uri_get_auth_source
function is refactored to better express how defaults are applied for each authentication mechanism. Notably there are four distinct cases to consider:_mongoc_cluster_auth_node
during handshake), thereforeauthMechanism
is still unset. In this case, the "database or"admin"
" behavior is applied. This case is deliberately treated separately from explicit authentication mechanism cases for future maintainability (i.e. ifMONGODB-OIDC
is added to the default authentication method)."admin"
": ifauthSource
is not specified, either the database name is used as the source (if provided), or it is defaulted to the"admin"
database. This applies toMONGODB-CR
,MONGODB-SHA-1
, andMONGODB-SHA-256
."$external"
": ifauthSource
is not specified, either the database name is used as the source (when provided), or it is defaulted to"$external"
to indicate an external database must be used for authentication. This only applies toPLAIN
.$external
": always "$external
", whether specified or defaulted. This applies toMONGODB-X509
,GSSAPI
, andMONGODB-AWS
.To avoid significant API breaking changes, validation is not performed by either
mongoc_uri_get_auth_source
ormongoc_uri_set_auth_source
. However, the return value ofmongoc_uri_get_auth_source
has been fixed to return"$external"
forMONGODB-AWS
.Note
mongoc_uri_get_auth_source
may return a source even whenauthSource
is not actually present in the URI string itself. This behavior is preserved for backward compatibility.Warning
API Breaking Change: when
authMechanism
is set toMONGODB-AWS
,mongoc_uri_get_auth_source
will now default to"$external"
(correct) instead of "database name or"admin"
(incorrect).URI Authentication Credentials Validation
Important
All behavior described below applies only to URI finalization during construction of a new
mongoc_uri_t
object from a connection string. It does not apply to other URI-related API (i.e. getters and setters for individual fields and properties).The internal
mongoc_uri_finalize_auth
function is refactored to better denote howMongoCredential
properties are validated for each authentication mechanism. The series of non-exclusiveif
statements grouped by validation and defaulting operations are replaced by a series of exclusiveif
statements grouped by authentication mechanism. This refactor allows for a more direct comparison between the spec and the implementation for correctness.authMechanism
ValidationAs with
mongoc_uri_get_auth_source
, the "default authentication method" is handled separately from explicitly specified authentication methods. Validation is deferred to_mongoc_cluster_auth_node
during handshake when the authentication mechanism is determined according tosaslSupportedMechs
. Remaining authentication methods are split into distinct branches by their requiredMongoCredential
property requirements.Even though the default authentication method can (at the moment) only resolve to
SCRAM-SHA-1
andSCRAM-SHA-256
, theMongoCredential
property requirements must not be validated "in-advance" of the actual handshake andsaslSupportedMechs
(e.g. thepassword
"MUST be specified" requirement). This is both for backward compatibility and for forward compatibility (i.e. ifMONGODB-OIDC
, for whichpassword
"MUST NOT be specified", is added to the default authentication method).However,
authMechanism
itself may be validated. Invalid values (including "empty" strings and incorrect casing) are now treated as a client error rather than ignored. This change exposed some typos of"SCRAM-SHA-1"
as"SCRAM-SHA1"
in test code which are now fixed.Warning
API Breaking Changes:
authMechanism
to be specified as an invalid or unsupported value (including case-sensitivity, e.g."scram-sha-1"
is not equivalent to"SCRAM-SHA-1"
).MONGODB-X509
.MONGODB-CR
,MONGODB-SHA-1
,MONGODB-SHA-256
, andPLAIN
.authMechanismProperties
to be specified forMONGODB-CR
,MONGODB-X509
,MONGODB-SHA-1
,MONGODB-SHA-256
, andPLAIN
.authSource
is set to a value that is not"$external"
forMONGODB-X509
,GSSAPI
, andMONGODB-AWS
.Note
Although CDRIVER-1959 demotes the username requirement for
MONGODB-X509
from "MUST" to "SHOULD NOT", backward compatibility and Connection String spec tests prevent us from validating this requirement as either a warning or an error. This restriction does not apply to password requirements, as described above.authMechanismProperties
ValidationAs part of this refactor, validation of authentication mechanisms and related fields is improved. Invalid and unsupported fields in
authMechanismProperties
(whenauthMechanism
is specified)now emit warning messages. For backward compatibility, invalid and unsupported fields are still preserved in the resultingare now reported as client errors.authMechanismProperties
document.The
CANONICALIZE_HOST_NAME
property forGSSAPI
is not fully supported as it should be due to CDRIVER-4128. Although the specification requires support for values such as "none", "forward", and "forwardAndReverse", mongoc currently only treats the legacy boolean value "true" (equivalent to "forwardAndReverse") as a significant value; all other values are ignored and treated as "false" (equivalent to "none"). Such instances in code are now labeled with a comment referencing CDRIVER-4128.For backward compatibility, a warning message is emitted rather than returning an error.Unsupported values are now diagnosed as a client error as required by spec.Warning
API Breaking Changes:
authMechanism
is specified, invalid or unsupported properties inauthMechanismProperties
for the specified authentication mechanism are now a client error (e.g.CANONICALIZE_HOST_NAME
).Important
New Behavior:
authMechanism
is set toGSSAPI
, a warning message is now emitted for invalid or unsupported values ofCANONICALIZE_HOST_NAME
during a handshake by_mongoc_sasl_set_properties
(currently, only"false"
and"true"
are supported). This may occur when the authentication mechanism or properties are set after URI finalization, such as viamongoc_uri_set_mechanism_properties
.Test Coverage
Comprehensive test coverage of each and every authentication mechanism is implemented as the new
test_mongoc_uri_auth_mechanisms
test function, replacingtest_mongoc_uri_authmechanismproperties
.This PR additionally applies modernization of URI and connection string test code to improve the quality of assertion messages when comparing integers, strings, and BSON documents.
Before (string comparison; similar for integers):
After (string comparison):
Before (BSON documents, exact match):
After (BSON documents, exact match):
Before (BSON documents, element matches):
After (BSON documents, element matches):