Skip to content

Commit

Permalink
Hardening Alpine and Debian image (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
create1st authored May 6, 2021
1 parent 650d4a5 commit a55c0d2
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .buildkite/scripts/harden_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -uexo pipefail
for dockerfile in images/Dockerfile.*
do
echo "Hardening: $dockerfile"
docker build . -f "$dockerfile" --tag "${dockerfile:18}-hardened"
DOCKER_BUILDKIT=0 docker build . -f "$dockerfile" --no-cache --tag "${dockerfile:18}-hardened"
done
5 changes: 3 additions & 2 deletions images/Dockerfile.openjdk11
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM openjdk:11.0.11-jdk

ADD scripts/ubuntu.sh /usr/scripts/
RUN /usr/scripts/ubuntu.sh
ADD scripts/* /usr/scripts/
ADD scripts/compliance/* /usr/scripts/compliance/
RUN /usr/scripts/debian.sh
5 changes: 5 additions & 0 deletions images/Dockerfile.openjdk11-alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:11.0.11-jdk

ADD scripts/* /usr/scripts/
ADD scripts/compliance/* /usr/scripts/compliance/
RUN /usr/scripts/alpine.sh
12 changes: 12 additions & 0 deletions scripts/alpine.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

SCRIPTS_PATH=$(dirname "$0")
# shellcheck disable=SC1090,SC1091
source "${SCRIPTS_PATH}/formatting.sh"

echo "${BOLD}Applying compliance requirements${NORMAL}"
"${SCRIPTS_PATH}"/compliance.sh

echo "${BOLD}Completed${NORMAL}"

rm -- "$0"
30 changes: 30 additions & 0 deletions scripts/compliance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -eo pipefail

function removeResources {
local COUNT=0
echo "Checking resources defined in ${BOLD}${1}${NORMAL}"
while IFS="" read -r RESOURCE || [ -n "${RESOURCE}" ]
do
if [[ -f "${RESOURCE}" ]]; then
echo -e "${2} ${BOLD}${RESOURCE}${NORMAL} found. ${GREEN}Removing...${NC}"
rm -fR "${RESOURCE}"
((COUNT=COUNT+1))
fi
done < "${1}"
echo "${BOLD}${COUNT}${NORMAL} problems found and removed"
}
echo "Running ${BOLD}compliance${NORMAL} script"

SCRIPTS_PATH=$(dirname "$0")

# shellcheck disable=SC1090,SC1091
source "${SCRIPTS_PATH}/formatting.sh"

COMPLIANCE_PATH="${SCRIPTS_PATH}/compliance"

removeResources "${COMPLIANCE_PATH}/prohibited.txt" "${RED}Problem!${NC}"
removeResources "${COMPLIANCE_PATH}/warnings.txt" "${YELLOW}Warning!${NC}"

echo "${BOLD}Compliance${NORMAL} script completed"
19 changes: 19 additions & 0 deletions scripts/compliance/prohibited.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/etc/crontabs
/etc/fstab
/etc/inittab
/etc/krb5.conf
/etc/logrotate.d
/etc/modprobe.d
/etc/modules-load.d
/etc/periodic
/etc/runlevels
/etc/securetty
/etc/sysctl.conf
/etc/sysctl.d
/media
/mnt
/mount
/sbin/apk
/srv
/var/cache
/var/spool/cron
11 changes: 11 additions & 0 deletions scripts/compliance/warnings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/bin/bash
/bin/csh
/bin/dash
/bin/od
/bin/sh
/etc/passwd-
/etc/shadow-
/usr/bin/od
/usr/bin/zsh
/usr/bin/hexdump
/usr/bin/strings
16 changes: 16 additions & 0 deletions scripts/debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

SCRIPTS_PATH=$(dirname "$0")
# shellcheck disable=SC1090,SC1091
source "${SCRIPTS_PATH}/formatting.sh"

echo "${BOLD}Updating apt${NORMAL}"
yes y | apt-get update
yes y | apt-get upgrade

echo "${BOLD}Applying compliance requirements${NORMAL}"
"${SCRIPTS_PATH}"/compliance.sh

echo "${BOLD}Completed${NORMAL}"

rm -- "$0"
11 changes: 11 additions & 0 deletions scripts/formatting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export TERM=xterm-256color
BOLD=$(tput bold)
export BOLD
NORMAL=$(tput sgr0)
export NORMAL
export RED='\033[0;31m'
export GREEN='\033[0;32m'
export YELLOW='\033[0;33m'
export NC='\033[0m' # No Color
41 changes: 0 additions & 41 deletions scripts/ubuntu.sh

This file was deleted.

0 comments on commit a55c0d2

Please sign in to comment.