|
| 1 | +#!/usr/bin/env bash |
| 2 | +#exit on error |
| 3 | +set -e |
| 4 | + |
| 5 | +readonly USERBASE="run" |
| 6 | +readonly BASHPATH="/bin/bash" |
| 7 | +readonly HOMEPATH="/home" |
| 8 | + |
| 9 | +function echo_error_and_exit { |
| 10 | + echo -e "ERROR: " "$@" >&2 |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +# make sure entrypoint is running as root |
| 15 | +if [[ $(id -u) -ne 0 ]]; then |
| 16 | + echo_error_and_exit "Container must run as root. Use environment variable USERID to set user.\n" \ |
| 17 | + "Example: \"TAG=latest && " \ |
| 18 | + "docker run -e USERID=$(id -u):$(id -g) -v $(pwd):/lint -w /lint ghcr.io/antonbabenko/pre-commit-terraform:$TAG run -a\"" |
| 19 | +fi |
| 20 | + |
| 21 | +# make sure USERID makes sense as UID:GID |
| 22 | +# it looks like the alpine distro limits UID and GID to 256000, but |
| 23 | +# could be more, so we accept any valid integers |
| 24 | +USERID=${USERID:-"0:0"} |
| 25 | +if [[ ! $USERID =~ ^[0-9]+:[0-9]+$ ]]; then |
| 26 | + echo_error_and_exit "USERID environment variable invalid, format is userid:groupid. Received: \"$USERID\"" |
| 27 | +fi |
| 28 | + |
| 29 | +# separate uid and gid |
| 30 | +uid=${USERID%%:*} |
| 31 | +gid=${USERID##*:} |
| 32 | + |
| 33 | +# if requested UID:GID is root, go ahead and run without other processing |
| 34 | +[[ $USERID == "0:0" ]] && exec su-exec "$USERID" pre-commit "$@" |
| 35 | + |
| 36 | +# make sure workdir and some files are readable/writable by the provided UID/GID |
| 37 | +# combo, otherwise will have errors when processing hooks |
| 38 | +wdir="$(pwd)" |
| 39 | +if ! su-exec "$USERID" "$BASHPATH" -c "test -w $wdir && test -r $wdir"; then |
| 40 | + echo_error_and_exit "uid:gid $USERID lacks permissions to $wdir/" |
| 41 | +fi |
| 42 | +wdirgitindex="$wdir/.git/index" |
| 43 | +if ! su-exec "$USERID" "$BASHPATH" -c "test -w $wdirgitindex && test -r $wdirgitindex"; then |
| 44 | + echo_error_and_exit "uid:gid $USERID cannot write to $wdirgitindex" |
| 45 | +fi |
| 46 | + |
| 47 | +# check if group by this GID already exists, if so get the name since adduser |
| 48 | +# only accepts names |
| 49 | +if groupinfo="$(getent group "$gid")"; then |
| 50 | + groupname="${groupinfo%%:*}" |
| 51 | +else |
| 52 | + # create group in advance in case GID is different than UID |
| 53 | + groupname="$USERBASE$gid" |
| 54 | + if ! err="$(addgroup -g "$gid" "$groupname" 2>&1)"; then |
| 55 | + echo_error_and_exit "failed to create gid \"$gid\" with name \"$groupname\"\ncommand output: \"$err\"" |
| 56 | + fi |
| 57 | +fi |
| 58 | + |
| 59 | +# check if user by this UID already exists, if so get the name since id |
| 60 | +# only accepts names |
| 61 | +if userinfo="$(getent passwd "$uid")"; then |
| 62 | + username="${userinfo%%:*}" |
| 63 | +else |
| 64 | + username="$USERBASE$uid" |
| 65 | + if ! err="$(adduser -h "$HOMEPATH$username" -s "$BASHPATH" -G "$groupname" -D -u "$uid" -k "$HOME" "$username" 2>&1)"; then |
| 66 | + echo_error_and_exit "failed to create uid \"$uid\" with name \"$username\" and group \"$groupname\"\ncommand output: \"$err\"" |
| 67 | + fi |
| 68 | +fi |
| 69 | + |
| 70 | +# it's possible it was not in the group specified, add it |
| 71 | +if ! idgroupinfo="$(id -G "$username" 2>&1)"; then |
| 72 | + echo_error_and_exit "failed to get group list for username \"$username\"\ncommand output: \"$idgroupinfo\"" |
| 73 | +fi |
| 74 | +if [[ ! " $idgroupinfo " =~ [[:blank:]]${gid}[[:blank:]] ]]; then |
| 75 | + if ! err="$(addgroup "$username" "$groupname" 2>&1)"; then |
| 76 | + echo_error_and_exit "failed to add user \"$username\" to group \"$groupname\"\ncommand output: \"$err\"" |
| 77 | + fi |
| 78 | +fi |
| 79 | + |
| 80 | +# user and group of specified UID/GID should exist now, and user should be |
| 81 | +# a member of group, so execute pre-commit |
| 82 | +exec su-exec "$USERID" pre-commit "$@" |
0 commit comments