Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USHIFT-1545: Full VM name validation #3672

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions test/bin/scenario.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,48 @@ full_vm_name() {
echo "${SCENARIO//@/-}-${base}"
}

# hostname validation
# based on https://github.com/rhinstaller/anaconda/blob/c95142f76735a2e9ae6d845f8569d46632ddd619/pyanaconda/network.py#L96-L120
validate_full_vm_name() {
local vm_name="$1"

if [ -z "${vm_name}" ]; then
error "VM hostname cannot be empty string"
record_junit "${vmname}" "vm_name_validation" "FAILED"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar thing in other places

Suggested change
record_junit "${vmname}" "vm_name_validation" "FAILED"
record_junit "${vm_name}" "vm_name_validation" "FAILED"

exit 1
fi

if [ ${#vm_name} -gt 64 ]; then
error "VM hostname is too long"
record_junit "${vmname}" "vm_name_validation" "FAILED"
exit 1
fi

if [[ $vm_name =~ ^\. ]]; then
error "VM hostname cannot start with '.'"
record_junit "${vmname}" "vm_name_validation" "FAILED"
exit 1
fi

# split by '.' and check every section separately
IFS='.' read -ra ARR <<< "$vm_name"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we validate $vm_hostname instead of $full_vmname, then we can remove this loop and flatten and merge the checks.

for i in "${ARR[@]}"; do
if [ ${#i} -gt 63 ]; then
error "VM hostname is invalid"
record_junit "${vmname}" "vm_name_validation" "FAILED"
exit 1
fi

if ! echo "$i" | grep -E '^([a-zA-Z0-9]+-*[a-zA-Z0-9]+)+$|^[a-zA-Z0-9]+$' > /dev/null; then
error "VM hostname is invalid"
record_junit "${vmname}" "vm_name_validation" "FAILED"
exit 1
fi
done

record_junit "${vmname}" "vm_name_validation" "OK"
}

vm_property_filename() {
local -r vmname="$1"
local -r property="$2"
Expand Down Expand Up @@ -227,6 +269,8 @@ prepare_kickstart() {
local -r vm_hostname="${full_vmname/./-}"
local -r hostname=$(hostname)

validate_full_vm_name $full_vmname
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to validate vm_hostname since it'll be used for hostname.

Suggested change
validate_full_vm_name $full_vmname
validate_full_vm_name "${vm_hostname}"


echo "Preparing kickstart file ${template} at ${output_dir}"
if [ ! -f "${KICKSTART_TEMPLATE_DIR}/${template}" ]; then
error "No ${template} in ${KICKSTART_TEMPLATE_DIR}"
Expand Down