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

Make remsh work with quoted cookie #4509

Merged
merged 1 commit into from Apr 12, 2023
Merged

Conversation

jiahuili430
Copy link
Contributor

Overview

Allow space and other special characters in cookies.

Testing recommendations

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

Related Issues or Pull Requests

fix #4405

Checklist

  • Code is written and works correctly
  • Changes are covered by tests
  • Any new configurable parameters are documented in rel/overlay/etc/default.ini
  • Documentation changes were made in the src/docs folder
  • Documentation changes were backported (separated PR) to affected branches

@jiahuili430 jiahuili430 marked this pull request as ready for review April 4, 2023 02:25
rel/overlay/bin/remsh Outdated Show resolved Hide resolved
@jiahuili430 jiahuili430 force-pushed the quoted-cookie branch 6 times, most recently from 1bcf5b8 to 263cfcf Compare April 10, 2023 19:17
@@ -49,10 +49,10 @@ 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 's/^.*setcookie[ ]*\(['\''"]\)*\([^'\''"]*\)\1*.*$/\2/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing this before the code was updated.

#!/bin/bash
COOKIE=$(echo "$ERL_FLAGS" | sed -E '
s/.*-setcookie[ ]*["]([^"]*)["].*/\1/;
s/.*-setcookie[ ]*([^ ]*).*/\1/;
')

echo "COOKIE='${COOKIE}'"

ERL_FLAGS_CLEAN=$(echo "$ERL_FLAGS" | sed -E '
s/-setcookie[ ]*[^"]*.*["]//
s/-setcookie[ ]*[^ ]*//
s/-name[ ]*[^ ]*//
')

echo "ERL_FLAGS_CLEAN='${ERL_FLAGS_CLEAN}'"
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret  123" -name node1@mydomain.com hello' ./test
COOKIE='secret  123'
ERL_FLAGS_CLEAN='ddd secret  123"  hello'
bash-3.2$ micro test
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret  123" -name node1@mydomain.com hello' ./test
COOKIE='secret  123'
ERL_FLAGS_CLEAN='ddd secret  123"  hello'
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret  123" -name node1@mydomain.com hello' ./test
COOKIE='secret  123'
ERL_FLAGS_CLEAN='ddd secret  123"  hello'
bash-3.2$ micro test
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret  123" -name node1@mydomain.com hello' ./test
COOKIE='secret  123'
ERL_FLAGS_CLEAN='ddd   hello'
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret  123"' ./test
COOKIE='secret  123'
ERL_FLAGS_CLEAN='ddd '
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret" -name node1@mydomain.com hello' ./test
COOKIE='secret'
ERL_FLAGS_CLEAN='ddd   hello'
bash-3.2$ ERL_FLAGS='ddd -setcookie "secret"' ./test
COOKIE='secret'
ERL_FLAGS_CLEAN='ddd '
bash-3.2$ ERL_FLAGS='ddd -setcookie secret -name node1@mydomain.com hello' ./test
COOKIE='secret'
ERL_FLAGS_CLEAN='ddd   hello'
bash-3.2$ ERL_FLAGS='ddd -setcookie secret' ./test
COOKIE='secret'
ERL_FLAGS_CLEAN='ddd '

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sed -E / sed -r is very powerful and helped me remove the if statement. Thank you!

@jiahuili430 jiahuili430 force-pushed the quoted-cookie branch 5 times, most recently from f6be3b4 to 4b7c090 Compare April 12, 2023 15:19
@nickva
Copy link
Contributor

nickva commented Apr 12, 2023

-E/-r looks great. Good find, Ilya! It makes the expression easier to read and understand.

Is there a difference between -E and -r I checked man sed on macos and freebsd and both seem to have both options and say:

-r	     Same as -E	for compatibility with GNU sed.

@nickva
Copy link
Contributor

nickva commented Apr 12, 2023

It looks like our oldest Linux CentOS 7 doesn't support sed -E would we be able to use -r only?

[root@2dfb04f0d80c /]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit
...

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
  ```
@jiahuili430
Copy link
Contributor Author

Changed them to sed -r, thanks for double-checking with different systems, Nick.

Copy link
Contributor

@nickva nickva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

ERL_FLAGS:

% export COUCHDB_COOKIE='a\t{}()[]$&^!-=+?|//c\\d\\\e\\\\f'
% export ERL_FLAGS="-setcookie ${COUCHDB_COOKIE}"
% ./bin/remsh
(remsh34975@127.0.0.1)1> erlang:get_cookie().
'a\t{}()[]$&^!-=+?|//c\\d\\\\e\\\\f'

vm.args:

-setcookie 'x b\n\t\xd#{}()[]$&^!-=+?|//c\\d\\\e\\\\f'
./bin/remsh
Erlang/OTP 24 [erts-12.3.2.9] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Eshell V12.3.2.9  (abort with ^G)
(couchdb@127.0.0.1)1> erlang:get_cookie().
'x b\\n\\t\\xd#{}()[]$&^!-=+?|//c\\\\d\\\\\\e\\\\\\\\f'

Well done @jiahuili430. Thanks for nice tip about -r @iilyak !

@nickva nickva merged commit e755e23 into apache:main Apr 12, 2023
14 checks passed
@jiahuili430 jiahuili430 deleted the quoted-cookie branch April 13, 2023 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

remsh doesn't works when vm.args cookie is quoted
4 participants