Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SSL mode in MySQL provider #27717

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions airflow/providers/mysql/hooks/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def _get_conn_config_mysql_client(self, conn: Connection) -> dict:
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:
Expand All @@ -144,6 +146,10 @@ def _get_conn_config_mysql_connector_python(self, conn: Connection) -> dict:

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

Expand Down
20 changes: 20 additions & 0 deletions tests/providers/mysql/hooks/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ def test_get_conn_ssl_as_string(self, mock_connect):
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):
Expand Down Expand Up @@ -216,6 +225,17 @@ def test_get_conn_allow_local_infile(self, mock_connect):
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"
Expand Down