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 5
5
6
6
app = Flask (__name__ )
7
7
8
- # Key Vault URL
8
+ # Your Key Vault URL (replace with yours)
9
9
KEY_VAULT_URL = "https://yashkeyvaultverysafe.vault.azure.net/"
10
10
11
- # Set up Azure Key Vault client
11
+ # Initialize Key Vault client with managed identity
12
12
credential = DefaultAzureCredential ()
13
13
client = SecretClient (vault_url = KEY_VAULT_URL , credential = credential )
14
14
15
15
@app .route ("/" )
16
16
def home ():
17
17
try :
18
- # Fetch secret inside the route
18
+ # Get secret from Key Vault (executed at request-time, not startup)
19
19
retrieved_secret = client .get_secret ("app-auth-secret" ).value
20
+
21
+ # Get API key from incoming request
20
22
api_key = request .headers .get ('x-api-key' )
23
+
24
+ # If API key is missing or incorrect, deny access
21
25
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
+
24
30
except Exception as e :
25
31
return f"❌ Error: { str (e )} " , 500
26
32
27
33
@app .route ("/ping" )
28
34
def ping ():
29
- return "App is alive !"
35
+ return "✅ App is running !"
30
36
31
37
if __name__ == "__main__" :
32
38
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