Skip to content

Commit

Permalink
Ensures system_info is written in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lorengordon committed Dec 8, 2022
1 parent 50fa134 commit 3292499
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions PostBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,51 +130,43 @@ function CreateFstab {

# Configure cloud-init
function ConfigureCloudInit {
local CLOUDCFG
local CLINITUSR

CLOUDCFG="${CHROOTMNT}/etc/cloud/cloud.cfg"
CLINITUSR=$( grep -E "name: (maintuser|centos|ec2-user|cloud-user)" \
"${CLOUDCFG}" | awk '{print $2}')

# Reset key parms in standard cloud.cfg file
if [ "${CLINITUSR}" = "" ]
then
err_exit "Astandard cloud-init file: can't reset default-user config"
else
# Ensure passwords *can* be used with SSH
err_exit "Allow password logins to SSH..." NONE
sed -i -e '/^ssh_pwauth/s/0$/1/' "${CLOUDCFG}" || \
err_exit "Failed allowing password logins"

# Delete current "system_info:" block
err_exit "Nuking standard system_info block..." NONE
sed -i '/^system_info/,/^ ssh_svcname/d' "${CLOUDCFG}" || \
err_exit "Failed to nuke standard system_info block"

# Replace deleted "system_info:" block
(
printf "system_info:\n"
printf " default_user:\n"
printf " name: '%s'\n" "${MAINTUSR}"
printf " lock_passwd: true\n"
printf " gecos: Local Maintenance User\n"
printf " groups: [wheel, adm]\n"
printf " sudo: [ 'ALL=(root) NOPASSWD:ALL' ]\n"
printf " shell: /bin/bash\n"
printf " selinux_user: unconfined_u\n"
printf " distro: rhel\n"
printf " paths:\n"
printf " cloud_dir: /var/lib/cloud\n"
printf " templates_dir: /etc/cloud/templates\n"
printf " ssh_svcname: sshd\n"
) >> "${CLOUDCFG}"

# Update NS-Switch map-file for SEL-enabled environment
err_exit "Enabling SEL lookups by nsswitch..." NONE
printf "%-12s %s\n" sudoers: files >> "${CHROOTMNT}/etc/nsswitch.conf" || \
err_exit "Failed enabling SEL lookups by nsswitch"
fi
local CLOUDCFG

CLOUDCFG="${CHROOTMNT}/etc/cloud/cloud.cfg"

# Ensure passwords *can* be used with SSH
err_exit "Allow password logins to SSH..." NONE
sed -i -e '/^ssh_pwauth/s/0$/1/' "${CLOUDCFG}" || \
err_exit "Failed allowing password logins"

# Delete current "system_info:" block
err_exit "Nuking standard system_info block..." NONE
sed -i '/^system_info/,/^$/d' "${CLOUDCFG}" || \
err_exit "Failed to nuke standard system_info block"

# Replace deleted "system_info:" block
(
printf "\n"
printf "system_info:\n"
printf " default_user:\n"
printf " name: '%s'\n" "${MAINTUSR}"
printf " lock_passwd: true\n"
printf " gecos: Local Maintenance User\n"
printf " groups: [wheel, adm]\n"
printf " sudo: [ 'ALL=(root) NOPASSWD:ALL' ]\n"
printf " shell: /bin/bash\n"
printf " selinux_user: unconfined_u\n"
printf " distro: rhel\n"
printf " paths:\n"
printf " cloud_dir: /var/lib/cloud\n"
printf " templates_dir: /etc/cloud/templates\n"
printf " ssh_svcname: sshd\n"
) >> "${CLOUDCFG}"

# Update NS-Switch map-file for SEL-enabled environment
err_exit "Enabling SEL lookups by nsswitch..." NONE
printf "%-12s %s\n" sudoers: files >> "${CHROOTMNT}/etc/nsswitch.conf" || \
err_exit "Failed enabling SEL lookups by nsswitch"

}

Expand Down

0 comments on commit 3292499

Please sign in to comment.