This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
example.py
65 lines (50 loc) · 1.71 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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
from flask import Flask
app = Flask(__name__)
# Deprecated Libraries
def get_key_vault_credentials():
"""This tries to get a token using MSI, or fallback to SP env variables.
"""
if "APPSETTING_WEBSITE_SITE_NAME" in os.environ:
return MSIAuthentication(
resource='https://vault.azure.net'
)
else:
return ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID'],
resource='https://vault.azure.net'
)
def run_example():
"""MSI Authentication 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
"" # The version of the secret. Empty string for latest
)
return "My secret value is {}".format(secret.value)
@app.route('/')
def hello_world():
try:
return run_example()
except Exception as err:
return str(err)
@app.route('/ping')
def ping():
return "Hello world"
if __name__ == '__main__':
app.run()