You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[DPE-7594] Sync up pg_hba changes and remove trigger (#1051)
* Sync relations with pg_hba
* Port from K8s
* Increase timeouts
* Switch observer to httpx
* Bump coverage
* Tactical sleep
* Try to clean up triggers
* Use edge for spaces test
* Blocking test app
* Wrong host
* Drop second trigger
# Create database function and event trigger to identify users created by PgBouncer.
902
-
cursor.execute("""
903
-
CREATE OR REPLACE FUNCTION update_pg_hba()
904
-
RETURNS event_trigger
905
-
LANGUAGE plpgsql
906
-
AS $$
907
-
DECLARE
908
-
temp_schema TEXT;
909
-
hba_file TEXT;
910
-
copy_command TEXT;
911
-
connection_type TEXT;
912
-
rec record;
913
-
insert_value TEXT;
914
-
changes INTEGER = 0;
915
-
BEGIN
916
-
-- Don't execute on replicas.
917
-
IF NOT pg_is_in_recovery() THEN
918
-
-- Load the current authorisation rules.
919
-
SELECT nspname INTO temp_schema FROM pg_namespace WHERE oid = pg_my_temp_schema();
920
-
IF temp_schema != '' THEN
921
-
PERFORM TRUE FROM pg_tables WHERE schemaname = temp_schema AND tablename = 'pg_hba';
922
-
IF FOUND THEN
923
-
DROP TABLE pg_hba;
924
-
END IF;
925
-
PERFORM TRUE FROM pg_tables WHERE schemaname = temp_schema AND tablename = 'relation_users';
926
-
IF FOUND THEN
927
-
DROP TABLE relation_users;
928
-
END IF;
929
-
END IF;
930
-
CREATE TEMPORARY TABLE pg_hba (lines TEXT);
931
-
SELECT setting INTO hba_file FROM pg_settings WHERE name = 'hba_file';
932
-
IF hba_file IS NOT NULL THEN
933
-
copy_command='COPY pg_hba FROM ''' || hba_file || '''' ;
934
-
EXECUTE copy_command;
935
-
-- Build a list of the relation users and the databases they can access.
936
-
CREATE TEMPORARY TABLE relation_users AS
937
-
SELECT t.user, STRING_AGG(DISTINCT t.database, ',') AS databases FROM( SELECT u.usename AS user, CASE WHEN u.usesuper THEN 'all' ELSE d.datname END AS database FROM ( SELECT usename, usesuper FROM pg_catalog.pg_user WHERE usename NOT IN ('backup', 'monitoring', 'operator', 'postgres', 'replication', 'rewind')) AS u JOIN ( SELECT datname FROM pg_catalog.pg_database WHERE NOT datistemplate ) AS d ON has_database_privilege(u.usename, d.datname, 'CONNECT') ) AS t GROUP BY 1;
938
-
IF (SELECT COUNT(lines) FROM pg_hba WHERE lines LIKE 'hostssl %') > 0 THEN
IF (SELECT COUNT(lines) FROM pg_hba WHERE lines = insert_value) = 0 THEN
948
-
INSERT INTO pg_hba (lines) VALUES (insert_value);
949
-
changes := changes + 1;
950
-
END IF;
951
-
END LOOP;
952
-
-- Remove users that don't exist anymore from the pg_hba file.
953
-
FOR rec IN SELECT h.lines FROM pg_hba AS h LEFT JOIN relation_users AS r ON SPLIT_PART(h.lines, ' ', 3) = r.user WHERE r.user IS NULL AND (SPLIT_PART(h.lines, ' ', 3) LIKE 'relation_id_%' OR SPLIT_PART(h.lines, ' ', 3) LIKE 'pgbouncer_auth_relation_%' OR SPLIT_PART(h.lines, ' ', 3) LIKE '%_user_%_%')
954
-
LOOP
955
-
DELETE FROM pg_hba WHERE lines = rec.lines;
956
-
changes := changes + 1;
957
-
END LOOP;
958
-
-- Apply the changes to the pg_hba file.
959
-
IF changes > 0 THEN
960
-
copy_command='COPY pg_hba TO ''' || hba_file || '''' ;
961
-
EXECUTE copy_command;
962
-
PERFORM pg_reload_conf();
963
-
END IF;
964
-
END IF;
965
-
END IF;
966
-
END;
967
-
$$ SECURITY DEFINER;
968
-
""")
969
-
cursor.execute(
970
-
"SELECT TRUE FROM pg_event_trigger WHERE evtname = 'update_pg_hba_on_create_schema';"
0 commit comments