Skip to content

Commit f48966d

Browse files
authored
CDRIVER-5985 remove username derivation for MONGODB-X509
Cherry-pick of 1040823
1 parent dd0ec1f commit f48966d

19 files changed

+253
-398
lines changed

.evergreen/config_generator/components/funcs/prepare_kerberos.py

-36
This file was deleted.

.evergreen/generated_configs/functions.yml

-21
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,6 @@ functions:
327327
args:
328328
- -c
329329
- .evergreen/scripts/compile-openssl-static.sh
330-
prepare-kerberos:
331-
- command: subprocess.exec
332-
type: setup
333-
params:
334-
binary: bash
335-
working_dir: mongoc
336-
silent: true
337-
args:
338-
- -c
339-
- |
340-
if test "${keytab|}" && command -v kinit >/dev/null; then
341-
echo "${keytab}" > /tmp/drivers.keytab.base64
342-
cat /tmp/drivers.keytab.base64 | base64 -d > /tmp/drivers.keytab
343-
if touch /etc/krb5.conf 2>/dev/null; then
344-
cat .evergreen/etc/kerberos.realm | tee -a /etc/krb5.conf
345-
elif command sudo true 2>/dev/null; then
346-
cat .evergreen/etc/kerberos.realm | sudo tee -a /etc/krb5.conf
347-
else
348-
echo "Cannot append kerberos.realm to /etc/krb5.conf; skipping." 1>&2
349-
fi
350-
fi
351330
restore-instance-profile:
352331
- command: subprocess.exec
353332
params:

.evergreen/generated_configs/legacy-config.yml

-5
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,6 @@ tasks:
12451245
- func: fetch-build
12461246
vars:
12471247
BUILD_NAME: debug-compile-sasl-openssl
1248-
- func: prepare-kerberos
12491248
- func: run auth tests
12501249
- name: authentication-tests-darwinssl
12511250
tags:
@@ -1258,7 +1257,6 @@ tasks:
12581257
- func: fetch-build
12591258
vars:
12601259
BUILD_NAME: debug-compile-sasl-darwinssl
1261-
- func: prepare-kerberos
12621260
- func: run auth tests
12631261
- name: authentication-tests-winssl
12641262
tags:
@@ -1271,7 +1269,6 @@ tasks:
12711269
- func: fetch-build
12721270
vars:
12731271
BUILD_NAME: debug-compile-sspi-winssl
1274-
- func: prepare-kerberos
12751272
- func: run auth tests
12761273
- name: authentication-tests-openssl-nosasl
12771274
tags:
@@ -1284,7 +1281,6 @@ tasks:
12841281
- func: fetch-build
12851282
vars:
12861283
BUILD_NAME: debug-compile-nosasl-openssl
1287-
- func: prepare-kerberos
12881284
- func: run auth tests
12891285
- name: test-mongohouse
12901286
depends_on:
@@ -1312,7 +1308,6 @@ tasks:
13121308
script: |-
13131309
set -o errexit
13141310
env SANITIZE=address SASL=AUTO SSL=OPENSSL EXTRA_CONFIGURE_FLAGS='-DENABLE_EXTRA_ALIGNMENT=OFF' .evergreen/scripts/compile.sh
1315-
- func: prepare-kerberos
13161311
- func: run auth tests
13171312
vars:
13181313
ASAN: 'on'

.evergreen/legacy_config_generator/evergreen_config_lib/tasks.py

-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@ def additional_dependencies(self) -> Iterable[DependencySpec]:
649649

650650
def post_commands(self) -> Iterable[Value]:
651651
yield func("fetch-build", BUILD_NAME=self.build_task_name)
652-
yield func("prepare-kerberos")
653652
yield func("run auth tests")
654653

655654
@property
@@ -701,7 +700,6 @@ def pre_commands(self) -> Iterable[Value]:
701700
""",
702701
add_expansions_to_env=True,
703702
),
704-
func("prepare-kerberos"),
705703
func("run auth tests", ASAN="on"),
706704
],
707705
)

.evergreen/scripts/run-auth-tests.sh

+38-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ mongoc_dir="$(to_absolute "${script_dir}/../..")"
1818
declare install_dir="${mongoc_dir}/install-dir"
1919
declare openssl_install_dir="${mongoc_dir}/openssl-install-dir"
2020

21+
# Create directory for secrets within Evergreen task directory. Task directory is cleaned up between tasks.
22+
declare secrets_dir
23+
secrets_dir="$(to_absolute "${mongoc_dir}/../secrets")"
24+
mkdir -p "${secrets_dir}"
25+
chmod 700 "${secrets_dir}"
26+
27+
# Create certificate to test X509 auth with Atlas:
28+
atlas_x509_path="${secrets_dir:?}/atlas_x509.pem"
29+
echo "${atlas_x509_cert_base64:?}" | base64 --decode > "${secrets_dir:?}/atlas_x509.pem"
30+
# On Windows, convert certificate to PKCS#1 to work around CDRIVER-4269:
31+
if $IS_WINDOWS; then
32+
openssl pkey -in "${secrets_dir:?}/atlas_x509.pem" -traditional > "${secrets_dir:?}/atlas_x509_pkcs1.pem"
33+
openssl x509 -in "${secrets_dir:?}/atlas_x509.pem" >> "${secrets_dir:?}/atlas_x509_pkcs1.pem"
34+
atlas_x509_path="$(cygpath -m "${secrets_dir:?}/atlas_x509_pkcs1.pem")"
35+
fi
36+
37+
# Create Kerberos config and keytab files.
38+
echo "Setting up Kerberos ... begin"
39+
if command -v kinit >/dev/null; then
40+
# Copy host config and append realm:
41+
if [ -e /etc/krb5.conf ]; then
42+
cat /etc/krb5.conf > "${secrets_dir:?}/krb5.conf"
43+
fi
44+
cat "${mongoc_dir}/.evergreen/etc/kerberos.realm" >> "${secrets_dir:?}/krb5.conf"
45+
# Set up keytab:
46+
echo "${keytab:?}" | base64 --decode > "${secrets_dir:?}/drivers.keytab"
47+
# Initialize kerberos:
48+
KRB5_CONFIG="${secrets_dir:?}/krb5.conf" kinit -k -t "${secrets_dir:?}/drivers.keytab" -p [email protected]
49+
echo "Setting up Kerberos ... done"
50+
else
51+
echo "No 'kinit' detected"
52+
echo "Setting up Kerberos ... skipping"
53+
fi
54+
2155
declare c_timeout="connectTimeoutMS=30000&serverSelectionTryOnce=false"
2256

2357
declare sasl="OFF"
@@ -62,10 +96,6 @@ esac
6296
: "${test_gssapi:?}"
6397
: "${ip_addr:?}"
6498

65-
if command -v kinit >/dev/null && [[ -f /tmp/drivers.keytab ]]; then
66-
kinit -k -t /tmp/drivers.keytab -p [email protected] || true
67-
fi
68-
6999
# Archlinux (which we use for testing various self-installed OpenSSL versions)
70100
# stores their trust list under /etc/ca-certificates/extracted/.
71101
# We need to copy it to our custom installed OpenSSL/LibreSSL trust store.
@@ -142,6 +172,10 @@ if [[ "${ssl}" != "OFF" ]]; then
142172
echo "Connecting to Atlas Serverless"
143173
LD_LIBRARY_PATH="${openssl_lib_prefix}" "${ping}" "${atlas_serverless:?}&${c_timeout}"
144174
fi
175+
176+
echo "Connecting to Atlas with X509"
177+
LD_LIBRARY_PATH="${openssl_lib_prefix}" "${ping}" "${atlas_x509:?}&tlsCertificateKeyFile=${atlas_x509_path}&${c_timeout}"
178+
145179
fi
146180

147181
echo "Authenticating using PLAIN"

CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ $ mongod --auth --setParameter enableTestCommands=1 --dbpath db/
148148
In another terminal, use the `mongosh` shell to create a user:
149149

150150
```
151-
$ mongosh --eval "db.createUser({user: 'admin', pwd: 'pass', roles: ['root']})" admin
151+
$ mongosh --eval "db.createUser({user: 'bob', pwd: 'pwd123', roles: ['root']})" admin
152152
```
153153

154154
Authentication in MongoDB 3.0 and later uses SCRAM-SHA-1, which in turn
@@ -157,8 +157,8 @@ requires a driver built with SSL.
157157
Set the user and password environment variables, then build and run the tests:
158158

159159
```
160-
$ export MONGOC_TEST_USER=admin
161-
$ export MONGOC_TEST_PASSWORD=pass
160+
$ export MONGOC_TEST_USER=bob
161+
$ export MONGOC_TEST_PASSWORD=pwd123
162162
$ ./test-libmongoc
163163
```
164164

src/libmongoc/src/mongoc/mongoc-cluster-private.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ _mongoc_cluster_create_server_stream (const mongoc_topology_description_t *td,
235235
mongoc_stream_t *stream);
236236

237237
bool
238-
_mongoc_cluster_get_auth_cmd_x509 (const mongoc_uri_t *uri,
239-
const mongoc_ssl_opt_t *ssl_opts,
240-
bson_t *cmd /* OUT */,
241-
bson_error_t *error /* OUT */);
238+
_mongoc_cluster_get_auth_cmd_x509 (const mongoc_uri_t *uri, bson_t *cmd /* OUT */, bson_error_t *error /* OUT */);
242239

243240
/* Returns true if a versioned server API has been selected, otherwise returns
244241
* false. */

src/libmongoc/src/mongoc/mongoc-cluster.c

+8-36
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int32_t
150150
_int32_from_le (const void *data)
151151
{
152152
BSON_ASSERT_PARAM (data);
153-
return bson_iter_int32_unsafe (&(bson_iter_t){.raw = data});
153+
return bson_iter_int32_unsafe (&(bson_iter_t) {.raw = data});
154154
}
155155

156156

@@ -813,12 +813,7 @@ _stream_run_hello (mongoc_cluster_t *cluster,
813813
_mongoc_topology_dup_handshake_cmd (cluster->client->topology, &handshake_command);
814814

815815
if (cluster->requires_auth && speculative_auth_response) {
816-
mongoc_ssl_opt_t *ssl_opts = NULL;
817-
#ifdef MONGOC_ENABLE_SSL
818-
ssl_opts = &cluster->client->ssl_opts;
819-
#endif
820-
821-
_mongoc_topology_scanner_add_speculative_authentication (&handshake_command, cluster->uri, ssl_opts, scram);
816+
_mongoc_topology_scanner_add_speculative_authentication (&handshake_command, cluster->uri, scram);
822817
}
823818

824819
if (negotiate_sasl_supported_mechs) {
@@ -1059,10 +1054,7 @@ _mongoc_cluster_auth_node_plain (mongoc_cluster_t *cluster,
10591054
}
10601055

10611056
bool
1062-
_mongoc_cluster_get_auth_cmd_x509 (const mongoc_uri_t *uri,
1063-
const mongoc_ssl_opt_t *ssl_opts,
1064-
bson_t *cmd /* OUT */,
1065-
bson_error_t *error /* OUT */)
1057+
_mongoc_cluster_get_auth_cmd_x509 (const mongoc_uri_t *uri, bson_t *cmd /* OUT */, bson_error_t *error /* OUT */)
10661058
{
10671059
#ifndef MONGOC_ENABLE_SSL
10681060
bson_set_error (error,
@@ -1073,41 +1065,21 @@ _mongoc_cluster_get_auth_cmd_x509 (const mongoc_uri_t *uri,
10731065
return false;
10741066
#else
10751067
const char *username_from_uri = NULL;
1076-
char *username_from_subject = NULL;
10771068

10781069
BSON_ASSERT (uri);
1070+
BSON_UNUSED (error);
10791071

10801072
username_from_uri = mongoc_uri_get_username (uri);
10811073
if (username_from_uri) {
10821074
TRACE ("%s", "X509: got username from URI");
1083-
} else {
1084-
if (!ssl_opts || !ssl_opts->pem_file) {
1085-
bson_set_error (error,
1086-
MONGOC_ERROR_CLIENT,
1087-
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1088-
"cannot determine username for "
1089-
"X-509 authentication.");
1090-
return false;
1091-
}
1092-
1093-
username_from_subject = mongoc_ssl_extract_subject (ssl_opts->pem_file, ssl_opts->pem_pwd);
1094-
if (!username_from_subject) {
1095-
bson_set_error (error,
1096-
MONGOC_ERROR_CLIENT,
1097-
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1098-
"No username provided for X509 authentication.");
1099-
return false;
1100-
}
1101-
1102-
TRACE ("%s", "X509: got username from certificate");
11031075
}
11041076

11051077
bson_init (cmd);
11061078
BSON_APPEND_INT32 (cmd, "authenticate", 1);
11071079
BSON_APPEND_UTF8 (cmd, "mechanism", "MONGODB-X509");
1108-
BSON_APPEND_UTF8 (cmd, "user", username_from_uri ? username_from_uri : username_from_subject);
1109-
1110-
bson_free (username_from_subject);
1080+
if (username_from_uri) {
1081+
BSON_APPEND_UTF8 (cmd, "user", username_from_uri);
1082+
}
11111083

11121084
return true;
11131085
#endif
@@ -1138,7 +1110,7 @@ _mongoc_cluster_auth_node_x509 (mongoc_cluster_t *cluster,
11381110
BSON_ASSERT (cluster);
11391111
BSON_ASSERT (stream);
11401112

1141-
if (!_mongoc_cluster_get_auth_cmd_x509 (cluster->uri, &cluster->client->ssl_opts, &cmd, error)) {
1113+
if (!_mongoc_cluster_get_auth_cmd_x509 (cluster->uri, &cmd, error)) {
11421114
return false;
11431115
}
11441116

src/libmongoc/src/mongoc/mongoc-openssl-private.h

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ bool
3838
_mongoc_openssl_check_peer_hostname (SSL *ssl, const char *host, bool allow_invalid_hostname);
3939
SSL_CTX *
4040
_mongoc_openssl_ctx_new (mongoc_ssl_opt_t *opt);
41-
char *
42-
_mongoc_openssl_extract_subject (const char *filename, const char *passphrase);
4341
void
4442
_mongoc_openssl_init (void);
4543
void

src/libmongoc/src/mongoc/mongoc-openssl.c

-51
Original file line numberDiff line numberDiff line change
@@ -1011,57 +1011,6 @@ _mongoc_openssl_ctx_new (mongoc_ssl_opt_t *opt)
10111011
return ctx;
10121012
}
10131013

1014-
1015-
char *
1016-
_mongoc_openssl_extract_subject (const char *filename, const char *passphrase)
1017-
{
1018-
X509_NAME *subject = NULL;
1019-
X509 *cert = NULL;
1020-
BIO *certbio = NULL;
1021-
BIO *strbio = NULL;
1022-
char *str = NULL;
1023-
int ret;
1024-
1025-
BSON_UNUSED (passphrase);
1026-
1027-
if (!filename) {
1028-
return NULL;
1029-
}
1030-
1031-
certbio = BIO_new (BIO_s_file ());
1032-
strbio = BIO_new (BIO_s_mem ());
1033-
1034-
BSON_ASSERT (certbio);
1035-
BSON_ASSERT (strbio);
1036-
1037-
1038-
if (BIO_read_filename (certbio, filename) && (cert = PEM_read_bio_X509 (certbio, NULL, 0, NULL))) {
1039-
if ((subject = X509_get_subject_name (cert))) {
1040-
ret = X509_NAME_print_ex (strbio, subject, 0, XN_FLAG_RFC2253);
1041-
1042-
if ((ret > 0) && (ret < INT_MAX)) {
1043-
str = (char *) bson_malloc (ret + 2);
1044-
BIO_gets (strbio, str, ret + 1);
1045-
str[ret] = '\0';
1046-
}
1047-
}
1048-
}
1049-
1050-
if (cert) {
1051-
X509_free (cert);
1052-
}
1053-
1054-
if (certbio) {
1055-
BIO_free (certbio);
1056-
}
1057-
1058-
if (strbio) {
1059-
BIO_free (strbio);
1060-
}
1061-
1062-
return str;
1063-
}
1064-
10651014
#if OPENSSL_VERSION_NUMBER < 0x10100000L
10661015
#ifdef _WIN32
10671016

src/libmongoc/src/mongoc/mongoc-secure-channel-private.h

-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232

3333
BSON_BEGIN_DECLS
3434

35-
36-
char *
37-
_mongoc_secure_channel_extract_subject (const char *filename, const char *passphrase);
38-
3935
bool
4036
mongoc_secure_channel_setup_ca (mongoc_stream_tls_secure_channel_t *secure_channel, mongoc_ssl_opt_t *opt);
4137

0 commit comments

Comments
 (0)