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: README.md
+67-11Lines changed: 67 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,17 @@ Guidance on onboarding samples to docs.microsoft.com/samples: https://review.doc
22
22
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
23
23
-->
24
24
25
-
TDB
25
+
An API should allow its users to securely access the data in the database used by the API itself. At the same time it also must assure that data is protected and secured from those users who doesn't have enough authorization. This is even more important when creating multi-tenant applications.
26
+
27
+
Azure SQL offers an amazing feature to secure data at the database level, so that all the burden of taking care of such important and critical effort is done automatically by the database engine, so that the API code can be cleaner and easier to maintain and evolve. Not to mention better performances and improved efficiency as data will not leave the database at all, if the user has not the correct permissions.
28
+
29
+
This repo guides you to the creation of a API solution, deployable in Azure, that take advantage of Azure SQL Row Level Security to create secure API using Python, Flask and JWT. The same approach could be used with .NET or any other language that allows you to connect to Azure SQL.
30
+
31
+
A detailed video on how this sample work is available here:
32
+
33
+
https://youtu.be/Qpv8ke8ZuQ8
34
+
35
+
The sample simulate an authenticated user by passing in the JWT token (that you'll generate using the `pyjwt` tool) the hashed User Id. From a security point of view you want to make sure that a user can access only to his own data (or to the data s/he has been authorized to).
26
36
27
37
## Install Sample Database
28
38
@@ -36,10 +46,6 @@ Otherwise you can restore the `rls_sample` database by using the
36
46
37
47
[How To Restore Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)
38
48
39
-
## Enabled Row Level Security
40
-
41
-
TDB
42
-
43
49
If you need any help in executing the SQL script, you can find a Quickstart here: [Quickstart: Use Azure Data Studio to connect and query Azure SQL database](https://docs.microsoft.com/en-us/sql/azure-data-studio/quickstart-sql-database)
@@ -98,22 +104,72 @@ Python will start the HTTP server and when everything is up and running you'll s
98
104
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
99
105
```
100
106
101
-
Using a REST Client (like [Insomnia](https://insomnia.rest/), [Postman](https://www.getpostman.com/) or curl), you can now call your API, for example:
107
+
Using a REST Client (like [Insomnia](https://insomnia.rest/), [Postman](https://www.getpostman.com/) or curl), you can now call your API. The API requires a Bearer Token that contains the Hashed User Id of the user you want to simulate:
108
+
109
+
|User|Hashed Id|
110
+
|---|---|
111
+
|Jane Dean|6134311589|
112
+
|John Doe|1225328053|
113
+
114
+
the definition of who can see what is stored in the `rls.SensitiveDataPermissions` table.
115
+
116
+
To generate the Bearer Token you can use the `pyjwt` that is automatically installed by the `pyjwt` python package. Use the key `mySUPERs3cr3t` to sign the JWT message.
(Invoke-WebRequest -Uri http://localhost:5000/sensitive-data/more -Method GET -Headers @{"Authorization"="Bearer $token"}).Content
130
+
```
131
+
108
132
and you'll get info on Customer 123:
109
133
110
134
```json
111
-
TDB
135
+
[
136
+
{
137
+
"Id": 1,
138
+
"FirstName": "Jane",
139
+
"LastName": "Dean",
140
+
"EvenMore": [...]
141
+
},
142
+
{
143
+
"Id": 2,
144
+
"FirstName": "John",
145
+
"LastName": "Doe",
146
+
"EvenMore": [...]
147
+
}
148
+
]
149
+
```
150
+
151
+
As you can see, data for both users is returned, even if you are invoking the API using a specific User. This is because the Row Level Security feature is *disabled*.
152
+
153
+
## Enable Row Level Security
154
+
155
+
To enable to Row Level Security Policy execute the following code in the sample database:
156
+
157
+
```sql
158
+
alter security policy rls.SensitiveDataPolicy with (state =on)
112
159
```
113
160
114
-
Check out more samples to test all implemented verbs here:
161
+
If you try to access the same API again, you'll now see only the data for the user you are simulating:
0 commit comments