From f1fa6145b4dd19b3bf9c256e52845b3cadd0e761 Mon Sep 17 00:00:00 2001 From: Davy Date: Thu, 24 Feb 2022 15:40:57 +0800 Subject: [PATCH] Correctly handle multiple '=' in LocalFileSystem secrets. (#21694) (cherry picked from commit 919b75ba20083cc83c4e84e35aae8102af2b5871) --- airflow/secrets/local_filesystem.py | 7 +++---- tests/secrets/test_local_filesystem.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/airflow/secrets/local_filesystem.py b/airflow/secrets/local_filesystem.py index d23969fbecb5d..227a77c6166b7 100644 --- a/airflow/secrets/local_filesystem.py +++ b/airflow/secrets/local_filesystem.py @@ -75,8 +75,8 @@ def _parse_env_file(file_path: str) -> Tuple[Dict[str, List[str]], List[FileSynt # Ignore comments continue - var_parts: List[str] = line.split("=", 2) - if len(var_parts) != 2: + key, sep, value = line.partition("=") + if not sep: errors.append( FileSyntaxError( line_no=line_no, @@ -85,8 +85,7 @@ def _parse_env_file(file_path: str) -> Tuple[Dict[str, List[str]], List[FileSynt ) continue - key, value = var_parts - if not key: + if not value: errors.append( FileSyntaxError( line_no=line_no, diff --git a/tests/secrets/test_local_filesystem.py b/tests/secrets/test_local_filesystem.py index 85f0aaa0b0c01..5993eb3af2d99 100644 --- a/tests/secrets/test_local_filesystem.py +++ b/tests/secrets/test_local_filesystem.py @@ -153,6 +153,23 @@ def test_env_file_should_load_connection(self, file_content, expected_connection assert expected_connection_uris == connection_uris_by_conn_id + @parameterized.expand( + ( + ( + "CONN_ID=mysql://host_1?param1=val1¶m2=val2", + {"CONN_ID": "mysql://host_1?param1=val1¶m2=val2"}, + ), + ) + ) + def test_parsing_with_params(self, content, expected_connection_uris): + with mock_local_file(content): + connections_by_conn_id = local_filesystem.load_connections_dict("a.env") + connection_uris_by_conn_id = { + conn_id: connection.get_uri() for conn_id, connection in connections_by_conn_id.items() + } + + assert expected_connection_uris == connection_uris_by_conn_id + @parameterized.expand( ( ("AA", 'Invalid line format. The line should contain at least one equal sign ("=")'),