Skip to content

Commit 9a436ec

Browse files
committed
updated sample and readme
1 parent 62a9026 commit 9a436ec

File tree

4 files changed

+140
-31
lines changed

4 files changed

+140
-31
lines changed

README.md

Lines changed: 135 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
---
22
page_type: sample
33
languages:
4-
- csharp
4+
- python
5+
- tsql
6+
- sql
57
products:
6-
- dotnet
7-
description: "Add 150 character max description"
8-
urlFragment: "update-this-to-unique-url-stub"
8+
- azure
9+
- vs-code
10+
- azure-sql-database
11+
description: "Creating API to securely access data using Row Level Security"
12+
urlFragment: "azure-sql-db-secure-data-access-api"
913
---
1014

11-
# Official Microsoft Sample
15+
# Creating API to securely access data using Azure SQL Row Level Security
1216

1317
<!--
1418
Guidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master
@@ -18,36 +22,142 @@ Guidance on onboarding samples to docs.microsoft.com/samples: https://review.doc
1822
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
1923
-->
2024

21-
Give a short description for your sample here. What does it do and why is it important?
25+
TDB
2226

23-
## Contents
27+
## Install Sample Database
2428

25-
Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.
29+
In order to run this sample, you need a Azure SQL database to use. If you already have one that can be used as a developer playground you can used that. Make sure create all the needed objects by executing the script:
2630

27-
| File/folder | Description |
28-
|-------------------|--------------------------------------------|
29-
| `src` | Sample source code. |
30-
| `.gitignore` | Define what to ignore at commit time. |
31-
| `CHANGELOG.md` | List of changes to the sample. |
32-
| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |
33-
| `README.md` | This README file. |
34-
| `LICENSE` | The license for the sample. |
31+
`./sql/00-SetupRLS.sql`
3532

36-
## Prerequisites
33+
Otherwise you can restore the `rls_sample` database by using the
3734

38-
Outline the required components and tools that a user might need to have on their machine in order to run the sample. This can be anything from frameworks, SDKs, OS versions or IDE releases.
35+
`./sql/rls_sample.bacpac`. If you already know how to restore a database, great!, go on and once restore is done move on to next section. Otherwise, or if you want some scripts to help, use the following link:
3936

40-
## Setup
37+
[How To Restore Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)
4138

42-
Explain how to prepare the sample once the user clones or downloads the repository. The section should outline every step necessary to install dependencies and set up any settings (for example, API keys and output folders).
39+
## Enabled Row Level Security
4340

44-
## Running the sample
41+
TDB
4542

46-
Outline step-by-step instructions to execute the sample and see its output. Include steps for executing the sample from the IDE, starting specific services in the Azure portal or anything related to the overall launch of the code.
43+
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)
4744

48-
## Key concepts
45+
## Run sample locally
4946

50-
Provide users with more context on the tools and services used in the sample. Explain some of the code that is being used and how services interact with each other.
47+
Make sure you have Python 3.7 installed on your machine. Clone this repo in a directory on our computer and then create a [virtual environment](https://www.youtube.com/watch?v=_eczHOiFMZA&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=34). For example:
48+
49+
```bash
50+
virtualenv venv --python C:\Python37\
51+
```
52+
53+
then activate the created virtual environment. For example, on Windows:
54+
55+
```powershell
56+
.\venv\Scripts\activate
57+
```
58+
59+
and then install all the required packages:
60+
61+
```bash
62+
pip install -r requirements
63+
```
64+
65+
The connections string is not saved in the python code for security reasons, so you need to assign it to an environment variable in order to run the sample successfully. You also want to enable [development environment](https://flask.palletsprojects.com/en/1.1.x/config/#environment-and-debug-features) for Flask:
66+
67+
Linux:
68+
69+
```bash
70+
export FLASK_ENV="development"
71+
export SQLAZURECONNSTR_WWIF="<your-connection-string>"
72+
```
73+
74+
Windows:
75+
76+
```powershell
77+
$Env:FLASK_ENV="development"
78+
$Env:SQLAZURECONNSTR_WWIF="<your-connection-string>"
79+
```
80+
81+
Your connection string is something like:
82+
83+
```
84+
DRIVER={ODBC Driver 17 for SQL Server};SERVER=<your-server-name>.database.windows.net;DATABASE=<your-database-name>;UID=MiddleTierUser;PWD=a987REALLY#$%TRONGpa44w0rd;
85+
```
86+
87+
Just replace `<your-server-name>` and `<your-database-name>` with the correct values for your environment.
88+
89+
To run and test the Python REST API local, just run
90+
91+
```bash
92+
flask run
93+
```
94+
95+
Python will start the HTTP server and when everything is up and running you'll see something like
96+
97+
```text
98+
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
99+
```
100+
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:
102+
103+
```bash
104+
export token=`pyjwt --key=mySUPERs3cr3t encode iss=me exp=+600 user-hash-id=1225328053`
105+
curl -s -H "Authorization: Bearer ${token}" -X GET http://localhost:5000/sensitive-data/more | jq .
106+
```
107+
108+
and you'll get info on Customer 123:
109+
110+
```json
111+
TDB
112+
```
113+
114+
Check out more samples to test all implemented verbs here:
115+
116+
[cUrl Samples](./sample-usage.md)
117+
118+
## Debug from Visual Studio Code
119+
120+
Debugging from Visual Studio Code is fully supported. Make sure you create an `.env` file the look like the following one (making sure you add your connection string)
121+
122+
```
123+
FLASK_ENV="development"
124+
SQLAZURECONNSTR_RLS=""
125+
```
126+
127+
and you'll be good to go.
128+
129+
## Deploy to Azure
130+
131+
Now that your REST API solution is ready, it's time to deploy it on Azure so that anyone can take advantage of it. A detailed article on how you can that that is here:
132+
133+
- [Deploying Python web apps to Azure App Services](https://medium.com/@GeekTrainer/deploying-python-web-apps-to-azure-app-services-413cc16d4d68)
134+
- [Quickstart: Create a Python app in Azure App Service on Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/quickstart-python?tabs=bash)
135+
136+
The only thing you have do in addition to what explained in the above articles is to add the connection string to the Azure Web App configuration. Using AZ CLI, for example:
137+
138+
```bash
139+
appName="azure-sql-db-secure-data-access-api"
140+
resourceGroup="my-resource-group"
141+
142+
az webapp config connection-string set \
143+
-g $resourceGroup \
144+
-n $appName \
145+
--settings RLS=$SQLAZURECONNSTR_RLS \
146+
--connection-string-type=SQLAzure
147+
```
148+
149+
Just make sure you correctly set `$appName` and `$resourceGroup` to match your environment and also that the variable `$SQLAZURECONNSTR_RLS` as also been set, as mentioned in section "Run sample locally". An example of a full script that deploy the REST API is available here: `azure-deploy.sh`.
150+
151+
Please note that connection string are accessible as environment variables from Python when running on Azure, *but they are prefixed* as documented here:
152+
153+
https://docs.microsoft.com/en-us/azure/app-service/configure-common#connection-strings
154+
155+
That's why the Python code in the sample look for `SQLAZURECONNSTR_RLS` but the Shell script write the `RLS` connection string name.
156+
157+
## Learn more
158+
159+
https://techcommunity.microsoft.com/t5/azure-sql-database/building-rest-api-with-python-flask-and-azure-sql/ba-p/1056637
160+
https://github.com/Azure-Samples/azure-sql-db-python-rest-api
51161

52162
## Contributing
53163

@@ -61,4 +171,4 @@ provided by the bot. You will only need to do this once across all repos using o
61171

62172
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
63173
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
64-
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
174+
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

azure-deploy.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ location="WestUS2"
1111
gitSource="https://github.com/azure-samples/azure-sql-db-secure-data-access-api"
1212

1313
# Make sure connection string variable is set
14-
15-
if [[ -z "${SQLAZURECONNSTR_WWIF:-}" ]]; then
14+
if [[ -z "${SQLAZURECONNSTR_RLS:-}" ]]; then
1615
echo "Plase export Azure SQL connection string:";
1716
echo "export SQLAZURECONNSTR_RLS\"your-connection-string-here\"";
1817
exit 1;

sql/00-SetupRLS.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ create clustered index ixc on rls.SensitiveDataPermissions (SensitiveDataId)
5858
go
5959

6060
insert into dbo.SensitiveData values
61-
(1, 'Davide', 'Mauri', '{"SuperPowers":"Fly"}'),
61+
(1, 'Jane', 'Dean', '{"SuperPowers":"Fly"}'),
6262
(2, 'John', 'Doe', '{"SuperPowers":"Laser Eyes"}')
6363
go
6464

@@ -71,8 +71,8 @@ insert into dbo.EvenMoreSensitiveData values
7171
go
7272

7373
insert into rls.SensitiveDataPermissions values
74-
(-6134311, 1, 1),
75-
(1225328053, 2, 1)
74+
(6134311589, 1, 1), -- Jane Dean
75+
(1225328053, 2, 1) -- Joen Doe
7676
go
7777

7878
create function rls.fn_securitypredicate(@SensitiveDataId int)
@@ -134,4 +134,4 @@ create security policy rls.SensitiveDataPolicy
134134
add filter predicate rls.fn_SecurityPredicate(Id) on dbo.SensitiveData,
135135
add filter predicate rls.fn_SecurityPredicate(SensitiveDataId) on dbo.EvenMoreSensitiveData
136136
with (state = off);
137-
137+
go

sql/rls_sample.bacpac

7.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)