Update host record validation during bootstrap

Ansible bootstrap playbook is updated to check and fail, if duplicate
DNS host records or invalid hostnames are specified in the
`user_dns_host_records` parameter.

Closes-bug: 2051553

Test Plan:
PASS: Verify bootstrap playbook with duplicate host records
PASS: Verify bootstrap playbook with valid & invalid hostname

Change-Id: Icdfa1bb2b1866020964ef22efe5fbced10618ff8
Signed-off-by: Joseph Vazhappilly <joseph.vazhappillypaily@windriver.com>
This commit is contained in:
Joseph Vazhappilly
2024-01-29 08:23:00 -05:00
parent e18ad6ac16
commit 57ba971e6d
2 changed files with 25 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python
#
# Copyright (c) 2023 Wind River Systems, Inc.
# Copyright (c) 2023-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -9,10 +9,23 @@
import sys
import yaml
from sysinv.common.utils import is_valid_dns_hostname
from sysinv.common.utils import is_valid_ipv4
from sysinv.common.utils import is_valid_ipv6
def check_duplicate_host_records(user_host_data):
host_record_list = []
for host, value in user_host_data.items():
value_set = set(x.strip() for x in value.split(','))
if value_set not in host_record_list:
host_record_list.append(value_set)
else:
msg = "User dns host-records has duplicate: %s" % (value)
raise ValueError(msg)
def parse_user_dns_host_records(user_host_data):
for host, values in user_host_data.items():
@@ -25,12 +38,16 @@ def parse_user_dns_host_records(user_host_data):
ipv4 = value
elif is_valid_ipv6(value):
ipv6 = value
else:
elif is_valid_dns_hostname(value if '.' in value else value + '.dummy'):
# If hostname does not contain Top-Level Domain (TLD),
# append 'dummy' as TLD for the input of is_valid_dns_hostname()
domain_names.append(value)
else:
raise ValueError("Invalid DNS domain name: %s" % (value))
if not domain_names or not (ipv4 or ipv6):
raise ValueError("""User dns host-records has either null ip address
or domain name for host: """ + host)
msg = "User dns host-records has invalid format: {%s: %s}" % (host, values)
raise ValueError(msg)
if ipv4:
ipv4_entry = " ".join([ipv4] + [str(domain_name) for domain_name in domain_names])
print(ipv4_entry)
@@ -44,6 +61,7 @@ if __name__ == '__main__':
user_host_data_yaml = sys.argv[1]
user_host_data = yaml.safe_load(user_host_data_yaml)
try:
check_duplicate_host_records(user_host_data)
parse_user_dns_host_records(user_host_data)
except ValueError as e:
print(f"An error occurred during parsing user dns host-records: {str(e)}")

View File

@@ -681,21 +681,7 @@
- set_fact:
user_dns_host_records_lines: ""
- name: Fetch the user dns host records for validation
set_fact:
host_records_values: "{{ host_records_values | default([]) +
user_dns_host_records[host_record].split(',') | map('regex_replace', '^\\s*|\\s*$', '') | list }}"
loop: "{{ user_dns_host_records.keys() }}"
loop_control:
loop_var: host_record
- name: Check for valid IP address and DNS name in user dns host records
loop: "{{ host_records_values }}"
loop_control:
loop_var: host_records_value
include: validate_address.yml input_address={{ host_records_value }}
- name: Create entries for /etc/hosts from user dns host records
- name: Validate and create entries for /etc/hosts from user dns host records
script: parse_user_dns_host_record.py "{{ user_dns_host_records }}"
register: result
failed_when: false
@@ -703,13 +689,8 @@
- name: Fail if script parse_user_dns_host_record.py failed or error occurred
fail:
msg: >
{% if result.rc != 0 %}
Error during parsing user dns host-records, failed with return code
{{ result.rc }}.
{% elif "An error occurred" in result.stdout %}
User dns host-records parsing encountered null value, please update the
dns host-records with valid inputs. Error output: {{ result.stderr }}
{% endif %}
Error parsing user dns host-records, return code: {{ result.rc }}
Error output: {{ result.stderr }}
when: result.rc != 0