Debian Codegraph @

powa_collector/notify.py

raw
1 """
2 Helper functions for the NOTIFY-based communication processing.
3 """
4 import psycopg2
5
6 from powa_collector.utils import get_powa_version
7
8
9 def notify_parse_force_snapshot(notif):
10 """Parse the payload of a received FORCE_SNAPSHOT notification"""
11 if (len(notif) != 1):
12 raise Exception('Command "%r" malformed' % notif)
13
14 try:
15 r_srvid = int(notif[0])
16 notif.pop(0)
17 except:
18 raise Exception(('invalid remote server id %s' % notif[0]))
19
20 return r_srvid
21
22
23 def notify_parse_refresh_db_cat(notif):
24 """Parse the payload of a received REFRESH_DB_CAT notification"""
25 if (len(notif) < 2):
26 raise Exception('Command "%r" malformed' % notif)
27
28 try:
29 r_srvid = int(notif[0])
30 notif.pop(0)
31 except:
32 raise Exception(('invalid remote server id %s' % notif[0]))
33
34 try:
35 r_nb_db = int(notif[0])
36 notif.pop(0)
37 except:
38 raise Exception(('invalid number of databases %s' % notif[0]))
39
40 r_dbnames = notif
41
42 if (r_nb_db != len(r_dbnames)):
43 raise Exception('Caller asked for %d db, but found %d: %r' %
44 (r_nb_db, len(r_dbnames), r_dbnames))
45
46 return (r_srvid, r_dbnames)
47
48 def notify_allowed(pid, conn):
49 """
50 Check that the role used in the given backend, identified by its pid, is
51 allowed to send notifications.
52 """
53 powa_roles = ['powa_signal_backend', 'powa_write_all_data', 'powa_admin']
54 roles_to_test = []
55 cur = conn.cursor()
56
57 powa_ver = get_powa_version(conn)
58
59 # Should not happen
60 if not powa_ver:
61 return False
62
63 # pg 14+ introduced predefined roles
64 if conn.server_version >= 140000:
65 roles_to_test.append('pg_signal_backend')
66
67 try:
68 # powa 5+ introduced various powa_* predefined roles so try them, but
69 # also possibly pg14+ predefined roles
70 if (int(powa_ver[0][0]) >= 5):
71 cur.execute("""WITH s(v) AS (
72 SELECT unnest(%s)
73 UNION ALL
74 SELECT rolname
75 FROM {powa}.powa_roles
76 WHERE powa_role = ANY (%s)
77 )
78 SELECT bool_or(pg_has_role(usesysid, v, 'USAGE'))
79 FROM pg_stat_activity a
80 CROSS JOIN s
81 WHERE pid = %s""", (roles_to_test, powa_roles, pid))
82 # if powa 4- but we have a list of predefined roles, check those
83 elif (len(roles_to_test) > 0):
84 cur.execute("""WITH s(v) AS (SELECT unnest(%s))
85 SELECT bool_or(pg_has_role(usesysid, v, 'USAGE'))
86 FROM pg_stat_activity a
87 CROSS JOIN s
88 WHERE pid = %s""", (roles_to_test, pid))
89 # if not (both old powa and postgres version), fallback to testing
90 # whether the given role has enough privileges to INSERT data in
91 # powa_statements to decide whether they should be allowed to use the
92 # nofication system.
93 else:
94 cur.execute("""SELECT
95 has_table_privilege(usesysid, '{powa}.powa_statements',
96 'INSERT')
97 FROM pg_stat_activity
98 WHERE pid = %s""", (pid, ))
99 except psycopg2.Error as e:
100 conn.rollback()
101 return False
102
103 row = cur.fetchone()
104
105 # Connection already closed, deny
106 if (not row):
107 return False
108
109 return (row[0] is True)