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
Copy file name to clipboardExpand all lines: serverless/sql-databases/api-cli/postgrest-row-level-security.mdx
+20-24Lines changed: 20 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ categories:
15
15
16
16
PostgREST's built-in Row Level Security based on users JWT relies either on [role impersonation](https://docs.postgrest.org/en/v12/references/auth.html#user-impersonation) or [transaction-scoped settings](https://docs.postgrest.org/en/v12/references/transactions.html#tx-settings).
17
17
18
-
Due to connection pooling, Serverless SQL Database currently only support transaction-scoped settings and requires using a single PostgreSQL role for all queries (the internal `role_readwrite` in PostgreSQL).
18
+
Due to connection pooling, Serverless SQL Databases currently only support transaction-scoped settings and requires using a single PostgreSQL role for all queries (the internal `role_readwrite` in PostgreSQL).
19
19
20
20
- A Scaleway account logged into the [console](https://console.scaleway.com)
21
21
-[Owner](/identity-and-access-management/iam/concepts/#owner) status or [IAM permissions](/identity-and-access-management/iam/concepts/#permission) allowing you to perform actions in the intended Organization
@@ -39,11 +39,6 @@ Due to connection pooling, Serverless SQL Database currently only support transa
39
39
ALTERTABLE pets ENABLE row level security;
40
40
```
41
41
42
-
3. Run the command below to enable **Row Level Security**:
43
-
```sql
44
-
ALTERTABLE pets ENABLE row level security;
45
-
```
46
-
47
42
4. Run the command below to create a PostgreSQL policy so that users or applications connecting with `role_readwrite` can access a `pet` row only if its `keeper` column value is `role_readwrite`:
48
43
```sql
49
44
CREATE POLICY pets_keeper ON pets TO role_readwrite USING (keeper =current_user);
@@ -53,10 +48,10 @@ Due to connection pooling, Serverless SQL Database currently only support transa
53
48
```sql
54
49
SELECT*FROM pets;
55
50
```
56
-
All the data contained in the database displays, as you are connected with `role_admin`.
51
+
All the data in the database displays, as you are connected with `role_admin`.
57
52
58
53
<Messagetype="tip">
59
-
You can verify the current role your are connected with using the following command:
54
+
You can verify the current role you are connected with using the following command:
60
55
```sql
61
56
SELECTcurrent_user;
62
57
```
@@ -67,67 +62,68 @@ Due to connection pooling, Serverless SQL Database currently only support transa
67
62
1. Install PostgREST by following the [official documentation](https://docs.postgrest.org/en/v12/tutorials/tut0.html#step-1-install-postgresql).
68
63
69
64
2. Create a `tutorial.conf` file with the following content:
-`db-uri` must use credentials with an [application](/identity-and-access-management/iam/how-to/create-application/) having **ServerlessSQLDatabaseDataReadWrite** permissions (neither **ServerlessSQLDatabaseReadWrite** nor **ServerlessSQLDatabaseFullAccess**)
71
+
72
+
Where:
73
+
-`db-uri` must use credentials with an [application](/identity-and-access-management/iam/how-to/create-application/) having **ServerlessSQLDatabaseDataReadWrite** permissions (not **ServerlessSQLDatabaseReadWrite** or **ServerlessSQLDatabaseFullAccess**)
77
74
-`db-schemas` is your database schema. Use `public` as a default value.
78
-
-`jwt-secret`can be generated using the following command:
75
+
-`jwt-secret`is a token generated using the following command:
79
76
```sh
80
77
openssl rand -base64 32
81
78
```
82
79
83
-
3.Run the command below to start a local PostgREST instance:
80
+
3.In a terminal, access the folder containing the `tutorial.conf` file, and run the command below to start a local PostgREST instance:
84
81
85
82
```bash
86
83
postgrest tutorial.conf
87
84
```
88
85
89
86
<Messagetype="tip">
90
-
You can check that your are able to query your database by [generating a JWT](https://docs.postgrest.org/en/v12/tutorials/tut1.html#step-3-sign-a-token) with `{"role": "role_readwrite"}` as the payload data, then running the command below:
87
+
You can check that you can query your database by [generating a JWT](https://docs.postgrest.org/en/v12/tutorials/tut1.html#step-3-sign-a-token) with `{"role": "role_readwrite"}` as the payload data, then running the command below, where `$TOKEN` is your generated JWT:
91
88
```bash
92
89
curl http://localhost:3000/pets \
93
90
-H "Authorization: Bearer $TOKEN"
94
91
```
95
-
where `$TOKEN` is your generated JWT.
96
-
A pet list should display.
92
+
A list of pets displays.
97
93
</Message>
98
94
99
-
4. Connect to your Serverless SQL Database with **ServerlessSQLDatabaseFullAccess** permissions, and delete the existing policy on the `pets` table:
95
+
4. Connect to your Serverless SQL Database with **ServerlessSQLDatabaseFullAccess** permissions, and run the following command to delete the `pets_keeper` policy previously applied to the `pets` table:
100
96
```sql
101
97
DROP POLICY pets_keeper ON pets;
102
98
```
103
99
104
-
5.Create a new policy on the `pets` table:
100
+
5.Run the command below to create a new policy on the `pets` table:
105
101
```sql
106
102
CREATE POLICY pets_keeper ON pets TO role_readwrite
107
103
USING (keeper = current_setting('request.jwt.claims', true)::json->>'user_type');
108
104
```
109
-
This policy will use `current_settings` instead on`current_user` and thus check for additional fields contained by the JWT instead of only the `"role"` field.
105
+
This policy uses `current_setting` instead of`current_user`, and thus checks for additional fields contained by the JWT, and not only the `role` field.
110
106
111
-
6. Generate a JWT with the following payload data:
107
+
6.[Generate a JWT](https://docs.postgrest.org/en/v12/tutorials/tut1.html#step-3-sign-a-token) with the following payload data:
112
108
```json
113
109
{
114
110
"role": "role_readwrite",
115
111
"user_type": "role_readwrite"
116
112
}
117
113
```
118
114
<Messagetype="tip">
119
-
In this configuration, `user_type` value from JWT will be checked against `keeper` column value in your database to authorize access. You can replace `"user_type": "role_readwrite"`by any alternative field name or value depending on your use case. However you need to keep `"role": "role_readwrite"` for any kind of users you want to authenticate through PostgREST, because alternative roles (such as `role_admin`) will already have too much privileges and be able to see any data.
115
+
Here, the `user_type` value from the JWT will be checked against the `keeper` column value in your database to authorize access. You can replace `"user_type": "role_readwrite"`with any alternative field name or value depending on your use case. However, you must keep `"role": "role_readwrite"` for any users you want to authenticate through PostgREST, because other roles (such as `role_admin`) have too many permissions and will be able to see any data.
120
116
</Message>
121
117
122
-
7.Query your database using this JWT through PostgREST:
118
+
7.Run the command below to query your database using the JWT you just created through PostgREST:
123
119
```bash
124
120
curl http://localhost:3000/pets \
125
121
-H "Authorization: Bearer $TOKEN"
126
122
```
127
-
You should only see pets with a `role_readwrite` value for `keeper`.
123
+
A list of pets with a `role_readwrite` value for `keeper` displays.
128
124
129
125
Your new application can now only access a specific subset of rows based on its permissions using transaction-scoped settings.
130
126
131
127
<Messagetype="tip">
132
-
You can change your JWT payload data with `"user_type": "role_admin"` and see that only another set of rows will be displayed. You can go further by adding any additional fields or values to filter, and edit your policy to filter on a more complex set of rules.
128
+
You can change your JWT payload data with `"user_type": "role_admin"` and see that only another set of rows will be displayed. You can go further by adding fields or values to filter, and edit your policy to filter on a more complex set of rules. Refer to the [official PostgREST](https://docs.postgrest.org/en/v12/explanations/db_authz.html) documentation for more information.
0 commit comments