-
Notifications
You must be signed in to change notification settings - Fork 13
Move fuse commands to a sidecar for jupyter-aou #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eae92d8
fuse in a sidecar
pantherman594 1dffd85
Pass arguments as a JSON string since eval is susceptible to injection
pantherman594 ae5d87d
Fix remotefuse cleanup
pantherman594 8193f19
comment
pantherman594 613f81e
Update image version
pantherman594 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
remotefuse fusermount "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
remotefuse gcsfuse "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
remotefuse goofys "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -o errexit | ||
|
||
# SSH passes arguments as a single string, so we will encode it as a JSON array. | ||
# First escape each argument, then join them into a JSON array. | ||
ARGS="$(for ARG in "$@"; do | ||
printf "%s" "$ARG" | jq -Rs | ||
done | jq -jsc)" | ||
readonly ARGS | ||
|
||
LC_ALL=C.UTF-8 /usr/bin/ssh -i /home/jupyter/.ssh/remotefuse -T -o "StrictHostKeyChecking no" remotefuse@remotefuse "$ARGS" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
FROM debian:latest | ||
|
||
RUN apt-get update --yes && \ | ||
apt-get install -yq --no-install-recommends \ | ||
jq \ | ||
openssh-server \ | ||
sudo \ | ||
curl \ | ||
lsb-release \ | ||
inotify-tools \ | ||
wget \ | ||
locales \ | ||
# gcloud CLI dependencies | ||
apt-transport-https \ | ||
ca-certificates \ | ||
gnupg \ | ||
fuse \ | ||
# aws CLI dependencies | ||
libc6 \ | ||
groff | ||
|
||
# Install gcloud CLI and gcsfuse | ||
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" > /etc/apt/sources.list.d/google-cloud-sdk.list \ | ||
&& echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt gcsfuse-$(lsb_release -c -s) main" > /etc/apt/sources.list.d/gcsfuse.list \ | ||
&& wget -qO- https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg \ | ||
&& apt-get update -y \ | ||
&& apt-get install -yq --no-install-recommends google-cloud-cli gcsfuse | ||
|
||
# Install aws CLI and goofys for s3 bucket mounting | ||
RUN apt-get update -y \ | ||
&& apt-get install -yq --no-install-recommends awscli \ | ||
&& wget "https://github.com/kahing/goofys/releases/latest/download/goofys" -O goofys \ | ||
&& chmod +x goofys \ | ||
&& mv goofys /usr/local/bin/ | ||
|
||
COPY remotefuse /remotefuse | ||
RUN chmod +x /remotefuse | ||
COPY entrypoint.sh /entrypoint.sh | ||
|
||
ENV USER=remotefuse | ||
ENV UID=1000 | ||
ENV USER_HOME_DIR=/home/remotefuse | ||
|
||
# Create a user with the shell set to /remotefuse. This prevents the user from | ||
# executing any other commands | ||
RUN useradd -l -m -d $USER_HOME_DIR \ | ||
-u $UID \ | ||
-g users \ | ||
-s /remotefuse $USER \ | ||
# Hide the motd and last login message | ||
&& touch $USER_HOME_DIR/.hushlogin \ | ||
# Uncomment user_allow_other in the fuse.conf to enable non-root user to mount files with -o allow-other option. | ||
&& sed -i '/user_allow_other/s/^#//g' /etc/fuse.conf | ||
|
||
ENTRYPOINT [ "/bin/sh", "/entrypoint.sh" ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o xtrace | ||
|
||
cleanup() { | ||
find /home/remotefuse/workspace/ -mindepth 1 -maxdepth 1 -type d -exec fusermount -u {} \; | ||
find /home/remotefuse/workspace/ -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} \; | ||
exit | ||
} | ||
trap cleanup INT TERM | ||
|
||
process_key() { | ||
if [ ! -f /ssh-keys/remotefuse.pub ]; then | ||
return | ||
fi | ||
|
||
# Add the app service's public key to authorized_keys in restricted mode | ||
(echo -n 'restrict '; cat /ssh-keys/remotefuse.pub) > "$SSH_DIR/authorized_keys" | ||
# Immediately remove the public key from the volume, so that we won't | ||
# try to reuse it. The main application container will generate a new | ||
# one. | ||
rm -f /ssh-keys/remotefuse.pub | ||
} | ||
|
||
watch_keys() { | ||
inotifywait -m -e create -e moved_to /ssh-keys | | ||
while read -r REPLY; do | ||
process_key | ||
done | ||
} | ||
|
||
readonly SSH_DIR="/home/remotefuse/.ssh" | ||
|
||
# SSH Key setup | ||
mkdir -p "$SSH_DIR" | ||
touch "$SSH_DIR/authorized_keys" | ||
chown -R remotefuse:users "$SSH_DIR" | ||
chmod 600 "$SSH_DIR/authorized_keys" | ||
|
||
process_key | ||
service ssh start | ||
|
||
# Keep the container running, but in the background so that interrupts can be | ||
# caught | ||
watch_keys & | ||
wait $! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/bin/bash | ||
set -o errexit | ||
|
||
if [ "$1" == "-c" ]; then | ||
shift | ||
fi | ||
|
||
# SSH commands are passed as a single string, so we need to split it into an | ||
# array. Arguments are expected to be a JSON array | ||
readarray -t ESCAPED_ARGS < <(jq -c '.[]' <<< "$1") | ||
readonly ESCAPED_ARGS | ||
|
||
# ESCAPED_ARGS are escaped JSON strings, so we need to unescape them | ||
ORIG_ARGS=() | ||
for ARG in "${ESCAPED_ARGS[@]}"; do | ||
ORIG_ARGS+=("$(jq -r <<< "$ARG")") | ||
done | ||
|
||
readonly COMMAND="${ORIG_ARGS[0]}" | ||
case "$COMMAND" in | ||
gcsfuse|goofys) | ||
;; | ||
fusermount) | ||
if [ "${ORIG_ARGS[1]}" != "-u" ]; then | ||
echo "Error: remotefuse fusermount must be called with -u option." | ||
exit 1 | ||
fi | ||
;; | ||
*) | ||
echo "Usage: remotefuse {gcsfuse|goofys|fusermount -u} [args...]" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
ORIG_ARGS=("${ORIG_ARGS[@]:1}") | ||
readonly ORIG_ARGS | ||
|
||
readonly PATH_MATCHER="^.+\/workspace\/(.+)$" | ||
ARGS=() | ||
for ARG in "${ORIG_ARGS[@]}"; do | ||
# Look for a workbench path in the arguments and replace it with one under | ||
# /home/remotefuse/workbench. | ||
# e.g. /home/jupyter/workbench/abc/def will be replaced with | ||
# /home/remotefuse/workbench/abc/def | ||
# | ||
# $PATH_MATCHER cannot be quoted, otherwise it will treat it as string | ||
# matching. | ||
if [[ "$ARG" =~ $PATH_MATCHER ]]; then | ||
MOUNT_PATH="/home/remotefuse/workspace/${BASH_REMATCH[1]}" | ||
ARGS+=("$MOUNT_PATH") | ||
else | ||
ARGS+=("$ARG") | ||
fi | ||
done | ||
readonly ARGS | ||
|
||
${COMMAND} "${ARGS[@]}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/bash | ||
|
||
# This script is a custom startup script for jupyter aou. It is used to generate | ||
# an SSH key pair for the jupyter user to be used in the remotefuse sidecar. | ||
# /ssh-keys should be a volume mounted to both containers. It also sets | ||
# permissions on /home/jupyter/workspace to allow the jupyter user to read/write to | ||
# it. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
set -o xtrace | ||
|
||
readonly USER_NAME="jupyter" | ||
readonly RUN_AS_LOGIN_USER="sudo -u ${USER_NAME} bash -l -c" | ||
|
||
rm -rf "/home/${USER_NAME}/.ssh" | ||
${RUN_AS_LOGIN_USER} "mkdir -p '/home/${USER_NAME}/.ssh'" | ||
${RUN_AS_LOGIN_USER} "ssh-keygen -q -f '/home/${USER_NAME}/.ssh/remotefuse' -N ''" | ||
cp "/home/${USER_NAME}/.ssh/remotefuse.pub" /ssh-keys/remotefuse.pub | ||
|
||
# The remaining commands are expected to fail if this is not the first run | ||
set +o errexit | ||
|
||
# This will fail if any resources are already mounted, since the mounted | ||
# resources can't be chowned | ||
chown -R ${USER_NAME}:users "/home/${USER_NAME}/workspace" | ||
|
||
# Modify the startup script so that /opt/remotefuse always takes priority over | ||
# /usr/bin | ||
sed -i 's/export PATH=\/usr\/bin:/export PATH=\/opt\/remotefuse:\/usr\/bin:/g' /workspace/startupscript/post-startup.sh |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.