Skip to content

Commit d71769d

Browse files
committed
Added authentication support for redis
1 parent 84b8d1b commit d71769d

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

redis/hiredis.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,10 @@ int hiredis_refdb_backend__reflog_delete(git_refdb_backend *_backend, const char
486486

487487
/* Constructors */
488488

489-
int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, const char* path, const char *host, int port)
489+
int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password)
490490
{
491491
hiredis_odb_backend *backend;
492+
redisReply *reply;
492493

493494
backend = calloc(1, sizeof (hiredis_odb_backend));
494495
if (backend == NULL)
@@ -497,10 +498,19 @@ int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, c
497498
backend->db = redisConnect(host, port);
498499
if (backend->db->err) {
499500
free(backend);
500-
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage couldn't connect to redis server");
501+
giterr_set_str(GITERR_REFERENCE, "Redis odb storage couldn't connect to redis server");
501502
return GIT_ERROR;
502503
}
503504

505+
if(password != NULL) {
506+
reply = redisCommand(backend->db, "AUTH %s", password);
507+
if (reply->type == REDIS_REPLY_ERROR) {
508+
giterr_set_str(GITERR_REFERENCE, "Redis odb storage authentication with redis server failed");
509+
return GIT_ERROR;
510+
}
511+
freeReplyObject(reply);
512+
}
513+
504514
backend->prefix = prefix;
505515
backend->repo_path = path;
506516

@@ -521,9 +531,10 @@ int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, c
521531
return GIT_OK;
522532
}
523533

524-
int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port)
534+
int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password)
525535
{
526536
hiredis_refdb_backend *backend;
537+
redisReply *reply;
527538

528539
backend = calloc(1, sizeof(hiredis_refdb_backend));
529540
if (backend == NULL)
@@ -536,6 +547,15 @@ int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefi
536547
return GIT_ERROR;
537548
}
538549

550+
if(password != NULL) {
551+
reply = redisCommand(backend->db, "AUTH %s", password);
552+
if (reply->type == REDIS_REPLY_ERROR) {
553+
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage authentication with redis server failed");
554+
return GIT_ERROR;
555+
}
556+
freeReplyObject(reply);
557+
}
558+
539559
backend->prefix = prefix;
540560
backend->repo_path = path;
541561

0 commit comments

Comments
 (0)