Don't allow nodes or groups named localhost

Story: 2008748
Change-Id: I6cf9fc60884150a76be5ebeb111f7a932dcf6eac
This commit is contained in:
Albin Vass 2021-03-24 20:43:33 +01:00 committed by James E. Blair
parent ad7bd9c6f2
commit 449e45a9da
2 changed files with 48 additions and 0 deletions

View File

@ -113,6 +113,50 @@ class TestTenantSimple(TenantParserTestCase):
{'registry': 'registry.example.org', 'image_name': 'foo'},
])
def test_deny_localhost_nodeset(self):
in_repo_conf = textwrap.dedent(
"""
- nodeset:
name: localhost
nodes:
- name: localhost
label: ubuntu
""")
file_dict = {'zuul.yaml': in_repo_conf}
A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
files=file_dict)
self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
self.waitUntilSettled()
# No job should have run due to the change introducing a config error
self.assertHistory([])
self.assertTrue(A.reported)
self.assertTrue("Nodes named 'localhost' are not allowed."
in A.messages[0])
in_repo_conf = textwrap.dedent(
"""
- nodeset:
name: localhost-group
nodes:
- name: ubuntu
label: ubuntu
groups:
- name: localhost
nodes: ubuntu
""")
file_dict = {'zuul.yaml': in_repo_conf}
B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
files=file_dict)
self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
self.waitUntilSettled()
# No job should have run due to the change introducing a config error
self.assertHistory([])
self.assertTrue(B.reported)
self.assertTrue("Groups named 'localhost' are not allowed."
in B.messages[0])
class TestTenantOverride(TenantParserTestCase):
tenant_config_file = 'config/tenant-parser/override.yaml'

View File

@ -497,6 +497,8 @@ class NodeSetParser(object):
allowed_labels=allowed_labels,
disallowed_labels=disallowed_labels)
for conf_node in as_list(conf['nodes']):
if "localhost" in as_list(conf_node['name']):
raise Exception("Nodes named 'localhost' are not allowed.")
for name in as_list(conf_node['name']):
if name in node_names:
raise DuplicateNodeError(name, conf_node['name'])
@ -505,6 +507,8 @@ class NodeSetParser(object):
for name in as_list(conf_node['name']):
node_names.add(name)
for conf_group in as_list(conf.get('groups', [])):
if "localhost" in conf_group['name']:
raise Exception("Groups named 'localhost' are not allowed.")
for node_name in as_list(conf_group['nodes']):
if node_name not in node_names:
raise NodeFromGroupNotFoundError(conf['name'], node_name,