기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Image Builder의 보안 모범 사례
EC2 Image Builder는 자체 보안 정책을 개발하고 구현할 때 고려해야 할 여러 보안 기능을 제공합니다. 다음 모범 사례는 일반적인 지침이며 완벽한 보안 솔루션을 나타내지는 않습니다. 이러한 모범 사례는 환경에 적절하지 않거나 충분하지 않을 수 있으므로 참고용으로만 사용합니다.
-
Image Builder 레시피에는 지나치게 허용적인 보안 그룹을 사용하지 않습니다.
-
신뢰할 수 없는 계정에 있는 이미지는 공유하지 않습니다.
-
비공개 또는 민감한 데이터가 포함된 이미지는 공개하지 않습니다.
-
이미지 빌드 중에 사용 가능한 모든 Windows 또는 Linux 보안 패치를 적용합니다.
-
관리형 AMI 업데이트를 macOS 레시피에 주기적으로 적용하고 새 이미지를 생성하여 최신 보안 패치가 있는 인스턴스를 시작합니다.
이미지를 테스트하여 보안 상태 및 해당 보안 규정 준수 수준을 검증하는 것이 좋습니다. Amazon Inspector와 같은 솔루션은 이미지의 보안 및 규정 준수 상태를 검증하는 데 도움이 될 수 있습니다.
Image Builder 파이프라인용 IMDSv2
Image Builder 파이프라인이 실행되면 Image Builder에서 이미지를 빌드하고 테스트하는 데 사용하는 EC2 인스턴스를 시작하기 위한 HTTP 요청을 보냅니다. 파이프라인이 시작 요청에 사용하는 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는 Systems Manager 에이전트를 제거하고 cronie 패키지를 제거하는 단계를 스크립트에 추가합니다. Image Builder는 /tmp/imagebuilder_service 서비스 작업 디렉터리의 빌드 중에 설치한 내용을 추적합니다. Image Builder는 이러한 마커를 사용하여 제거할 대상을 결정합니다.
-
Systems Manager 에이전트 - Image Builder가 에이전트를 제거하는지 여부는 이미지 레시피의 systemsManagerAgent.uninstallAfterBuild 설정과 Image Builder가 에이전트를 설치했는지 여부에 따라 달라집니다. 자세한 내용은의 uninstallAfterBuild 설정을 참조하세요를 사용하여 이미지 레시피 생성 AWS CLI.
-
cronie 패키지 -이 단계는 Amazon Linux 1, Amazon Linux 2 및 Amazon Linux 2023에 적용됩니다. Image Builder가 빌드 중에 crontab을 설치한 경우 서비스 작업 디렉터리에 crontab_installed 마커를 기록합니다. 그런 다음 Image Builder는 정리의 일부로 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 Init 인스턴스 기록을 재설정합니다.
|
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
|