本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Image Builder 的安全最佳实践
EC2 Image Builder 提供了在您开发和实施自己的安全策略时需要考虑的大量安全功能。以下最佳实践是一般指导原则,并不代表完整安全解决方案。这些最佳实践可能不适合环境或不满足环境要求,请将其视为有用的考虑因素而不是惯例。
-
不要在 Image Builder 配方中使用过于宽松的安全组。
-
不要与您不信任的账户共享镜像。
-
不要公开包含私有或敏感数据的镜像。
-
在镜像生成期间应用所有可用的 Windows 或 Linux 安全补丁。
-
定期将托管 AMI 更新应用于您的 macOS 配方,并创建新映像以启动安装了最新安全补丁的实例。
我们强烈建议您测试映像,以验证安全状况和适用的安全合规性级别。Amazon Inspector 等此类解决方案可以帮助验证映像的安全和合规性状况。
适用于 Image Builder 管道的 IMDSv2
当您的 Image Builder 管道运行时,它会发送 HTTP 请求以启动 EC2 实例,Image Builder 使用这些实例来构建和测试您的映像。要配置您的管道用于启动请求的 IMDS 版本,请在 Image Builder 基础架构配置实例元数据设置中设置 httpTokens 参数。
我们建议,将 Image Builder 从管道版本中启动的所有 EC2 实例配置为使用 Imdsv2,这样实例元数据检索请求将需要签名令牌标头。
有关 Image Builder 基础设施配置的更多信息,请参阅 管理 Image Builder 基础设施配置 有关 Linux 映像的 EC2 实例元数据选项的更多信息,请参阅《Amazon EC2 用户指南》中的配置实例元数据服务选项。有关 Windows 映像,请参阅《Amazon EC2 用户指南》中的配置实例元数据服务选项。
需要在构建后进行清理
Image Builder 完成自定义映像的所有构建步骤后,Image Builder 会为测试和映像创建准备构建实例。在关闭构建实例以创建快照之前,Image Builder 会执行以下清理操作以确保映像的安全:
- Linux
-
Image Builder 管道运行清理脚本,以帮助确保最终映像遵循安全最佳实践,并删除任何不应延续到快照中的构建构件或设置。但是,您可以跳过脚本的各个部分,或者完全覆盖用户数据。因此,Image Builder 管道生成的映像不一定符合任何特定的监管标准。
当管道完成其构建期和测试期后,Image Builder 会在创建输出映像之前自动运行以下清理脚本。
如果您在配方中覆盖用户数据,则脚本将无法运行。在这种情况下,请确保在用户数据中包含一个用于创建名为 perform_cleanup 的空文件的命令。Image Builder 会检测到此文件并在创建新映像之前运行清理脚本。
文件清理完成后,Image Builder 会在脚本中添加卸载系统管理器代理和删除cronie软件包的步骤。Image Builder 会跟踪它在构建期间在/tmp/imagebuilder_service服务工作目录中安装的内容。图像生成器使用这些标记来决定要移除的内容:
-
Systems Manager 代理 — Image Builder 是否卸载代理取决于您的图像配方中的systemsManagerAgent.uninstallAfterBuild设置以及 Image Builder 是否安装了代理。有关更多信息,请参阅中的uninstallAfterBuild设置使用创建图像配方 AWS CLI。
-
cronie软件包 — 此步骤适用于亚马逊 Linux 1、亚马逊 Linux 2 和亚马逊 Linux 2023。如果映像生成器在构建期间安装了 crontab,它会在服务工作crontab_installed目录中记录一个标记。然后,映像生成器会在清理过程中移除该cronie软件包。要保留cronie最终映像,请在清理运行之前通过向组件或用户数据添加以下命令来删除标记文件:
rm -f /tmp/imagebuilder_service/crontab_installed
#!/bin/bash
if [[ ! -f {{workingDirectory}}/perform_cleanup ]]; then
echo "Skipping cleanup"
exit 0
else
sudo rm -f {{workingDirectory}}/perform_cleanup
fi
function cleanup() {
FILES=("$@")
for FILE in "${FILES[@]}"; do
if [[ -f "$FILE" ]]; then
echo "Deleting $FILE";
sudo shred -zuf $FILE;
fi;
if [[ -f $FILE ]]; then
echo "Failed to delete '$FILE'. Failing."
exit 1
fi;
done
};
# Clean up for cloud-init files
CLOUD_INIT_FILES=(
"/etc/sudoers.d/90-cloud-init-users"
"/etc/locale.conf"
"/var/log/cloud-init.log"
"/var/log/cloud-init-output.log"
)
if [[ -f {{workingDirectory}}/skip_cleanup_cloudinit_files ]]; then
echo "Skipping cleanup of cloud init files"
else
echo "Cleaning up cloud init files"
cleanup "${CLOUD_INIT_FILES[@]}"
if [[ -d "/var/lib/cloud" ]]; then
if [[ $( sudo find /var/lib/cloud -type f | sudo wc -l ) -gt 0 ]]; then
echo "Deleting files within /var/lib/cloud/*"
sudo find /var/lib/cloud -type f -exec shred -zuf {} \;
fi;
if [[ $( sudo ls /var/lib/cloud | sudo wc -l ) -gt 0 ]]; then
echo "Deleting /var/lib/cloud/*"
sudo rm -rf /var/lib/cloud/* || true
fi;
fi;
fi;
# Clean up for temporary instance files
INSTANCE_FILES=(
"/etc/.updated"
"/etc/aliases.db"
"/etc/hostname"
"/var/lib/misc/postfix.aliasesdb-stamp"
"/var/lib/postfix/master.lock"
"/var/spool/postfix/pid/master.pid"
"/var/.updated"
"/var/cache/yum/x86_64/2/.gpgkeyschecked.yum"
)
if [[ -f {{workingDirectory}}/skip_cleanup_instance_files ]]; then
echo "Skipping cleanup of instance files"
else
echo "Cleaning up instance files"
cleanup "${INSTANCE_FILES[@]}"
fi;
# Clean up for ssh files
SSH_FILES=(
"/etc/ssh/ssh_host_rsa_key"
"/etc/ssh/ssh_host_rsa_key.pub"
"/etc/ssh/ssh_host_ecdsa_key"
"/etc/ssh/ssh_host_ecdsa_key.pub"
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
"/root/.ssh/authorized_keys"
)
if [[ -f {{workingDirectory}}/skip_cleanup_ssh_files ]]; then
echo "Skipping cleanup of ssh files"
else
echo "Cleaning up ssh files"
cleanup "${SSH_FILES[@]}"
USERS=$(ls /home/)
for user in $USERS; do
echo Deleting /home/"$user"/.ssh/authorized_keys;
sudo find /home/"$user"/.ssh/authorized_keys -type f -exec shred -zuf {} \;
done
for user in $USERS; do
if [[ -f /home/"$user"/.ssh/authorized_keys ]]; then
echo Failed to delete /home/"$user"/.ssh/authorized_keys;
exit 1
fi;
done;
fi;
# Clean up for instance log files
INSTANCE_LOG_FILES=(
"/var/log/audit/audit.log"
"/var/log/boot.log"
"/var/log/dmesg"
"/var/log/cron"
)
if [[ -f {{workingDirectory}}/skip_cleanup_instance_log_files ]]; then
echo "Skipping cleanup of instance log files"
else
echo "Cleaning up instance log files"
cleanup "${INSTANCE_LOG_FILES[@]}"
fi;
# Clean up for TOE files
if [[ -f {{workingDirectory}}/skip_cleanup_toe_files ]]; then
echo "Skipping cleanup of TOE files"
else
echo "Cleaning TOE files"
shopt -s nullglob
TOE_MATCHES=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_MATCHES[@]} -gt 0 ]]; then
if [[ $( sudo find "${TOE_MATCHES[@]}" -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within {{workingDirectory}}/TOE_*"
sudo find "${TOE_MATCHES[@]}" -type f -exec shred -zuf {} \;
fi
shopt -s nullglob
TOE_REMAINING=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_REMAINING[@]} -gt 0 ]]; then
if [[ $( sudo find "${TOE_REMAINING[@]}" -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete {{workingDirectory}}/TOE_*"
exit 1
fi
echo "Deleting {{workingDirectory}}/TOE_*"
sudo rm -rf "${TOE_REMAINING[@]}"
fi
shopt -s nullglob
TOE_FINAL=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_FINAL[@]} -gt 0 ]]; then
echo "Failed to delete {{workingDirectory}}/TOE_*"
exit 1
fi
fi
fi
# Clean up for ssm log files
if [[ -f {{workingDirectory}}/skip_cleanup_ssm_log_files ]]; then
echo "Skipping cleanup of ssm log files"
else
echo "Cleaning up ssm log files"
if [[ -d "/var/log/amazon/ssm" ]]; then
if [[ $( sudo find /var/log/amazon/ssm -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within /var/log/amazon/ssm/*"
sudo find /var/log/amazon/ssm -type f -exec shred -zuf {} \;
fi
if [[ $( sudo find /var/log/amazon/ssm -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete /var/log/amazon/ssm"
exit 1
fi
echo "Deleting /var/log/amazon/ssm/*"
sudo rm -rf /var/log/amazon/ssm
if [[ -d "/var/log/amazon/ssm" ]]; then
echo "Failed to delete /var/log/amazon/ssm"
exit 1
fi
fi
fi
shopt -s nullglob
SA_MATCHES=(/var/log/sa/sa*)
shopt -u nullglob
if [[ ${#SA_MATCHES[@]} -gt 0 ]]; then
echo "Deleting /var/log/sa/sa*"
sudo shred -zuf "${SA_MATCHES[@]}"
shopt -s nullglob
SA_REMAINING=(/var/log/sa/sa*)
shopt -u nullglob
if [[ ${#SA_REMAINING[@]} -gt 0 ]]; then
echo "Failed to delete /var/log/sa/sa*"
exit 1
fi
fi
shopt -s nullglob
DHCLIENT_MATCHES=(/var/lib/dhclient/dhclient*.lease)
shopt -u nullglob
if [[ ${#DHCLIENT_MATCHES[@]} -gt 0 ]]; then
echo "Deleting /var/lib/dhclient/dhclient*.lease"
sudo shred -zuf "${DHCLIENT_MATCHES[@]}"
shopt -s nullglob
DHCLIENT_REMAINING=(/var/lib/dhclient/dhclient*.lease)
shopt -u nullglob
if [[ ${#DHCLIENT_REMAINING[@]} -gt 0 ]]; then
echo "Failed to delete /var/lib/dhclient/dhclient*.lease"
exit 1
fi
fi
if [[ $( sudo find /var/tmp -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within /var/tmp/*"
sudo find /var/tmp -type f -exec shred -zuf {} \;
fi
if [[ $( sudo find /var/tmp -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete /var/tmp"
exit 1
fi
if [[ $( sudo ls /var/tmp | sudo wc -l ) -gt 0 ]]; then
echo "Deleting /var/tmp/*"
sudo rm -rf /var/tmp/*
fi
if [[ -f "/var/lib/systemd/random-seed" ]]; then
echo "Deleting /var/lib/systemd/random-seed"
sudo shred -zuf /var/lib/systemd/random-seed
sudo rm -f /var/lib/systemd/random-seed
fi
# Shredding is not guaranteed to work well on rolling logs
if [[ -f "/var/lib/rsyslog/imjournal.state" ]]; then
echo "Deleting /var/lib/rsyslog/imjournal.state"
sudo shred -zuf /var/lib/rsyslog/imjournal.state
sudo rm -f /var/lib/rsyslog/imjournal.state
fi
if [[ -d "/var/log/journal" ]] && [[ $( sudo ls /var/log/journal/ | sudo wc -l ) -gt 0 ]]; then
echo "Deleting /var/log/journal/*"
sudo find /var/log/journal/ -type f -exec shred -zuf {} \;
sudo rm -rf /var/log/journal/*
fi
if [[ -f "/etc/machine-id" ]]; then
echo "Truncating /etc/machine-id"
sudo truncate -s 0 /etc/machine-id
fi
if [[ -f "/var/lib/dbus/machine-id" ]]; then
echo "Truncating /var/lib/dbus/machine-id"
sudo truncate -s 0 /var/lib/dbus/machine-id
fi
sudo touch /etc/machine-id
# Flush all pending writes to disk before instance shutdown and snapshot
sync
echo "Sanitize OK"
###############################################################################
# Image Builder appends the following steps to the clean up script to uninstall
# the Systems Manager (SSM) agent and, on Amazon Linux 1, Amazon Linux 2, and
# Amazon Linux 2023, to remove the cronie package when Image Builder installed a
# crontab during the build. Image Builder uses the /tmp/imagebuilder_service
# working directory to track what it installed.
###############################################################################
SERVICE_ROOT_WORKING_DIR="/tmp/imagebuilder_service"
# SSM_UNINSTALL_CONDITION reflects the systemsManagerAgent.uninstallAfterBuild
# recipe setting and how the SSM agent was installed:
# SSM_INSTALLED_BY_CUSTOMER - always uninstall the SSM agent
# SSM_INSTALLED_BY_IMAGE_BUILDER - uninstall only if Image Builder installed it
# (any other value) - leave the SSM agent in the final image
SSM_UNINSTALL_CONDITION="<uninstallAfterBuild condition>"
function error_exit {
echo "$1" 1>&2
exit 1
}
function ssm_exists() {
eval "$1" > /dev/null 2>&1
echo $?
}
function cleanup_image() {
rm -rf "${SERVICE_ROOT_WORKING_DIR}"
}
function uninstall_ssm_agent() {
uninstall_package="$1"
uninstall_all=""
uninstall_success="false"
if [ "${uninstall_package}" == "" ]; then
uninstall_all="true"
fi
yum="sudo yum search amazon-ssm-agent | grep amazon-ssm-agent"
snap="sudo snap list amazon-ssm-agent"
rpm="sudo rpm -qa amazon-ssm-agent | grep amazon-ssm-agent"
dpkg="sudo dpkg --get-selections | grep amazon-ssm-agent"
pkg="su -m root -c \"pkg info -l amazon-ssm-agent | grep amazon-ssm-agent\""
if [[ ("${uninstall_all}" == "true" || "${uninstall_package}" == "snap") && $(ssm_exists "${snap}") -eq 0 ]]; then
echo "Package found in Snap.... Uninstalling"
(sleep 30 ; sudo snap remove amazon-ssm-agent) &>/dev/null &
uninstall_success="true"
fi
if [[ ("${uninstall_all}" == "true" || "${uninstall_package}" == "yum") && $(ssm_exists "${yum}") -eq 0 ]]; then
echo "Package found in Yum.... Uninstalling"
(sleep 30 ; sudo yum remove -y amazon-ssm-agent) &>/dev/null &
uninstall_success="true"
fi
if [[ ("${uninstall_all}" == "true" || "${uninstall_package}" == "rpm") && $(ssm_exists "${rpm}") -eq 0 ]]; then
echo "Package found in Rpm.... Uninstalling"
(sleep 30 ; sudo rpm -e amazon-ssm-agent) &>/dev/null &
uninstall_success="true"
fi
if [[ ("${uninstall_all}" == "true" || "${uninstall_package}" == "dpkg") && $(ssm_exists "${dpkg}") -eq 0 ]]; then
echo "Package found in Dpkg.... Uninstalling"
(sleep 30 ; sudo dpkg -r --force-all amazon-ssm-agent) &>/dev/null &
uninstall_success="true"
fi
if [[ ("${uninstall_all}" == "true" || "${uninstall_package}" == "pkg") && $(ssm_exists "${pkg}") -eq 0 ]]; then
echo "Package found in FreeBSD.... Uninstalling"
(sleep 30 ; su -m root -c "pkg remove -y amazon-ssm-agent") &> /dev/null &
uninstall_success="true"
fi
if [ "${uninstall_success}" == "false" ] ; then
error_exit "Unable to uninstall an SSM agent"
fi
}
# Amazon Linux releases where Image Builder installs cronie to provide a crontab:
# Amazon Linux AMI (AL1), AL2, and AL2023.
function is_cronie_supported_amazon_linux() {
if [ "$(get_os_type)" != "amzn" ]; then
return 1
fi
case "$(get_os_version)" in
2|2023) return 0 ;;
# AL1 releases use date-based versions from 2010 through 2018.
201[0-8].[0-9][0-9]) return 0 ;;
*) return 1 ;;
esac
}
function uninstall_crontab() {
if is_cronie_supported_amazon_linux ; then
echo "Uninstalling cronie package"
sudo yum remove -y cronie
fi
}
function get_os_type() {
FILE=/etc/os-release
if [ -e $FILE ]; then
. $FILE
echo $ID
else
echo ""
fi
}
function get_os_version() {
FILE=/etc/os-release
if [ -e $FILE ]; then
. $FILE
echo $VERSION_ID
else
echo ""
fi
}
if [ "${SSM_UNINSTALL_CONDITION}" == "SSM_INSTALLED_BY_CUSTOMER" ] ; then
echo "Uninstall after build set to true. Uninstalling SSM agent."
uninstall_ssm_agent
elif [ "${SSM_UNINSTALL_CONDITION}" == "SSM_INSTALLED_BY_IMAGE_BUILDER" ] ; then
echo "Checking if the SSM agent was installed by Image Builder"
if [[ -f ${SERVICE_ROOT_WORKING_DIR}/ssm_installed ]] ; then
package_manager="$(cat ${SERVICE_ROOT_WORKING_DIR}/ssm_installed)"
echo "Uninstalling the SSM agent installed by Image Builder using ${package_manager}"
uninstall_ssm_agent "${package_manager}"
fi
else
echo "Uninstall after build set to false. Skipping SSM agent uninstall."
fi
# When Image Builder installs a crontab during the build (on Amazon Linux 1,
# Amazon Linux 2, or Amazon Linux 2023), it records a crontab_installed marker
# and removes the cronie package here. To keep cronie in your final image,
# delete the marker before clean up runs:
# rm -f /tmp/imagebuilder_service/crontab_installed
if [[ -f ${SERVICE_ROOT_WORKING_DIR}/crontab_installed ]] ; then
echo "Uninstalling crontab installed by Image Builder"
uninstall_crontab
fi
cleanup_image
- Windows
-
Image Builder 管道在自定义 Windows 映像后,将运行 Microsoft Sysprep 实用程序。这些操作遵循强化和清理图像AWS的最佳实践。
- macOS
-
Image Builder 管道运行清理脚本,以帮助确保最终映像遵循安全最佳实践,并删除任何不应延续到快照中的构建构件或设置。但是,您可以跳过脚本的各个部分,或者完全覆盖用户数据。因此,Image Builder 管道生成的映像不一定符合任何特定的监管标准。
当管道完成其构建期和测试期后,Image Builder 会在创建输出映像之前自动运行以下清理脚本。
如果您在配方中覆盖用户数据,则脚本将无法运行。在这种情况下,请确保在用户数据中包含一个用于创建名为 perform_cleanup 的空文件的命令。Image Builder 会检测到此文件并在创建新映像之前运行清理脚本。
#!/bin/bash
if [[ ! -f {{workingDirectory}}/perform_cleanup ]]; then
echo "Skipping cleanup"
exit 0
else
sudo rm -f {{workingDirectory}}/perform_cleanup
fi
function cleanup() {
FILES=("$@")
for FILE in "${FILES[@]}"; do
if [[ -f "$FILE" ]]; then
echo "Deleting $FILE";
sudo rm -f $FILE;
fi;
if [[ -f $FILE ]]; then
echo "Failed to delete '$FILE'. Failing."
exit 1
fi;
done
};
# Reset EC2 macOS Init instance history so the image behaves as a first boot
if [[ -f {{workingDirectory}}/skip_cleanup_ec2_macos_init_files ]]; then
echo "Skipping cleanup of ec2-macos-init instance history"
else
echo "Cleaning up ec2-macos-init instance history"
if [[ -x /usr/local/bin/ec2-macos-init ]]; then
sudo /usr/local/bin/ec2-macos-init clean -all
fi
fi
# Clean up for temporary instance files
INSTANCE_FILES=(
"/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist"
)
if [[ -f {{workingDirectory}}/skip_cleanup_instance_files ]]; then
echo "Skipping cleanup of instance files"
else
echo "Cleaning up instance files"
cleanup "${INSTANCE_FILES[@]}"
fi;
# Clean up for ssh files
SSH_FILES=(
"/etc/ssh/ssh_host_rsa_key"
"/etc/ssh/ssh_host_rsa_key.pub"
"/etc/ssh/ssh_host_ecdsa_key"
"/etc/ssh/ssh_host_ecdsa_key.pub"
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
"/var/root/.ssh/authorized_keys"
)
if [[ -f {{workingDirectory}}/skip_cleanup_ssh_files ]]; then
echo "Skipping cleanup of ssh files"
else
echo "Cleaning up ssh files"
cleanup "${SSH_FILES[@]}"
USERS=$(ls /Users/)
for user in $USERS; do
if [[ -f /Users/"$user"/.ssh/authorized_keys ]]; then
echo Deleting /Users/"$user"/.ssh/authorized_keys;
sudo rm -f /Users/"$user"/.ssh/authorized_keys;
fi;
done
for user in $USERS; do
if [[ -f /Users/"$user"/.ssh/authorized_keys ]]; then
echo Failed to delete /Users/"$user"/.ssh/authorized_keys;
exit 1
fi;
done;
fi;
# Clean up for instance log files
INSTANCE_LOG_FILES=(
"/var/log/amazon/ec2/ec2-macos-init.log"
"/var/log/amazon/ec2/ena-ethernet.log"
"/var/log/amazon/ec2/system-monitoring.log"
)
if [[ -f {{workingDirectory}}/skip_cleanup_instance_log_files ]]; then
echo "Skipping cleanup of instance log files"
else
echo "Cleaning up instance log files"
cleanup "${INSTANCE_LOG_FILES[@]}"
fi;
# Clean up for TOE files
if [[ -f {{workingDirectory}}/skip_cleanup_toe_files ]]; then
echo "Skipping cleanup of TOE files"
else
echo "Cleaning TOE files"
shopt -s nullglob
TOE_MATCHES=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_MATCHES[@]} -gt 0 ]]; then
if [[ $( sudo find "${TOE_MATCHES[@]}" -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within {{workingDirectory}}/TOE_*"
sudo find "${TOE_MATCHES[@]}" -type f -exec rm -f {} \;
fi
shopt -s nullglob
TOE_REMAINING=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_REMAINING[@]} -gt 0 ]]; then
if [[ $( sudo find "${TOE_REMAINING[@]}" -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete {{workingDirectory}}/TOE_*"
exit 1
fi
echo "Deleting {{workingDirectory}}/TOE_*"
sudo rm -rf "${TOE_REMAINING[@]}"
fi
shopt -s nullglob
TOE_FINAL=({{workingDirectory}}/TOE_*)
shopt -u nullglob
if [[ ${#TOE_FINAL[@]} -gt 0 ]]; then
echo "Failed to delete {{workingDirectory}}/TOE_*"
exit 1
fi
fi
fi
# Clean up for ssm log files
if [[ -f {{workingDirectory}}/skip_cleanup_ssm_log_files ]]; then
echo "Skipping cleanup of ssm log files"
else
echo "Cleaning up ssm log files"
if [[ -d "/var/log/amazon/ssm" ]]; then
if [[ $( sudo find /var/log/amazon/ssm -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within /var/log/amazon/ssm/*"
sudo find /var/log/amazon/ssm -type f -exec rm -f {} \;
fi
if [[ $( sudo find /var/log/amazon/ssm -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete /var/log/amazon/ssm"
exit 1
fi
echo "Deleting /var/log/amazon/ssm/*"
sudo rm -rf /var/log/amazon/ssm
if [[ -d "/var/log/amazon/ssm" ]]; then
echo "Failed to delete /var/log/amazon/ssm"
exit 1
fi
fi
fi
# Clean up for DHCP lease files
shopt -s nullglob
DHCP_LEASE_MATCHES=(/var/db/dhcpclient/leases/*)
shopt -u nullglob
if [[ ${#DHCP_LEASE_MATCHES[@]} -gt 0 ]]; then
echo "Deleting /var/db/dhcpclient/leases/*"
sudo rm -f "${DHCP_LEASE_MATCHES[@]}"
shopt -s nullglob
DHCP_LEASE_REMAINING=(/var/db/dhcpclient/leases/*)
shopt -u nullglob
if [[ ${#DHCP_LEASE_REMAINING[@]} -gt 0 ]]; then
echo "Failed to delete /var/db/dhcpclient/leases/*"
exit 1
fi
fi
if [[ $( sudo find /var/tmp -type f | sudo wc -l) -gt 0 ]]; then
echo "Deleting files within /var/tmp/*"
sudo find /var/tmp -type f -exec rm -f {} \;
fi
if [[ $( sudo find /var/tmp -type f | sudo wc -l) -gt 0 ]]; then
echo "Failed to delete /var/tmp"
exit 1
fi
if [[ $( sudo ls /var/tmp | sudo wc -l ) -gt 0 ]]; then
echo "Deleting /var/tmp/*"
sudo rm -rf /var/tmp/*
fi
# Flush all pending writes to disk before instance shutdown and snapshot
sync
覆盖 Linux 清理脚本
Image Builder 创建的映像在默认情况下是安全的,并遵循我们的安全最佳实践。但是,一些更高级的使用案例可能需要您跳过内置清理脚本的一个或多个部分。如果您确实需要跳过一些清理部分,我们强烈建议您测试输出 AMI,以确保映像的安全。
跳过清理脚本中的一些部分可能会导致敏感信息(例如所有者帐户详细信息或 SSH 密钥)被包含在最终映像中,并被包含在从该映像启动的任何实例中。在不同的可用区、区域或账户中启动时,您也可能会遇到问题。
下表概述了清理脚本的各个部分,这些部分中删除的文件,以及可用于标记 Image Builder 应跳过的某个部分的文件名。要跳过清理脚本的特定部分,您可以使用 CreateFile 组件操作模块或用户数据中的命令(如果已覆盖)来创建一个空文件,其名称在跳过部分文件名列中指定。
为跳过清理脚本的某一部分而创建的文件不应包含文件扩展名。例如,如果您想跳过脚本的 CLOUD_INIT_FILES 部分,但创建了一个名为 skip_cleanup_cloudinit_files.txt 的文件,那么 Image Builder 将无法识别要跳过的文件。
Input
清理部分 |
文件已删除 |
跳过部分文件名 |
CLOUD_INIT_FILES
|
/etc/sudoers.d/90-cloud-init-users
/etc/locale.conf
/var/log/cloud-init.log
/var/log/cloud-init-output.log
下方的所有文件 /var/lib/cloud/
|
skip_cleanup_cloudinit_files
|
INSTANCE_FILES
|
/etc/.updated
/etc/aliases.db
/etc/hostname
/var/lib/misc/postfix.aliasesdb-stamp
/var/lib/postfix/master.lock
/var/spool/postfix/pid/master.pid
/var/.updated
/var/cache/yum/x86_64/2/.gpgkeyschecked.yum
|
skip_cleanup_instance_files
|
SSH_FILES
|
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/root/.ssh/authorized_keys
/home/<all users>/.ssh/authorized_keys
|
skip_cleanup_ssh_files
|
INSTANCE_LOG_FILES
|
/var/log/audit/audit.log
/var/log/boot.log
/var/log/dmesg
/var/log/cron
|
skip_cleanup_instance_log_files
|
TOE_FILES
|
{{workingDirectory}}/TOE_*
|
skip_cleanup_toe_files
|
SSM_LOG_FILES
|
/var/log/amazon/ssm/*
|
skip_cleanup_ssm_log_files
|
上表中的部分是清理脚本中唯一可以跳过的部分。脚本处理这些部分后,它还会删除以下项目,你不能跳过这些步骤:
/var/log/sa/sa*
/var/lib/dhclient/dhclient*.lease
下方的所有文件 /var/tmp/
/var/lib/systemd/random-seed
/var/lib/rsyslog/imjournal.state
下方的所有文件 /var/log/journal/
该脚本还会截断机器 ID 文件/etc/machine-id和。/var/lib/dbus/machine-id这可确保从映像启动的每个实例生成一个唯一的计算机 ID。
覆盖 macOS 清理脚本
Image Builder 创建的映像在默认情况下是安全的,并遵循我们的安全最佳实践。但是,一些更高级的使用案例可能需要您跳过内置清理脚本的一个或多个部分。如果您确实需要跳过一些清理部分,我们强烈建议您测试输出 AMI,以确保映像的安全。
跳过清理脚本中的一些部分可能会导致敏感信息(例如所有者帐户详细信息或 SSH 密钥)被包含在最终映像中,并被包含在从该映像启动的任何实例中。在不同的可用区、区域或账户中启动时,您也可能会遇到问题。
下表概述了清理脚本的各个部分,这些部分中删除的文件,以及可用于标记 Image Builder 应跳过的某个部分的文件名。要跳过清理脚本的特定部分,您可以使用 CreateFile 组件操作模块或用户数据中的命令(如果已覆盖)来创建一个空文件,其名称在跳过部分文件名列中指定。
为跳过清理脚本的某一部分而创建的文件不应包含文件扩展名。例如,如果您想跳过脚本的 INSTANCE_FILES 部分,但创建了一个名为 skip_cleanup_instance_files.txt 的文件,那么 Image Builder 将无法识别要跳过的文件。
Input
清理部分 |
文件已删除 |
跳过部分文件名 |
EC2_MACOS_INIT_FILES
|
运行ec2-macos-init clean -all以重置 EC2 macOS 初始化实例历史记录,使映像在首次启动时表现良好。
|
skip_cleanup_ec2_macos_init_files
|
INSTANCE_FILES
|
/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist
|
skip_cleanup_instance_files
|
SSH_FILES
|
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/var/root/.ssh/authorized_keys
/Users/<all users>/.ssh/authorized_keys
|
skip_cleanup_ssh_files
|
INSTANCE_LOG_FILES
|
/var/log/amazon/ec2/ec2-macos-init.log
/var/log/amazon/ec2/ena-ethernet.log
/var/log/amazon/ec2/system-monitoring.log
|
skip_cleanup_instance_log_files
|
TOE_FILES
|
{{workingDirectory}}/TOE_*
|
skip_cleanup_toe_files
|
SSM_LOG_FILES
|
/var/log/amazon/ssm/*
|
skip_cleanup_ssm_log_files
|