File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 55
66app = Flask (__name__ )
77
8- # Key Vault URL
8+ # Your Key Vault URL (replace with yours)
99KEY_VAULT_URL = "https://yashkeyvaultverysafe.vault.azure.net/"
1010
11- # Set up Azure Key Vault client
11+ # Initialize Key Vault client with managed identity
1212credential = DefaultAzureCredential ()
1313client = SecretClient (vault_url = KEY_VAULT_URL , credential = credential )
1414
1515@app .route ("/" )
1616def 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" )
2834def ping ():
29- return "App is alive !"
35+ return "✅ App is running !"
3036
3137if __name__ == "__main__" :
3238 app .run (host = '0.0.0.0' , port = 8000 )
33- @app .route ("/ping" )
34- def ping ():
35- return "App is alive!"
You can’t perform that action at this time.
0 commit comments