Merge pull request #7738 from osirisinferi/nginx-hostname

[nginx] Parse $hostname in `server_name`
This commit is contained in:
schoen
2020-02-06 14:44:03 -08:00
committed by GitHub
5 changed files with 22 additions and 10 deletions
@@ -313,9 +313,6 @@ class NginxConfigurator(common.Installer):
.. todo:: This should maybe return list if no obvious answer .. todo:: This should maybe return list if no obvious answer
is presented. is presented.
.. todo:: The special name "$hostname" corresponds to the machine's
hostname. Currently we just ignore this.
:param str target_name: domain name :param str target_name: domain name
:param bool create_if_no_match: If we should create a new vhost from default :param bool create_if_no_match: If we should create a new vhost from default
when there is no match found. If we can't choose a default, raise a when there is no match found. If we can't choose a default, raise a
@@ -598,6 +595,12 @@ class NginxConfigurator(common.Installer):
all_names = set() # type: Set[str] all_names = set() # type: Set[str]
for vhost in self.parser.get_vhosts(): for vhost in self.parser.get_vhosts():
try:
vhost.names.remove("$hostname")
vhost.names.add(socket.gethostname())
except KeyError:
pass
all_names.update(vhost.names) all_names.update(vhost.names)
for addr in vhost.addrs: for addr in vhost.addrs:
+6 -4
View File
@@ -36,7 +36,7 @@ class NginxConfiguratorTest(util.NginxTest):
def test_prepare(self): def test_prepare(self):
self.assertEqual((1, 6, 2), self.config.version) self.assertEqual((1, 6, 2), self.config.version)
self.assertEqual(11, len(self.config.parser.parsed)) self.assertEqual(12, len(self.config.parser.parsed))
@mock.patch("certbot_nginx._internal.configurator.util.exe_exists") @mock.patch("certbot_nginx._internal.configurator.util.exe_exists")
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen") @mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
@@ -76,15 +76,17 @@ class NginxConfiguratorTest(util.NginxTest):
else: # pragma: no cover else: # pragma: no cover
self.fail("Exception wasn't raised!") self.fail("Exception wasn't raised!")
@mock.patch("certbot_nginx._internal.configurator.socket.gethostname")
@mock.patch("certbot_nginx._internal.configurator.socket.gethostbyaddr") @mock.patch("certbot_nginx._internal.configurator.socket.gethostbyaddr")
def test_get_all_names(self, mock_gethostbyaddr): def test_get_all_names(self, mock_gethostbyaddr, mock_gethostname):
mock_gethostbyaddr.return_value = ('155.225.50.69.nephoscale.net', [], []) mock_gethostbyaddr.return_value = ('155.225.50.69.nephoscale.net', [], [])
mock_gethostname.return_value = ('example.net')
names = self.config.get_all_names() names = self.config.get_all_names()
self.assertEqual(names, { self.assertEqual(names, {
"155.225.50.69.nephoscale.net", "www.example.org", "another.alias", "155.225.50.69.nephoscale.net", "www.example.org", "another.alias",
"migration.com", "summer.com", "geese.com", "sslon.com", "migration.com", "summer.com", "geese.com", "sslon.com",
"globalssl.com", "globalsslsetssl.com", "ipv6.com", "ipv6ssl.com", "globalssl.com", "globalsslsetssl.com", "ipv6.com", "ipv6ssl.com",
"headers.com"}) "headers.com", "example.net"})
def test_supported_enhancements(self): def test_supported_enhancements(self):
self.assertEqual(['redirect', 'ensure-http-header', 'staple-ocsp'], self.assertEqual(['redirect', 'ensure-http-header', 'staple-ocsp'],
@@ -924,7 +926,7 @@ class NginxConfiguratorTest(util.NginxTest):
prefer_ssl=False, prefer_ssl=False,
no_ssl_filter_port='80') no_ssl_filter_port='80')
# Check that the dialog was called with only port 80 vhosts # Check that the dialog was called with only port 80 vhosts
self.assertEqual(len(mock_select_vhs.call_args[0][0]), 5) self.assertEqual(len(mock_select_vhs.call_args[0][0]), 6)
class InstallSslOptionsConfTest(util.NginxTest): class InstallSslOptionsConfTest(util.NginxTest):
+4 -3
View File
@@ -58,7 +58,8 @@ class NginxParserTest(util.NginxTest):
'sites-enabled/sslon.com', 'sites-enabled/sslon.com',
'sites-enabled/globalssl.com', 'sites-enabled/globalssl.com',
'sites-enabled/ipv6.com', 'sites-enabled/ipv6.com',
'sites-enabled/ipv6ssl.com']}, 'sites-enabled/ipv6ssl.com',
'sites-enabled/example.net']},
set(nparser.parsed.keys())) set(nparser.parsed.keys()))
self.assertEqual([['server_name', 'somename', 'alias', 'another.alias']], self.assertEqual([['server_name', 'somename', 'alias', 'another.alias']],
nparser.parsed[nparser.abs_path('server.conf')]) nparser.parsed[nparser.abs_path('server.conf')])
@@ -88,7 +89,7 @@ class NginxParserTest(util.NginxTest):
parsed = nparser._parse_files(nparser.abs_path( parsed = nparser._parse_files(nparser.abs_path(
'sites-enabled/example.com.test')) 'sites-enabled/example.com.test'))
self.assertEqual(3, len(glob.glob(nparser.abs_path('*.test')))) self.assertEqual(3, len(glob.glob(nparser.abs_path('*.test'))))
self.assertEqual(8, len( self.assertEqual(9, len(
glob.glob(nparser.abs_path('sites-enabled/*.test')))) glob.glob(nparser.abs_path('sites-enabled/*.test'))))
self.assertEqual([[['server'], [['listen', '69.50.225.155:9000'], self.assertEqual([[['server'], [['listen', '69.50.225.155:9000'],
['listen', '127.0.0.1'], ['listen', '127.0.0.1'],
@@ -171,7 +172,7 @@ class NginxParserTest(util.NginxTest):
'*.www.example.com']), '*.www.example.com']),
[], [2, 1, 0]) [], [2, 1, 0])
self.assertEqual(13, len(vhosts)) self.assertEqual(14, len(vhosts))
example_com = [x for x in vhosts if 'example.com' in x.filep][0] example_com = [x for x in vhosts if 'example.com' in x.filep][0]
self.assertEqual(vhost3, example_com) self.assertEqual(vhost3, example_com)
default = [x for x in vhosts if 'default' in x.filep][0] default = [x for x in vhosts if 'default' in x.filep][0]
@@ -0,0 +1,5 @@
server {
listen 80;
listen [::]:80;
server_name $hostname;
}
+1
View File
@@ -24,6 +24,7 @@ More details about these changes can be found on our GitHub repo.
### Added ### Added
* Added support for Cloudflare's limited-scope API Tokens * Added support for Cloudflare's limited-scope API Tokens
* Added support for `$hostname` in nginx `server_name` directive
### Changed ### Changed