Skip to content

Commit 693b675

Browse files
jmikolaalcaeus
andauthored
PHPC-2441: Remove deprecated Manager constructor options (#1719)
* Use canonical TLS options in tests * Remove tests for deprecated TLS options * Remove deprecated Manager driver options Co-authored-by: Andreas Braun <git@alcaeus.org>
1 parent ca01041 commit 693b675

25 files changed

+78
-819
lines changed

UPGRADE-2.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ UPGRADE FROM 1.x to 2.0
4141
* The `MongoDB\Driver\Manager` constructor now throws if the URI options array
4242
includes a non-boolean value for an option expecting a boolean. This behavior
4343
is now consistent with validation for other option types.
44+
* Removed the following driver options from `MongoDB\Driver\Manager`:
45+
`allow_invalid_hostname` (use `tlsAllowInvalidHostnames` URI option instead),
46+
`ca_file` (use `tlsCAFile`), `context`,
47+
`pem_file` (use `tlsCertificateKeyFile`),
48+
`pem_pwd` (use `tlsCertificateKeyFilePassword`), and
49+
`weak_cert_validation` (use `tlsAllowInvalidCertificates`).

src/MongoDB/Manager.c

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,53 +54,6 @@
5454
*/
5555
zend_class_entry* php_phongo_manager_ce;
5656

57-
/* Checks if driverOptions contains a stream context resource in the "context"
58-
* key and incorporates any of its SSL options into the base array that did not
59-
* already exist (i.e. array union). The "context" key is then unset from the
60-
* base array.
61-
*
62-
* This handles the merging of any legacy SSL context options and also makes
63-
* driverOptions suitable for serialization by removing the resource zval. */
64-
static bool php_phongo_manager_merge_context_options(zval* zdriverOptions)
65-
{
66-
php_stream_context* context;
67-
zval * zcontext, *zcontextOptions;
68-
69-
if (!php_array_existsc(zdriverOptions, "context")) {
70-
return true;
71-
}
72-
73-
zcontext = php_array_fetchc_deref(zdriverOptions, "context");
74-
context = php_stream_context_from_zval(zcontext, 1);
75-
76-
if (!context) {
77-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "\"context\" driver option is not a valid Stream-Context resource");
78-
return false;
79-
}
80-
81-
zcontextOptions = php_array_fetchc_array(&context->options, "ssl");
82-
83-
if (!zcontextOptions) {
84-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Stream-Context resource does not contain \"ssl\" options array");
85-
return false;
86-
}
87-
88-
/* When running PHP in debug mode, php_error_docref duplicates the current
89-
* scope, leading to a COW violation in zend_hash_merge and
90-
* zend_symtable_str_del (called by php_array_unsetc). This macro allows
91-
* that violation in debug mode and is a NOOP when in non-debug. */
92-
HT_ALLOW_COW_VIOLATION(Z_ARRVAL_P(zdriverOptions));
93-
94-
php_error_docref(NULL, E_DEPRECATED, "The \"context\" driver option is deprecated.");
95-
96-
/* Perform array union (see: add_function() in zend_operators.c) */
97-
zend_hash_merge(Z_ARRVAL_P(zdriverOptions), Z_ARRVAL_P(zcontextOptions), zval_add_ref, 0);
98-
99-
php_array_unsetc(zdriverOptions, "context");
100-
101-
return true;
102-
}
103-
10457
/* Prepare authMechanismProperties for BSON encoding by converting a boolean
10558
* value for the "CANONICALIZE_HOST_NAME" option to a string.
10659
*
@@ -253,9 +206,9 @@ static PHP_METHOD(MongoDB_Driver_Manager, __construct)
253206

254207
intern = Z_MANAGER_OBJ_P(getThis());
255208

256-
/* Separate the options and driverOptions zvals, since we may end up
257-
* modifying them in php_phongo_manager_prep_uri_options() and
258-
* php_phongo_manager_merge_context_options() below, respectively. */
209+
/* Separate the options zval, since it may be modified in
210+
* php_phongo_manager_prep_uri_options(). Also separate driverOptions, since
211+
* it may be modified in php_phongo_manager_prepare_manager_for_hash(). */
259212
PHONGO_PARSE_PARAMETERS_START(0, 3)
260213
Z_PARAM_OPTIONAL
261214
Z_PARAM_STRING_OR_NULL(uri_string, uri_string_len)
@@ -267,11 +220,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, __construct)
267220
php_phongo_manager_prep_uri_options(options);
268221
}
269222

270-
if (driverOptions && !php_phongo_manager_merge_context_options(driverOptions)) {
271-
/* Exception should already have been thrown */
272-
return;
273-
}
274-
275223
phongo_manager_init(intern, uri_string ? uri_string : PHONGO_MANAGER_URI_DEFAULT, options, driverOptions);
276224

277225
if (EG(exception)) {

src/phongo_client.c

Lines changed: 27 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
544544
while (bson_iter_next(&iter)) {
545545
const char* key = bson_iter_key(&iter);
546546

547+
/* Note: although "safe" is deprecated and undocumented, we still handle
548+
* it here for consistency with _mongoc_uri_build_write_concern() */
547549
if (!ignore_safe && !strcasecmp(key, MONGOC_URI_SAFE)) {
548550
if (!BSON_ITER_HOLDS_BOOL(&iter)) {
549551
PHONGO_URI_INVALID_TYPE(iter, "boolean");
@@ -678,81 +680,64 @@ static void php_phongo_mongoc_ssl_opts_from_uri(mongoc_ssl_opt_t* ssl_opt, mongo
678680
}
679681
}
680682

681-
static inline char* php_phongo_fetch_ssl_opt_string(zval* zoptions, const char* key)
683+
/* This function abstracts php_array_fetch_string() and always returns a string
684+
* that must be freed by the caller. */
685+
static inline char* php_phongo_fetch_string(zval* zarr, const char* key)
682686
{
683687
int plen;
684688
zend_bool pfree;
685-
char* pval;
686689
char* value;
687690

688-
pval = php_array_fetch_string(zoptions, key, &plen, &pfree);
689-
value = pfree ? pval : estrndup(pval, plen);
691+
value = php_array_fetch_string(zarr, key, &plen, &pfree);
690692

691-
return value;
693+
return pfree ? value : estrndup(value, plen);
692694
}
693695

694-
static mongoc_ssl_opt_t* php_phongo_make_ssl_opt(mongoc_uri_t* uri, zval* zoptions)
696+
static mongoc_ssl_opt_t* php_phongo_make_ssl_opt(mongoc_uri_t* uri, zval* driverOptions)
695697
{
696698
mongoc_ssl_opt_t* ssl_opt;
697699
bool any_ssl_option_set = false;
698700

699-
if (!zoptions) {
701+
if (!driverOptions) {
700702
return NULL;
701703
}
702704

703705
#if defined(MONGOC_ENABLE_SSL_SECURE_CHANNEL) || defined(MONGOC_ENABLE_SSL_SECURE_TRANSPORT)
704-
if (php_array_existsc(zoptions, "ca_dir")) {
706+
if (php_array_existsc(driverOptions, "ca_dir")) {
705707
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "\"ca_dir\" option is not supported by Secure Channel and Secure Transport");
706708
return NULL;
707709
}
708-
709-
if (php_array_existsc(zoptions, "capath")) {
710-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "\"capath\" option is not supported by Secure Channel and Secure Transport");
711-
return NULL;
712-
}
713710
#endif
714711

715712
#if defined(MONGOC_ENABLE_SSL_LIBRESSL) || defined(MONGOC_ENABLE_SSL_SECURE_TRANSPORT)
716-
if (php_array_existsc(zoptions, "crl_file")) {
713+
if (php_array_existsc(driverOptions, "crl_file")) {
717714
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "\"crl_file\" option is not supported by LibreSSL and Secure Transport");
718715
return NULL;
719716
}
720717
#endif
721718

719+
/* Note: consider copying from mongoc_ssl_opt_get_default() if
720+
* MONGOC_SSL_DEFAULT_TRUST_FILE and MONGOC_SSL_DEFAULT_TRUST_DIR are ever
721+
* used, but libmongoc currently defines them as null. */
722722
ssl_opt = ecalloc(1, sizeof(mongoc_ssl_opt_t));
723723

724-
/* If SSL options are set in the URL, we need to read them and set them on
725-
* the options struct so we can merge potential options from passed in
726-
* driverOptions (zoptions) */
724+
/* Apply TLS options to the ssl_opt struct before driver options */
727725
if (mongoc_uri_get_tls(uri)) {
728726
php_phongo_mongoc_ssl_opts_from_uri(ssl_opt, uri, &any_ssl_option_set);
729727
}
730728

731-
#define PHONGO_SSL_OPTION_SWAP_STRING(o, n) \
732-
if ((o)) { \
733-
efree((char*) (o)); \
734-
} \
735-
(o) = php_phongo_fetch_ssl_opt_string(zoptions, n);
736-
737729
/* Apply driver options that don't have a corresponding URI option. These
738730
* are set directly on the SSL options struct. */
739-
if (php_array_existsc(zoptions, "ca_dir")) {
740-
PHONGO_SSL_OPTION_SWAP_STRING(ssl_opt->ca_dir, "ca_dir");
731+
if (php_array_existsc(driverOptions, "ca_dir")) {
732+
ssl_opt->ca_dir = php_phongo_fetch_string(driverOptions, "ca_dir");
741733
any_ssl_option_set = true;
742-
} else if (php_array_existsc(zoptions, "capath")) {
743-
PHONGO_SSL_OPTION_SWAP_STRING(ssl_opt->ca_dir, "capath");
744-
any_ssl_option_set = true;
745-
746-
php_error_docref(NULL, E_DEPRECATED, "The \"capath\" context driver option is deprecated. Please use the \"ca_dir\" driver option instead.");
747734
}
748735

749-
if (php_array_existsc(zoptions, "crl_file")) {
750-
PHONGO_SSL_OPTION_SWAP_STRING(ssl_opt->crl_file, "crl_file");
736+
if (php_array_existsc(driverOptions, "crl_file")) {
737+
ssl_opt->crl_file = php_phongo_fetch_string(driverOptions, "crl_file");
751738
any_ssl_option_set = true;
752739
}
753740

754-
#undef PHONGO_SSL_OPTION_SWAP_STRING
755-
756741
if (!any_ssl_option_set) {
757742
efree(ssl_opt);
758743
return NULL;
@@ -785,110 +770,6 @@ static void php_phongo_free_ssl_opt(mongoc_ssl_opt_t* ssl_opt)
785770

786771
efree(ssl_opt);
787772
}
788-
789-
static inline bool php_phongo_apply_driver_option_to_uri(mongoc_uri_t* uri, zval* zoptions, const char* driverOptionKey, const char* optionKey)
790-
{
791-
bool ret;
792-
char* value;
793-
794-
value = php_phongo_fetch_ssl_opt_string(zoptions, driverOptionKey);
795-
ret = mongoc_uri_set_option_as_utf8(uri, optionKey, value);
796-
efree(value);
797-
798-
return ret;
799-
}
800-
801-
static bool php_phongo_apply_driver_options_to_uri(mongoc_uri_t* uri, zval* zoptions)
802-
{
803-
if (!zoptions) {
804-
return true;
805-
}
806-
807-
/* Map TLS driver options to the canonical tls options in the URI. */
808-
if (php_array_existsc(zoptions, "allow_invalid_hostname")) {
809-
if (!mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES, php_array_fetchc_bool(zoptions, "allow_invalid_hostname"))) {
810-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "allow_invalid_hostname");
811-
812-
return false;
813-
}
814-
815-
php_error_docref(NULL, E_DEPRECATED, "The \"allow_invalid_hostname\" driver option is deprecated. Please use the \"tlsAllowInvalidHostnames\" URI option instead.");
816-
}
817-
818-
if (php_array_existsc(zoptions, "weak_cert_validation")) {
819-
if (!mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES, php_array_fetchc_bool(zoptions, "weak_cert_validation"))) {
820-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "weak_cert_validation");
821-
822-
return false;
823-
}
824-
825-
php_error_docref(NULL, E_DEPRECATED, "The \"weak_cert_validation\" driver option is deprecated. Please use the \"tlsAllowInvalidCertificates\" URI option instead.");
826-
} else if (php_array_existsc(zoptions, "allow_self_signed")) {
827-
if (!mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES, php_array_fetchc_bool(zoptions, "allow_self_signed"))) {
828-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "allow_self_signed");
829-
830-
return false;
831-
}
832-
833-
php_error_docref(NULL, E_DEPRECATED, "The \"allow_self_signed\" context driver option is deprecated. Please use the \"tlsAllowInvalidCertificates\" URI option instead.");
834-
}
835-
836-
if (php_array_existsc(zoptions, "pem_file")) {
837-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "pem_file", MONGOC_URI_TLSCERTIFICATEKEYFILE)) {
838-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "pem_file");
839-
840-
return false;
841-
}
842-
843-
php_error_docref(NULL, E_DEPRECATED, "The \"pem_file\" driver option is deprecated. Please use the \"tlsCertificateKeyFile\" URI option instead.");
844-
} else if (php_array_existsc(zoptions, "local_cert")) {
845-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "local_cert", MONGOC_URI_TLSCERTIFICATEKEYFILE)) {
846-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "local_cert");
847-
848-
return false;
849-
}
850-
851-
php_error_docref(NULL, E_DEPRECATED, "The \"local_cert\" context driver option is deprecated. Please use the \"tlsCertificateKeyFile\" URI option instead.");
852-
}
853-
854-
if (php_array_existsc(zoptions, "pem_pwd")) {
855-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "pem_pwd", MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD)) {
856-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "pem_pwd");
857-
858-
return false;
859-
}
860-
861-
php_error_docref(NULL, E_DEPRECATED, "The \"pem_pwd\" driver option is deprecated. Please use the \"tlsCertificateKeyFilePassword\" URI option instead.");
862-
} else if (php_array_existsc(zoptions, "passphrase")) {
863-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "passphrase", MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD)) {
864-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "passphrase");
865-
866-
return false;
867-
}
868-
869-
php_error_docref(NULL, E_DEPRECATED, "The \"passphrase\" context driver option is deprecated. Please use the \"tlsCertificateKeyFilePassword\" URI option instead.");
870-
}
871-
872-
if (php_array_existsc(zoptions, "ca_file")) {
873-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "ca_file", MONGOC_URI_TLSCAFILE)) {
874-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "ca_file");
875-
876-
return false;
877-
}
878-
879-
php_error_docref(NULL, E_DEPRECATED, "The \"ca_file\" driver option is deprecated. Please use the \"tlsCAFile\" URI option instead.");
880-
} else if (php_array_existsc(zoptions, "cafile")) {
881-
if (!php_phongo_apply_driver_option_to_uri(uri, zoptions, "cafile", MONGOC_URI_TLSCAFILE)) {
882-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" driver option", "cafile");
883-
884-
return false;
885-
}
886-
887-
php_error_docref(NULL, E_DEPRECATED, "The \"cafile\" context driver option is deprecated. Please use the \"tlsCAFile\" URI option instead.");
888-
}
889-
890-
return true;
891-
}
892773
#endif /* MONGOC_ENABLE_SSL */
893774

894775
static zval* php_phongo_manager_prepare_manager_for_hash(zval* driverOptions, bool* free)
@@ -907,21 +788,21 @@ static zval* php_phongo_manager_prepare_manager_for_hash(zval* driverOptions, bo
907788
}
908789

909790
if (!php_array_existsc(driverOptions, "autoEncryption")) {
910-
goto ref;
791+
return driverOptions;
911792
}
912793

913794
autoEncryptionOpts = php_array_fetchc(driverOptions, "autoEncryption");
914795
if (Z_TYPE_P(autoEncryptionOpts) != IS_ARRAY) {
915-
goto ref;
796+
return driverOptions;
916797
}
917798

918799
if (!php_array_existsc(autoEncryptionOpts, "keyVaultClient")) {
919-
goto ref;
800+
return driverOptions;
920801
}
921802

922803
keyVaultClient = php_array_fetchc(autoEncryptionOpts, "keyVaultClient");
923804
if (Z_TYPE_P(keyVaultClient) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(keyVaultClient), php_phongo_manager_ce)) {
924-
goto ref;
805+
return driverOptions;
925806
}
926807

927808
*free = true;
@@ -938,10 +819,6 @@ static zval* php_phongo_manager_prepare_manager_for_hash(zval* driverOptions, bo
938819
ADD_ASSOC_ZVAL_EX(driverOptionsClone, "autoEncryption", autoEncryptionOptsClone);
939820

940821
return driverOptionsClone;
941-
942-
ref:
943-
Z_ADDREF_P(driverOptions);
944-
return driverOptions;
945822
}
946823

947824
/* Creates a hash for a client by concatenating the URI string with serialized
@@ -972,6 +849,10 @@ static char* php_phongo_manager_make_client_hash(const char* uri_string, zval* o
972849
if (driverOptions) {
973850
serializable_driver_options = php_phongo_manager_prepare_manager_for_hash(driverOptions, &free_driver_options);
974851
ADD_ASSOC_ZVAL_EX(&args, "driverOptions", serializable_driver_options);
852+
/* Add a reference to driverOptions unless a new copy was returned */
853+
if (!free_driver_options) {
854+
Z_ADDREF_P(serializable_driver_options);
855+
}
975856
} else {
976857
ADD_ASSOC_NULL_EX(&args, "driverOptions");
977858
}
@@ -1504,11 +1385,6 @@ void phongo_manager_init(php_phongo_manager_t* manager, const char* uri_string,
15041385
}
15051386

15061387
#ifdef MONGOC_ENABLE_SSL
1507-
if (!php_phongo_apply_driver_options_to_uri(uri, driverOptions)) {
1508-
/* Exception should already have been thrown */
1509-
goto cleanup;
1510-
}
1511-
15121388
ssl_opt = php_phongo_make_ssl_opt(uri, driverOptions);
15131389

15141390
/* An exception may be thrown during SSL option creation */

0 commit comments

Comments
 (0)