Skip to content

Commit de0722a

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

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

menu/navigation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3958,7 +3958,7 @@
39583958
"slug": "configure-autoscaling"
39593959
},
39603960
{
3961-
"label": "How to manage backups for Serverless SQL Databases",
3961+
"label": "Manage backups for Serverless SQL Databases",
39623962
"slug": "manage-backups"
39633963
},
39643964
{

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ categories:
1515

1616
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).
1717

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).
1919

2020
- A Scaleway account logged into the [console](https://console.scaleway.com)
2121
- [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
3939
ALTER TABLE pets ENABLE row level security;
4040
```
4141

42-
3. Run the command below to enable **Row Level Security**:
43-
```sql
44-
ALTER TABLE pets ENABLE row level security;
45-
```
46-
4742
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`:
4843
```sql
4944
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
5348
```sql
5449
SELECT * FROM pets;
5550
```
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`.
5752

5853
<Message type="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:
6055
```sql
6156
SELECT current_user;
6257
```
@@ -67,67 +62,68 @@ Due to connection pooling, Serverless SQL Database currently only support transa
6762
1. Install PostgREST by following the [official documentation](https://docs.postgrest.org/en/v12/tutorials/tut0.html#step-1-install-postgresql).
6863

6964
2. Create a `tutorial.conf` file with the following content:
70-
```
65+
66+
```json
7167
db-uri = "postgres://[user-or-application-id]:[api-secret-key]@[database-hostname]:5432/[database-name]?sslmode=require"
7268
db-schemas = "[your database schema]"
7369
jwt-secret = "[your jwt secret]"
7470
```
75-
where:
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**)
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**)
7774
- `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:
7976
```sh
8077
openssl rand -base64 32
8178
```
8279

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:
8481

8582
```bash
8683
postgrest tutorial.conf
8784
```
8885

8986
<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:
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:
9188
```bash
9289
curl http://localhost:3000/pets \
9390
-H "Authorization: Bearer $TOKEN"
9491
```
95-
where `$TOKEN` is your generated JWT.
96-
A pet list should display.
92+
A list of pets displays.
9793
</Message>
9894

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:
10096
```sql
10197
DROP POLICY pets_keeper ON pets;
10298
```
10399

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:
105101
```sql
106102
CREATE POLICY pets_keeper ON pets TO role_readwrite
107103
USING (keeper = current_setting('request.jwt.claims', true)::json->>'user_type');
108104
```
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.
110106

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:
112108
```json
113109
{
114110
"role": "role_readwrite",
115111
"user_type": "role_readwrite"
116112
}
117113
```
118114
<Message type="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.
120116
</Message>
121117

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:
123119
```bash
124120
curl http://localhost:3000/pets \
125121
-H "Authorization: Bearer $TOKEN"
126122
```
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.
128124

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

131127
<Message type="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.
133129
</Message>

0 commit comments

Comments
 (0)