Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Delete the sample in v4 and update the readme (#7)
Browse files Browse the repository at this point in the history
* Delete the sample in v4 and update the readme

* update

* update Readme

* Update by comments

* Update Readme
  • Loading branch information
v-xuto authored Sep 30, 2020
1 parent 3fd8a85 commit fe24302
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 211 deletions.
File renamed without changes.
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ urlFragment: get-set-keyvault-secrets-managed-id-python

# How to set and get secrets from Azure Key Vault with Azure Managed Identities and Python

## SDK Versions
In this sample, you will find the following folders:
* **v3** - references Key Vault SDK v3
* **v4** - references Key Vault SDK v4
## This sample shows how to do the following operations of Key Vault secret with Key Vault SDK
- Get Key Vault MSIAuthentication or ServicePrincipalCredentials
- Create a Key Vault client
- Get an existing secret

## Use latest Key Vault SDK
The Key Vault SDK package version in this repo is **0.3.x**. It's strongly recommended that you use the [latest](https://pypi.org/project/azure-keyvault-secrets/) version of the Key Vault secret SDK package, please refer to the following examples:

* [helloworld.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/keyvault/azure-keyvault-secrets/samples/hello_world.py) - Examples for common Key Vault secret tasks:

* Get DefaultAzureCredential
* Create a secret client
* Create a new secret
* Get an existing secret
* Update an existing secret
* Delete a secret

## Background
For service to service authentication, the approach involved creating an Azure AD application and associated credential, and using that credential to get a token. While this approach works well, there are two shortcomings:
Expand All @@ -31,14 +43,14 @@ To run and deploy this sample, you need the following:
2. [Azure CLI 2.0] to run the application on your local development machine.

### Step 1: Create an App Service with an Azure Managed Identity
<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure-Samples%2Fapp-service-msi-keyvault-python%2Fmaster%2Fazuredeploy.json" target="_blank">
<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure-Samples%2Fazure-sdk-for-python-keyvault-secrets-get-set-managedid%2Fmaster%2Fazuredeploy.json" target="_blank">
<img src="http://azuredeploy.net/deploybutton.png"/>
</a>

Use the "Deploy to Azure" button to deploy an ARM template to create the following resources:
1. App Service with [Azure Managed Identities].
2. Key Vault with a secret, and an access policy that grants the App Service access to **Get Secrets**.
>Note: When filling out the template you will see a textbox labeled 'Key Vault Secret'. Enter a secret value there. A secret with the name 'secret' and value from what you entered will be created in the Key Vault.
>Note: When preparing the deployment, there will be a few required fields to fill out (subscription, resource group, region, website name, Key Vault name, and secret value). The secret value will be the value of the secret named "secret" created in the Key Vault upon deployment.
Review the resources created using the Azure portal. You should see an App Service and a Key Vault. View the access policies of the Key Vault to see that the App Service has access to it.

Expand All @@ -53,7 +65,7 @@ Using the Azure Portal, go to the Key Vault's access policies, and grant yoursel

1. Search for your Key Vault in “Search Resources dialog box” in Azure Portal.
2. Select "Overview", and click on Access policies
3. Click on "Add New", select "Secret Management" from the dropdown for "Configure from template"
3. Click on "Add Access Policy", select "Secret Management" from the dropdown for "Configure from template"
4. Click on "Select Principal", add your account
5. Save the Access Policies

Expand Down Expand Up @@ -83,25 +95,20 @@ You can also create an Azure service principal either through

```
git clone https://github.com/Azure-Samples/azure-sdk-for-python-keyvault-secrets-get-set-managedid.git
cd azure-sdk-for-python-keyvault-secrets-get-set-managedid
```

4. Run the following command to install dependencies for "SDK version 3" and "SDK version 4":

- SDK version 4

```
cd v4
pip install -r requirements.txt
```
4. Run the following command to install dependencies:

- SDK version 3

```
cd v3
pip install -r requirements.txt
```
```
pip install -r requirements.txt
```

5. Set up the environment variable `KEY_VAULT_URL` with your KeyVault URL or replace the variable in the example file.
5. Set up the environment variable `KEY_VAULT_URI` with your KeyVault URI or replace the variable in the example file.
```
SET KEY_VAULT_URI=https://{your vault name}.vault.azure.net/ # setting environment variable in Windows command prompt
```

6. Export these environment variables into your current shell or update the credentials in the example file.

Expand All @@ -124,7 +131,7 @@ pip install -r requirements.txt

## Deploying on Azure Web App

1. Set the `KEY_VAULT_URL` environment variable using the "Application Settings" of your Web App.
1. Set the `KEY_VAULT_URI` environment variable using the "Application Settings" of your Web App.

1. Connect to the [Kudu console] and install the dependencies. If you installed the Python 3.6.2x86 extension, the command line will be:

Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions v3/example.py → example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file uses an outdated library. Please see the readme to find the latest version.
from msrestazure.azure_active_directory import MSIAuthentication, ServicePrincipalCredentials
from azure.keyvault import KeyVaultClient
import os
Expand All @@ -6,6 +7,7 @@
app = Flask(__name__)


# Deprecated Libraries
def get_key_vault_credentials():
"""This tries to get a token using MSI, or fallback to SP env variables.
"""
Expand All @@ -28,13 +30,16 @@ def run_example():
# Get credentials
credentials = get_key_vault_credentials()

# Deprecated Libraries
# Create a KeyVault client
key_vault_client = KeyVaultClient(
credentials
)

# Deprecated Libraries
key_vault_uri = os.environ.get("KEY_VAULT_URI")

# Deprecated Libraries
secret = key_vault_client.get_secret(
key_vault_uri, # Your KeyVault URL
"secret", # Name of your secret. If you followed the README 'secret' should exists
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
azure-keyvault==0.3.6
flask
2 changes: 0 additions & 2 deletions v3/requirements.txt

This file was deleted.

Empty file removed v4/.skipPythonDeployment
Empty file.
128 changes: 0 additions & 128 deletions v4/azuredeploy.json

This file was deleted.

43 changes: 0 additions & 43 deletions v4/example.py

This file was deleted.

3 changes: 0 additions & 3 deletions v4/requirements.txt

This file was deleted.

13 changes: 0 additions & 13 deletions v4/web.config

This file was deleted.

File renamed without changes.

0 comments on commit fe24302

Please sign in to comment.