Skip to content

Commit 38d60e4

Browse files
committed
add more code
1 parent 6c307a7 commit 38d60e4

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

declarative_checks.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
BEGIN;
2+
3+
CREATE TABLE permissions.t_desired_permission
4+
(
5+
id int4 PRIMARY KEY,
6+
rolname text,
7+
perms text[],
8+
operation text,
9+
obj text,
10+
sub_obj text
11+
);
12+
13+
INSERT INTO permissions.t_desired_permission
14+
VALUES (1, 'anon', '{"SELECT", "INSERT"}', 'all_tables_in_schema', 'public', NULL),
15+
-- anon should have SELECT and INSERT in all tables in schema public
16+
17+
(2, NULL, '{"SELECT"}', 'all_tables_in_schema', 'sales', NULL),
18+
-- everbody should have SELECT on all tables in schema sales
19+
20+
(3, NULL, '{"USAGE"}', 'all_schemas', NULL, NULL),
21+
-- everybody should have usage rights in all schemas
22+
23+
(4, 'joe', '{"SELECT"}', 'on_table', 'public', 't_test'),
24+
-- joe should have SELECT on table public.t_test
25+
26+
(5, NULL, '{"SELECT", "INSERT"}', NULL, NULL, NULL),
27+
-- everbody should have SELECT and INSERT on all tables and views in all schemas
28+
29+
(6, 'joe', '{"SELECT"}', 'on_view', 'public', 'v_test'),
30+
-- joe should have SELECT on view public.v_test
31+
32+
(7, NULL, '{"EXECUTE"}', 'on_functions_in_schema', 'public', NULL)
33+
-- everbody should have EXECUTE on all function in schema public
34+
;
35+
36+
ROLLBACK;
37+

materialize_permissions.sql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
BEGIN;
2+
3+
CREATE MATERIALIZED VIEW security_status AS SELECT * FROM permissions.all_permissions;
4+
5+
SELECT count(*) FROM security_status;
6+
7+
8+
CREATE TABLE track_security_changes (LIKE security_status);
9+
ALTER TABLE track_security_changes ADD COLUMN t timestamptz DEFAULT now();
10+
ALTER TABLE track_security_changes ADD COLUMN change text CHECK (change IN ('added', 'removed')) NOT NULL;
11+
12+
-- create a function and an event trigger
13+
CREATE OR REPLACE FUNCTION detect_permission_change()
14+
RETURNS event_trigger AS
15+
$$
16+
BEGIN
17+
RAISE NOTICE 'checking security situation for % and %', tg_event, tg_tag;
18+
INSERT INTO track_security_changes
19+
SELECT *, now(), 'added'
20+
FROM (
21+
SELECT * FROM permissions.all_permissions
22+
EXCEPT
23+
SELECT * FROM security_status
24+
) AS x
25+
UNION ALL
26+
SELECT *, now(), 'removed'
27+
FROM (
28+
SELECT * FROM security_status
29+
EXCEPT
30+
SELECT * FROM permissions.all_permissions
31+
) AS x;
32+
END;
33+
$$ LANGUAGE 'plpgsql';
34+
35+
CREATE EVENT TRIGGER detect_permission_change
36+
ON ddl_command_end
37+
EXECUTE PROCEDURE detect_permission_change();
38+
39+
40+
-- test the code
41+
-- CREATE TABLE t_abc (id int);
42+
-- GRANT ALL ON t_abc TO jane;
43+
-- DROP TABLE t_test;
44+
45+
-- SELECT * FROM track_security_changes WHERE object_type <> 'column';
46+

0 commit comments

Comments
 (0)