forked from mattfeltonma/python-sample-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (77 loc) · 2.71 KB
/
app.py
File metadata and controls
88 lines (77 loc) · 2.71 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import json
import os
import requests
from sys import exc_info
from flask import Flask, render_template, request, redirect
from opencensus.ext.azure.log_exporter import AzureLogHandler
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
# Setup logging mechanism
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler(
connection_string=os.getenv('APPLICATIONINSIGHTS_CONNECTION_STRING'))
)
# Setup up a Flask instance
app = Flask(__name__)
# Obtain time from public api
def query_time():
try:
response = requests.get(
url="http://worldtimeapi.org/api/timezone/america/new_york",
timeout=5
)
if response.status_code == 200:
time = (json.loads(response.text))['datetime']
logger.info('Successfully queried public API')
return time
elif response.status_code != 200:
logger.error(
f"Error querying API. Status code: {response.status_code}")
return "Unavailable"
except Exception:
logger.error('Failed to contact public api', exc_info=True)
return "Unavailable"
# Get Key Vault secret
def get_secret():
VAULT_NAME = os.getenv('KEY_VAULT_NAME')
KEY_VAULT_SECRET_NAME = os.getenv('KEY_VAULT_SECRET_NAME')
try:
if 'MSI_CLIENT_ID':
credential = DefaultAzureCredential(
managed_identity_client_id=os.getenv('MSI_CLIENT_ID')
)
else:
raise Exception
except Exception:
logger.error('Failed to obtain access token', exc_info=True)
raise Exception(
'Failed to obtain access token'
)
try:
secret_client = SecretClient(
vault_url=f"https://{VAULT_NAME}.vault.azure.net/", credential=credential)
secret = secret_client.get_secret(f"{KEY_VAULT_SECRET_NAME}")
return secret.value
except Exception:
logger.error('Failed to get secret', exc_info=True)
raise Exception(
'Failed to get secret'
)
def get_ip(web_request):
if 'X-Forwarded-For' in web_request.headers:
xforwardfor = web_request.headers['X-Forwarded-For']
return web_request.remote_addr + f" and X-Forwarded-For header value of {xforwardfor}"
else:
return web_request.remote_addr
# Render the template
@app.route("/")
def index():
ipinfo = get_ip(web_request=request)
wordoftheday = get_secret()
todaystime = query_time()
return render_template('index.html', wordoftheday=wordoftheday, time=todaystime, ip=ipinfo)