Skip to content

Commit f37274a

Browse files
committed
Update Health Endpoint Authentication
1 parent 59e1eca commit f37274a

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

code/function/fastapp/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

code/function/fastapp/health/validate_request.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@
1010

1111

1212
async 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

code/function/fastapp/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
)

0 commit comments

Comments
 (0)