From bfcd5c5a31dd103cb15863b7206147dc9353d608 Mon Sep 17 00:00:00 2001 From: William Horton Date: Wed, 8 Aug 2018 13:21:34 -0400 Subject: [PATCH] [AIRFLOW-2875] Escape env vars in tmp config --- airflow/configuration.py | 4 +++- airflow/utils/configuration.py | 2 +- run_unit_tests.sh | 1 + tests/configuration.py | 9 +++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/airflow/configuration.py b/airflow/configuration.py index ed8943ac77061..e5a2a000ec8a1 100644 --- a/airflow/configuration.py +++ b/airflow/configuration.py @@ -330,7 +330,7 @@ def getsection(self, section): _section[key] = val return _section - def as_dict(self, display_source=False, display_sensitive=False): + def as_dict(self, display_source=False, display_sensitive=False, escape_env_vars=False): """ Returns the current configuration as an OrderedDict of OrderedDicts. :param display_source: If False, the option value is returned. If True, @@ -378,6 +378,8 @@ def as_dict(self, display_source=False, display_sensitive=False): if opt: if not display_sensitive: opt = '< hidden >' + if escape_env_vars: + opt = opt.replace('%', '%%') if display_source: opt = (opt, 'bash cmd') cfg.setdefault(section, OrderedDict()).update({key: opt}) diff --git a/airflow/utils/configuration.py b/airflow/utils/configuration.py index 18a338c23f6ff..6ad43646be9b8 100644 --- a/airflow/utils/configuration.py +++ b/airflow/utils/configuration.py @@ -32,7 +32,7 @@ def tmp_configuration_copy(): settings. :return: a path to a temporary file """ - cfg_dict = conf.as_dict(display_sensitive=True) + cfg_dict = conf.as_dict(display_sensitive=True, escape_env_vars=True) temp_fd, cfg_path = mkstemp() with os.fdopen(temp_fd, 'w') as temp_file: diff --git a/run_unit_tests.sh b/run_unit_tests.sh index 27e4d08af1416..dfda2c3dcb014 100755 --- a/run_unit_tests.sh +++ b/run_unit_tests.sh @@ -26,6 +26,7 @@ export AIRFLOW__CORE__UNIT_TEST_MODE=True # configuration test export AIRFLOW__TESTSECTION__TESTKEY=testvalue +export AIRFLOW__TESTSECTION__TESTKEYWITHPERCENT=test%with%percent # use Airflow 2.0-style imports export AIRFLOW_USE_NEW_IMPORTS=1 diff --git a/tests/configuration.py b/tests/configuration.py index ac6f7b7db7746..07d817e7d1525 100644 --- a/tests/configuration.py +++ b/tests/configuration.py @@ -7,9 +7,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -69,6 +69,11 @@ def test_conf_as_dict(self): self.assertEqual( cfg_dict['testsection']['testkey'], ('testvalue', 'env var')) + # test escape_env_vars + cfg_dict = conf.as_dict(escape_env_vars=True) + self.assertEqual( + cfg_dict['testsection']['testkeywithpercent'], 'test%%with%%percent') + def test_command_config(self): TEST_CONFIG = '''[test] key1 = hello