Skip to content

Commit

Permalink
Make remsh work with quoted cookie
Browse files Browse the repository at this point in the history
Allow space and other special characters in cookies.

Test:
  First set the cookie in `vm.args`, then run the script below
  e.g.: `-setcookie 'a b\n\t\xd#{}()[]$&^!-=+?|//c\\d\\\e\\\\f'`
     or `-setcookie "a b\n\t\xd#{}()[]$&^!-=+?|//c\\d\\\e\\\\f"`

  ```
  make release
  cd rel/couchdb
  ./bin/couchdb
  ./bin/remsh
  ```
  • Loading branch information
jiahuili authored and jiahuili430 committed Apr 12, 2023
1 parent a6ab75b commit 81f2125
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions rel/overlay/bin/remsh
Expand Up @@ -49,10 +49,14 @@ NODE="${NODE:-$DEFAULT_NODE}"

# If present, extract cookie from ERL_FLAGS
# This is used by the CouchDB Dockerfile and Helm chart
COOKIE=$(echo "$ERL_FLAGS" | sed 's/^.*setcookie \([^ ][^ ]*\).*$/\1/g')
COOKIE=$(echo "$ERL_FLAGS" | sed -r '
s/.*-setcookie[ ]*['\''](.*)['\''].*/\1/
s/.*-setcookie[ ]*["](.*)["].*/\1/
s/.*-setcookie[ ]*([^ ]*).*/\1/
')
if test -f "$ARGS_FILE"; then
# else attempt to extract from vm.args
ARGS_FILE_COOKIE=$(awk '$1=="-setcookie"{print $2}' "$ARGS_FILE")
ARGS_FILE_COOKIE=$(awk '$1=="-setcookie"{st=index($0," "); print substr($0,st+1)}' "$ARGS_FILE" | tr -d \" | tr -d \')
COOKIE="${COOKIE:-$ARGS_FILE_COOKIE}"
fi

Expand Down Expand Up @@ -111,7 +115,12 @@ fi

# If present, strip -name or -setcookie from ERL_FLAGS
# to avoid conflicts with the cli parameters
ERL_FLAGS_CLEAN=$(echo "$ERL_FLAGS" | sed 's/-setcookie \([^ ][^ ]*\)//g' | sed 's/-name \([^ ][^ ]*\)//g')
ERL_FLAGS_CLEAN=$(echo "$ERL_FLAGS" | sed -r '
s/-setcookie[ ]*['\''].*['\'']//
s/-setcookie[ ]*["].*["]//
s/-setcookie[ ]*[^ ]*//
s/-name[ ]*[^ ]*//
')

if [ -z "${COOKIE}" ]; then
echo "No Erlang cookie could be found, please specify with -c" >&2
Expand All @@ -120,11 +129,11 @@ fi

if [ -z "$TLSCONF" ]; then
exec env ERL_FLAGS="$ERL_FLAGS_CLEAN" "$BINDIR/erl" -boot "$ROOTDIR/releases/$APP_VSN/start_clean" \
-name remsh$$@$LHOST -remsh $NODE -hidden -setcookie $COOKIE \
-name remsh$$@$LHOST -remsh $NODE -hidden -setcookie "$COOKIE" \
"$@"
else
exec env ERL_FLAGS="$ERL_FLAGS_CLEAN" "$BINDIR/erl" -boot "$ROOTDIR/releases/$APP_VSN/start_clean" \
-name remsh$$@$LHOST -remsh $NODE -hidden -setcookie $COOKIE \
-name remsh$$@$LHOST -remsh $NODE -hidden -setcookie "$COOKIE" \
-proto_dist inet_tls -ssl_dist_optfile $TLSCONF \
"$@"
fi

0 comments on commit 81f2125

Please sign in to comment.