Skip to content

Commit 96c93f3

Browse files
committed
Allow auth using rdb channel
1 parent 9916f80 commit 96c93f3

File tree

3 files changed

+75
-37
lines changed

3 files changed

+75
-37
lines changed

src/replication.c

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,16 +2660,31 @@ void fullSyncWithMaster(connection* conn) {
26602660
goto error;
26612661
}
26622662
/* Send replica capabilities */
2663-
if (server.repl_rdb_conn_state == REPL_RDB_CONN_SEND_CAPA) {
2664-
serverLog(LL_DEBUG, "Received first reply from primary using rdb connection. Sending capa");
2665-
2663+
if (server.repl_rdb_conn_state == REPL_RDB_CONN_SEND_HANDSHAKE) {
2664+
serverLog(LL_DEBUG, "Received first reply from primary using rdb connection.");
2665+
/* AUTH with the master if required. */
2666+
if (server.masterauth) {
2667+
char *args[3] = {"AUTH",NULL,NULL};
2668+
size_t lens[3] = {4,0,0};
2669+
int argc = 1;
2670+
if (server.masteruser) {
2671+
args[argc] = server.masteruser;
2672+
lens[argc] = strlen(server.masteruser);
2673+
argc++;
2674+
}
2675+
args[argc] = server.masterauth;
2676+
lens[argc] = sdslen(server.masterauth);
2677+
argc++;
2678+
err = sendCommandArgv(conn, argc, args, lens);
2679+
if (err) goto write_error;
2680+
}
26662681
/* Send replica lisening port to master for clarification */
26672682
sds portstr = getReplicaPortString();
26682683
err = sendCommand(conn, "REPLCONF", "capa", "eof",
26692684
"rdb-only", "1", "rdb-conn", "1", "listening-port", portstr, NULL);
26702685
sdsfree(portstr);
26712686
if (err) goto write_error;
2672-
server.repl_rdb_conn_state = REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY;
2687+
server.repl_rdb_conn_state = REPL_RDB_CONN_RECEIVE_AUTH_REPLY;
26732688

26742689
if (connSetReadHandler(conn, fullSyncWithMaster) == C_ERR) {
26752690
char conninfo[CONN_INFO_LEN];
@@ -2680,6 +2695,23 @@ void fullSyncWithMaster(connection* conn) {
26802695
}
26812696
return;
26822697
}
2698+
if (server.repl_rdb_conn_state == REPL_RDB_CONN_RECEIVE_AUTH_REPLY && !server.masterauth) {
2699+
server.repl_rdb_conn_state = REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY;
2700+
}
2701+
/* Receive AUTH reply. */
2702+
if (server.repl_rdb_conn_state == REPL_RDB_CONN_RECEIVE_AUTH_REPLY) {
2703+
err = receiveSynchronousResponse(conn);
2704+
if (err == NULL) goto no_response_error;
2705+
if (err[0] == '-') {
2706+
serverLog(LL_WARNING,"Unable to AUTH to MASTER: %s",err);
2707+
sdsfree(err);
2708+
goto error;
2709+
}
2710+
sdsfree(err);
2711+
err = NULL;
2712+
server.repl_rdb_conn_state = REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY;
2713+
return;
2714+
}
26832715
/* Receive replconf response */
26842716
if (server.repl_rdb_conn_state == REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY) {
26852717
err = receiveSynchronousResponse(conn);
@@ -3297,37 +3329,39 @@ void setupMainConnForPsync(connection *conn) {
32973329
* RDB Channel Sync
32983330
* ┌──────────────────────────────────────────────────────────────┐
32993331
* │ RDB connection states Main connection state │
3300-
* ┌───────────────────┐ ┌────────────┐ │ ──────────────────────────┐ ┌───────────────────┐ │
3301-
* │RECEIVE_PING_REPLY │ ┌───►SEND_PSYNC │ -FULLSYNCNEEDED───┤RDB_CONN_SEND_CAPA │ ┌──►SEND_HANDSHAKE │ │
3302-
* └────────┬──────────┘ │ └─┬──────────┘ │ │ ──┬───────────────────────┘ │ └──┬────────────────┘ │
3332+
* ┌───────────────────┐ ┌────────────┐ │ ┌────────────────────────────┐ ┌───────────────────┐ │
3333+
* │RECEIVE_PING_REPLY │ ┌───►SEND_PSYNC │ -FULLSYNCNEEDED─┤REPL_RDB_CONN_SEND_HANDSHAKE│ ┌──►SEND_HANDSHAKE │ │
3334+
* └────────┬──────────┘ │ └─┬──────────┘ │ │ └────┬───────────────────────┘ │ └──┬────────────────┘ │
33033335
* │+PONG │ │PSYNC (use cached-master) │ │ │REPLCONF set-rdb-conn-id
33043336
* ┌────────▼──────────┐ │ ┌─▼─────────────────┐ │ │ ┌───────▼───────────────────────┐ │ ┌──▼────────────────┐ │
3305-
* │SEND_HANDSHAKE │ │ ┌─┤RECEIVE_PSYNC_REPLY├─┘ │ │RDB_CONN_RECEIVE_REPLCONF_REPLY│ │ │RECEIVE_CAPA_REPLY │ │
3337+
* │SEND_HANDSHAKE │ │ ┌─┤RECEIVE_PSYNC_REPLY├─┘ │ │RDB_CONN_RECEIVE_AUTH_REPLY │ │ │RECEIVE_CAPA_REPLY │ │
33063338
* └────────┬──────────┘ │ │ └─┬─────────────────┘ │ └───────┬───────────────────────┘ │ └──┬────────────────┘ │
3307-
* │ │ │ │+FULLRESYNC │ │ │ │+OK │
3308-
* ┌────────▼──────────┐ │ │ ┌─▼─────────────────┐ │ ┌───────▼─────────────── │ ┌──▼────────────────┐ │
3309-
* │RECEIVE_AUTH_REPLY │ │ │ │TRANSFER │ │ │RDB_CONN_RECEIVE_ENDOFF│ │ │SEND_PSYNC │ │
3310-
* └────────┬──────────┘ │ │ └───────────────────┘ │ └───────┬─────────────── │ └──┬────────────────┘ │
3311-
* │+OK │ │ │ │$ENDOFF │ │PSYNC use snapshot │
3312-
* ┌────────▼──────────┐ │ │ │ ─────────────────────────┘ │end-offset provided│
3313-
* │RECEIVE_PORT_REPLY │ │ │ │ │by the master │
3314-
* └────────┬──────────┘ │ │ │ ────────────────────── ┌──▼────────────────┐ │
3315-
* │+OK │ │+CONTINUE │ │RDB_CONN_RDB_LOAD │RECEIVE_PSYNC_REPLY│ │
3316-
* ┌────────▼──────────┐ │ │ │ ────────────────────── └──┬────────────────┘ │
3317-
* │RECEIVE_IP_REPLY │ │ │ │ │Done loading snapshot │+CONTINUE │
3339+
* │ │ │ │+FULLRESYNC │ │+OK │ │+OK │
3340+
* ┌────────▼──────────┐ │ │ ┌─▼─────────────────┐ │ ┌───────▼───────────────────────┐ │ ┌──▼────────────────┐ │
3341+
* │RECEIVE_AUTH_REPLY │ │ │ │TRANSFER │ │ │RDB_CONN_RECEIVE_REPLCONF_REPLY│ │ │SEND_PSYNC │ │
3342+
* └────────┬──────────┘ │ │ └───────────────────┘ │ └───────┬───────────────────────┘ │ └──┬────────────────┘ │
3343+
* │+OK │ │ │ │+OK │ │PSYNC use snapshot │
3344+
* ┌────────▼──────────┐ │ │ │ ──────────────────────┐ │ │end-offset provided│
3345+
* │RECEIVE_PORT_REPLY │ │ │ │ │RDB_CONN_RECEIVE_ENDOFF│ │by the master │
3346+
* └────────┬──────────┘ │ │ │ ────────────────────── ┌──▼────────────────┐ │
3347+
* │+OK │ │+CONTINUE │ │$ENDOFF │RECEIVE_PSYNC_REPLY│ │
3348+
* ┌────────▼──────────┐ │ │ │ ─────────────────────────┘ └──┬────────────────┘ │
3349+
* │RECEIVE_IP_REPLY │ │ │ │ │ │+CONTINUE │
33183350
* └────────┬──────────┘ │ │ │ ┌───────▼───────────────┐ ┌──▼────────────────┐ │
3319-
* │+OK │ │ │ │RDB_CONN_RDB_LOADED │ │TRANSFER │ │
3351+
* │+OK │ │ │ │RDB_CONN_RDB_LOAD │ │TRANSFER │ │
33203352
* ┌────────▼──────────┐ │ │ │ └───────┬───────────────┘ └────┬──────────────┘ │
3321-
* │RECEIVE_IP_REPLY │ │ │ │ │ │ │
3322-
* └────────┬──────────┘ │ │ │ │Slave loads local replication │ │
3323-
* │+OK │ │ │ │buffer into memory │ │
3324-
* ┌────────▼────────────────┐ │ │ ┌─────────┼──────────┴─────────────────────────────────┘ │
3325-
* │RECEIVE_NO_FULLSYNC_REPLY│ │ │ │ │ │
3326-
* └─┬────┬──────────────────┘ │ └────┐ │ └──────────────────────────────────────────────────────────────┘
3327-
* │+OK │Unrecognized REPLCONF │ │
3328-
* ┌─▼────▼────────────┐ │ ┌──▼───────────▼────┐
3329-
* │RECEIVE_CAPA_REPLY ├───────┘ │CONNECTED │
3330-
* └───────────────────┘ └───────────────────┘
3353+
* │RECEIVE_IP_REPLY │ │ │ │ │Done loading snapshot │ │
3354+
* └────────┬──────────┘ │ │ │ ┌───────▼───────────────┐ │ │
3355+
* │+OK │ │ │ │RDB_CONN_RDB_LOADED │ │ │
3356+
* ┌────────▼────────────────┐ │ │ │ └───────┬───────────────┘ │ │
3357+
* │RECEIVE_NO_FULLSYNC_REPLY│ │ │ │ │ │ │
3358+
* └─┬────┬──────────────────┘ │ └────┐ │ │Slave loads local replication │ │
3359+
* │+OK │Unrecognized REPLCONF │ │ │buffer into memory │ │
3360+
* ┌─▼────▼────────────┐ │ │ ┌────────┼──────────┴─────────────────────────────────┘ │
3361+
* │RECEIVE_CAPA_REPLY ├───────┘ │ │ │ │
3362+
* └───────────────────┘ ┌──▼────────────▼───┐ │ │
3363+
* │CONNECTED │ └──────────────────────────────────────────────────────────────┘
3364+
* └───────────────────┘
33313365
*/
33323366
/* This handler fires when the non blocking connect was able to
33333367
* establish a connection with the master. */
@@ -3646,7 +3680,7 @@ void syncWithMaster(connection *conn) {
36463680
strerror(errno), connGetInfo(conn, conninfo, sizeof(conninfo)));
36473681
goto error;
36483682
}
3649-
server.repl_rdb_conn_state = REPL_RDB_CONN_SEND_CAPA;
3683+
server.repl_rdb_conn_state = REPL_RDB_CONN_SEND_HANDSHAKE;
36503684
return;
36513685
}
36523686
/* Setup the non blocking download of the bulk file. */

src/server.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,11 @@ typedef enum {
467467
* slaves to remember what to do next. */
468468
typedef enum {
469469
REPL_RDB_CONN_STATE_NONE = 0, /* No active replication */
470-
REPL_RDB_CONN_SEND_CAPA, /* Send replica cob-channel capabilities */
471-
REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY, /* Wait for REPLCONF reply */
472-
REPL_RDB_CONN_RECEIVE_ENDOFF, /* Wait for $ENDOFF reply */
473-
REPL_RDB_CONN_RDB_LOAD, /* Loading rdb using rdb channel */
470+
REPL_RDB_CONN_SEND_HANDSHAKE, /* Send handshake sequence to master */
471+
REPL_RDB_CONN_RECEIVE_AUTH_REPLY, /* Wait for AUTH reply */
472+
REPL_RDB_CONN_RECEIVE_REPLCONF_REPLY, /* Wait for REPLCONF reply */
473+
REPL_RDB_CONN_RECEIVE_ENDOFF, /* Wait for $ENDOFF reply */
474+
REPL_RDB_CONN_RDB_LOAD, /* Loading rdb using rdb channel */
474475
REPL_RDB_CONN_RDB_LOADED,
475476
} repl_rdb_conn_state;
476477

tests/unit/auth.tcl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ start_server {tags {"auth external:skip"} overrides {requirepass foobar}} {
4747
}
4848
}
4949

50+
foreach rdbchann {yes no} {
5051
start_server {tags {"auth_binary_password external:skip"}} {
5152
test {AUTH fails when binary password is wrong} {
5253
r config set requirepass "abc\x00def"
@@ -65,12 +66,13 @@ start_server {tags {"auth_binary_password external:skip"}} {
6566
set master_port [srv -1 port]
6667
set slave [srv 0 client]
6768

68-
test {MASTERAUTH test with binary password} {
69+
test "MASTERAUTH test with binary password rdbchannel = $rdbchann" {
6970
$master config set requirepass "abc\x00def"
70-
71+
$master config set repl-rdb-channel $rdbchann
7172
# Configure the replica with masterauth
7273
set loglines [count_log_lines 0]
7374
$slave config set masterauth "abc"
75+
$slave config set repl-rdb-channel $rdbchann
7476
$slave slaveof $master_host $master_port
7577

7678
# Verify replica is not able to sync with master
@@ -87,3 +89,4 @@ start_server {tags {"auth_binary_password external:skip"}} {
8789
}
8890
}
8991
}
92+
}

0 commit comments

Comments
 (0)