Skip to content

Commit e78d515

Browse files
committed
docs(SDB): update
1 parent a8aec89 commit e78d515

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

serverless/sql-databases/api-cli/postgrest-row-level-security.mdx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ categories:
1313
- serverless
1414
---
1515

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+
1620
- A Scaleway account logged into the [console](https://console.scaleway.com)
1721
- [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
1822
- [Created a Serverless SQL Database](/serverless/sql-databases/how-to/create-a-database/)
1923

20-
## Add sample data and create PostgreSQL Row Level Security
24+
## How to add sample data and enable PostgreSQL Row Level Security
2125

2226
1. [Connect to your Serverless SQL Database](/serverless/sql-databases/quickstart/#how-to-connect-to-a-database) with a PostgreSQL client such as `psql`:
2327
```bash
@@ -35,11 +39,32 @@ categories:
3539
ALTER TABLE pets ENABLE row level security;
3640
```
3741

38-
## Use Row Level Security with PostgREST
42+
3. Run the command below to enable **Row Level Security**:
43+
```sql
44+
ALTER TABLE pets ENABLE row level security;
45+
```
3946

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);
50+
```
4151

42-
1. [Install PostgREST](https://docs.postgrest.org/en/v12/tutorials/tut0.html#step-1-install-postgresql)
52+
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+
<Message type="tip">
59+
You can verify the current role your are connected with using the following command:
60+
```sql
61+
SELECT current_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).
4368

4469
2. Create a `tutorial.conf` file with the following content:
4570
```
@@ -48,21 +73,28 @@ PostgREST built-in Row Level Security based on users JWT relies either on [role
4873
jwt-secret = "[your jwt secret]"
4974
```
5075
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:
5484

55-
3. Run PostgREST:
5685
```bash
5786
postgrest tutorial.conf
5887
```
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+
<Message type="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:
6091
```bash
6192
curl http://localhost:3000/pets \
6293
-H "Authorization: Bearer $TOKEN"
6394
```
6495
where `$TOKEN` is your generated JWT.
6596
A pet list should display.
97+
</Message>
6698

6799
4. Connect to your Serverless SQL Database with **ServerlessSQLDatabaseFullAccess** permissions, and delete the existing policy on the `pets` table:
68100
```sql
@@ -92,7 +124,7 @@ PostgREST built-in Row Level Security based on users JWT relies either on [role
92124
curl http://localhost:3000/pets \
93125
-H "Authorization: Bearer $TOKEN"
94126
```
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`.
96128

97129
Your new application can now only access a specific subset of rows based on its permissions using transaction-scoped settings.
98130

serverless/sql-databases/how-to/use-row-level-security.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This requires setting up different [IAM permissions sets](/identity-and-access-m
2525
- [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
2626
- [Created a Serverless SQL Database](/serverless/sql-databases/how-to/create-a-database/)
2727

28-
## Add sample data and create PostgreSQL Row Level Security
28+
## How to add sample data and enable PostgreSQL Row Level Security
2929

3030
1. [Connect to your Serverless SQL Database](/serverless/sql-databases/quickstart/#how-to-connect-to-a-database) with a PostgreSQL client such as `psql`:
3131
```bash
@@ -61,7 +61,7 @@ This requires setting up different [IAM permissions sets](/identity-and-access-m
6161
```
6262
</Message>
6363

64-
## Create an IAM application with Row Level Security enabled
64+
## How to create an IAM application with Row Level Security enabled
6565

6666
1. Create a new [IAM application](/identity-and-access-management/iam/how-to/create-application/).
6767

0 commit comments

Comments
 (0)