File tree Expand file tree Collapse file tree 3 files changed +13
-14
lines changed Expand file tree Collapse file tree 3 files changed +13
-14
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ class Settings(BaseSettings):
2121 WEBSITE_AUTH_ENCRYPTION_KEY : str = Field (
2222 default = "" , alias = "WEBSITE_AUTH_ENCRYPTION_KEY"
2323 )
24+ WEBSITE_OS_TYPE : str = Field (default = "test" , alias = "WEBSITE_OS_TYPE" )
2425 MY_SECRET_CONFIG : str = Field (default = "" , alias = "MY_SECRET_CONFIG" )
2526
2627
Original file line number Diff line number Diff line change 1010
1111
1212async def verify_health_auth_header (
13- x_ms_auth_internal_token : Annotated [str , Header ()] = ""
13+ x_ms_auth_internal_token : Annotated [str | None , Header ()] = None
1414) -> bool :
1515 """Returns true if SHA256 of header_value matches WEBSITE_AUTH_ENCRYPTION_KEY.
16+ This only works on Windows-based app services. Therefore, this feature is turned off for other OS types.
1617 Documentation: https://learn.microsoft.com/en-us/azure/app-service/monitor-instances-health-check?tabs=python#authentication-and-security
1718
1819 x_ms_auth_internal_token: Value of the x-ms-auth-internal-token header.
1920 RETURNS (bool): Specifies whether the header matches.
2021 """
21- logger .info (f"Header value: '{ x_ms_auth_internal_token } '" )
22- logger .info (f"Encryption key: '{ settings .WEBSITE_AUTH_ENCRYPTION_KEY } '" )
23- website_auth_encryption_key = settings .WEBSITE_AUTH_ENCRYPTION_KEY
24- hash = base64 .b64encode (
25- sha256 (website_auth_encryption_key .encode ('utf-8' )).digest ()
26- ).decode ('utf-8' )
27- # if hash != x_ms_auth_internal_token:
28- # raise HTTPException(
29- # status_code=400, detail="x-ms-auth-internal-token is invalid"
30- # )
31- # else:
32- # return True
22+ if settings .WEBSITE_OS_TYPE .lower () == "windows" :
23+ website_auth_encryption_key = settings .WEBSITE_AUTH_ENCRYPTION_KEY
24+ hash = base64 .b64encode (
25+ sha256 (website_auth_encryption_key .encode ("utf-8" )).digest ()
26+ ).decode ("utf-8" )
27+ if hash != x_ms_auth_internal_token :
28+ raise HTTPException (
29+ status_code = 400 , detail = "x-ms-auth-internal-token is invalid"
30+ )
3331 return True
Original file line number Diff line number Diff line change @@ -128,7 +128,7 @@ def setup_opentelemetry(app: FastAPI):
128128 # Create instrumenter
129129 FastAPIInstrumentor .instrument_app (
130130 app ,
131- excluded_urls = f".*.in.applicationinsights.azure.com/.*" ,
131+ excluded_urls = f".*.in.applicationinsights.azure.com/.*, { settings . API_V1_STR } /health/heartbeat " ,
132132 tracer_provider = tracer_provider ,
133133 meter_provider = meter_provider ,
134134 )
You can’t perform that action at this time.
0 commit comments