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
+42-10Lines changed: 42 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,15 @@ categories:
13
13
- serverless
14
14
---
15
15
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
+
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).
19
+
16
20
- A Scaleway account logged into the [console](https://console.scaleway.com)
17
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
18
22
-[Created a Serverless SQL Database](/serverless/sql-databases/how-to/create-a-database/)
19
23
20
-
## Add sample data and create PostgreSQL Row Level Security
24
+
## How to add sample data and enable PostgreSQL Row Level Security
21
25
22
26
1.[Connect to your Serverless SQL Database](/serverless/sql-databases/quickstart/#how-to-connect-to-a-database) with a PostgreSQL client such as `psql`:
23
27
```bash
@@ -35,11 +39,32 @@ categories:
35
39
ALTERTABLE pets ENABLE row level security;
36
40
```
37
41
38
-
## Use Row Level Security with PostgREST
42
+
3. Run the command below to enable **Row Level Security**:
43
+
```sql
44
+
ALTERTABLE pets ENABLE row level security;
45
+
```
39
46
40
-
PostgREST 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). 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).
47
+
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
+
```sql
49
+
CREATE POLICY pets_keeper ON pets TO role_readwrite USING (keeper =current_user);
5. (Optional) Run the command below to check that you can see all the data with your current connection:
53
+
```sql
54
+
SELECT*FROM pets;
55
+
```
56
+
All the data contained in the database displays, as you are connected with `role_admin`.
57
+
58
+
<Messagetype="tip">
59
+
You can verify the current role your are connected with using the following command:
60
+
```sql
61
+
SELECTcurrent_user;
62
+
```
63
+
</Message>
64
+
65
+
## How to use Row Level Security with PostgREST
66
+
67
+
1. Install PostgREST by following the [official documentation](https://docs.postgrest.org/en/v12/tutorials/tut0.html#step-1-install-postgresql).
43
68
44
69
2. Create a `tutorial.conf` file with the following content:
45
70
```
@@ -48,21 +73,28 @@ PostgREST built-in Row Level Security based on users JWT relies either on [role
48
73
jwt-secret = "[your jwt secret]"
49
74
```
50
75
where:
51
-
-`db-uri` should use credentials with an application having **ServerlessSQLDatabaseDataReadWrite** permissions (and not **ServerlessSQLDatabaseDataReadWrite** neither **ServerlessSQLDatabaseFullAccess**)
52
-
-`db-schemas` is your database schema. You can use `"public"` as a default value.
53
-
-`jwt-secret` can be generated using the command `openssl rand -base64 32`
76
+
-`db-uri` must use credentials with an [application](/identity-and-access-management/iam/how-to/create-application/) having **ServerlessSQLDatabaseDataReadWrite** permissions (neither **ServerlessSQLDatabaseReadWrite** nor **ServerlessSQLDatabaseFullAccess**)
77
+
-`db-schemas` is your database schema. Use `public` as a default value.
78
+
-`jwt-secret` can be generated using the following command:
79
+
```sh
80
+
openssl rand -base64 32
81
+
```
82
+
83
+
3. Run the command below to start a local PostgREST instance:
54
84
55
-
3. Run PostgREST:
56
85
```bash
57
86
postgrest tutorial.conf
58
87
```
59
-
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 the payload data `{"role": "role_readwrite"}`:
88
+
89
+
<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:
60
91
```bash
61
92
curl http://localhost:3000/pets \
62
93
-H "Authorization: Bearer $TOKEN"
63
94
```
64
95
where `$TOKEN` is your generated JWT.
65
96
A pet list should display.
97
+
</Message>
66
98
67
99
4. Connect to your Serverless SQL Database with **ServerlessSQLDatabaseFullAccess** permissions, and delete the existing policy on the `pets` table:
68
100
```sql
@@ -92,7 +124,7 @@ PostgREST built-in Row Level Security based on users JWT relies either on [role
92
124
curl http://localhost:3000/pets \
93
125
-H "Authorization: Bearer $TOKEN"
94
126
```
95
-
You should only see pets with a `keeper` column value of `role_readwrite`.
127
+
You should only see pets with a `role_readwrite`value for `keeper`.
96
128
97
129
Your new application can now only access a specific subset of rows based on its permissions using transaction-scoped settings.
Copy file name to clipboardExpand all lines: serverless/sql-databases/how-to/use-row-level-security.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ This requires setting up different [IAM permissions sets](/identity-and-access-m
25
25
-[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
26
26
-[Created a Serverless SQL Database](/serverless/sql-databases/how-to/create-a-database/)
27
27
28
-
## Add sample data and create PostgreSQL Row Level Security
28
+
## How to add sample data and enable PostgreSQL Row Level Security
29
29
30
30
1.[Connect to your Serverless SQL Database](/serverless/sql-databases/quickstart/#how-to-connect-to-a-database) with a PostgreSQL client such as `psql`:
31
31
```bash
@@ -61,7 +61,7 @@ This requires setting up different [IAM permissions sets](/identity-and-access-m
61
61
```
62
62
</Message>
63
63
64
-
## Create an IAM application with Row Level Security enabled
64
+
## How to create an IAM application with Row Level Security enabled
65
65
66
66
1. Create a new [IAM application](/identity-and-access-management/iam/how-to/create-application/).
0 commit comments