|
|
|
@@ -10,6 +10,17 @@
|
|
|
|
|
# NOTE: This script depends on so-common
|
|
|
|
|
IMAGEREPO=security-onion-solutions
|
|
|
|
|
STATUS_CONF='/opt/so/conf/so-status/so-status.conf'
|
|
|
|
|
# PIDs of the current launch_pool children, so the INT/TERM trap can kill them.
|
|
|
|
|
POOL_PIDS=()
|
|
|
|
|
_pool_tree=()
|
|
|
|
|
IMAGE_PARALLEL_JOBS=4
|
|
|
|
|
IMAGE_INSPECT_TIMEOUT=120
|
|
|
|
|
IMAGE_RETRIES=5
|
|
|
|
|
IMAGE_RETRY_DELAY=30
|
|
|
|
|
# The preflight is a quick check, not a retry budget: one retry at most, and a
|
|
|
|
|
# definite answer from the registry ends it right away.
|
|
|
|
|
PREFLIGHT_ATTEMPTS=2
|
|
|
|
|
PREFLIGHT_RETRY_DELAY=10
|
|
|
|
|
|
|
|
|
|
# shellcheck disable=SC2120
|
|
|
|
|
container_list() {
|
|
|
|
@@ -79,6 +90,420 @@ container_list() {
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registry_host() {
|
|
|
|
|
if [ -z "$HOSTNAME" ]; then
|
|
|
|
|
HOSTNAME=$(hostname)
|
|
|
|
|
fi
|
|
|
|
|
printf '%s' "$HOSTNAME"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
manifest_blob_digests() {
|
|
|
|
|
jq -r '(.config.digest // empty), (.layers[]? | .digest)' "$1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Registries link blobs per repo and buildx cannot mount across registries, so
|
|
|
|
|
# pre-link shared layers or every image re-downloads them from ghcr.io.
|
|
|
|
|
mount_shared_blobs() {
|
|
|
|
|
local host=$1
|
|
|
|
|
local target_repo=$2
|
|
|
|
|
local manifest_file=$3
|
|
|
|
|
local log_file=$4
|
|
|
|
|
local -n blob_repo=$5
|
|
|
|
|
local digest source_repo code
|
|
|
|
|
while IFS= read -r digest; do
|
|
|
|
|
if [ -z "$digest" ]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
source_repo="${blob_repo[$digest]:-}"
|
|
|
|
|
if [ -z "$source_repo" ] || [ "$source_repo" == "$target_repo" ]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
code=$(curl -sk --connect-timeout 5 --max-time 30 -o /dev/null -w '%{http_code}' -X POST \
|
|
|
|
|
"https://${host}:5000/v2/${IMAGEREPO}/${target_repo}/blobs/uploads/?mount=${digest}&from=${IMAGEREPO}/${source_repo}" \
|
|
|
|
|
2>> "$log_file")
|
|
|
|
|
if [ "$code" != "201" ]; then
|
|
|
|
|
echo "Could not pre-link $digest from $source_repo into $target_repo (HTTP ${code:-error}), buildx will download it" >> "$log_file"
|
|
|
|
|
fi
|
|
|
|
|
done < <(manifest_blob_digests "$manifest_file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
record_shared_blobs() {
|
|
|
|
|
local target_repo=$1
|
|
|
|
|
local manifest_file=$2
|
|
|
|
|
local -n blob_repo=$3
|
|
|
|
|
local digest
|
|
|
|
|
while IFS= read -r digest; do
|
|
|
|
|
if [ -n "$digest" ] && [ -z "${blob_repo[$digest]:-}" ]; then
|
|
|
|
|
blob_repo["$digest"]=$target_repo
|
|
|
|
|
fi
|
|
|
|
|
done < <(manifest_blob_digests "$manifest_file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image_ref() { # image_ref <image:tag> => the full registry reference
|
|
|
|
|
printf '%s/%s/%s' "$CONTAINER_REGISTRY" "$IMAGEREPO" "$1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# One small function per inspect: retry_cmd logs its attempts on stdout, so the
|
|
|
|
|
# command's own output is redirected to its file here.
|
|
|
|
|
fetch_manifest() { # fetch_manifest <ref> <out file>
|
|
|
|
|
timeout "$IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect --raw "$1" >"$2"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# buildx's --format output is the contract with the signed .txt, so it stays in
|
|
|
|
|
# one place that is easy to adjust.
|
|
|
|
|
fetch_config() { # fetch_config <ref> <out file>
|
|
|
|
|
timeout "$IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect \
|
|
|
|
|
--format '{{json .Image}}' "$1" >"$2"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# The ES image follows the elasticsearch version, everything else the SO
|
|
|
|
|
# version. Prints "<image>:<tag> <signature url>".
|
|
|
|
|
resolve_image() { # resolve_image <image>
|
|
|
|
|
local i=$1
|
|
|
|
|
local image_version
|
|
|
|
|
if [[ " ${IMAGES_USING_ES_VERSION[*]} " =~ [[:space:]]${i}[[:space:]] ]]; then
|
|
|
|
|
# this is an es container so use version defined in elasticsearch defaults.yaml
|
|
|
|
|
local UPDATE_DIR='/tmp/sogh/securityonion'
|
|
|
|
|
if [ ! -d "$UPDATE_DIR" ]; then
|
|
|
|
|
UPDATE_DIR=/securityonion
|
|
|
|
|
fi
|
|
|
|
|
local v1=0
|
|
|
|
|
local v2=0
|
|
|
|
|
if [[ -f "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" ]]; then
|
|
|
|
|
v1=$(grep -E " +version: " "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]')
|
|
|
|
|
fi
|
|
|
|
|
if [[ -f "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" ]]; then
|
|
|
|
|
v2=$(grep -E " +version: " "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]')
|
|
|
|
|
fi
|
|
|
|
|
image_version=$(compare_es_versions "$v1" "$v2")
|
|
|
|
|
printf '%s:%s%s https://sigs.securityonion.net/es-%s/%s:%s%s.sig\n' \
|
|
|
|
|
"$i" "$image_version" "$IMAGE_TAG_SUFFIX" "$image_version" "$i" "$image_version" "$IMAGE_TAG_SUFFIX"
|
|
|
|
|
else
|
|
|
|
|
# this is not an es container so use the so version for the version
|
|
|
|
|
image_version=$VERSION
|
|
|
|
|
printf '%s:%s%s https://sigs.securityonion.net/%s/%s:%s%s.sig\n' \
|
|
|
|
|
"$i" "$image_version" "$IMAGE_TAG_SUFFIX" "$image_version" "$i" "$image_version" "$IMAGE_TAG_SUFFIX"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verify_trusted_image() {
|
|
|
|
|
local i=$1
|
|
|
|
|
local IMAGE_LOG=$SIGNPATH/$i.verify.log
|
|
|
|
|
|
|
|
|
|
local image sig_url
|
|
|
|
|
read -r image sig_url < <(resolve_image "$i")
|
|
|
|
|
|
|
|
|
|
local image_name=$i
|
|
|
|
|
local manifest_file=$SIGNPATH/$image.manifest
|
|
|
|
|
local config_file=$SIGNPATH/$image.config
|
|
|
|
|
local REF
|
|
|
|
|
REF=$(image_ref "$image")
|
|
|
|
|
local VERIFIED_REF
|
|
|
|
|
|
|
|
|
|
local rc=0
|
|
|
|
|
local digest_re='^sha256:[a-f0-9]{64}$'
|
|
|
|
|
|
|
|
|
|
retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" fetch_manifest "$REF" "$manifest_file" >>"$IMAGE_LOG" 2>&1 || rc=$?
|
|
|
|
|
if [ "$rc" -ne 0 ]; then
|
|
|
|
|
echo "Could not fetch manifest for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
if [ ! -s "$manifest_file" ] || ! jq -e . "$manifest_file" >/dev/null 2>&1; then
|
|
|
|
|
echo "Unable to read the manifest for $image: empty or not JSON" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
# The sig covers these exact bytes, so an index would sign the wrong document.
|
|
|
|
|
if jq -e '.manifests | type == "array"' "$manifest_file" >/dev/null 2>&1; then
|
|
|
|
|
echo "$image manifest is a multi-platform index, expected a single manifest" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local manifest_digest
|
|
|
|
|
manifest_digest=$(sha256sum "$manifest_file" | awk '{print $1}')
|
|
|
|
|
local config_digest
|
|
|
|
|
config_digest=$(jq -r '.config.digest // empty' "$manifest_file")
|
|
|
|
|
if [ -z "$manifest_digest" ] || [ -z "$config_digest" ] || [ "$config_digest" = "null" ]; then
|
|
|
|
|
echo "Unable to read the manifest for $image: no config digest" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Every digest ends up in a URL (blob fetches, blob mounts, the wave
|
|
|
|
|
# planner), so one malformed digest rejects the whole manifest.
|
|
|
|
|
if [[ ! $config_digest =~ $digest_re ]]; then
|
|
|
|
|
echo "Unable to inspect $image: invalid config digest '$config_digest'" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
local layer_digest
|
|
|
|
|
while IFS= read -r layer_digest; do
|
|
|
|
|
if [ -z "$layer_digest" ]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
if [[ ! $layer_digest =~ $digest_re ]]; then
|
|
|
|
|
echo "Unable to inspect $image: invalid layer digest '$layer_digest'" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
done < <(jq -r '.layers[]? | .digest' "$manifest_file")
|
|
|
|
|
|
|
|
|
|
rc=0
|
|
|
|
|
# By digest, not by tag: a tag can be re-pushed between the two calls, and the
|
|
|
|
|
# .txt must describe the manifest bytes that sha256sum and the sig cover.
|
|
|
|
|
VERIFIED_REF="${CONTAINER_REGISTRY}/${IMAGEREPO}/${image_name}@sha256:${manifest_digest}"
|
|
|
|
|
retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" fetch_config "$VERIFIED_REF" "$config_file" >>"$IMAGE_LOG" 2>&1 || rc=$?
|
|
|
|
|
if [ "$rc" -ne 0 ]; then
|
|
|
|
|
echo "Could not fetch image config for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
if [ ! -s "$config_file" ]; then
|
|
|
|
|
echo "Unable to read the config for $image" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Get signature
|
|
|
|
|
rc=0
|
|
|
|
|
curl --fail --connect-timeout 10 --max-time 120 --retry 5 --retry-delay 60 -sSL \
|
|
|
|
|
-A "$CURLTYPE/$CURRENTVERSION/$OS/$(uname -r)" \
|
|
|
|
|
--output "$SIGNPATH/$image.sig" "$sig_url" >>"$IMAGE_LOG" 2>&1 || rc=$?
|
|
|
|
|
if [ "$rc" -ne 0 ]; then
|
|
|
|
|
echo "Could not pull signature file for $image, please ensure connectivity to https://sigs.securityonion.net " >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# .txt layout must stay byte-identical to the old `docker inspect | jq` output
|
|
|
|
|
# because the gpg signature is verified against it.
|
|
|
|
|
if ! printf '%s\n' "\"${VERIFIED_REF}\"" >"$SIGNPATH/$image.txt"; then
|
|
|
|
|
echo "Unable to write the signed ref line for $image" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
if ! jq ".created, .rootfs.diff_ids" "$config_file" >>"$SIGNPATH/$image.txt"; then
|
|
|
|
|
echo "Unable to extract created and diff_ids for $image" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
local GPGTEST
|
|
|
|
|
if ! GPGTEST=$(gpg --verify "$SIGNPATH/$image.sig" "$SIGNPATH/$image.txt" 2>&1); then
|
|
|
|
|
echo "Signature verification failed for $image. Details:" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
echo "$GPGTEST" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
printf '%s\n%s\n%s\n' "$image" "$VERIFIED_REF" "$manifest_file" >"$SIGNPATH/$i.ref"
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# retry()/run_check_net_err() eval their command string, so the argv helper is
|
|
|
|
|
# used for anything that must not be re-parsed by the shell.
|
|
|
|
|
retry_cmd() {
|
|
|
|
|
local max_attempts=$1
|
|
|
|
|
local delay=$2
|
|
|
|
|
shift 2
|
|
|
|
|
local attempt=1 rc=0
|
|
|
|
|
while :; do
|
|
|
|
|
if "$@"; then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
rc=$?
|
|
|
|
|
fi
|
|
|
|
|
echo "Command failed with error $rc (attempt $attempt of $max_attempts): $*"
|
|
|
|
|
if [ "$attempt" -ge "$max_attempts" ]; then
|
|
|
|
|
return $rc
|
|
|
|
|
fi
|
|
|
|
|
attempt=$((attempt + 1))
|
|
|
|
|
sleep "$delay"
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copy_verified_image() {
|
|
|
|
|
local i=$1
|
|
|
|
|
local IMAGE_LOG=$SIGNPATH/$i.copy.log
|
|
|
|
|
local image VERIFIED_REF manifest_file
|
|
|
|
|
{
|
|
|
|
|
read -r image
|
|
|
|
|
read -r VERIFIED_REF
|
|
|
|
|
read -r manifest_file
|
|
|
|
|
} <"$SIGNPATH/$i.ref"
|
|
|
|
|
|
|
|
|
|
local host
|
|
|
|
|
host=$(registry_host)
|
|
|
|
|
mount_shared_blobs "$host" "$i" "$manifest_file" "$IMAGE_LOG" pushed_blob_repo
|
|
|
|
|
# imagetools create, not docker push: Docker 29's containerd store cannot push a
|
|
|
|
|
# freshly pulled image. GODEBUG prefix keeps http2client=0 local (#16342).
|
|
|
|
|
# It reads from ghcr.io, so it gets the same budget as the verify fetches.
|
|
|
|
|
retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" env GODEBUG="${GODEBUG:+${GODEBUG},}http2client=0" \
|
|
|
|
|
docker buildx imagetools create --tag "${host}:5000/${IMAGEREPO}/${image}" "$VERIFIED_REF" \
|
|
|
|
|
>>"$IMAGE_LOG" 2>&1 || {
|
|
|
|
|
echo "Unable to copy $image to embedded registry" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pull_verified_image() {
|
|
|
|
|
local i=$1
|
|
|
|
|
local IMAGE_LOG=$SIGNPATH/$i.pull.log
|
|
|
|
|
local image VERIFIED_REF
|
|
|
|
|
{
|
|
|
|
|
read -r image
|
|
|
|
|
read -r VERIFIED_REF
|
|
|
|
|
} <"$SIGNPATH/$i.ref"
|
|
|
|
|
|
|
|
|
|
local host
|
|
|
|
|
host=$(registry_host)
|
|
|
|
|
# Pull by the digest that gpg verified, not by the mutable tag a compromised
|
|
|
|
|
# grid node could have re-pushed. Keep the tag name visible to `docker images`
|
|
|
|
|
# and to the salt checks that look for it.
|
|
|
|
|
local digest=${VERIFIED_REF#*@}
|
|
|
|
|
local digest_ref="${host}:5000/${IMAGEREPO}/${image%:*}@${digest}"
|
|
|
|
|
local tag_ref="${host}:5000/${IMAGEREPO}/${image}"
|
|
|
|
|
retry_cmd 3 10 docker pull "$digest_ref" >>"$IMAGE_LOG" 2>&1 || {
|
|
|
|
|
echo "Unable to pull $image from embedded registry" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
if ! docker tag "$digest_ref" "$tag_ref" >>"$IMAGE_LOG" 2>&1; then
|
|
|
|
|
echo "Unable to tag $image" >>"$IMAGE_LOG" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_downloading() {
|
|
|
|
|
echo "Downloading $1" >> "$LOG_FILE" 2>&1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Kill a pool job and everything it spawned: in a non-interactive shell the
|
|
|
|
|
# background docker/curl children ignore SIGINT, so killing only the subshell
|
|
|
|
|
# would leave them writing to $SIGNPATH. Only PIDs the pool started are walked.
|
|
|
|
|
pool_collect_tree() { # pool_collect_tree <pid>; appends to _pool_tree
|
|
|
|
|
local pid=$1
|
|
|
|
|
local child
|
|
|
|
|
_pool_tree+=("$pid")
|
|
|
|
|
while IFS= read -r child; do
|
|
|
|
|
if [ -n "$child" ]; then
|
|
|
|
|
pool_collect_tree "$child"
|
|
|
|
|
fi
|
|
|
|
|
done < <(pgrep -P "$pid" 2>/dev/null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pool_abort() { # _pool_abort <exit code>
|
|
|
|
|
local code=$1
|
|
|
|
|
local pid
|
|
|
|
|
trap '' INT TERM
|
|
|
|
|
_pool_tree=()
|
|
|
|
|
# Skipping reaped PIDs matters: their numbers may already belong to an
|
|
|
|
|
# unrelated process, whose tree would then be killed as root.
|
|
|
|
|
for pid in "${POOL_PIDS[@]}"; do
|
|
|
|
|
if kill -0 "$pid" 2>/dev/null; then
|
|
|
|
|
pool_collect_tree "$pid"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
for pid in "${_pool_tree[@]}"; do
|
|
|
|
|
kill -TERM "$pid" 2>/dev/null || true
|
|
|
|
|
done
|
|
|
|
|
sleep 2
|
|
|
|
|
for pid in "${_pool_tree[@]}"; do
|
|
|
|
|
kill -KILL "$pid" 2>/dev/null || true
|
|
|
|
|
done
|
|
|
|
|
exit "$code"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Fail fast: once a finished job of this pool failed, no new job is launched, so
|
|
|
|
|
# an unreachable registry costs one round of retries instead of one per image.
|
|
|
|
|
pool_failed() { # pool_failed <phase> <image>...
|
|
|
|
|
local phase=$1
|
|
|
|
|
shift
|
|
|
|
|
local img rc
|
|
|
|
|
for img in "$@"; do
|
|
|
|
|
# No .rc yet means the job is still running, so it is not a failure.
|
|
|
|
|
if rc=$(cat "$SIGNPATH/$img.$phase.rc" 2>/dev/null) && [ "$rc" != "0" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Append one phase's per-image logs, then name what failed or was skipped.
|
|
|
|
|
pool_collect() { # pool_collect <phase> <image>...; non-zero if any did not pass
|
|
|
|
|
local phase=$1
|
|
|
|
|
shift
|
|
|
|
|
local img failed=0
|
|
|
|
|
local -a bad=() skipped=()
|
|
|
|
|
for img in "$@"; do
|
|
|
|
|
cat "$SIGNPATH/$img.$phase.log" >> "$LOG_FILE" 2>/dev/null
|
|
|
|
|
if [ ! -f "$SIGNPATH/$img.$phase.rc" ]; then
|
|
|
|
|
skipped+=("$img")
|
|
|
|
|
failed=1
|
|
|
|
|
elif [ "$(cat "$SIGNPATH/$img.$phase.rc" 2>/dev/null)" != "0" ]; then
|
|
|
|
|
bad+=("$img")
|
|
|
|
|
failed=1
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if [ "$failed" -ne 0 ]; then
|
|
|
|
|
echo "$phase failed: ${bad[*]:-none}; skipped: ${skipped[*]:-none}" >> "$LOG_FILE" 2>&1
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
launch_pool() {
|
|
|
|
|
local func=$1
|
|
|
|
|
local phase=$2
|
|
|
|
|
local jobs=$3
|
|
|
|
|
local hook=$4
|
|
|
|
|
shift 4
|
|
|
|
|
local -a active=() still=() launched=()
|
|
|
|
|
local img pid rc
|
|
|
|
|
local stop=0
|
|
|
|
|
POOL_PIDS=()
|
|
|
|
|
local saved_int saved_term
|
|
|
|
|
saved_int=$(trap -p INT)
|
|
|
|
|
saved_term=$(trap -p TERM)
|
|
|
|
|
trap '_pool_abort 130' INT
|
|
|
|
|
trap '_pool_abort 143' TERM
|
|
|
|
|
for img in "$@"; do
|
|
|
|
|
if [ "$stop" -ne 0 ]; then
|
|
|
|
|
# No .rc is written, so the caller counts this image as failed too.
|
|
|
|
|
echo "Skipped $img: an earlier image failed" >>"$SIGNPATH/$img.$phase.log" 2>&1
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
if [ -n "$hook" ]; then
|
|
|
|
|
"$hook" "$img"
|
|
|
|
|
fi
|
|
|
|
|
(
|
|
|
|
|
("$func" "$img") >>"$SIGNPATH/$img.$phase.log" 2>&1
|
|
|
|
|
rc=$?
|
|
|
|
|
# Write then rename: a truncated .rc reads as a failure to pool_failed.
|
|
|
|
|
echo "$rc" >"$SIGNPATH/$img.$phase.rc.tmp"
|
|
|
|
|
mv "$SIGNPATH/$img.$phase.rc.tmp" "$SIGNPATH/$img.$phase.rc"
|
|
|
|
|
) &
|
|
|
|
|
pid=$!
|
|
|
|
|
POOL_PIDS+=("$pid")
|
|
|
|
|
active+=("$pid")
|
|
|
|
|
launched+=("$img")
|
|
|
|
|
while [ ${#active[@]} -ge "$jobs" ]; do
|
|
|
|
|
wait -n "${active[@]}" 2>/dev/null || true
|
|
|
|
|
still=()
|
|
|
|
|
for pid in "${active[@]}"; do
|
|
|
|
|
if kill -0 "$pid" 2>/dev/null; then
|
|
|
|
|
still+=("$pid")
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
active=("${still[@]}")
|
|
|
|
|
if [ "$stop" -eq 0 ] && pool_failed "$phase" "${launched[@]}"; then
|
|
|
|
|
stop=1
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
# Only wait on PIDs started here: soup may run its own background jobs in this
|
|
|
|
|
# shell (e.g. the grid highstate), and a bare `wait` would block on those.
|
|
|
|
|
if [ ${#POOL_PIDS[@]} -gt 0 ]; then
|
|
|
|
|
wait "${POOL_PIDS[@]}" 2>/dev/null || true
|
|
|
|
|
fi
|
|
|
|
|
# Restore whatever the caller had; soup installs its own EXIT/ERR handlers.
|
|
|
|
|
eval "${saved_int:-trap - INT}"
|
|
|
|
|
eval "${saved_term:-trap - TERM}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update_docker_containers() {
|
|
|
|
|
local CURLTYPE=$1
|
|
|
|
|
local IMAGE_TAG_SUFFIX=$2
|
|
|
|
@@ -130,91 +555,117 @@ update_docker_containers() {
|
|
|
|
|
grep -q "so-logscan" "$STATUS_CONF" && TRUSTED_CONTAINERS+=("so-logscan")
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Download the containers from the interwebs
|
|
|
|
|
for i in "${TRUSTED_CONTAINERS[@]}"
|
|
|
|
|
do
|
|
|
|
|
if [ -z "$PROGRESS_CALLBACK" ]; then
|
|
|
|
|
echo "Downloading $i" >> "$LOG_FILE" 2>&1
|
|
|
|
|
else
|
|
|
|
|
$PROGRESS_CALLBACK $i
|
|
|
|
|
fi
|
|
|
|
|
local -A pushed_blob_repo=()
|
|
|
|
|
|
|
|
|
|
if [[ " ${IMAGES_USING_ES_VERSION[*]} " =~ [[:space:]]${i}[[:space:]] ]]; then
|
|
|
|
|
# this is an es container so use version defined in elasticsearch defaults.yaml
|
|
|
|
|
local UPDATE_DIR='/tmp/sogh/securityonion'
|
|
|
|
|
if [ ! -d "$UPDATE_DIR" ]; then
|
|
|
|
|
UPDATE_DIR=/securityonion
|
|
|
|
|
fi
|
|
|
|
|
local v1=0
|
|
|
|
|
local v2=0
|
|
|
|
|
if [[ -f "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" ]]; then
|
|
|
|
|
v1=$(egrep " +version: " "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]')
|
|
|
|
|
fi
|
|
|
|
|
if [[ -f "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" ]]; then
|
|
|
|
|
v2=$(egrep " +version: " "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]')
|
|
|
|
|
fi
|
|
|
|
|
local highest_es_version=$(compare_es_versions "$v1" "$v2")
|
|
|
|
|
local image=$i:$highest_es_version$IMAGE_TAG_SUFFIX
|
|
|
|
|
local sig_url=https://sigs.securityonion.net/es-$highest_es_version/$image.sig
|
|
|
|
|
else
|
|
|
|
|
# this is not an es container so use the so version for the version
|
|
|
|
|
local image=$i:$VERSION$IMAGE_TAG_SUFFIX
|
|
|
|
|
local sig_url=https://sigs.securityonion.net/$VERSION/$image.sig
|
|
|
|
|
fi
|
|
|
|
|
# Pull down the trusted docker image
|
|
|
|
|
run_check_net_err \
|
|
|
|
|
"docker pull $CONTAINER_REGISTRY/$IMAGEREPO/$image" \
|
|
|
|
|
"Could not pull $image, please ensure connectivity to $CONTAINER_REGISTRY" >> "$LOG_FILE" 2>&1
|
|
|
|
|
local jobs=$IMAGE_PARALLEL_JOBS
|
|
|
|
|
|
|
|
|
|
# Get signature
|
|
|
|
|
run_check_net_err \
|
|
|
|
|
"curl --retry 5 --retry-delay 60 -A '$CURLTYPE/$CURRENTVERSION/$OS/$(uname -r)' $sig_url --output $SIGNPATH/$image.sig" \
|
|
|
|
|
"Could not pull signature file for $image, please ensure connectivity to https://sigs.securityonion.net " \
|
|
|
|
|
noretry >> "$LOG_FILE" 2>&1
|
|
|
|
|
# Dump our hash values
|
|
|
|
|
DOCKERINSPECT=$(docker inspect $CONTAINER_REGISTRY/$IMAGEREPO/$image)
|
|
|
|
|
|
|
|
|
|
echo "$DOCKERINSPECT" | jq ".[0].RepoDigests[] | select(. | contains(\"$CONTAINER_REGISTRY\"))" > $SIGNPATH/$image.txt
|
|
|
|
|
echo "$DOCKERINSPECT" | jq ".[0].Created, .[0].RootFS.Layers" >> $SIGNPATH/$image.txt
|
|
|
|
|
|
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
|
echo "Unable to inspect $image" >> "$LOG_FILE" 2>&1
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
GPGTEST=$(gpg --verify $SIGNPATH/$image.sig $SIGNPATH/$image.txt 2>&1)
|
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
|
if [[ -z "$SKIP_TAGPUSH" ]]; then
|
|
|
|
|
# Tag it with the new registry destination
|
|
|
|
|
if [ -z "$HOSTNAME" ]; then
|
|
|
|
|
HOSTNAME=$(hostname)
|
|
|
|
|
fi
|
|
|
|
|
docker tag $CONTAINER_REGISTRY/$IMAGEREPO/$image $HOSTNAME:5000/$IMAGEREPO/$image >> "$LOG_FILE" 2>&1 || {
|
|
|
|
|
echo "Unable to tag $image" >> "$LOG_FILE" 2>&1
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
# Push to the embedded registry via a registry-to-registry copy. Avoids
|
|
|
|
|
# `docker push`, which on Docker 29.x with the containerd image store
|
|
|
|
|
# represents freshly-pulled images as an index whose layer content
|
|
|
|
|
# isn't reachable through the push path. The local `docker tag` above
|
|
|
|
|
# is preserved so so-image-pull's `:5000` existence check still works.
|
|
|
|
|
# Pin to the digest already gpg-verified above so we copy exactly the
|
|
|
|
|
# bytes we approved.
|
|
|
|
|
local VERIFIED_REF
|
|
|
|
|
VERIFIED_REF=$(echo "$DOCKERINSPECT" | jq -r ".[0].RepoDigests[] | select(. | contains(\"$CONTAINER_REGISTRY\"))" | head -n 1)
|
|
|
|
|
if [ -z "$VERIFIED_REF" ] || [ "$VERIFIED_REF" = "null" ]; then
|
|
|
|
|
echo "Unable to determine verified digest for $image" >> "$LOG_FILE" 2>&1
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
docker buildx imagetools create --tag $HOSTNAME:5000/$IMAGEREPO/$image "$VERIFIED_REF" >> "$LOG_FILE" 2>&1 || {
|
|
|
|
|
echo "Unable to copy $image to embedded registry" >> "$LOG_FILE" 2>&1
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "There is a problem downloading the $image image. Details: " >> "$LOG_FILE" 2>&1
|
|
|
|
|
echo "" >> "$LOG_FILE" 2>&1
|
|
|
|
|
echo $GPGTEST >> "$LOG_FILE" 2>&1
|
|
|
|
|
# Nothing is copied or pulled unless every image verified (a bad signature
|
|
|
|
|
# publishes nothing). The hook runs in the parent: it mutates caller globals.
|
|
|
|
|
local progress_hook
|
|
|
|
|
if [ -z "$PROGRESS_CALLBACK" ]; then
|
|
|
|
|
progress_hook=log_downloading
|
|
|
|
|
else
|
|
|
|
|
progress_hook=$PROGRESS_CALLBACK
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# One preflight before the pool, so every verify job does not spend the long
|
|
|
|
|
# retry budget in parallel. It is a quick check: a definite answer from the
|
|
|
|
|
# registry ends it at once, and only an unclear error gets one more try.
|
|
|
|
|
# Upstream registry, not :5000. --foreground keeps it in this shell's process
|
|
|
|
|
# group, so Ctrl-C reaches it before launch_pool installs its own trap.
|
|
|
|
|
if [ ${#TRUSTED_CONTAINERS[@]} -gt 0 ]; then
|
|
|
|
|
local preflight_image preflight_ref preflight_out
|
|
|
|
|
local attempt=1 preflight_ok=0 preflight_kind=transient
|
|
|
|
|
read -r preflight_image _ < <(resolve_image "${TRUSTED_CONTAINERS[0]}")
|
|
|
|
|
preflight_ref=$(image_ref "$preflight_image")
|
|
|
|
|
while [ "$attempt" -le "$PREFLIGHT_ATTEMPTS" ]; do
|
|
|
|
|
if preflight_out=$(timeout --foreground "$IMAGE_INSPECT_TIMEOUT" \
|
|
|
|
|
docker buildx imagetools inspect --raw "$preflight_ref" 2>&1); then
|
|
|
|
|
preflight_ok=1
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
# A registry answer never changes on a retry; anything else might.
|
|
|
|
|
case "$preflight_out" in
|
|
|
|
|
*"not found"*|*"manifest unknown"*) preflight_kind=notfound ;;
|
|
|
|
|
*denied*|*unauthorized*) preflight_kind=denied ;;
|
|
|
|
|
esac
|
|
|
|
|
if [ "$preflight_kind" != transient ]; then
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
attempt=$((attempt + 1))
|
|
|
|
|
if [ "$attempt" -le "$PREFLIGHT_ATTEMPTS" ]; then
|
|
|
|
|
sleep "$PREFLIGHT_RETRY_DELAY"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if [ "$preflight_ok" -ne 1 ]; then
|
|
|
|
|
echo "Could not inspect $preflight_ref on $CONTAINER_REGISTRY" >>"$LOG_FILE" 2>&1
|
|
|
|
|
case "$preflight_kind" in
|
|
|
|
|
notfound) echo "The image or tag for $preflight_image was not found" >>"$LOG_FILE" 2>&1 ;;
|
|
|
|
|
denied) echo "Access to $preflight_ref was denied by $CONTAINER_REGISTRY" >>"$LOG_FILE" 2>&1 ;;
|
|
|
|
|
*) echo "Check connectivity to $CONTAINER_REGISTRY or the proxy" >>"$LOG_FILE" 2>&1 ;;
|
|
|
|
|
esac
|
|
|
|
|
echo "$preflight_out" >>"$LOG_FILE" 2>&1
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
launch_pool verify_trusted_image verify "$jobs" "$progress_hook" "${TRUSTED_CONTAINERS[@]}"
|
|
|
|
|
|
|
|
|
|
pool_collect verify "${TRUSTED_CONTAINERS[@]}" || exit 1
|
|
|
|
|
|
|
|
|
|
# Verify only from here: no copy and no local pull.
|
|
|
|
|
if [[ -n "$SKIP_TAGPUSH" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local -A ref_manifest=()
|
|
|
|
|
for i in "${TRUSTED_CONTAINERS[@]}"; do
|
|
|
|
|
{ read -r _; read -r _; read -r ref_manifest["$i"]; } < "$SIGNPATH/$i.ref"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Copy in waves: a wave's images share no unstored blob, so each layer is
|
|
|
|
|
# downloaded once. The parent must record the blob map (subshells can't).
|
|
|
|
|
local -a remaining=("${TRUSTED_CONTAINERS[@]}")
|
|
|
|
|
local wave_no=0
|
|
|
|
|
while [ ${#remaining[@]} -gt 0 ]; do
|
|
|
|
|
wave_no=$((wave_no + 1))
|
|
|
|
|
local -a wave=() deferred=()
|
|
|
|
|
local -A claimed=()
|
|
|
|
|
local img digest conflict
|
|
|
|
|
for img in "${remaining[@]}"; do
|
|
|
|
|
conflict=0
|
|
|
|
|
while IFS= read -r digest; do
|
|
|
|
|
[ -z "$digest" ] && continue
|
|
|
|
|
[ -n "${pushed_blob_repo[$digest]:-}" ] && continue
|
|
|
|
|
if [ -n "${claimed[$digest]:-}" ]; then
|
|
|
|
|
conflict=1
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
done < <(manifest_blob_digests "${ref_manifest[$img]}")
|
|
|
|
|
if [ "$conflict" -eq 0 ]; then
|
|
|
|
|
wave+=("$img")
|
|
|
|
|
while IFS= read -r digest; do
|
|
|
|
|
[ -n "$digest" ] && claimed["$digest"]=1
|
|
|
|
|
done < <(manifest_blob_digests "${ref_manifest[$img]}")
|
|
|
|
|
else
|
|
|
|
|
deferred+=("$img")
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if [ ${#wave[@]} -eq 0 ]; then
|
|
|
|
|
wave=("${remaining[0]}")
|
|
|
|
|
deferred=("${remaining[@]:1}")
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "Copying images (batch $wave_no): ${wave[*]}" >> "$LOG_FILE" 2>&1
|
|
|
|
|
launch_pool copy_verified_image copy "$jobs" "" "${wave[@]}"
|
|
|
|
|
|
|
|
|
|
pool_collect copy "${wave[@]}" || exit 1
|
|
|
|
|
for img in "${wave[@]}"; do
|
|
|
|
|
record_shared_blobs "$img" "${ref_manifest[$img]}" pushed_blob_repo
|
|
|
|
|
done
|
|
|
|
|
remaining=("${deferred[@]}")
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
launch_pool pull_verified_image pull "$jobs" "" "${TRUSTED_CONTAINERS[@]}"
|
|
|
|
|
pool_collect pull "${TRUSTED_CONTAINERS[@]}" || exit 1
|
|
|
|
|
}
|