Skip to content

Commit db100c4

Browse files
committed
Sentinel: Support for AUTH.
1 parent 0ee3f05 commit db100c4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

sentinel.conf

+19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ port 26379
1313
# The valid charset is A-z 0-9 and the three characters ".-_".
1414
sentinel monitor mymaster 127.0.0.1 6379 2
1515

16+
# sentinel auth-pass <master-name> <password>
17+
#
18+
# Set the password to use to authenticate with the master and slaves.
19+
# Useful if there is a password set in the Redis instances to monitor.
20+
#
21+
# Note that the master password is also used for slaves, so it is not
22+
# possible to set a different password in masters and slaves instances
23+
# if you want to be able to monitor these instances with Sentinel.
24+
#
25+
# However you can have Redis instances without the authentication enabled
26+
# mixed with Redis instances requiring the authentication (as long as the
27+
# password set is the same for all the instances requiring the password) as
28+
# the AUTH command will have no effect in Redis instances with authentication
29+
# switched off.
30+
#
31+
# Example:
32+
#
33+
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
34+
1635
# sentinel down-after-milliseconds <master-name> <milliseconds>
1736
#
1837
# Number of milliseconds the master (or any attached slave or sentinel) should

src/sentinel.c

+26
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ typedef struct sentinelRedisInstance {
162162
dict *slaves; /* Slaves for this master instance. */
163163
int quorum; /* Number of sentinels that need to agree on failure. */
164164
int parallel_syncs; /* How many slaves to reconfigure at same time. */
165+
char *auth_pass; /* Password to use for AUTH against master & slaves. */
165166

166167
/* Slave specific. */
167168
mstime_t master_link_down_time; /* Slave replication link down time. */
@@ -326,6 +327,7 @@ void sentinelEvent(int level, char *type, sentinelRedisInstance *ri, const char
326327
sentinelRedisInstance *sentinelSelectSlave(sentinelRedisInstance *master);
327328
void sentinelScheduleScriptExecution(char *path, ...);
328329
void sentinelStartFailover(sentinelRedisInstance *master, int state);
330+
void sentinelDiscardReplyCallback(redisAsyncContext *c, void *reply, void *privdata);
329331

330332
/* ========================= Dictionary types =============================== */
331333

@@ -874,6 +876,7 @@ sentinelRedisInstance *createSentinelRedisInstance(char *name, int flags, char *
874876
ri->down_after_period = master ? master->down_after_period :
875877
SENTINEL_DOWN_AFTER_PERIOD;
876878
ri->master_link_down_time = 0;
879+
ri->auth_pass = NULL;
877880
ri->slave_priority = SENTINEL_DEFAULT_SLAVE_PRIORITY;
878881
ri->slave_reconf_sent_time = 0;
879882
ri->slave_master_host = NULL;
@@ -921,6 +924,7 @@ void releaseSentinelRedisInstance(sentinelRedisInstance *ri) {
921924
sdsfree(ri->client_reconfig_script);
922925
sdsfree(ri->slave_master_host);
923926
sdsfree(ri->leader);
927+
sdsfree(ri->auth_pass);
924928
releaseSentinelAddr(ri->addr);
925929

926930
/* Clear state into the master if needed. */
@@ -1205,6 +1209,11 @@ char *sentinelHandleConfiguration(char **argv, int argc) {
12051209
return "Client reconfiguration script seems non existing or "
12061210
"non executable.";
12071211
ri->client_reconfig_script = sdsnew(argv[2]);
1212+
} else if (!strcasecmp(argv[0],"auth-pass") && argc == 3) {
1213+
/* auth-pass <name> <password> */
1214+
ri = sentinelGetMasterByName(argv[1]);
1215+
if (!ri) return "No such master with specified name.";
1216+
ri->auth_pass = sdsnew(argv[2]);
12081217
} else {
12091218
return "Unrecognized sentinel configuration statement.";
12101219
}
@@ -1263,6 +1272,21 @@ void sentinelDisconnectCallback(const redisAsyncContext *c, int status) {
12631272
sentinelDisconnectInstanceFromContext(c);
12641273
}
12651274

1275+
/* Send the AUTH command with the specified master password if needed.
1276+
* Note that for slaves the password set for the master is used.
1277+
*
1278+
* We don't check at all if the command was successfully transmitted
1279+
* to the instance as if it fails Sentinel will detect the instance down,
1280+
* will disconnect and reconnect the link and so forth. */
1281+
void sentinelSendAuthIfNeeded(sentinelRedisInstance *ri, redisAsyncContext *c) {
1282+
char *auth_pass = (ri->flags & SRI_MASTER) ? ri->auth_pass :
1283+
ri->master->auth_pass;
1284+
1285+
if (auth_pass)
1286+
redisAsyncCommand(c, sentinelDiscardReplyCallback, NULL, "AUTH %s",
1287+
auth_pass);
1288+
}
1289+
12661290
/* Create the async connections for the specified instance if the instance
12671291
* is disconnected. Note that the SRI_DISCONNECTED flag is set even if just
12681292
* one of the two links (commands and pub/sub) is missing. */
@@ -1284,6 +1308,7 @@ void sentinelReconnectInstance(sentinelRedisInstance *ri) {
12841308
sentinelLinkEstablishedCallback);
12851309
redisAsyncSetDisconnectCallback(ri->cc,
12861310
sentinelDisconnectCallback);
1311+
sentinelSendAuthIfNeeded(ri,ri->cc);
12871312
}
12881313
}
12891314
/* Pub / Sub */
@@ -1303,6 +1328,7 @@ void sentinelReconnectInstance(sentinelRedisInstance *ri) {
13031328
sentinelLinkEstablishedCallback);
13041329
redisAsyncSetDisconnectCallback(ri->pc,
13051330
sentinelDisconnectCallback);
1331+
sentinelSendAuthIfNeeded(ri,ri->pc);
13061332
/* Now we subscribe to the Sentinels "Hello" channel. */
13071333
retval = redisAsyncCommand(ri->pc,
13081334
sentinelReceiveHelloMessages, NULL, "SUBSCRIBE %s",

0 commit comments

Comments
 (0)