Skip to content

Commit 77e1317

Browse files
committed
Add the postgres tests in whatever state I left them in.
1 parent d253521 commit 77e1317

5 files changed

+210
-0
lines changed

Diff for: pg-test/01-create-actors.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
source ../default_psql_opts.sh
4+
5+
#
6+
# Create the users/groups needed for the example
7+
#
8+
9+
SQL="
10+
CREATE ROLE admins;
11+
CREATE ROLE customer_devs;
12+
13+
CREATE USER anonymous;
14+
CREATE USER dba1 IN ROLE admins,customer_devs;
15+
CREATE USER alice IN ROLE customer_devs;
16+
CREATE USER bob IN ROLE customer_devs;
17+
18+
CREATE DATABASE customer_project;
19+
CREATE DATABASE dba_project;
20+
"
21+
22+
psql $PSQL_OPTS <<< "${SQL}";

Diff for: pg-test/02-create-permissions.sh

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
source ../default_psql_opts.sh
4+
5+
#
6+
# Are there any Postgres commands that you can insert here to make the
7+
# tests work? Note that this is already completely insane compared to
8+
# the filesystem example.
9+
#
10+
11+
DATABASES="customer_project dba_project"
12+
13+
ADMIN_QUERY="
14+
SELECT usename
15+
FROM (pg_user INNER JOIN pg_group
16+
ON pg_user.usesysid = any(pg_group.grolist))
17+
WHERE pg_group.groname = 'admins';
18+
"
19+
ADMINS=$(psql $PSQL_OPTS \
20+
--no-align \
21+
--tuples-only \
22+
--dbname postgres \
23+
<<< "${ADMIN_QUERY}" )
24+
25+
DEV_QUERY="
26+
SELECT usename
27+
FROM (pg_user INNER JOIN pg_group
28+
ON pg_user.usesysid = any(pg_group.grolist))
29+
WHERE pg_group.groname = 'customer_devs';
30+
"
31+
CUSTOMER_DEVS=$(psql $PSQL_OPTS \
32+
--no-align \
33+
--tuples-only \
34+
--dbname postgres \
35+
<<< "${DEV_QUERY}" )
36+
37+
ALL_USERS="${ADMINS} ${CUSTOMER_DEVS}"
38+
39+
for database in $DATABASES; do
40+
for user in $ALL_USERS; do
41+
SQL="
42+
GRANT ALL PRIVILEGES ON SCHEMA public TO admins;
43+
44+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
45+
GRANT ALL PRIVILEGES ON TABLES TO admins;
46+
47+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
48+
GRANT ALL PRIVILEGES ON SEQUENCES TO admins;
49+
50+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
51+
GRANT ALL PRIVILEGES ON FUNCTIONS TO admins;
52+
53+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
54+
GRANT ALL PRIVILEGES ON TYPES TO admins;
55+
"
56+
psql -q -U "${PSQL_USER}" -d "${database}" <<< "${SQL}"
57+
done
58+
done
59+
60+
61+
for user in $ALL_USERS; do
62+
SQL="
63+
GRANT ALL PRIVILEGES ON SCHEMA public TO customer_devs;
64+
65+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
66+
GRANT ALL PRIVILEGES ON TABLES TO customer_devs;
67+
68+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
69+
GRANT ALL PRIVILEGES ON SEQUENCES TO customer_devs;
70+
71+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
72+
GRANT ALL PRIVILEGES ON FUNCTIONS TO customer_devs;
73+
74+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
75+
GRANT ALL PRIVILEGES ON TYPES TO customer_devs;
76+
"
77+
78+
psql -q -U "${PSQL_USER}" -d customer_project <<< "${SQL}"
79+
80+
SQL="
81+
GRANT USAGE ON SCHEMA public TO anonymous;
82+
83+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
84+
GRANT SELECT ON TABLES TO anonymous;
85+
86+
ALTER DEFAULT PRIVILEGES FOR ROLE \"${user}\"
87+
GRANT SELECT ON SEQUENCES TO anonymous;
88+
"
89+
psql -q -U "${PSQL_USER}" -d customer_project <<< "${SQL}"
90+
91+
done
92+

Diff for: pg-test/03-run-tests.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
source ../die.sh
4+
5+
# Ignore expected errors.
6+
exec 2>/dev/null
7+
8+
# The admins and customer-devs can create files in customer-project.
9+
10+
psql -q -U alice -d customer_project -c 'CREATE TABLE alice ( id int );'
11+
psql -q -U bob -d customer_project -c 'CREATE TABLE bob ( id int );'
12+
psql -q -U dba1 -d customer_project -c 'CREATE TABLE dba1 ( id int );'
13+
14+
# Each user can insert into the tables created by the others.
15+
psql -q -U bob -d customer_project -c 'INSERT INTO dba1 VALUES (1);' \
16+
|| die "bob can't modify dba1's table."
17+
18+
psql -q -U dba1 -d customer_project -c 'INSERT INTO alice VALUES (1);' \
19+
|| die "dba1 can't modify alice's table."
20+
21+
psql -q -U alice -d customer_project -c 'INSERT INTO bob VALUES (1);' \
22+
|| die "alice can't modify bob's table."
23+
24+
# The anonymous user can read bob's table.
25+
psql -o /dev/null -q -U anonymous -d customer_project \
26+
-c 'SELECT * FROM bob;' \
27+
|| die "The anonymous user can't read bob's table."
28+
29+
# The admins' databases are accessible only to themselves.
30+
psql -q -U dba1 -d dba_project -c 'CREATE TABLE dba1 ( id int );'
31+
32+
psql -q -U alice -d dba_project -c 'INSERT INTO dba1 VALUES (1);' \
33+
&& die "alice can modify dba1's table."
34+

Diff for: pg-test/04-add-new-user-and-retest.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
source ../default_psql_opts.sh
4+
source ../die.sh
5+
6+
# Ignore expected errors.
7+
exec 2>/dev/null
8+
9+
#
10+
# Now we add another admin. He should be able to access everything
11+
# without having to go back and set permissions manually. Likewise,
12+
# other people should be able to modify his stuff.
13+
#
14+
psql $PSQL_OPTS -c 'CREATE USER dba2 in ROLE admins,customer_devs;'
15+
16+
17+
# dba2 is automatically allowed to modify alice's table.
18+
psql -q -U dba2 -d customer_project -c 'INSERT INTO alice VALUES (2);' \
19+
|| die "dba2 can't modify alice's table."
20+
21+
# Now dba2 creates a file in the customer's project.
22+
psql -q -U dba2 -d customer_project -c 'CREATE TABLE dba2 ( id int );'
23+
24+
# Alice can modify it.
25+
psql -U alice -d customer_project -c 'INSERT INTO dba2 VALUES (2);' \
26+
|| die "alice can't modify dba2's table."
27+
28+
# And the anonymous user can read it.
29+
psql -o /dev/null -q -U anonymous -d customer_project \
30+
-c 'SELECT * FROM dba2;' \
31+
|| die "The anonymous user can't read dba2's table."
32+
33+
# dba2 should also be able to modify dba1's table.
34+
psql -q -U dba2 -d dba_project -c 'INSERT INTO dba1 VALUES (2);' \
35+
|| die "dba2 can't modify dba1's table."

Diff for: pg-test/05-destroy-actors.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
source ../default_psql_opts.sh
4+
5+
# Ignore "does not exist" errors.
6+
exec 2>/dev/null
7+
8+
#
9+
# Destroy the users/groups created for the test.
10+
#
11+
12+
DROP_OPTS="-U ${PSQL_USER} --if-exists"
13+
14+
dropuser $DROP_OPTS anonymous
15+
dropuser $DROP_OPTS dba1
16+
dropuser $DROP_OPTS dba2
17+
dropuser $DROP_OPTS alice
18+
dropuser $DROP_OPTS bob
19+
20+
dropuser $DROP_OPTS admins
21+
dropuser $DROP_OPTS customer_devs
22+
23+
#
24+
# Remove the databases and tables created during the test.
25+
#
26+
dropdb $DROP_OPTS customer_project
27+
dropdb $DROP_OPTS dba_project

0 commit comments

Comments
 (0)