From 2bf4502d06b105c240baaff731a911271df6ee2f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 4 Apr 2023 15:45:16 -0400 Subject: [PATCH] docker: Add set_user/start_docker scripts Leave the image's default user as 'root' so we can modify the non-root user's uid/gid. This is a common technique used in containers to gain file permissions in mounted dirs. The default user's uid/gid can't be changed because a bash process runs under their ownership as long as the container is up. A user's uid/gid cannot be changed while that user owns a process (at least not via 'usermod'). --- docker/sparkd/dev/Dockerfile | 3 - docker/sparkd/dev/devcontainer.json | 1 + docker/sparkd/dev/set_user.sh | 250 ++++++++++++++++++++++++++++ docker/sparkd/dev/start_docker.sh | 229 +++++++++++++++++++++++++ 4 files changed, 480 insertions(+), 3 deletions(-) create mode 100755 docker/sparkd/dev/set_user.sh create mode 100755 docker/sparkd/dev/start_docker.sh diff --git a/docker/sparkd/dev/Dockerfile b/docker/sparkd/dev/Dockerfile index 7dec02e23..e2095a716 100644 --- a/docker/sparkd/dev/Dockerfile +++ b/docker/sparkd/dev/Dockerfile @@ -101,6 +101,3 @@ RUN \ -G sudo \ --password "$(openssl passwd -1 "$docker_pass")" \ "$docker_user" - -# Set the default user -USER $docker_user diff --git a/docker/sparkd/dev/devcontainer.json b/docker/sparkd/dev/devcontainer.json index ffd1e7f4d..b413c0d03 100644 --- a/docker/sparkd/dev/devcontainer.json +++ b/docker/sparkd/dev/devcontainer.json @@ -2,6 +2,7 @@ "image": "johncolvin/bluetradingsystems:sparkd-dev", "initializeCommand": "${localWorkspaceFolder}/docker/sparkd/dev/devcontainer_initialize.sh", "postCreateCommand": "/home/bts/spark/docker/sparkd/dev/devcontainer_post_create.sh", + "containerUser": "bts", "workspaceMount": "source=${localWorkspaceFolder},target=/home/bts/spark,type=bind", "workspaceFolder": "/home/bts/spark", "updateRemoteUserUID": true, diff --git a/docker/sparkd/dev/set_user.sh b/docker/sparkd/dev/set_user.sh new file mode 100755 index 000000000..23f854191 --- /dev/null +++ b/docker/sparkd/dev/set_user.sh @@ -0,0 +1,250 @@ +#!/usr/bin/env bash + +function _exit() { + if [[ "$#" -gt 1 ]]; then + # When there are multiple args, the last arg is the exit code. + echo "${@:1:($# - 1)}" + exit "${@: -1}" + fi + echo "$1" + exit 0 +} + +help_text="\ +Add or update a user's uid + +This script is intended to be run in a Docker container as a means to obtain +file permissions in mounted directories owned by the specified UID. + +Usage: set_user.sh [OPTIONS] USER UID + +Positionals: + USER The name of the user + +Options: + --user USER The name of the user (defaults to 'bts') + --uid UID The new uid to give USER (defaults to '1000') + --group GROUP The primary group to give USER (defaults to USER) + --gid GID The primary gid to give USER (defaults to UID) + --pass PASSWORD The password to give USER (defaults to USER, only applies + if USER hasn't been created yet) + --modify-existing Update UID/GID even if USER already exists + -h,--help Print this help message and exit + +" + +function _add_group() { + if [[ "$#" -ne 2 ]]; then + _exit "'_add_group' requires exactly 2 args (group, gid). Received $#." 1 + fi + local group="$1" + local gid="$2" + # Check if gid already exists + local existing_group="" + existing_group="$(getent group "$gid" | cut -d : -f 1)" + if [[ -n "$existing_group" ]]; then + if [[ "$existing_group" != "$group" ]]; then + echo "\ +The gid '$gid' is already in use by group '$existing_group' (will not rename \ +the group)" + fi + return + fi + + # Check if group with same name already exists + local existing_gid="" + existing_gid="$(getent group "$group" | cut -d : -f 3)" + if [[ -n "$existing_gid" ]]; then + # The group already exists. Only modify if --modify-existing was specified. + if [[ "$modify_existing" -eq 0 ]]; then + _exit "\ +The group '$group' already exists with gid '$existing_gid'. +Specify --modify-existing to change the gid anyways." 1 + fi + echo "Changing gid for group '$group' from '$existing_gid' to '$gid'" + groupmod -g "$gid" "$group" + else + # The group doesn't exist. Add it + echo "Adding group '$group:$gid'" + groupadd -g "$gid" "$group" + fi +} + +function _add_user() { + local user="$1" + local uid="$2" + local group="$3" + local gid="$4" + local pass="$5" + if [[ "$#" -ne 5 ]]; then + _exit "'_add_user' requires exactly 5 args (user, uid, group, gid, pass). \ +Received $#." 1 + fi + + # Check if uid is being used by another user + local existing_user="" + existing_user="$(getent passwd "$uid" | cut -d : -f 1)" + if [[ -n "$existing_user" ]]; then + if [[ "$existing_user" != "$user" ]]; then + _exit "The uid '$uid' is already in use by user '$existing_user'." 1 + fi + + echo "The user '$user' already has the specified uid '$uid'." + # Modify gid if necessary + local existing_gid="" + existing_gid="$(id -g "$user" 2> /dev/null)" + if [[ "$existing_gid" = "$gid" ]]; then + echo "The user '$user' already has the specified gid '$gid'." + return + fi + + if [[ "$modify_existing" -eq 0 ]]; then + _exit "\ +The user '$user' is associated with the primary group '$existing_gid'. +Specify --modify-existing to change the gid anyways." 1 + fi + + echo "Changing primary group for '$user' from '$existing_gid' to '$gid'." + usermod -g "$gid" "$user" + return + fi + + # Check if user with same name already exists + local existing_uid="" + existing_uid="$(id -u "$user" 2> /dev/null)" + if [[ -n "$existing_uid" ]]; then + if [[ "$existing_uid" = "$uid" ]]; then + _exit "The user '$user' already has the specified uid '$uid'" 0 + fi + + if [[ "$modify_existing" -eq 0 ]]; then + _exit "\ +User '$user' already exists with uid '$existing_uid'. +Specify --modify-existing to change the uid anyways." 1 + fi + + echo "Changing uid for user '$user' from '$existing_uid' to '$uid'" + if ! usermod -u "$uid" -g "$gid" "$user"; then + _exit "usermod exited with code $?" 1 + fi + return + fi + + # The user doesn't exist. Add it + echo "Adding user '$user:$uid' (group='$group:$gid', password='$pass')" + useradd -m -u "$uid" -g "$gid" -G sudo --password "$(openssl passwd -1 "$pass")" "$user" +} + +function _update_fish_dir_perms() { + local user="$1" + local uid="$2" + local gid="$3" + # When a fish shell session starts, a temp dir named '/tmp/fish.' is + # created with the user's current uid/gid. If such a dir exists for the + # specified USER, make sure the uid/gid match the new values. + fish_dir="/tmp/fish.$user" + if [[ -d "$fish_dir" ]]; then + perms="$uid:$gid" + existing_perms="$(stat -c %u:%g "$fish_dir")" + if [[ "$existing_perms" != "$perms" ]]; then + echo "Changing uid:gid for dir '$fish_dir' from '$existing_perms' to '$perms'" + chown -R "$perms" "$fish_dir" + fi + fi +} + +user="" +group="" +pass="" +declare -i uid=0 +declare -i gid=0 +declare -i modify_existing=0 + +while [[ $# -gt 0 ]]; do + case "$1" in + --user) + if [[ -z "$2" ]]; then + _exit "USER (--user) must be non-empty" 1 + elif [[ "$2" = "root" ]]; then + _exit "USER (--user) cannot be 'root'" 1 + fi + user="$2" + shift 2 + ;; + --group) + if [[ -z "$2" ]]; then + _exit "GROUP (--group) must be non-empty" 1 + elif [[ "$2" = "root" ]]; then + _exit "GROUP (--group) cannot be 'root'" 1 + fi + group="$2" + shift 2 + ;; + --uid) + if [[ -z "$2" ]]; then + _exit "UID (--uid) must be non-empty" 1 + fi + uid="$2" + shift 2 + ;; + --gid) + if [[ -z "$2" ]]; then + _exit "GID (--gid) must be non-empty" 1 + fi + gid="$2" + shift 2 + ;; + --pass) + if [[ -z "$2" ]]; then + _exit "PASS (--pass) must be non-empty" 1 + fi + pass="$2" + shift 2 + ;; + --modify-existing) + modify_existing=1 + shift 1 + ;; + -h) + ;& + --help) + _exit "$help_text" 0 + ;; + *) + if [[ -z "$1" ]]; then + _exit "Invalid positional argument '$1'" 1 + elif [[ -z "$user" ]]; then + user="$1" + elif [[ -z "$uid" ]]; then + uid="$1" + else + _exit "Unexpected argument '$1'" 1 + fi + shift 1 + ;; + esac +done + +if [[ -z "$user" ]]; then + user="bts" +fi + +if [[ $uid -eq 0 ]]; then + uid=1000 +fi + +# In general, a user's primary group/gid matches the user's name/uid. +# If group/gid args weren't specified, set them to name/uid. +if [[ -z "$group" ]]; then + group="$user" +fi +if [[ $gid -eq 0 ]]; then + gid=$uid +fi +if [[ -z "$pass" ]]; then + pass="$user" +fi + +_add_group "$group" "$gid" +_add_user "$user" "$uid" "$group" "$gid" "$pass" +_update_fish_dir_perms "$user" "$uid" "$gid" diff --git a/docker/sparkd/dev/start_docker.sh b/docker/sparkd/dev/start_docker.sh new file mode 100755 index 000000000..45b7a5c80 --- /dev/null +++ b/docker/sparkd/dev/start_docker.sh @@ -0,0 +1,229 @@ +#!/usr/bin/env bash + +function _exit() { + if [[ "$#" -gt 1 ]]; then + # When there are multiple args, the last arg is the exit code. + echo "${@:1:($# - 1)}" + exit "${@: -1}" + fi + echo "$1" + exit 0 +} + +help_text="\ +Start a docker dev container +Usage: start_docker.sh [OPTIONS] image container + +Positionals: + image The image to instantiate + container The name for the container + +Options: + -u,--user USER The user to add in the container (defaults to 'bts') + -p,--pass PASS The password for USER in the container (defaults to USER) + -i,--image IMAGE The image to instantiate + -c,--container CONT The name for the container + -m,--mount MOUNT The name for the folder (outside the container) to create/mount to the home + directory in the container. Mount folders are added here: $mount_prefix + --mount-existing Mount the specified dir even if it already exists + --port : OR [:,...] Port mapping(s). May be + specified multiple times. + -h,--help Print this help message and exit + +" + +mount_prefix="$HOME/.sparkd-dev/mounts" +declare docker_user="" +declare docker_pass="" +declare image_name="" +declare cont_name="" +declare mount_name="" +declare -i mount_existing=0 +declare -a port_mappings=() + +while [[ $# -gt 0 ]]; do + case "$1" in + -u) + ;& + --user) + if [[ -z "$2" ]]; then + _exit "User (-u,--user) must be non-empty" 1 + fi + docker_user="$2" + shift 2 + ;; + -p) + ;& + --pass) + if [[ -z "$2" ]]; then + _exit "Password (-p,--pass) must be non-empty" 1 + fi + docker_pass="$2" + shift 2 + ;; + -i) + ;& + --image) + if [[ -n "$image_name" ]]; then + _exit "Unexpected argument '$1'" 1 + elif [[ -z "$2" ]]; then + _exit "Image (-i,--image) must be non-empty" 1 + fi + image_name="$2" + shift 2 + ;; + -c) + ;& + --container) + if [[ -n "$cont_name" ]]; then + _exit "Unexpected argument '$1'" 1 + elif [[ -z "$2" ]]; then + _exit "Container (-c,--container) must be non-empty" 1 + fi + cont_name="$2" + shift 2 + ;; + --port) + # Parse port(s) arg, and append the result to 'port_mappings' + # Note: port(s) may be specified in bracketed notation: + # [a:b,c:d,...,y:z] + next_ports="$(echo "$2" | sed -e 's/\[//g' -e 's/\]//g' -e 's/,/ /g')" + if [[ "${#port_mappings[@]}" -gt 0 ]]; then + next_ports=" $next_ports" + fi + port_mappings+=("$next_ports") + shift 2 + ;; + -m) + ;& + --mount) + if [[ -z "$2" ]]; then + echo "Mount (-m,--mount) must be non-empty" + fi + mount_name="$2" + shift 2 + ;; + --mount-existing) + mount_existing=1 + shift 1 + ;; + -h) + ;& + --help) + _exit "$help_text" 0 + ;; + *) + if [[ -z "$1" ]]; then + _exit "Invalid positional argument '$1'" "$help_text" 1 + elif [[ -z "$image_name" ]]; then + image_name="$1" + elif [[ -z "$cont_name" ]]; then + cont_name="$1" + else + _exit "Unexpected argument '$1'" "$help_text" 1 + fi + shift 1 + ;; + esac +done + + +if [[ -z "$image_name" ]]; then + image_name="johncolvin/bluetradingsystems:sparkd-dev" +fi + +host_name="$(hostname)" +if [[ -n "$cont_name" ]]; then + host_name="$host_name.$cont_name" +fi + +if [[ -z "$mount_name" ]]; then + mount_name="$cont_name" +fi + +if [[ -z "$mount_name" && -z "$cont_name" ]]; then + _exit "Please specify mount (-m,--mount) and/or container (-c,--container)" 1 +fi + +if [[ -z "$docker_user" ]]; then + docker_user="bts" +fi + +if [[ "$docker_user" = "root" ]]; then + _exit "USER (--user) cannot be 'root'" 1 +fi + +cont_home="/home/$docker_user" +home_src="$mount_prefix/$mount_name" +home_tgt="$cont_home" +this_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +repo_src="$(cd "$this_dir" && git rev-parse --show-toplevel)" +repo_tgt="$cont_home/spark" +working_dir="$cont_home" + +if [[ $mount_existing -eq 0 && -e "$home_src" ]]; then + # Don't overwrite existing dir (might be mounted in another container) + _exit "Mount dir '$home_src' already exists (use --mount-existing to mount \ +an existing directory)" 1 +fi + +mkdir -p "$home_src" + +if [[ "${#port_mappings[@]}" -eq 0 ]]; then + port_mappings=("8000:8000" "8001:8001" "8002:8002" "8003:8003") +fi + +# Adapt port_mappings into args for `docker run` by prepending "-p " to each +# item +declare -a docker_port_args=() +for port_mapping in "${port_mappings[@]}"; do + docker_port_args+=("-p $port_mapping") +done + +echo "Starting container '$cont_name' +image_name : $image_name +cont_name : $cont_name +home_src : $home_src +home_tgt : $home_tgt +repo_src : $repo_src +repo_tgt : $repo_tgt +working dir : $working_dir +ports : ${port_mappings[*]} +" + +# DON'T wrap $docker_port_args in quotes. We want bash to split args by +# whitespace +docker run \ + --net bridge \ + -h "$host_name" \ + -e "TZ=America/New_York" \ + --cap-add=SYS_PTRACE \ + --security-opt seccomp=unconfined \ + --name "$cont_name" \ + -v "$home_src:$home_tgt" \ + -v "$repo_src:$repo_tgt" \ + -w "$working_dir" \ + --shm-size=4024m \ + -t \ + -i \ + -d \ + -e DISPLAY="$DISPLAY" \ + "$image_name" + +# Add user w/ same uid and gid as the current user outside of docker, so the +# user inside the docker has file permissions in the mounted dir. +docker_group="$docker_user" +docker_uid="$(id -u "$USER")" +docker_gid="$(id -g "$USER")" +if [[ -z "$docker_pass" ]]; then + docker_pass="$docker_user" +fi +docker exec -u root -it "$cont_name" "$repo_tgt/docker/sparkd/dev/set_user.sh" \ +--user "$docker_user" \ +--uid "$docker_uid" \ +--group "$docker_group" \ +--gid "$docker_gid" \ +--pass "$docker_pass" + +# Run same script VS Code runs to initialize the container +docker exec -u "$docker_user" -it "$cont_name" "$repo_tgt/docker/sparkd/dev/devcontainer_post_create.sh"