@@ -170,7 +170,6 @@ def parse(
170170 conn_max_age ,
171171 conn_health_checks ,
172172 disable_server_side_cursors ,
173- ssl_require ,
174173 test_options ,
175174 )
176175
@@ -211,6 +210,9 @@ def parse(
211210 parsed_config ["OPTIONS" ].update (settings .pop ("OPTIONS" , {}))
212211 parsed_config .update (settings )
213212
213+ if ssl_require :
214+ _configure_ssl (parsed_config )
215+
214216 if not parsed_config ["OPTIONS" ]:
215217 parsed_config .pop ("OPTIONS" )
216218 return parsed_config
@@ -229,12 +231,43 @@ def _parse_value(value: str) -> OptionType:
229231 return value
230232
231233
234+ def _configure_ssl (parsed_config : DBConfig ) -> None :
235+ assert "OPTIONS" in parsed_config
236+ options = parsed_config ["OPTIONS" ]
237+ assert "ENGINE" in parsed_config
238+ backend = parsed_config ["ENGINE" ]
239+
240+ if backend in (
241+ "django.db.backends.mysql" ,
242+ "django.contrib.gis.db.backends.mysql" ,
243+ ):
244+ options ["ssl_mode" ] = "REQUIRED"
245+ elif backend in (
246+ "django.db.backends.postgresql" ,
247+ "django.contrib.gis.db.backends.postgis" ,
248+ "django_redshift_backend" ,
249+ "django_cockroachdb" ,
250+ "timescale.db.backends.postgresql" ,
251+ "timescale.db.backends.postgis" ,
252+ ):
253+ options ["sslmode" ] = "require"
254+ elif backend in (
255+ "mssql" ,
256+ "sql_server.pyodbc" ,
257+ ):
258+ current_extra = options .get ("extra_params" , "" )
259+ if "Encrypt=yes" not in current_extra :
260+ if current_extra :
261+ options ["extra_params" ] = f"{ current_extra } ;Encrypt=yes"
262+ else :
263+ options ["extra_params" ] = "Encrypt=yes"
264+
265+
232266def _convert_to_settings (
233267 engine : Optional [str ],
234268 conn_max_age : Optional [int ],
235269 conn_health_checks : bool ,
236270 disable_server_side_cursors : bool ,
237- ssl_require : bool ,
238271 test_options : Optional [dict [str , Any ]],
239272) -> DBConfig :
240273 settings : DBConfig = {
@@ -244,9 +277,6 @@ def _convert_to_settings(
244277 }
245278 if engine :
246279 settings ["ENGINE" ] = engine
247- if ssl_require :
248- settings ["OPTIONS" ] = {}
249- settings ["OPTIONS" ]["sslmode" ] = "require"
250280 if test_options :
251281 settings ["TEST" ] = test_options
252282 return settings
0 commit comments