This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 83dc604650 Allow SSL mode in MySQL provider (#27717)
83dc604650 is described below

commit 83dc604650d13d64e34014f2c2323e764033c4a5
Author: Aditya Malik <[email protected]>
AuthorDate: Thu Nov 17 23:45:17 2022 +0530

    Allow SSL mode in MySQL provider (#27717)
---
 airflow/providers/mysql/hooks/mysql.py    |  6 ++++++
 tests/providers/mysql/hooks/test_mysql.py | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/airflow/providers/mysql/hooks/mysql.py 
b/airflow/providers/mysql/hooks/mysql.py
index 7987f76eb1..843ac0b8aa 100644
--- a/airflow/providers/mysql/hooks/mysql.py
+++ b/airflow/providers/mysql/hooks/mysql.py
@@ -127,6 +127,8 @@ class MySqlHook(DbApiHook):
             if isinstance(dejson_ssl, str):
                 dejson_ssl = json.loads(dejson_ssl)
             conn_config["ssl"] = dejson_ssl
+        if conn.extra_dejson.get("ssl_mode", False):
+            conn_config["ssl_mode"] = conn.extra_dejson["ssl_mode"]
         if conn.extra_dejson.get("unix_socket"):
             conn_config["unix_socket"] = conn.extra_dejson["unix_socket"]
         if local_infile:
@@ -144,6 +146,10 @@ class MySqlHook(DbApiHook):
 
         if conn.extra_dejson.get("allow_local_infile", False):
             conn_config["allow_local_infile"] = True
+        # Ref: 
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
+        for key, value in conn.extra_dejson.items():
+            if key.startswith("ssl_"):
+                conn_config[key] = value
 
         return conn_config
 
diff --git a/tests/providers/mysql/hooks/test_mysql.py 
b/tests/providers/mysql/hooks/test_mysql.py
index d7270dafdd..f0c32b5091 100644
--- a/tests/providers/mysql/hooks/test_mysql.py
+++ b/tests/providers/mysql/hooks/test_mysql.py
@@ -153,6 +153,15 @@ class TestMySqlHookConn(unittest.TestCase):
         assert args == ()
         assert kwargs["ssl"] == SSL_DICT
 
+    @mock.patch("MySQLdb.connect")
+    def test_get_ssl_mode(self, mock_connect):
+        self.connection.extra = json.dumps({"ssl_mode": "DISABLED"})
+        self.db_hook.get_conn()
+        assert mock_connect.call_count == 1
+        args, kwargs = mock_connect.call_args
+        assert args == ()
+        assert kwargs["ssl_mode"] == "DISABLED"
+
     @mock.patch("MySQLdb.connect")
     
@mock.patch("airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook.get_client_type")
     def test_get_conn_rds_iam(self, mock_client, mock_connect):
@@ -216,6 +225,17 @@ class 
TestMySqlHookConnMySqlConnectorPython(unittest.TestCase):
         assert args == ()
         assert kwargs["allow_local_infile"] == 1
 
+    @mock.patch("mysql.connector.connect")
+    def test_get_ssl_mode(self, mock_connect):
+        extra_dict = self.connection.extra_dejson
+        extra_dict.update(ssl_disabled=True)
+        self.connection.extra = json.dumps(extra_dict)
+        self.db_hook.get_conn()
+        assert mock_connect.call_count == 1
+        args, kwargs = mock_connect.call_args
+        assert args == ()
+        assert kwargs["ssl_disabled"] == 1
+
 
 class MockMySQLConnectorConnection:
     DEFAULT_AUTOCOMMIT = "default"

Reply via email to