Skip to content

Commit e0d343a

Browse files
authored
Fix/skip hba propagation (#609)
* Add --skip-pg-hba to pg_autoctl do tmux session. This is usable from the Makefile thanks to the new CLUSTER_OPTS variable. * Add OPTION_POSTGRESQL_HBA_LEVEL to expected KeeperConfig INI settings. In passing, also fix several oddities in the pghba file editing and review the comman line option parsing logic for the --auth --skip-pg-hba and --pg-hba-kan options.
1 parent a8aa44c commit e0d343a

10 files changed

Lines changed: 185 additions & 41 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ NODES_ASYNC ?= 0 # count of replication-quorum false nodes
3535
NODES_PRIOS ?= 50 # either "50", or "50,50", or "50,50,0" etc
3636
NODES_SYNC_SB ?= -1
3737
FIRST_PGPORT ?= 5500
38+
CLUSTER_OPTS = "" # could be "--skip-pg-hba"
3839

3940
TMUX_EXTRA_COMMANDS ?= ""
4041
TMUX_LAYOUT ?= even-vertical # could be "tiled"
@@ -150,7 +151,8 @@ $(TMUX_SCRIPT): bin
150151
--async-nodes $(NODES_ASYNC) \
151152
--node-priorities $(NODES_PRIOS) \
152153
--sync-standbys $(NODES_SYNC_SB) \
153-
--layout $(TMUX_LAYOUT) > $@
154+
$(CLUSTER_OPTS) \
155+
--layout $(TMUX_LAYOUT) > $@
154156

155157
tmux-script: $(TMUX_SCRIPT) ;
156158

@@ -168,6 +170,7 @@ cluster: install tmux-clean
168170
--async-nodes $(NODES_ASYNC) \
169171
--node-priorities $(NODES_PRIOS) \
170172
--sync-standbys $(NODES_SYNC_SB) \
173+
$(CLUSTER_OPTS) \
171174
--layout $(TMUX_LAYOUT)
172175

173176

src/bin/pg_autoctl/cli_common.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,23 @@ cli_common_keeper_getopts(int argc, char **argv,
193193
case 'A':
194194
{
195195
/* { "auth", required_argument, NULL, 'A' }, */
196-
if (!IS_EMPTY_STRING_BUFFER(LocalOptionConfig.pgSetup.authMethod))
196+
if (LocalOptionConfig.pgSetup.hbaLevel == HBA_EDIT_SKIP)
197197
{
198198
errors++;
199199
log_error("Please use either --auth or --skip-pg-hba");
200200
}
201201

202202
strlcpy(LocalOptionConfig.pgSetup.authMethod, optarg, NAMEDATALEN);
203203
log_trace("--auth %s", LocalOptionConfig.pgSetup.authMethod);
204+
205+
if (LocalOptionConfig.pgSetup.hbaLevel == HBA_EDIT_UNKNOWN)
206+
{
207+
strlcpy(LocalOptionConfig.pgSetup.hbaLevelStr,
208+
pgsetup_hba_level_to_string(HBA_EDIT_MINIMAL),
209+
sizeof(LocalOptionConfig.pgSetup.hbaLevelStr));
210+
211+
LocalOptionConfig.pgSetup.hbaLevel = HBA_EDIT_MINIMAL;
212+
}
204213
break;
205214
}
206215

@@ -231,8 +240,7 @@ cli_common_keeper_getopts(int argc, char **argv,
231240
case 'L':
232241
{
233242
/* { "pg-hba-lan", required_argument, NULL, 'L' }, */
234-
if (LocalOptionConfig.pgSetup.hbaLevel != HBA_EDIT_UNKNOWN &&
235-
LocalOptionConfig.pgSetup.hbaLevel != HBA_EDIT_LAN)
243+
if (LocalOptionConfig.pgSetup.hbaLevel == HBA_EDIT_SKIP)
236244
{
237245
errors++;
238246
log_error("Please use either --skip-pg-hba or --pg-hba-lan");
@@ -511,6 +519,10 @@ cli_common_keeper_getopts(int argc, char **argv,
511519
/* the default HBA editing level is MINIMAL, time to install it */
512520
if (LocalOptionConfig.pgSetup.hbaLevel == HBA_EDIT_UNKNOWN)
513521
{
522+
strlcpy(LocalOptionConfig.pgSetup.hbaLevelStr,
523+
pgsetup_hba_level_to_string(HBA_EDIT_MINIMAL),
524+
sizeof(LocalOptionConfig.pgSetup.hbaLevelStr));
525+
514526
LocalOptionConfig.pgSetup.hbaLevel = HBA_EDIT_MINIMAL;
515527
}
516528

src/bin/pg_autoctl/cli_do_root.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,29 @@ CommandLine do_tmux_script =
243243
make_command("script",
244244
"Produce a tmux script for a demo or a test case (debug only)",
245245
"[option ...]",
246-
" --root path where to create a cluster\n"
247-
" --first-pgport first Postgres port to use (5500)\n"
248-
" --nodes number of Postgres nodes to create (2)\n"
249-
" --layout tmux layout to use (even-vertical)",
246+
" --root path where to create a cluster\n"
247+
" --first-pgport first Postgres port to use (5500)\n"
248+
" --nodes number of Postgres nodes to create (2)\n"
249+
" --async-nodes number of async nodes within nodes (0)\n"
250+
" --node-priorities list of nodes priorities (50)\n"
251+
" --sync-standbys number-sync-standbys to set (0 or 1)\n"
252+
" --skip-pg-hba use --skip-pg-hba when creating nodes\n"
253+
" --layout tmux layout to use (even-vertical)",
250254
cli_do_tmux_script_getopts,
251255
cli_do_tmux_script);
252256

253257
CommandLine do_tmux_session =
254258
make_command("session",
255259
"Run a tmux session for a demo or a test case",
256260
"[option ...]",
257-
" --root path where to create a cluster\n"
258-
" --first-pgport first Postgres port to use (5500)\n"
259-
" --nodes number of Postgres nodes to create (2)\n"
260-
" --layout tmux layout to use (even-vertical)",
261+
" --root path where to create a cluster\n"
262+
" --first-pgport first Postgres port to use (5500)\n"
263+
" --nodes number of Postgres nodes to create (2)\n"
264+
" --async-nodes number of async nodes within nodes (0)\n"
265+
" --node-priorities list of nodes priorities (50)\n"
266+
" --sync-standbys number-sync-standbys to set (0 or 1)\n"
267+
" --skip-pg-hba use --skip-pg-hba when creating nodes\n"
268+
" --layout tmux layout to use (even-vertical)",
261269
cli_do_tmux_script_getopts,
262270
cli_do_tmux_session);
263271

@@ -286,9 +294,14 @@ CommandLine do_tmux_wait =
286294
make_command("wait",
287295
"Wait until a given node has been registered on the monitor",
288296
"[option ...] nodename [ targetState ]",
289-
" --root path where to create a cluster\n"
290-
" --first-pgport first Postgres port to use (5500)\n"
291-
" --nodes number of Postgres nodes to create (2)",
297+
" --root path where to create a cluster\n"
298+
" --first-pgport first Postgres port to use (5500)\n"
299+
" --nodes number of Postgres nodes to create (2)\n"
300+
" --async-nodes number of async nodes within nodes (0)\n"
301+
" --node-priorities list of nodes priorities (50)\n"
302+
" --sync-standbys number-sync-standbys to set (0 or 1)\n"
303+
" --skip-pg-hba use --skip-pg-hba when creating nodes\n"
304+
" --layout tmux layout to use (even-vertical)",
292305
cli_do_tmux_script_getopts,
293306
cli_do_tmux_wait);
294307

src/bin/pg_autoctl/cli_do_tmux.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ cli_do_tmux_script_getopts(int argc, char **argv)
210210
{ "async-nodes", required_argument, NULL, 'a' },
211211
{ "node-priorities", required_argument, NULL, 'P' },
212212
{ "sync-standbys", required_argument, NULL, 's' },
213+
{ "skip-pg-hba", required_argument, NULL, 'S' },
213214
{ "layout", required_argument, NULL, 'l' },
214215
{ "version", no_argument, NULL, 'V' },
215216
{ "verbose", no_argument, NULL, 'v' },
@@ -225,6 +226,7 @@ cli_do_tmux_script_getopts(int argc, char **argv)
225226
options.nodes = 2;
226227
options.asyncNodes = 0;
227228
options.numSync = -1; /* use pg_autoctl defaults */
229+
options.skipHBA = false;
228230
strlcpy(options.root, "/tmp/pgaf/tmux", sizeof(options.root));
229231
strlcpy(options.layout, "even-vertical", sizeof(options.layout));
230232

@@ -329,6 +331,13 @@ cli_do_tmux_script_getopts(int argc, char **argv)
329331
break;
330332
}
331333

334+
case 'S':
335+
{
336+
options.skipHBA = true;
337+
log_trace("--skip-pg-hba");
338+
break;
339+
}
340+
332341
case 'l':
333342
{
334343
strlcpy(options.layout, optarg, MAXPGPATH);
@@ -613,9 +622,13 @@ tmux_add_new_session(PQExpBuffer script, const char *root, int pgport)
613622
void
614623
tmux_pg_autoctl_create_monitor(PQExpBuffer script,
615624
const char *root,
616-
int pgport)
625+
int pgport,
626+
bool skipHBA)
617627
{
618-
char *pg_ctl_opts = "--hostname localhost --ssl-self-signed --auth trust";
628+
char *pg_ctl_opts =
629+
skipHBA
630+
? "--hostname localhost --ssl-self-signed --skip-pg-hba"
631+
: "--hostname localhost --ssl-self-signed --auth trust";
619632

620633
tmux_add_send_keys_command(script, "export PGPORT=%d", pgport);
621634

@@ -639,10 +652,14 @@ tmux_pg_autoctl_create_postgres(PQExpBuffer script,
639652
int pgport,
640653
const char *name,
641654
bool replicationQuorum,
642-
int candidatePriority)
655+
int candidatePriority,
656+
bool skipHBA)
643657
{
644658
char monitor[BUFSIZE] = { 0 };
645-
char *pg_ctl_opts = "--hostname localhost --ssl-self-signed --auth trust";
659+
char *pg_ctl_opts =
660+
skipHBA
661+
? "--hostname localhost --ssl-self-signed --skip-pg-hba"
662+
: "--hostname localhost --ssl-self-signed --auth trust --pg-hba-lan";
646663

647664
tmux_add_send_keys_command(script, "export PGPORT=%d", pgport);
648665

@@ -661,7 +678,6 @@ tmux_pg_autoctl_create_postgres(PQExpBuffer script,
661678
"--monitor %s "
662679
"--name %s "
663680
"--dbname demo "
664-
"--pg-hba-lan "
665681
"--replication-quorum %s "
666682
"--candidate-priority %d "
667683
"--run",
@@ -696,7 +712,7 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
696712

697713
/* start a monitor */
698714
(void) tmux_add_xdg_environment(script);
699-
tmux_pg_autoctl_create_monitor(script, root, pgport++);
715+
tmux_pg_autoctl_create_monitor(script, root, pgport++, options->skipHBA);
700716

701717
/* start the Postgres nodes, using the monitor URI */
702718
sformat(previousName, sizeof(previousName), "monitor");
@@ -726,7 +742,8 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
726742
node->pgport,
727743
node->name,
728744
node->replicationQuorum,
729-
node->candidatePriority);
745+
node->candidatePriority,
746+
options->skipHBA);
730747

731748
strlcpy(previousName, node->name, sizeof(previousName));
732749
}

src/bin/pg_autoctl/cli_do_tmux.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct TmuxOptions
3636
int asyncNodes; /* number of async nodes, within the total */
3737
int priorities[MAX_NODES]; /* node priorities */
3838
int numSync; /* number-sync-standbys */
39+
bool skipHBA; /* do we want to use --skip-pg-hba? */
3940
char layout[BUFSIZE];
4041
} TmuxOptions;
4142

@@ -77,14 +78,16 @@ bool tmux_prepare_XDG_environment(const char *root,
7778

7879
void tmux_pg_autoctl_create_monitor(PQExpBuffer script,
7980
const char *root,
80-
int pgport);
81+
int pgport,
82+
bool skipHBA);
8183

8284
void tmux_pg_autoctl_create_postgres(PQExpBuffer script,
8385
const char *root,
8486
int pgport,
8587
const char *name,
8688
bool replicationQuorum,
87-
int candidatePriority);
89+
int candidatePriority,
90+
bool skipHBA);
8891

8992
bool tmux_start_server(const char *scriptName);
9093
bool pg_autoctl_getpid(const char *pgdata, pid_t *pid);

src/bin/pg_autoctl/keeper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ keeper_update_group_hba(Keeper *keeper, NodeAddressArray *diffNodesArray)
15711571
postgresSetup->dbname,
15721572
PG_AUTOCTL_REPLICA_USERNAME,
15731573
authMethod,
1574-
postgresSetup->hbaLevel))
1574+
keeper->config.pgSetup.hbaLevel))
15751575
{
15761576
log_error("Failed to edit HBA file \"%s\" to update rules to current "
15771577
"list of nodes registered on the monitor",
@@ -1584,7 +1584,7 @@ keeper_update_group_hba(Keeper *keeper, NodeAddressArray *diffNodesArray)
15841584
* edited the HBA and it's going to take effect at next restart of
15851585
* Postgres, so we're good here.
15861586
*/
1587-
if (postgresSetup->hbaLevel > HBA_EDIT_SKIP &&
1587+
if (keeper->config.pgSetup.hbaLevel >= HBA_EDIT_MINIMAL &&
15881588
pg_setup_is_running(postgresSetup))
15891589
{
15901590
if (!pgsql_reload_conf(pgsql))

src/bin/pg_autoctl/keeper_config.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
OPTION_POSTGRESQL_PROXY_PORT(config), \
196196
OPTION_POSTGRESQL_LISTEN_ADDRESSES(config), \
197197
OPTION_POSTGRESQL_AUTH_METHOD(config), \
198+
OPTION_POSTGRESQL_HBA_LEVEL(config), \
198199
OPTION_SSL_ACTIVE(config), \
199200
OPTION_SSL_MODE(config), \
200201
OPTION_SSL_CA_FILE(config), \
@@ -213,6 +214,7 @@
213214
}
214215

215216
static bool keeper_config_init_nodekind(KeeperConfig *config);
217+
static bool keeper_config_init_hbalevel(KeeperConfig *config);
216218
static bool keeper_config_set_backup_directory(KeeperConfig *config, int nodeId);
217219

218220

@@ -289,6 +291,13 @@ keeper_config_init(KeeperConfig *config,
289291
exit(EXIT_CODE_BAD_CONFIG);
290292
}
291293

294+
if (!keeper_config_init_hbalevel(config))
295+
{
296+
log_error("Failed to initialize postgresql.hba_level");
297+
log_error("Please review your setup options per above messages");
298+
exit(EXIT_CODE_BAD_CONFIG);
299+
}
300+
292301
if (!pg_setup_init(&pgSetup,
293302
&(config->pgSetup),
294303
missingPgdataIsOk,
@@ -405,9 +414,10 @@ keeper_config_read_file_skip_pgsetup(KeeperConfig *config,
405414
/*
406415
* Turn the configuration string for hbaLevel into our enum value.
407416
*/
408-
if (IS_EMPTY_STRING_BUFFER(config->pgSetup.hbaLevelStr))
417+
if (!keeper_config_init_hbalevel(config))
409418
{
410-
strlcpy(config->pgSetup.hbaLevelStr, "minimal", NAMEDATALEN);
419+
log_error("Failed to initialize postgresql.hba_level");
420+
return false;
411421
}
412422

413423
/* set the ENUM value for hbaLevel */
@@ -724,6 +734,30 @@ keeper_config_init_nodekind(KeeperConfig *config)
724734
}
725735

726736

737+
/*
738+
* keeper_config_init_hbalevel initializes the config->pgSetup.hbaLevel and
739+
* hbaLevelStr when no command line option switch has been used that places a
740+
* value (see --auth, --skip-pg-hba, and --pg-hba-lan).
741+
*/
742+
static bool
743+
keeper_config_init_hbalevel(KeeperConfig *config)
744+
{
745+
/*
746+
* Turn the configuration string for hbaLevel into our enum value.
747+
*/
748+
if (IS_EMPTY_STRING_BUFFER(config->pgSetup.hbaLevelStr))
749+
{
750+
strlcpy(config->pgSetup.hbaLevelStr, "minimal", NAMEDATALEN);
751+
}
752+
753+
/* set the ENUM value for hbaLevel */
754+
config->pgSetup.hbaLevel =
755+
pgsetup_parse_hba_level(config->pgSetup.hbaLevelStr);
756+
757+
return true;
758+
}
759+
760+
727761
/*
728762
* keeper_config_set_backup_directory sets the pg_basebackup target directory
729763
* to ${PGDATA}/../backup/${hostname} by default. Adding the local hostname

0 commit comments

Comments
 (0)