Skip to content

Commit 8e8e9d1

Browse files
Moved Key Vault call inside route to fix startup crash
1 parent 141556a commit 8e8e9d1

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

app.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55

66
app = Flask(__name__)
77

8-
# Key Vault URL (replace with yours)
8+
# Key Vault URL
99
KEY_VAULT_URL = "https://yashkeyvaultverysafe.vault.azure.net/"
1010

1111
# Set up Azure Key Vault client
1212
credential = DefaultAzureCredential()
1313
client = SecretClient(vault_url=KEY_VAULT_URL, credential=credential)
1414

15-
# Get secret from Key Vault
16-
retrieved_secret = client.get_secret("app-auth-secret").value
17-
1815
@app.route("/")
1916
def home():
20-
# Check if 'x-api-key' header matches the secret
21-
api_key = request.headers.get('x-api-key')
22-
if api_key != retrieved_secret:
23-
abort(403) # Forbidden
24-
return "✅ Authorized! You accessed a secure route."
17+
try:
18+
# Fetch secret inside the route
19+
retrieved_secret = client.get_secret("app-auth-secret").value
20+
api_key = request.headers.get('x-api-key')
21+
if api_key != retrieved_secret:
22+
abort(403)
23+
return "✅ Authorized! You accessed a secure route."
24+
except Exception as e:
25+
return f"❌ Error: {str(e)}", 500
26+
27+
@app.route("/ping")
28+
def ping():
29+
return "App is alive!"
2530

2631
if __name__ == "__main__":
2732
app.run(host='0.0.0.0', port=8000)

0 commit comments

Comments
 (0)