Skip to content
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

onyxia-init.sh / Vault : add optional mode for service account based auth (+ other fixes) #197

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions scripts/onyxia-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,45 @@ fi

if [[ -n "$VAULT_RELATIVE_PATH" ]]; then

JSON=$(wget -qO- \
--header="X-Vault-Token: $VAULT_TOKEN" \
$VAULT_ADDR/v1/$VAULT_MOUNT/data/$VAULT_TOP_DIR/$VAULT_RELATIVE_PATH )
if [[ "$VAULT_INJECTION_SA_ENABLED" = "true" && "$VAULT_INJECTION_SA_MODE" = "jwt" ]]; then
echo "using service account injection jwt to get vault token"
VAULT_TOKEN=$(vault write -field="token" auth/$VAULT_INJECTION_SA_AUTH_PATH/login role=$VAULT_INJECTION_SA_AUTH_ROLE jwt=@/var/run/secrets/kubernetes.io/serviceaccount/token)
fi

KEYS=""
# If a token is available (either personal Token injected by Onyxia UI, or SA token
# obtained in the previous paragraph), proceed to read secrets and export them as envvars
if [[ -n "$VAULT_TOKEN" ]]; then
JSON=$(wget -qO- \
--header="X-Vault-Token: $VAULT_TOKEN" \
$VAULT_ADDR/v1/$VAULT_MOUNT/data/$VAULT_TOP_DIR/$VAULT_RELATIVE_PATH )

if [ "$(jq -r '.data.data.".onyxia"' <<< "$JSON")" == "null" ]
then
KEYS=$(jq -r '.data.data | keys | .[]' <<< "$JSON")
else
KEYS=$(jq -r '.data.data.".onyxia".keysOrdering | .[]' <<< "$JSON")
fi
KEYS=""

for i in $KEYS;
do
echo $i
export "$i=$(eval echo $(jq -r ".data.data.$i" <<< "$JSON"))"
if [[ $SUDO -eq 0 ]]; then
echo "sudo alternative"
sudo sh -c "echo $i=\"`jq -r \".data.data.$i\" <<< \"$JSON\"`\" >> /etc/environment"
if [[ -e "${R_HOME}/etc/" ]]; then
sudo sh -c "echo $i=\"`jq -r \".data.data.$i\" <<< \"$JSON\"`\" >> ${R_HOME}/etc/Renviron.site"
fi
if [ "$(jq -r '.data.data.".onyxia"' <<< "$JSON")" == "null" ]
then
KEYS=$(jq -r '.data.data | keys | .[]' <<< "$JSON")
else
echo "not sudo alternative"
sh -c "echo export $i=\"`jq -r \".data.data.$i\" <<< \"$JSON\"`\" >> ~/.bashrc"
if [[ -e "${R_HOME}/etc/" ]]; then
sh -c "echo $i=\"`jq -r \".data.data.$i\" <<< \"$JSON\"`\" >> ${R_HOME}/etc/Renviron.site"
fi
KEYS=$(jq -r '.data.data.".onyxia".keysOrdering | .[]' <<< "$JSON")
fi
done

for i in $KEYS;
do
echo $i
value=$(jq -r .data.data.$i <<< $JSON)
export $i="${value}"
if [[ $SUDO -eq 0 ]]; then
sudo sh -c "printf '%s=\"%s\"\n' $i \"$value\" >> /etc/environment"
if command -v R; then
sudo sh -c "printf '%s=\"%s\"\n' $i \"$value\" >> ${R_HOME}/etc/Renviron.site"
fi
else
sh -c "printf 'export %s=\"%s\"\n' $i \"$value\" >> ~/.bashrc"
if command -v R; then
sh -c "printf '%s=\"%s\"\n' $i \"$value\" >> ${R_HOME}/etc/Renviron.site"
fi
fi
done
fi
fi

if [ "`which kubectl`" != "" ]; then
Expand Down