Skip to content

Commit 7fbeeae

Browse files
authored
Merge pull request #110 from PerfectThymeTech/marvinbuss/bugfix_header_auth
Bugfix Header Auth Health Endpoint
2 parents ba7a9c7 + 21fa5ac commit 7fbeeae

File tree

5 files changed

+50
-37
lines changed

5 files changed

+50
-37
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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@
44

55
from fastapi import Header, HTTPException
66
from fastapp.core.config import settings
7+
from fastapp.utils import setup_logging
8+
9+
logger = setup_logging(__name__)
710

811

912
async def verify_health_auth_header(
10-
x_ms_auth_internal_token: Annotated[str, Header()]
13+
x_ms_auth_internal_token: Annotated[str | None, Header()] = None
1114
) -> bool:
1215
"""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.
1317
Documentation: https://learn.microsoft.com/en-us/azure/app-service/monitor-instances-health-check?tabs=python#authentication-and-security
1418
1519
x_ms_auth_internal_token: Value of the x-ms-auth-internal-token header.
1620
RETURNS (bool): Specifies whether the header matches.
1721
"""
18-
website_auth_encryption_key = settings.WEBSITE_AUTH_ENCRYPTION_KEY
19-
hash = base64.b64encode(
20-
sha256(website_auth_encryption_key.encode("utf-8")).digest()
21-
).decode("utf-8")
22-
if hash != x_ms_auth_internal_token:
23-
raise HTTPException(
24-
status_code=400, detail="x-ms-auth-internal-token is invalid"
25-
)
26-
else:
27-
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+
)
31+
return True

code/infra/function.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ resource "azapi_resource" "function" {
121121
name = "WEBSITE_CONTENTOVERVNET"
122122
value = "1"
123123
},
124+
{
125+
name = "WEBSITE_OS_TYPE"
126+
value = azurerm_service_plan.service_plan.os_type
127+
},
124128
{
125129
name = "WEBSITE_RUN_FROM_PACKAGE"
126130
value = "0"

code/infra/logging.tf

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@ resource "azurerm_application_insights" "application_insights" {
1616
workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
1717
}
1818

19-
data "azurerm_monitor_diagnostic_categories" "diagnostic_categories_application_insights" {
20-
resource_id = azurerm_application_insights.application_insights.id
21-
}
22-
23-
resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting_application_insights" {
24-
name = "logAnalytics"
25-
target_resource_id = azurerm_application_insights.application_insights.id
26-
log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
27-
28-
dynamic "enabled_log" {
29-
iterator = entry
30-
for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_application_insights.log_category_groups
31-
content {
32-
category_group = entry.value
33-
}
34-
}
35-
36-
dynamic "metric" {
37-
iterator = entry
38-
for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_application_insights.metrics
39-
content {
40-
category = entry.value
41-
enabled = true
42-
}
43-
}
44-
}
19+
# data "azurerm_monitor_diagnostic_categories" "diagnostic_categories_application_insights" { # Disable to avoid duplicate logs in Application Insights
20+
# resource_id = azurerm_application_insights.application_insights.id
21+
# }
22+
23+
# resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting_application_insights" {
24+
# name = "logAnalytics"
25+
# target_resource_id = azurerm_application_insights.application_insights.id
26+
# log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
27+
28+
# dynamic "enabled_log" {
29+
# iterator = entry
30+
# for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_application_insights.log_category_groups
31+
# content {
32+
# category_group = entry.value
33+
# }
34+
# }
35+
36+
# dynamic "metric" {
37+
# iterator = entry
38+
# for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_application_insights.metrics
39+
# content {
40+
# category = entry.value
41+
# enabled = true
42+
# }
43+
# }
44+
# }
4545

4646
resource "azurerm_log_analytics_workspace" "log_analytics_workspace" {
4747
name = "${local.prefix}-log001"

code/infra/storage.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ resource "azurerm_storage_account" "storage" {
3434
default_action = "Deny"
3535
ip_rules = []
3636
virtual_network_subnet_ids = []
37+
private_link_access {
38+
endpoint_resource_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/providers/Microsoft.Security/datascanners/storageDataScanner"
39+
endpoint_tenant_id = data.azurerm_client_config.current.tenant_id
40+
}
3741
}
3842
nfsv3_enabled = false
3943
public_network_access_enabled = false

0 commit comments

Comments
 (0)