Skip to content

Commit 28f2462

Browse files
Clean Key Vault auth test app
1 parent 121e45a commit 28f2462

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

app.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,34 @@
55

66
app = Flask(__name__)
77

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

11-
# Set up Azure Key Vault client
11+
# Initialize Key Vault client with managed identity
1212
credential = DefaultAzureCredential()
1313
client = SecretClient(vault_url=KEY_VAULT_URL, credential=credential)
1414

1515
@app.route("/")
1616
def home():
1717
try:
18-
# Fetch secret inside the route
18+
# Get secret from Key Vault (executed at request-time, not startup)
1919
retrieved_secret = client.get_secret("app-auth-secret").value
20+
21+
# Get API key from incoming request
2022
api_key = request.headers.get('x-api-key')
23+
24+
# If API key is missing or incorrect, deny access
2125
if api_key != retrieved_secret:
22-
abort(403)
23-
return "✅ Authorized! You accessed a secure route."
26+
abort(403, "Invalid API key")
27+
28+
return "✅ Authorized! Access granted."
29+
2430
except Exception as e:
2531
return f"❌ Error: {str(e)}", 500
2632

2733
@app.route("/ping")
2834
def ping():
29-
return "App is alive!"
35+
return "App is running!"
3036

3137
if __name__ == "__main__":
3238
app.run(host='0.0.0.0', port=8000)
33-
@app.route("/ping")
34-
def ping():
35-
return "App is alive!"

0 commit comments

Comments
 (0)