From 7448e7e1df892cde1e7ab1cb61c24f62a4eae8da Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 26 Jul 2013 11:34:17 -0500 Subject: Fix hostname conflict in config generator When the system hostname is the same as a default value in the config, the _sanitize_default function may replace it incorrectly. This change checks that the name of the option contains the substring 'host' to make sure it isn't changing non-hostname defaults. Fixes bug 1205411 Change-Id: I93edb2902304c5cd9bfa817b1bccdaec5d807e6c --- openstack/common/config/generator.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'openstack/common') diff --git a/openstack/common/config/generator.py b/openstack/common/config/generator.py index 07aa296..1ae6c4d 100644 --- a/openstack/common/config/generator.py +++ b/openstack/common/config/generator.py @@ -181,24 +181,24 @@ def _get_my_ip(): return None -def _sanitize_default(s): +def _sanitize_default(name, value): """Set up a reasonably sensible default for pybasedir, my_ip and host.""" - if s.startswith(sys.prefix): + if value.startswith(sys.prefix): # NOTE(jd) Don't use os.path.join, because it is likely to think the # second part is an absolute pathname and therefore drop the first # part. - s = os.path.normpath("/usr/" + s[len(sys.prefix):]) - elif s.startswith(BASEDIR): - return s.replace(BASEDIR, '/usr/lib/python/site-packages') - elif BASEDIR in s: - return s.replace(BASEDIR, '') - elif s == _get_my_ip(): + value = os.path.normpath("/usr/" + value[len(sys.prefix):]) + elif value.startswith(BASEDIR): + return value.replace(BASEDIR, '/usr/lib/python/site-packages') + elif BASEDIR in value: + return value.replace(BASEDIR, '') + elif value == _get_my_ip(): return '10.0.0.1' - elif s == socket.gethostname(): + elif value == socket.gethostname() and 'host' in name: return 'oslo' - elif s.strip() != s: - return '"%s"' % s - return s + elif value.strip() != value: + return '"%s"' % value + return value def _print_opt(opt): @@ -219,7 +219,8 @@ def _print_opt(opt): print('#%s=' % opt_name) elif opt_type == STROPT: assert(isinstance(opt_default, basestring)) - print('#%s=%s' % (opt_name, _sanitize_default(opt_default))) + print('#%s=%s' % (opt_name, _sanitize_default(opt_name, + opt_default))) elif opt_type == BOOLOPT: assert(isinstance(opt_default, bool)) print('#%s=%s' % (opt_name, str(opt_default).lower())) -- cgit