Fix KeyError raised when max hostname length exceeded

The function osa_toolkit.generate._add_container_hosts contains a bug in
the code used to check if provided hostnames have exceeded their max
allowed length. The logic used to perform this check depends on the
`is_metal` flag within each container's properties. Unfortunately the
`is_metal` flag is accessed within the `properties` dictionary using
bracket notation rather than the safer `dict.get` method, causing a
`KeyError` to be raised when a host's properties dictionary does not
contain the `is_metal` flag.

It is not expected that a `KeyError` would be raised in the function if
hostnames have exceeded their max length. It is instead expected that a
`SystemExit` exception would be raised warning the user of their invalid
hostname(s).

This bug will impacts deployments where hostnames actually do exceed the
max allowed length due to the short circuit logic used in the if-elif
tree.

Closes-Bug: #1886905

Change-Id: Ic1acfea71f27f94e277aa443f0a53ef16b4eb417
This commit is contained in:
Ryan Drew 2020-07-09 12:01:00 -06:00
parent 8760541ced
commit d2657174ce
No known key found for this signature in database
GPG Key ID: CD468642C1EC0A48
1 changed files with 3 additions and 3 deletions

View File

@ -370,8 +370,8 @@ def _add_container_hosts(assignment, config, container_name, container_type,
type_and_name = '{}_{}'.format(host_type, container_name)
logger.debug("Generated container name %s", type_and_name)
max_hostname_len = 52
if len(type_and_name) > max_hostname_len and \
not properties['is_metal']:
is_metal = properties.get('is_metal', False)
if len(type_and_name) > max_hostname_len and not is_metal:
raise SystemExit(
'The resulting combination of [ "{}" + "{}" ] is longer than'
' {} characters. This combination will result in a container'
@ -383,7 +383,7 @@ def _add_container_hosts(assignment, config, container_name, container_type,
host_type, container_name, max_hostname_len
)
)
elif len(host_type) > 63 and properties['is_metal']:
elif len(host_type) > 63 and is_metal:
raise SystemExit(
'The resulting hostname "{0}" is longer than 63 characters.'
' This combination may result in a name that is longer than'