Repository navigation
Expand file tree
/
Copy pathpg_durable--0.2.8--0.2.9.sql
More file actions
193 lines (170 loc) · 8.61 KB
/
Copy pathpg_durable--0.2.8--0.2.9.sql
File metadata and controls
193 lines (170 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
-- Copyright (c) Microsoft Corporation.
-- Licensed under the PostgreSQL License.
-- pg_durable upgrade: 0.2.8 -> 0.2.9
--
-- See docs/upgrade-testing.md for the upgrade-script and backward-compatibility
-- requirements (Scenario A / B1 / B2).
--
CREATE TABLE df._installation (
singleton pg_catalog.bool PRIMARY KEY DEFAULT true CHECK (singleton),
id pg_catalog.uuid NOT NULL DEFAULT pg_catalog.gen_random_uuid()
);
INSERT INTO df._installation (singleton) VALUES (true);
REVOKE ALL ON TABLE df._installation FROM PUBLIC;
GRANT SELECT ON TABLE df._installation TO PUBLIC;
CREATE FUNCTION df.validate_installation() RETURNS bool
STRICT
LANGUAGE c
AS 'MODULE_PATHNAME', 'validate_installation_wrapper';
DO $$
BEGIN
PERFORM df.validate_installation();
END $$;
-- HTTP options are additive; existing function ABIs, OIDs and ACLs stay unchanged.
CREATE FUNCTION df."with_http_options"(
"fut" TEXT,
"options" jsonb
) RETURNS TEXT
LANGUAGE c
AS 'MODULE_PATHNAME', 'with_http_options_wrapper';
CREATE FUNCTION df.endpoint_option_validator(
"options" pg_catalog.text[],
"catalog" pg_catalog.oid
) RETURNS pg_catalog.void
LANGUAGE c STRICT
AS 'MODULE_PATHNAME', 'endpoint_option_validator_wrapper';
CREATE FOREIGN DATA WRAPPER pg_durable_fdw
NO HANDLER VALIDATOR df.endpoint_option_validator;
REVOKE ALL ON FOREIGN DATA WRAPPER pg_durable_fdw FROM PUBLIC;
CREATE TYPE df.http_endpoint AS (server pg_catalog.text, path pg_catalog.text);
CREATE FUNCTION df.endpoint("server" pg_catalog.text, "path" pg_catalog.text)
RETURNS df.http_endpoint
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE
AS 'MODULE_PATHNAME', 'endpoint_wrapper';
CREATE FUNCTION df.http(
"url" df.http_endpoint,
"method" pg_catalog.text DEFAULT 'POST',
"body" pg_catalog.text DEFAULT NULL,
"headers" pg_catalog.jsonb DEFAULT NULL,
"timeout_seconds" pg_catalog.int4 DEFAULT 30
) RETURNS pg_catalog.text
LANGUAGE c
AS 'MODULE_PATHNAME', 'http_endpoint_wrapper';
CREATE FUNCTION df.http_multipart(
"url" df.http_endpoint,
"method" pg_catalog.text DEFAULT 'POST',
"parts" pg_catalog.jsonb DEFAULT NULL,
"headers" pg_catalog.jsonb DEFAULT NULL,
"timeout_seconds" pg_catalog.int4 DEFAULT 30
) RETURNS pg_catalog.text
LANGUAGE c
AS 'MODULE_PATHNAME', 'http_multipart_endpoint_wrapper';
REVOKE EXECUTE ON FUNCTION df.http(df.http_endpoint, text, text, jsonb, integer) FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION df.http_multipart(df.http_endpoint, text, jsonb, jsonb, integer) FROM PUBLIC;
CREATE FUNCTION df.secret("server" pg_catalog.text, "key" pg_catalog.text)
RETURNS pg_catalog.jsonb
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE
AS 'MODULE_PATHNAME', 'secret_wrapper';
CREATE OR REPLACE FUNCTION df.grant_usage(
p_role TEXT,
include_http boolean DEFAULT false,
with_grant boolean DEFAULT false
)
RETURNS VOID
LANGUAGE plpgsql
SET search_path = pg_catalog, pg_temp
AS $fn$
DECLARE
grant_opt TEXT := '';
BEGIN
IF with_grant THEN
grant_opt := ' WITH GRANT OPTION';
END IF;
-- Schema access — the access gate for ordinary df.* functions (see header).
EXECUTE pg_catalog.format('GRANT USAGE ON SCHEMA df TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
-- df.http() — opt-in because it makes outbound network requests.
IF include_http THEN
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.http(text, text, text, jsonb, integer) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.http(df.http_endpoint, text, text, jsonb, integer) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
-- df.http_multipart() shares the same opt-in (HTTP egress is one privilege).
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.http_multipart(text, text, jsonb, jsonb, integer) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.http_multipart(df.http_endpoint, text, jsonb, jsonb, integer) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
END IF;
-- Admin helpers and system-wide metrics — with_grant => true marks a
-- pg_durable admin, so it also grants df.metrics() (cluster-wide aggregate
-- counts).
IF with_grant THEN
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.grant_usage(text, boolean, boolean) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.revoke_usage(text) TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT EXECUTE ON FUNCTION df.metrics() TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
END IF;
-- Table privileges
EXECUTE pg_catalog.format('GRANT SELECT ON df.instances TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT UPDATE (status, updated_at) ON df.instances TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT SELECT ON df.nodes TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT INSERT (id, label, root_node, submitted_by, database) ON df.instances TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT INSERT (id, instance_id, node_type, query, result_name, left_node, right_node, submitted_by, database) ON df.nodes TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
EXECUTE pg_catalog.format('GRANT SELECT, INSERT, UPDATE, DELETE ON df.vars TO %I', p_role) OPERATOR(pg_catalog.||) grant_opt;
RAISE NOTICE 'pg_durable: granted df usage privileges to "%"', p_role;
END;
$fn$;
CREATE OR REPLACE FUNCTION df.revoke_usage(p_role TEXT)
RETURNS VOID
LANGUAGE plpgsql
SET search_path = pg_catalog, pg_temp
AS $fn$
BEGIN
-- Mirror of df.grant_usage(): undo exactly what it grants. Revoking schema
-- USAGE is the access gate that locks the role out of ordinary df.*
-- functions; the sensitive functions and table privileges are undone below.
-- CASCADE also removes any sub-grants the role made via WITH GRANT OPTION.
-- Sensitive functions (granted explicitly by grant_usage()). A delegated
-- admin may lack privilege on some of these (e.g. df.http); skip those.
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.http(text, text, text, jsonb, integer) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.http(df.http_endpoint, text, text, jsonb, integer) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.metrics() FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.http_multipart(text, text, jsonb, jsonb, integer) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.http_multipart(df.http_endpoint, text, jsonb, jsonb, integer) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.grant_usage(text, boolean, boolean) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
BEGIN
EXECUTE pg_catalog.format('REVOKE EXECUTE ON FUNCTION df.revoke_usage(text) FROM %I CASCADE', p_role);
EXCEPTION WHEN insufficient_privilege THEN
NULL;
END;
-- Table privileges.
-- Column-level revokes must match the column-level grants from grant_usage().
EXECUTE pg_catalog.format('REVOKE SELECT, INSERT, UPDATE, DELETE ON df.vars FROM %I CASCADE', p_role);
EXECUTE pg_catalog.format('REVOKE INSERT (id, instance_id, node_type, query, result_name, left_node, right_node, submitted_by, database) ON df.nodes FROM %I CASCADE', p_role);
EXECUTE pg_catalog.format('REVOKE SELECT ON df.nodes FROM %I CASCADE', p_role);
EXECUTE pg_catalog.format('REVOKE INSERT (id, label, root_node, submitted_by, database) ON df.instances FROM %I CASCADE', p_role);
EXECUTE pg_catalog.format('REVOKE UPDATE (status, updated_at) ON df.instances FROM %I CASCADE', p_role);
EXECUTE pg_catalog.format('REVOKE SELECT ON df.instances FROM %I CASCADE', p_role);
-- Schema access — the access gate for all ordinary df.* functions.
EXECUTE pg_catalog.format('REVOKE USAGE ON SCHEMA df FROM %I CASCADE', p_role);
RAISE NOTICE 'pg_durable: revoked df usage privileges granted by "%" from "%"', current_user, p_role;
END;
$fn$;