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
# Creating API to securely access data using Azure SQL Row Level Security
12
16
13
17
<!--
14
18
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
18
22
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
19
23
-->
20
24
21
-
Give a short description for your sample here. What does it do and why is it important?
25
+
TDB
22
26
23
-
## Contents
27
+
## Install Sample Database
24
28
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:
|`.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`
35
32
36
-
## Prerequisites
33
+
Otherwise you can restore the `rls_sample` database by using the
37
34
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:
39
36
40
-
## Setup
37
+
[How To Restore Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)
41
38
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
43
40
44
-
## Running the sample
41
+
TDB
45
42
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)
47
44
48
-
## Key concepts
45
+
## Run sample locally
49
46
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:
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:
0 commit comments