Skip to content

Commit

Permalink
Add redisSetUser() pre-connect configuration function
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Sep 4, 2024
1 parent c4e9d76 commit 7ae03a2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ you application:
redisxSetTcpBuf(65536);
```
Optionally you can select the database index to use now (and also later, after connecting), if not the default (index
0):
```c
Redis *redis = ...
// Select the database index 2
redisxSelectDB(redis);
```

(Note that you can switch the database index any time, with the caveat that it's not possible to change it for the
subscription client when there are active subscriptions.)

<a name="connecting"></a>
### Connecting

Expand Down
1 change: 1 addition & 0 deletions include/redisx-priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef struct {
uint32_t addr; ///< The 32-bit inet address
int port; ///< port number (usually 6379)
int dbIndex; ///< the zero-based database index
char *username; ///< REdis user name (if any)
char *password; ///< Redis password (if any)

RedisClient *clients;
Expand Down
5 changes: 3 additions & 2 deletions include/redisx.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ void redisxSetTcpBuf(int size);
int redisxGetTcpBuf();

void redisxSetPort(Redis *redis, int port);
void redisxSetPassword(Redis *redis, const char *passwd);
int redisxSetUser(Redis *redis, const char *username);
int redisxSetPassword(Redis *redis, const char *passwd);
int redisxSelectDB(Redis *redis, int idx);

int redisxSetTransmitErrorHandler(Redis *redis, RedisErrorHandler f);

Expand All @@ -235,7 +237,6 @@ boolean redisxIsConnected(Redis *redis);
boolean redisxHasPipeline(Redis *redis);

RedisClient *redisxGetClient(Redis *redis, enum redisx_channel channel);
int redisxSelectDB(Redis *redis, int idx, boolean confirm);

void redisxAddConnectHook(Redis *redis, void (*setupCall)(Redis *));
void redisxRemoveConnectHook(Redis *redis, void (*setupCall)(Redis *));
Expand Down
5 changes: 4 additions & 1 deletion src/redisx-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static int rAuthAsync(RedisClient *cl) {
const RedisPrivate *p = (RedisPrivate *) cp->redis->priv;
RESP *reply;

int status = redisxSendRequestAsync(cl, "AUTH", p->password, NULL, NULL);
int status = p->username ? redisxSendRequestAsync(cl, "AUTH", p->username, p->password, NULL) : redisxSendRequestAsync(cl, "AUTH", p->password, NULL, NULL);
if(status) return redisxError("rAuthAsync()", status);

reply = redisxReadReplyAsync(cl);
Expand Down Expand Up @@ -582,7 +582,10 @@ void redisxSetPort(Redis *redis, int port) {
*
* \sa redisxInit()
* \sa redisxSetPort()
* \sa redisxSetUser()
* \sa redisxSetPassword()
* \sa redisxSetTcpBuf()
* \sa redisxSelectDB()
* \sa redisxDisconnect()
*/
int redisxConnect(Redis *redis, boolean usePipeline) {
Expand Down
50 changes: 41 additions & 9 deletions src/redisx.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,56 @@ boolean redisxIsVerbose() {
return xIsVerbose();
}

/**
* Sets the user name to use for authenticating on the Redis server after connection. See the
* `AUTH` Redis command for more explanation. Naturally, you need to call this prior to connecting
* your Redis instance to have the desired effect.
*
* @param redis Pointer to the Redis instance for which to set credentials
* @param username the password to use for authenticating on the server, or NULL to clear a
* previously configured password.
* @return X_SUCCESS (0) if successful, X_NULL if the redis argument is NULL, or
* X_ALREADY_OPEN if called after Redis was already connected.
*
* @sa redisxSetPassword()
*/
int redisxSetUser(Redis *redis, const char *username) {
RedisPrivate *p;

if(!redis) return X_NULL;
if(redisxIsConnected(redis)) return redisxError("redisxSetUser()", X_ALREADY_OPEN);

p = (RedisPrivate *) redis->priv;
if(p->username) free(p->username);
p->username = xStringCopyOf(username);

return X_SUCCESS;
}

/**
* Sets the password to use for authenticating on the Redis server after connection. See the AUTH
* Redis command for more explanation. Naturally, you need to call this prior to connecting
* your Redis instance to have the desired effect.
*
* @param redis Pointer to the Redis instance for which to set credentials
* @param passwd the password to use for authenticating on the server, or NULL to clear a
* previously configured password.
* @param redis Pointer to the Redis instance for which to set credentials
* @param passwd the password to use for authenticating on the server, or NULL to clear a
* previously configured password.
* @return X_SUCCESS (0) if successful, X_NULL if the redis argument is NULL, or
* X_ALREADY_OPEN if called after Redis was already connected.
*
* @sa redisxSetUser()
*/
void redisxSetPassword(Redis *redis, const char *passwd) {
int redisxSetPassword(Redis *redis, const char *passwd) {
RedisPrivate *p;

if(!redis) return;
if(!redis) return X_NULL;
if(redisxIsConnected(redis)) return redisxError("redisxSetPassword()", X_ALREADY_OPEN);

p = (RedisPrivate *) redis->priv;
if(p->password) free(p->password);
p->password = xStringCopyOf(passwd);

return X_SUCCESS;
}

/**
Expand Down Expand Up @@ -463,7 +496,7 @@ int redisxSelectClientDBAsync(RedisClient *cl, int idx, boolean confirm) {

static void rAffirmDB(Redis *redis) {
const RedisPrivate *p = (RedisPrivate *) redis->priv;
redisxSelectDB(redis, p->dbIndex, TRUE);
redisxSelectDB(redis, p->dbIndex);
}

/**
Expand All @@ -473,7 +506,6 @@ static void rAffirmDB(Redis *redis) {
*
* @param redis Pointer to a Redis instance.
* @param idx zero-based database index
* @param confirm Whether to wait for confirmation from Redis, and check the response.
* @return X_SUCCESS (0) if successful, or
* X_NULL if the redis argument is NULL,
* X_INCOMPLETE if there is an active subscription channel that cannot be switched or
Expand All @@ -483,7 +515,7 @@ static void rAffirmDB(Redis *redis) {
* @sa redisxSelectDB()
* @sa redisxLockEnabled()
*/
int redisxSelectDB(Redis *redis, int idx, boolean confirm) {
int redisxSelectDB(Redis *redis, int idx) {
RedisPrivate *p;
enum redisx_channel c;
int status = X_SUCCESS;
Expand Down Expand Up @@ -511,7 +543,7 @@ int redisxSelectDB(Redis *redis, int idx, boolean confirm) {
continue;
}

s = redisxSelectClientDBAsync(cl, idx, confirm && c != PIPELINE_CHANNEL);
s = redisxSelectClientDBAsync(cl, idx, c != PIPELINE_CHANNEL);
redisxUnlockClient(cl);

if(s) status = X_INCOMPLETE;
Expand Down

0 comments on commit 7ae03a2

Please sign in to comment.