Support getting configuration from both stdin and file at the same time (#7893)
This allows supplying secret configuration (for example - masterauth) via a secure channel instead of having it in a plaintext file / command line param, while still allowing for most of the configuration to reside there. Also, remove 'special' case handling for --check-rdb which hasn't been relevant since 4.0.0.
This commit is contained in:
parent
c3f9e01794
commit
dab5ec9b8d
21
src/config.c
21
src/config.c
@ -467,7 +467,7 @@ void loadServerConfigFromString(char *config) {
|
|||||||
fclose(logfp);
|
fclose(logfp);
|
||||||
}
|
}
|
||||||
} else if (!strcasecmp(argv[0],"include") && argc == 2) {
|
} else if (!strcasecmp(argv[0],"include") && argc == 2) {
|
||||||
loadServerConfig(argv[1],NULL);
|
loadServerConfig(argv[1], 0, NULL);
|
||||||
} else if ((!strcasecmp(argv[0],"client-query-buffer-limit")) && argc == 2) {
|
} else if ((!strcasecmp(argv[0],"client-query-buffer-limit")) && argc == 2) {
|
||||||
server.client_max_querybuf_len = memtoll(argv[1],NULL);
|
server.client_max_querybuf_len = memtoll(argv[1],NULL);
|
||||||
} else if ((!strcasecmp(argv[0],"slaveof") ||
|
} else if ((!strcasecmp(argv[0],"slaveof") ||
|
||||||
@ -619,28 +619,31 @@ loaderr:
|
|||||||
* Both filename and options can be NULL, in such a case are considered
|
* Both filename and options can be NULL, in such a case are considered
|
||||||
* empty. This way loadServerConfig can be used to just load a file or
|
* empty. This way loadServerConfig can be used to just load a file or
|
||||||
* just load a string. */
|
* just load a string. */
|
||||||
void loadServerConfig(char *filename, char *options) {
|
void loadServerConfig(char *filename, char config_from_stdin, char *options) {
|
||||||
sds config = sdsempty();
|
sds config = sdsempty();
|
||||||
char buf[CONFIG_MAX_LINE+1];
|
char buf[CONFIG_MAX_LINE+1];
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
/* Load the file content */
|
/* Load the file content */
|
||||||
if (filename) {
|
if (filename) {
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
if (filename[0] == '-' && filename[1] == '\0') {
|
|
||||||
fp = stdin;
|
|
||||||
} else {
|
|
||||||
if ((fp = fopen(filename,"r")) == NULL) {
|
if ((fp = fopen(filename,"r")) == NULL) {
|
||||||
serverLog(LL_WARNING,
|
serverLog(LL_WARNING,
|
||||||
"Fatal error, can't open config file '%s': %s",
|
"Fatal error, can't open config file '%s': %s",
|
||||||
filename, strerror(errno));
|
filename, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
|
while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
|
||||||
config = sdscat(config,buf);
|
config = sdscat(config,buf);
|
||||||
if (fp != stdin) fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
/* Append content from stdin */
|
||||||
|
if (config_from_stdin) {
|
||||||
|
serverLog(LL_WARNING,"Reading config from stdin");
|
||||||
|
fp = stdin;
|
||||||
|
while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
|
||||||
|
config = sdscat(config,buf);
|
||||||
|
}
|
||||||
|
|
||||||
/* Append the additional options */
|
/* Append the additional options */
|
||||||
if (options) {
|
if (options) {
|
||||||
config = sdscat(config,"\n");
|
config = sdscat(config,"\n");
|
||||||
|
49
src/server.c
49
src/server.c
@ -4907,7 +4907,7 @@ void version(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void usage(void) {
|
void usage(void) {
|
||||||
fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf] [options]\n");
|
fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf] [options] [-]\n");
|
||||||
fprintf(stderr," ./redis-server - (read config from stdin)\n");
|
fprintf(stderr," ./redis-server - (read config from stdin)\n");
|
||||||
fprintf(stderr," ./redis-server -v or --version\n");
|
fprintf(stderr," ./redis-server -v or --version\n");
|
||||||
fprintf(stderr," ./redis-server -h or --help\n");
|
fprintf(stderr," ./redis-server -h or --help\n");
|
||||||
@ -4917,6 +4917,7 @@ void usage(void) {
|
|||||||
fprintf(stderr," ./redis-server /etc/redis/6379.conf\n");
|
fprintf(stderr," ./redis-server /etc/redis/6379.conf\n");
|
||||||
fprintf(stderr," ./redis-server --port 7777\n");
|
fprintf(stderr," ./redis-server --port 7777\n");
|
||||||
fprintf(stderr," ./redis-server --port 7777 --replicaof 127.0.0.1 8888\n");
|
fprintf(stderr," ./redis-server --port 7777 --replicaof 127.0.0.1 8888\n");
|
||||||
|
fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose -\n");
|
||||||
fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose\n\n");
|
fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose\n\n");
|
||||||
fprintf(stderr,"Sentinel mode:\n");
|
fprintf(stderr,"Sentinel mode:\n");
|
||||||
fprintf(stderr," ./redis-server /etc/sentinel.conf --sentinel\n");
|
fprintf(stderr," ./redis-server /etc/sentinel.conf --sentinel\n");
|
||||||
@ -5242,6 +5243,7 @@ int iAmMaster(void) {
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int j;
|
int j;
|
||||||
|
char config_from_stdin = 0;
|
||||||
|
|
||||||
#ifdef REDIS_TEST
|
#ifdef REDIS_TEST
|
||||||
if (argc == 3 && !strcasecmp(argv[1], "test")) {
|
if (argc == 3 && !strcasecmp(argv[1], "test")) {
|
||||||
@ -5319,7 +5321,6 @@ int main(int argc, char **argv) {
|
|||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
j = 1; /* First option to parse in argv[] */
|
j = 1; /* First option to parse in argv[] */
|
||||||
sds options = sdsempty();
|
sds options = sdsempty();
|
||||||
char *configfile = NULL;
|
|
||||||
|
|
||||||
/* Handle special options --help and --version */
|
/* Handle special options --help and --version */
|
||||||
if (strcmp(argv[1], "-v") == 0 ||
|
if (strcmp(argv[1], "-v") == 0 ||
|
||||||
@ -5336,30 +5337,29 @@ int main(int argc, char **argv) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Parse command line options
|
||||||
/* First argument is the config file name? */
|
* Precedence wise, File, stdin, explicit options -- last config is the one that matters.
|
||||||
if (argv[j][0] != '-' || argv[j][1] != '-') {
|
*
|
||||||
configfile = argv[j];
|
* First argument is the config file name? */
|
||||||
server.configfile = getAbsolutePath(configfile);
|
if (argv[1][0] != '-') {
|
||||||
/* Replace the config file in server.exec_argv with
|
/* Replace the config file in server.exec_argv with its absolute path. */
|
||||||
* its absolute path. */
|
server.configfile = getAbsolutePath(argv[1]);
|
||||||
zfree(server.exec_argv[j]);
|
zfree(server.exec_argv[1]);
|
||||||
server.exec_argv[j] = zstrdup(server.configfile);
|
server.exec_argv[1] = zstrdup(server.configfile);
|
||||||
j++;
|
j = 2; // Skip this arg when parsing options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while(j < argc) {
|
||||||
|
/* Either first or last argument - Should we read config from stdin? */
|
||||||
|
if (argv[j][0] == '-' && argv[j][1] == '\0' && (j == 1 || j == argc-1)) {
|
||||||
|
config_from_stdin = 1;
|
||||||
|
}
|
||||||
/* All the other options are parsed and conceptually appended to the
|
/* All the other options are parsed and conceptually appended to the
|
||||||
* configuration file. For instance --port 6380 will generate the
|
* configuration file. For instance --port 6380 will generate the
|
||||||
* string "port 6380\n" to be parsed after the actual file name
|
* string "port 6380\n" to be parsed after the actual config file
|
||||||
* is parsed, if any. */
|
* and stdin input are parsed (if they exist). */
|
||||||
while(j != argc) {
|
else if (argv[j][0] == '-' && argv[j][1] == '-') {
|
||||||
if (argv[j][0] == '-' && argv[j][1] == '-') {
|
|
||||||
/* Option name */
|
/* Option name */
|
||||||
if (!strcmp(argv[j], "--check-rdb")) {
|
|
||||||
/* Argument has no options, need to skip for parsing. */
|
|
||||||
j++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (sdslen(options)) options = sdscat(options,"\n");
|
if (sdslen(options)) options = sdscat(options,"\n");
|
||||||
options = sdscat(options,argv[j]+2);
|
options = sdscat(options,argv[j]+2);
|
||||||
options = sdscat(options," ");
|
options = sdscat(options," ");
|
||||||
@ -5370,14 +5370,13 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
if (server.sentinel_mode && configfile && *configfile == '-') {
|
|
||||||
serverLog(LL_WARNING,
|
if (server.sentinel_mode && ! server.configfile) {
|
||||||
"Sentinel config from STDIN not allowed.");
|
|
||||||
serverLog(LL_WARNING,
|
serverLog(LL_WARNING,
|
||||||
"Sentinel needs config file on disk to save state. Exiting...");
|
"Sentinel needs config file on disk to save state. Exiting...");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
loadServerConfig(configfile,options);
|
loadServerConfig(server.configfile, config_from_stdin, options);
|
||||||
sdsfree(options);
|
sdsfree(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2115,7 +2115,7 @@ int keyspaceEventsStringToFlags(char *classes);
|
|||||||
sds keyspaceEventsFlagsToString(int flags);
|
sds keyspaceEventsFlagsToString(int flags);
|
||||||
|
|
||||||
/* Configuration */
|
/* Configuration */
|
||||||
void loadServerConfig(char *filename, char *options);
|
void loadServerConfig(char *filename, char config_from_stdin, char *options);
|
||||||
void appendServerSaveParams(time_t seconds, int changes);
|
void appendServerSaveParams(time_t seconds, int changes);
|
||||||
void resetServerSaveParams(void);
|
void resetServerSaveParams(void);
|
||||||
struct rewriteConfigState; /* Forward declaration to export API. */
|
struct rewriteConfigState; /* Forward declaration to export API. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user