Skip to content

Commit a336890

Browse files
Key Vault-gated homepage (no API key header)
1 parent d8a166e commit a336890

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

app.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
from flask import Flask, request, abort
1+
from flask import Flask
22
from azure.identity import DefaultAzureCredential
33
from azure.keyvault.secrets import SecretClient
44
import os
55

66
app = Flask(__name__)
77

8-
# Your Key Vault URL (replace with yours)
98
KEY_VAULT_URL = "https://yashkeyvaultverysafe.vault.azure.net/"
10-
11-
# Initialize Key Vault client with managed identity
129
credential = DefaultAzureCredential()
1310
client = SecretClient(vault_url=KEY_VAULT_URL, credential=credential)
1411

1512
@app.route("/")
1613
def home():
1714
try:
18-
# Get secret from Key Vault (executed at request-time, not startup)
19-
retrieved_secret = client.get_secret("app-auth-secret").value
20-
21-
# Get API key from incoming request
22-
api_key = request.headers.get('x-api-key')
23-
24-
# If API key is missing or incorrect, deny access
25-
if api_key != retrieved_secret:
26-
abort(403, "Invalid API key")
15+
# Try to access a secret from Key Vault
16+
secret_value = client.get_secret("app-auth-secret").value
2717

28-
return "✅ Authorized! Access granted."
18+
return f"✅ Welcome! Secret loaded from Key Vault: {secret_value}"
2919

3020
except Exception as e:
31-
return f"❌ Error: {str(e)}", 500
21+
return f"❌ Access denied or error reading from Key Vault:<br><br>{str(e)}", 500
3222

3323
@app.route("/ping")
3424
def ping():

0 commit comments

Comments
 (0)