Skip to content

Commit

Permalink
Refactored ops and whitelist processing into its own file (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Jan 27, 2022
1 parent 8a42dfe commit 21602a7
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -62,7 +62,7 @@ RUN easy-add --var os=${TARGETOS} --var arch=${TARGETARCH}${TARGETVARIANT} \
--var version=0.1.1 --var app=maven-metadata-release --file {{.app}} \
--from https://github.com/itzg/{{.app}}/releases/download/{{.version}}/{{.app}}_{{.version}}_{{.os}}_{{.arch}}.tar.gz

ARG MC_HELPER_VERSION=1.16.0
ARG MC_HELPER_VERSION=1.16.2
ARG MC_HELPER_BASE_URL=https://github.com/itzg/mc-image-helper/releases/download/v${MC_HELPER_VERSION}
RUN curl -fsSL ${MC_HELPER_BASE_URL}/mc-image-helper-${MC_HELPER_VERSION}.tgz \
| tar -C /usr/share -zxf - \
Expand Down
2 changes: 1 addition & 1 deletion scripts/start-configuration
Expand Up @@ -3,7 +3,7 @@ set -euo pipefail
IFS=$'\n\t'

# shellcheck source=start-utils
. ${SCRIPTS:-/}start-utils
. "${SCRIPTS:-/}start-utils"

: "${EULA:=}"
: "${PROXY:=}"
Expand Down
94 changes: 3 additions & 91 deletions scripts/start-finalExec
@@ -1,97 +1,9 @@
#!/bin/bash

. ${SCRIPTS:-/}start-utils
# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
isDebugging && set -x

if versionLessThan 1.7.6; then
opsFile=ops.txt
whitelistFile=white-list.txt
else
opsFile=ops.json
whitelistFile=whitelist.json
fi

function process_user_file() {
local output=$1
local source=$2

if isURL "$source"; then
log "Downloading $output from $source"
if ! get -o /data/$output "$source"; then
log "ERROR: failed to download from $source"
exit 2
fi
else
log "Copying $output from $source"
if ! cp "$source" /data/$output; then
log "ERROR: failed to copy from $source"
exit 1
fi
fi
}

function process_user_csv() {
local output=$1
local list=$2
local playerDataList

if [[ "$output" == *"ops"* ]]; then
# Extra data for ops.json
userData='{"uuid": .id, "name": .username, "level": 4}'
else
userData='{"uuid": .id, "name": .username}'
fi

log "Updating ${output%.*}"
for i in ${list//,/ }
do
if [ -e "$output" ] && grep -q "$i" "$output"; then
log "$i already present in $output, skipping"
continue
fi
if ! playerData=$(get "https://playerdb.co/api/player/minecraft/$i" | jq -re ".data.player"); then
log "WARNING: Could not lookup user $i for ${output} addition"
else
playerDataList=$playerDataList$(echo $playerData | jq -r "$userData")
fi
done
local newUsers=$(echo $playerDataList | jq -s .)
if [[ $output =~ .*\.txt ]]; then
# username list for txt config (Minecraft <= 1.7.5)
echo $newUsers | jq -r '.[].name' >> /data/${output}
sort -u /data/${output} -o /data/${output}
elif [ -e /data/${output} ]; then
# Merge with existing json file
local currentUsers=$(cat /data/${output})
jq --argjson current "$currentUsers" --argjson new "$newUsers" -n '$new + $current | unique_by(.uuid)' > /data/${output}
else
# New json file
echo $newUsers > /data/${output}
fi
}

if isTrue "${OVERRIDE_OPS}"; then
log "Recreating ${opsFile} file at server startup"
rm -f /data/${opsFile}
fi
if [ -n "${OPS_FILE}" ] && [ ! -e "/data/${opsFile}" ]; then
process_user_file ${opsFile} "$OPS_FILE"
fi
if [ -n "${OPS}" ]; then
process_user_csv ${opsFile} "$OPS"
fi

if isTrue "${OVERRIDE_WHITELIST}"; then
log "Recreating ${whitelistFile} file at server startup"
rm -f /data/${whitelistFile}
fi
if [ -n "${WHITELIST_FILE}" ] && [ ! -e "/data/${whitelistFile}" ]; then
process_user_file ${whitelistFile} "$WHITELIST_FILE"
fi
if [ -n "${WHITELIST}" ]; then
process_user_csv ${whitelistFile} "$WHITELIST"
fi

if [ -n "$ICON" ]; then
if [ ! -e server-icon.png ] || [ "${OVERRIDE_ICON}" == "TRUE" ]; then
log "Using server icon from $ICON..."
Expand Down Expand Up @@ -169,7 +81,7 @@ if versionLessThan 1.14 && [[ ${CONSOLE,,} = false ]]; then
fi

# Optional disable GUI for headless servers
if [[ ${GUI} = false || ${GUI} = FALSE ]]; then
if [[ ${GUI,,} = false ]]; then
EXTRA_ARGS+=" nogui"
fi

Expand Down
2 changes: 1 addition & 1 deletion scripts/start-setupEnvVariables
Expand Up @@ -32,4 +32,4 @@ if [[ ${PATCH_DEFINITIONS} ]]; then
"${PATCH_DEFINITIONS}"
fi

exec "${SCRIPTS:-/}start-finalExec" "$@"
exec "${SCRIPTS:-/}start-setupRbac" "$@"
98 changes: 98 additions & 0 deletions scripts/start-setupRbac
@@ -0,0 +1,98 @@
#!/bin/bash

# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
isDebugging && set -x

if versionLessThan 1.7.6; then
opsFile=ops.txt
whitelistFile=white-list.txt
else
opsFile=ops.json
whitelistFile=whitelist.json
fi

function process_user_file() {
local output=$1
local source=$2

if isURL "$source"; then
log "Downloading $output from $source"
if ! get -o "/data/$output" "$source"; then
log "ERROR: failed to download from $source"
exit 2
fi
else
log "Copying $output from $source"
if ! cp "$source" "/data/$output"; then
log "ERROR: failed to copy from $source"
exit 1
fi
fi
}

function process_user_csv() {
local output=$1
local list=$2
local playerDataList

if [[ "$output" == *"ops"* ]]; then
# Extra data for ops.json
userData='{"uuid": .id, "name": .username, "level": 4}'
else
userData='{"uuid": .id, "name": .username}'
fi

log "Updating ${output%.*}"
for i in ${list//,/ }
do
if [ -e "$output" ] && grep -q "$i" "$output"; then
log "$i already present in $output, skipping"
continue
fi
if ! playerData=$(get "https://playerdb.co/api/player/minecraft/$i" | jq -re ".data.player"); then
log "WARNING: Could not lookup user $i for ${output} addition"
else
playerDataList=$playerDataList$(echo "$playerData" | jq -r "$userData")
fi
done
local newUsers=$(echo "$playerDataList" | jq -s .)
if [[ $output =~ .*\.txt ]]; then
# username list for txt config (Minecraft <= 1.7.5)
echo $newUsers | jq -r '.[].name' >> "/data/${output}"
sort -u /data/${output} -o /data/${output}
elif [ -e /data/${output} ]; then
# Merge with existing json file
local currentUsers=$(cat "/data/${output}")
jq --argjson current "$currentUsers" --argjson new "$newUsers" -n '$new + $current | unique_by(.uuid)' > "/data/${output}"
else
# New json file
echo $newUsers > "/data/${output}"
fi
}

if isTrue "${OVERRIDE_OPS}"; then
log "Recreating ${opsFile} file at server startup"
rm -f /data/${opsFile}
fi
if [ -n "${OPS_FILE}" ] && [ ! -e "/data/${opsFile}" ]; then
process_user_file ${opsFile} "$OPS_FILE"
fi
if [ -n "${OPS}" ]; then
process_user_csv ${opsFile} "$OPS"
fi

if isTrue "${OVERRIDE_WHITELIST}"; then
log "Recreating ${whitelistFile} file at server startup"
rm -f /data/${whitelistFile}
fi
if [ -n "${WHITELIST_FILE}" ] && [ ! -e "/data/${whitelistFile}" ]; then
process_user_file ${whitelistFile} "$WHITELIST_FILE"
fi
if [ -n "${WHITELIST}" ]; then
process_user_csv ${whitelistFile} "$WHITELIST"
fi



exec "${SCRIPTS:-/}start-finalExec" "$@"
13 changes: 13 additions & 0 deletions tests/setuponlytests/ops_from_scratch/docker-compose.yml
@@ -0,0 +1,13 @@
version: "3"

services:
mc:
restart: "no"
image: ${IMAGE_TO_TEST:-itzg/minecraft-server}
environment:
EULA: "TRUE"
SETUP_ONLY: "TRUE"
VERSION: ${MINECRAFT_VERSION:-LATEST}
OPS: itzg
volumes:
- ./data:/data
3 changes: 3 additions & 0 deletions tests/setuponlytests/ops_from_scratch/verify.sh
@@ -0,0 +1,3 @@
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].name' --expect=itzg
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].uuid' --expect=5cddfd26-fc86-4981-b52e-c42bb10bfdef
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].level' --expect=4
6 changes: 3 additions & 3 deletions tests/setuponlytests/test.sh
Expand Up @@ -11,8 +11,8 @@ setupOnlyMinecraftTest(){
cd "$folder"
result=0

if ! logs=$(docker compose run --quiet-pull mc 2>&1); then
echo "${folder} setup FAILED"
if ! logs=$(docker-compose run mc 2>&1); then
echo "${folder} test scenario FAILED"
echo ":::::::::::: LOGS ::::::::::::::::
$logs
::::::::::::::::::::::::::::::::::
Expand All @@ -29,7 +29,7 @@ $logs
echo "${folder} PASS"
fi

docker compose down -v --remove-orphans
docker-compose down -v --remove-orphans
cd ..

return $result
Expand Down
15 changes: 15 additions & 0 deletions tests/setuponlytests/whitelist_from_scratch/docker-compose.yml
@@ -0,0 +1,15 @@
version: "3"

services:
mc:
restart: "no"
image: ${IMAGE_TO_TEST:-itzg/minecraft-server}
environment:
EULA: "TRUE"
SETUP_ONLY: "TRUE"
VERSION: ${MINECRAFT_VERSION:-LATEST}
WHITELIST: itzg
ENFORCE_WHITELIST: "true"
OVERRIDE_SERVER_PROPERTIES: "true"
volumes:
- ./data:/data
4 changes: 4 additions & 0 deletions tests/setuponlytests/whitelist_from_scratch/verify.sh
@@ -0,0 +1,4 @@
mc-image-helper assert jsonPathEquals --file=whitelist.json --path='$[0].name' --expect=itzg
mc-image-helper assert jsonPathEquals --file=whitelist.json --path='$[0].uuid' --expect=5cddfd26-fc86-4981-b52e-c42bb10bfdef
mc-image-helper assert propertyEquals --file=server.properties --property=white-list --expect=true
mc-image-helper assert propertyEquals --file=server.properties --property=enforce-whitelist --expect=true

0 comments on commit 21602a7

Please sign in to comment.