From f514d04faa26108f1d4fb049b55334e749d5955b Mon Sep 17 00:00:00 2001 From: Slach Date: Thu, 26 Apr 2018 10:48:10 +0500 Subject: [PATCH 1/3] add ipv6, ipv4 and hostname support when check pg_hba rules --- pgrepup/commands/check.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pgrepup/commands/check.py b/pgrepup/commands/check.py index b0d04cd..a839fb6 100644 --- a/pgrepup/commands/check.py +++ b/pgrepup/commands/check.py @@ -23,6 +23,12 @@ from ..helpers.database import * from ..helpers.ui import * +ipv4_address = re.compile( + '^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' +) +ipv6_address = re.compile( + '^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,4}:){5}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)$' +) @dispatch.on('check') def check(**kwargs): @@ -249,13 +255,19 @@ def checks(target, single_test=None, db_conn=None): if not rows: continue - replication_rule = re.compile("^[ ]*host[ ]+replication[ ]+%s[ ]+%s/32[ ]+md5" % ( + destination_host = get_connection_params('Destination')["host"] + if ipv4_address.match(destination_host): + destination_host += '/32' + if ipv6_address.match(destination_host): + destination_host += '/128' + + replication_rule = re.compile("^[ ]*host[ ]+replication[ ]+%s[ ]+%s[ ]+md5" % ( get_pgrepup_replication_user(), - get_connection_params('Destination')["host"] + destination_host )) - connection_rule = re.compile("^[ ]*host[ ]+all[ ]+%s[ ]+%s/32[ ]+md5" % ( + connection_rule = re.compile("^[ ]*host[ ]+all[ ]+%s[ ]+%s[ ]+md5" % ( get_pgrepup_replication_user(), - get_connection_params('Destination')["host"] + destination_host )) replication_rule_present = False connection_rule_present = False From fe2146af5d8965a7fd6193669923eb9fbe7eaccc Mon Sep 17 00:00:00 2001 From: Slach Date: Thu, 26 Apr 2018 11:41:54 +0500 Subject: [PATCH 2/3] return missed kwargs, see https://github.com/rtshome/pgrepup/issues/15 Signed-off-by: Slach --- pgrepup/commands/fix.py | 2 +- pgrepup/commands/setup.py | 2 +- pgrepup/commands/start.py | 2 +- pgrepup/commands/status.py | 2 +- pgrepup/commands/stop.py | 2 +- pgrepup/commands/uninstall.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pgrepup/commands/fix.py b/pgrepup/commands/fix.py index 1bd14fb..d67bcc0 100644 --- a/pgrepup/commands/fix.py +++ b/pgrepup/commands/fix.py @@ -21,7 +21,7 @@ @dispatch.on('fix') -def fix(): +def fix(**kwargs): # Shortcut to ask master password before output Configuration message decrypt(config().get('Source', 'password')) diff --git a/pgrepup/commands/setup.py b/pgrepup/commands/setup.py index 9ff3447..7af3e78 100644 --- a/pgrepup/commands/setup.py +++ b/pgrepup/commands/setup.py @@ -24,7 +24,7 @@ @dispatch.on('setup') -def setup(): +def setup(**kwargs): result = True if check_destination_subscriptions(): diff --git a/pgrepup/commands/start.py b/pgrepup/commands/start.py index f18d902..c117e05 100644 --- a/pgrepup/commands/start.py +++ b/pgrepup/commands/start.py @@ -22,7 +22,7 @@ @dispatch.on('start') -def start(): +def start(**kwargs): # Shortcut to ask master password before output Configuration message decrypt(config().get('Source', 'password')) diff --git a/pgrepup/commands/status.py b/pgrepup/commands/status.py index 281b9d7..e7631d7 100644 --- a/pgrepup/commands/status.py +++ b/pgrepup/commands/status.py @@ -24,7 +24,7 @@ @dispatch.on('status') -def status(): +def status(**kwargs): targets = ['Source', 'Destination'] # Shortcut to ask master password before output Configuration message diff --git a/pgrepup/commands/stop.py b/pgrepup/commands/stop.py index b82a22c..f1ff4a0 100644 --- a/pgrepup/commands/stop.py +++ b/pgrepup/commands/stop.py @@ -21,7 +21,7 @@ @dispatch.on('stop') -def stop(): +def stop(**kwargs): # Shortcut to ask master password before output Configuration message decrypt(config().get('Source', 'password')) diff --git a/pgrepup/commands/uninstall.py b/pgrepup/commands/uninstall.py index 66790c6..a4257cc 100644 --- a/pgrepup/commands/uninstall.py +++ b/pgrepup/commands/uninstall.py @@ -22,7 +22,7 @@ @dispatch.on('uninstall') -def uninstall(): +def uninstall(**kwargs): stop() output_cli_message("Uninstall operations", color='cyan') From bf378bd3ff6f144ada51e67116ff04b68796dc98 Mon Sep 17 00:00:00 2001 From: Slach Date: Thu, 26 Apr 2018 14:43:25 +0500 Subject: [PATCH 3/3] fix setup in cygwin again --- pgrepup/commands/setup.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pgrepup/commands/setup.py b/pgrepup/commands/setup.py index 7af3e78..6d1f752 100644 --- a/pgrepup/commands/setup.py +++ b/pgrepup/commands/setup.py @@ -25,7 +25,6 @@ @dispatch.on('setup') def setup(**kwargs): - result = True if check_destination_subscriptions(): result = False @@ -112,13 +111,15 @@ def _setup_source(conn, pg_pass): pg_dumpall_schema = "%s/pg_dumpall_schema_%s.sql" % (get_tmp_folder(), uuid.uuid4().hex) output_cli_message("Dump globals and schema of all databases") - pg_dumpall_schema_result = \ - os.system('sh -c "PGPASSFILE=%(pgpass)s pg_dumpall -U %(user)s -h %(host)s -p%(port)s -s -f %(fname)s ' + - '--if-exists -c"' % - merge_two_dicts( - get_connection_params('Source'), - {"fname": pg_dumpall_schema, "pgpass": pg_pass} - )) + sh_cmd = 'PGPASSFILE=%(pgpass)s pg_dumpall -U %(user)s -h %(host)s -p%(port)s -s -f %(fname)s --if-exists -c' + sh_cmd = sh_cmd % ( + merge_two_dicts( + get_connection_params('Source'), + {"fname": pg_dumpall_schema, "pgpass": pg_pass} + ) + ) + sh_cmd = 'sh -c "%(sh_cmd)s"' % {'sh_cmd': sh_cmd} + pg_dumpall_schema_result = os.system(sh_cmd) result['result'] = result['result'] and pg_dumpall_schema_result == 0 print(output_cli_result(result['result'])) @@ -158,14 +159,13 @@ def _setup_destination(conn, pg_pass, source_setup_results): result = {'result': True} output_cli_message("Create and import source globals and schema") if 'pg_dumpall' in source_setup_results: - restore_schema_result = \ - os.system( - 'sh -c "PGPASSFILE=%(pgpass)s psql -U %(user)s -h %(host)s -p%(port)s -f %(fname)s -d postgres ' + - '>/dev/null 2>&1"' - % merge_two_dicts( - get_connection_params('Destination'), - {"fname": source_setup_results['pg_dumpall'], "pgpass": pg_pass} - )) + sh_cmd = 'PGPASSFILE=%(pgpass)s psql -U %(user)s -h %(host)s -p%(port)s -f %(fname)s -d postgres >/dev/null 2>&1' + sh_cmd = sh_cmd % merge_two_dicts( + get_connection_params('Destination'), + {"fname": source_setup_results['pg_dumpall'], "pgpass": pg_pass} + ) + sh_cmd = 'sh -c "%(sh_cmd)s"' % {'sh_cmd': sh_cmd} + restore_schema_result = os.system(sh_cmd) result['result'] = result['result'] and restore_schema_result == 0 print(output_cli_result(restore_schema_result == 0))