From b7281cf2c584cde8f516ba206d90024af03236dd Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Mon, 1 Sep 2014 11:22:53 -0700 Subject: [PATCH] make the bind_port config setting required In a long-term effort to change the recommended ports for Swift, the first step is to require the bind_port in config files. Later, we can change the recommended setting. Anyone currently explicitly setting the ports will not be affected. Anyone not setting the ports will need to specify them to match their rings. DocImpact Change-Id: Icca83a263acdd0afc9016424a3e9f8c15e944789 --- bin/swift-account-server | 3 +-- bin/swift-container-server | 3 +-- bin/swift-object-server | 2 +- bin/swift-proxy-server | 2 +- etc/account-server.conf-sample | 2 +- etc/container-server.conf-sample | 2 +- etc/object-server.conf-sample | 2 +- etc/proxy-server.conf-sample | 2 +- swift/common/wsgi.py | 23 ++++++++++++++++++----- test/unit/common/test_wsgi.py | 18 ++++++++++++++---- 10 files changed, 40 insertions(+), 19 deletions(-) diff --git a/bin/swift-account-server b/bin/swift-account-server index a1f48e16d3..a2deb69f6e 100755 --- a/bin/swift-account-server +++ b/bin/swift-account-server @@ -20,5 +20,4 @@ from swift.common.wsgi import run_wsgi if __name__ == '__main__': conf_file, options = parse_options() - sys.exit(run_wsgi(conf_file, - 'account-server', default_port=6002, **options)) + sys.exit(run_wsgi(conf_file, 'account-server', **options)) diff --git a/bin/swift-container-server b/bin/swift-container-server index ffd6840f94..cab084dd57 100755 --- a/bin/swift-container-server +++ b/bin/swift-container-server @@ -20,5 +20,4 @@ from swift.common.wsgi import run_wsgi if __name__ == '__main__': conf_file, options = parse_options() - sys.exit(run_wsgi(conf_file, - 'container-server', default_port=6001, **options)) + sys.exit(run_wsgi(conf_file, 'container-server', **options)) diff --git a/bin/swift-object-server b/bin/swift-object-server index a3306b2d8a..d3c134a919 100755 --- a/bin/swift-object-server +++ b/bin/swift-object-server @@ -22,6 +22,6 @@ from swift.obj import server if __name__ == '__main__': conf_file, options = parse_options() - sys.exit(run_wsgi(conf_file, 'object-server', default_port=6000, + sys.exit(run_wsgi(conf_file, 'object-server', global_conf_callback=server.global_conf_callback, **options)) diff --git a/bin/swift-proxy-server b/bin/swift-proxy-server index c6846bcf3e..4d61a0bcd7 100755 --- a/bin/swift-proxy-server +++ b/bin/swift-proxy-server @@ -20,4 +20,4 @@ from swift.common.wsgi import run_wsgi if __name__ == '__main__': conf_file, options = parse_options() - sys.exit(run_wsgi(conf_file, 'proxy-server', default_port=8080, **options)) + sys.exit(run_wsgi(conf_file, 'proxy-server', **options)) diff --git a/etc/account-server.conf-sample b/etc/account-server.conf-sample index 5b03886921..97d6eabca3 100644 --- a/etc/account-server.conf-sample +++ b/etc/account-server.conf-sample @@ -1,6 +1,6 @@ [DEFAULT] # bind_ip = 0.0.0.0 -# bind_port = 6002 +bind_port = 6002 # bind_timeout = 30 # backlog = 4096 # user = swift diff --git a/etc/container-server.conf-sample b/etc/container-server.conf-sample index e47b2287a4..6ded6d6dbf 100644 --- a/etc/container-server.conf-sample +++ b/etc/container-server.conf-sample @@ -1,6 +1,6 @@ [DEFAULT] # bind_ip = 0.0.0.0 -# bind_port = 6001 +bind_port = 6001 # bind_timeout = 30 # backlog = 4096 # user = swift diff --git a/etc/object-server.conf-sample b/etc/object-server.conf-sample index 5079e36ba2..678f2cfae1 100644 --- a/etc/object-server.conf-sample +++ b/etc/object-server.conf-sample @@ -1,6 +1,6 @@ [DEFAULT] # bind_ip = 0.0.0.0 -# bind_port = 6000 +bind_port = 6000 # bind_timeout = 30 # backlog = 4096 # user = swift diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index 12f4eac57e..a2d6660011 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -1,6 +1,6 @@ [DEFAULT] # bind_ip = 0.0.0.0 -# bind_port = 80 +bind_port = 8080 # bind_timeout = 30 # backlog = 4096 # swift_dir = /etc/swift diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 35a00990e0..5291746e6b 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -139,17 +139,19 @@ def monkey_patch_mimetools(): mimetools.Message.parsetype = parsetype -def get_socket(conf, default_port=8080): +def get_socket(conf): """Bind socket to bind ip:port in conf :param conf: Configuration dict to read settings from - :param default_port: port to use if not specified in conf :returns : a socket object as returned from socket.listen or ssl.wrap_socket if conf specifies cert_file """ - bind_addr = (conf.get('bind_ip', '0.0.0.0'), - int(conf.get('bind_port', default_port))) + try: + bind_port = int(conf['bind_port']) + except (ValueError, KeyError, TypeError): + raise ConfigFilePortError() + bind_addr = (conf.get('bind_ip', '0.0.0.0'), bind_port) address_family = [addr[0] for addr in socket.getaddrinfo( bind_addr[0], bind_addr[1], socket.AF_UNSPEC, socket.SOCK_STREAM) if addr[0] in (socket.AF_INET, socket.AF_INET6)][0] @@ -421,7 +423,14 @@ def run_wsgi(conf_path, app_section, *args, **kwargs): return 1 # bind to address and port - sock = get_socket(conf, default_port=kwargs.get('default_port', 8080)) + try: + sock = get_socket(conf) + except ConfigFilePortError: + msg = 'bind_port wasn\'t properly set in the config file. ' \ + 'It must be explicitly set to a valid port number.' + logger.error(msg) + print(msg) + return 1 # remaining tasks should not require elevated privileges drop_privileges(conf.get('user', 'swift')) @@ -495,6 +504,10 @@ class ConfigFileError(Exception): pass +class ConfigFilePortError(ConfigFileError): + pass + + def _initrp(conf_path, app_section, *args, **kwargs): try: conf = appconfig(conf_path, name=app_section) diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index c2f0a3eb56..aa10264898 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -200,13 +200,23 @@ class TestWSGI(unittest.TestCase): logger.info('testing') self.assertEquals('proxy-server', log_name) + def test_get_socket_bad_values(self): + # first try with no port set + self.assertRaises(wsgi.ConfigFilePortError, wsgi.get_socket, {}) + # next try with a bad port value set + self.assertRaises(wsgi.ConfigFilePortError, wsgi.get_socket, + {'bind_port': 'abc'}) + self.assertRaises(wsgi.ConfigFilePortError, wsgi.get_socket, + {'bind_port': None}) + def test_get_socket(self): # stubs - conf = {} - ssl_conf = { + conf = {'bind_port': 54321} + ssl_conf = conf.copy() + ssl_conf.update({ 'cert_file': '', 'key_file': '', - } + }) # mocks class MockSocket(object): @@ -263,7 +273,7 @@ class TestWSGI(unittest.TestCase): def test_address_in_use(self): # stubs - conf = {} + conf = {'bind_port': 54321} # mocks def mock_listen(*args, **kwargs):