Skip to content

Commit

Permalink
Support connection schemes valkey:// and valkeys:// (valkey-io#199)
Browse files Browse the repository at this point in the history
1. Support valkey:// and valkeys:// scheme in valkey-cli and
valkey-benchmark. Retain the original Redis schemes for compatibility.
2. Add unit tests for valid URI, all schemes.

Fixes: valkey-io#198
Fixes: valkey-io#200

---------

Signed-off-by: Lipeng Zhu <[email protected]>
  • Loading branch information
lipzhu authored Apr 23, 2024
1 parent a989ee5 commit 87a5bfc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static sds percentDecode(const char *pe, size_t len) {
/* Parse a URI and extract the server connection information.
* URI scheme is based on the provisional specification[1] excluding support
* for query parameters. Valid URIs are:
* scheme: "redis://"
* scheme: "valkey://"
* authority: [[<username> ":"] <password> "@"] [<hostname> [":" <port>]]
* path: ["/" [<db>]]
*
Expand All @@ -318,8 +318,11 @@ void parseRedisUri(const char *uri, const char* tool_name, cliConnInfo *connInfo
UNUSED(tls_flag);
#endif

const char *scheme = "redis://";
const char *tlsscheme = "rediss://";
const char *scheme = "valkey://";
const char *tlsscheme = "valkeys://";
/* We need to support redis:// and rediss:// too for compatibility. */
const char *redisScheme = "redis://";
const char *redisTlsscheme = "rediss://";
const char *curr = uri;
const char *end = uri + strlen(uri);
const char *userinfo, *username, *port, *host, *path;
Expand All @@ -330,11 +333,21 @@ void parseRedisUri(const char *uri, const char* tool_name, cliConnInfo *connInfo
*tls_flag = 1;
curr += strlen(tlsscheme);
#else
fprintf(stderr,"rediss:// is only supported when %s is compiled with OpenSSL\n", tool_name);
fprintf(stderr,"valkeys:// and rediss:// are only supported when %s is compiled with OpenSSL\n", tool_name);
exit(1);
#endif
} else if (!strncasecmp(redisTlsscheme, curr, strlen(redisTlsscheme))) {
#ifdef USE_OPENSSL
*tls_flag = 1;
curr += strlen(redisTlsscheme);
#else
fprintf(stderr,"valkeys:// and rediss:// are only supported when %s is compiled with OpenSSL\n", tool_name);
exit(1);
#endif
} else if (!strncasecmp(scheme, curr, strlen(scheme))) {
curr += strlen(scheme);
} else if (!strncasecmp(redisScheme, curr, strlen(redisScheme))) {
curr += strlen(redisScheme);
} else {
fprintf(stderr,"Invalid URI scheme\n");
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions src/valkey-benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,10 +1592,10 @@ int parseOptions(int argc, char **argv) {
" -s <socket> Server socket (overrides host and port)\n"
" -a <password> Password for Valkey Auth\n"
" --user <username> Used to send ACL style 'AUTH username pass'. Needs -a.\n"
" -u <uri> Server URI on format redis://user:password@host:port/dbnum\n"
" -u <uri> Server URI on format valkey://user:password@host:port/dbnum\n"
" User, password and dbnum are optional. For authentication\n"
" without a username, use username 'default'. For TLS, use\n"
" the scheme 'rediss'.\n"
" the scheme 'valkeys'.\n"
" -c <clients> Number of parallel connections (default 50).\n"
" Note: If --cluster is used then number of clients has to be\n"
" the same or higher than the number of nodes.\n"
Expand Down
6 changes: 3 additions & 3 deletions src/valkey-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -3040,10 +3040,10 @@ static void usage(int err) {
" --askpass Force user to input password with mask from STDIN.\n"
" If this argument is used, '-a' and " CLI_AUTH_ENV "\n"
" environment variable will be ignored.\n"
" -u <uri> Server URI on format redis://user:password@host:port/dbnum\n"
" -u <uri> Server URI on format valkey://user:password@host:port/dbnum\n"
" User, password and dbnum are optional. For authentication\n"
" without a username, use username 'default'. For TLS, use\n"
" the scheme 'rediss'.\n"
" the scheme 'valkeys'.\n"
" -r <repeat> Execute specified command N times.\n"
" -i <interval> When -r is used, waits <interval> seconds per command.\n"
" It is possible to specify sub-second times like -i 0.1.\n"
Expand Down Expand Up @@ -3130,7 +3130,7 @@ version,tls_usage);
" Use --cluster help to list all available cluster manager commands.\n"
"\n"
"Examples:\n"
" valkey-cli -u redis://default:PASSWORD@localhost:6379/0\n"
" valkey-cli -u valkey://default:PASSWORD@localhost:6379/0\n"
" cat /etc/passwd | valkey-cli -x set mypasswd\n"
" valkey-cli -D \"\" --raw dump key > key.dump && valkey-cli -X dump_tag restore key2 0 dump_tag replace < key.dump\n"
" valkey-cli -r 100 lpush mylist x\n"
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/valkey-cli.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,26 @@ if {!$::tls} { ;# fake_redis_node doesn't support TLS
assert_equal 3 [exec {*}$cmdline ZCARD new_zset]
assert_equal "a\n1\nb\n2\nc\n3" [exec {*}$cmdline ZRANGE new_zset 0 -1 WITHSCORES]
}
test "Valid Connection Scheme: redis://" {
set cmdline [valkeycliuri "redis://" [srv host] [srv port]]
assert_equal {PONG} [exec {*}$cmdline PING]
}
test "Valid Connection Scheme: valkey://" {
set cmdline [valkeycliuri "valkey://" [srv host] [srv port]]
assert_equal {PONG} [exec {*}$cmdline PING]
}
if {$::tls} {
test "Valid Connection Scheme: rediss://" {
set cmdline [valkeycliuri "rediss://" [srv host] [srv port]]
assert_equal {PONG} [exec {*}$cmdline PING]
}
test "Valid Connection Scheme: valkeys://" {
set cmdline [valkeycliuri "valkeys://" [srv host] [srv port]]
assert_equal {PONG} [exec {*}$cmdline PING]
}
}
}
4 changes: 2 additions & 2 deletions tests/support/benchmark.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ proc redisbenchmark {host port {opts {}}} {
}

proc redisbenchmarkuri {host port {opts {}}} {
set cmd [list src/valkey-benchmark -u redis://$host:$port]
set cmd [list src/valkey-benchmark -u valkey://$host:$port]
lappend cmd {*}[redisbenchmark_tls_config "tests"]
lappend cmd {*}$opts
return $cmd
}

proc redisbenchmarkuriuserpass {host port user pass {opts {}}} {
set cmd [list src/valkey-benchmark -u redis://$user:$pass@$host:$port]
set cmd [list src/valkey-benchmark -u valkey://$user:$pass@$host:$port]
lappend cmd {*}[redisbenchmark_tls_config "tests"]
lappend cmd {*}$opts
return $cmd
Expand Down
7 changes: 7 additions & 0 deletions tests/support/cli.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ proc valkeycli {host port {opts {}}} {
return $cmd
}

proc valkeycliuri {scheme host port {opts {}}} {
set cmd [list src/valkey-cli -u $scheme$host:$port]
lappend cmd {*}[valkeycli_tls_config "tests"]
lappend cmd {*}$opts
return $cmd
}

# Returns command line for executing valkey-cli with a unix socket address
proc valkeycli_unixsocket {unixsocket {opts {}}} {
return [list src/valkey-cli -s $unixsocket {*}$opts]
Expand Down

0 comments on commit 87a5bfc

Please sign in to comment.