Skip to content

Commit 2cac8f4

Browse files
bjorihanumantmk
authored andcommitted
CDRIVER-489: Use the provided username, if available for X.509 auth
Falls back to extracting the subject from the cert for backwards compatibility Closes #145
1 parent e422877 commit 2cac8f4

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,17 +1664,25 @@ _mongoc_cluster_auth_node_x509 (mongoc_cluster_t *cluster,
16641664
BSON_ASSERT (cluster);
16651665
BSON_ASSERT (node);
16661666

1667-
if (!cluster->client->ssl_opts.pem_file) {
1668-
bson_set_error (error,
1669-
MONGOC_ERROR_CLIENT,
1670-
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1671-
"mongoc_client_set_ssl_opts() must be called "
1672-
"with pem file for X-509 auth.");
1673-
return false;
1674-
}
1667+
username = mongoc_uri_get_username(cluster->uri);
1668+
if (username) {
1669+
MONGOC_INFO ("X509: got username (%s) from URI", username);
1670+
} else {
1671+
if (!cluster->client->ssl_opts.pem_file) {
1672+
bson_set_error (error,
1673+
MONGOC_ERROR_CLIENT,
1674+
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1675+
"cannot determine username "
1676+
"please either set it as part of the connection string or "
1677+
"call mongoc_client_set_ssl_opts() "
1678+
"with pem file for X-509 auth.");
1679+
return false;
1680+
}
16751681

1676-
if (cluster->client->pem_subject) {
1677-
username = cluster->client->pem_subject;
1682+
if (cluster->client->pem_subject) {
1683+
username = cluster->client->pem_subject;
1684+
MONGOC_INFO ("X509: got username (%s) from certificate", username);
1685+
}
16781686
}
16791687

16801688
bson_init (&cmd);

0 commit comments

Comments
 (0)