Skip to content

Commit 9ef28a4

Browse files
authored
fix: api key extraction in single tenant mode and dynatrace fixes (#587)
1 parent 3f81fbf commit 9ef28a4

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

keep/api/core/dependencies.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,27 @@ def get_user_email(request: Request) -> str | None:
4646
)
4747

4848

49-
def verify_api_key(
50-
request: Request,
51-
api_key: str = Security(auth_header),
52-
authorization: HTTPAuthorizationCredentials = Security(http_basic),
49+
def __extract_api_key(
50+
request: Request, api_key: str, authorization: HTTPAuthorizationCredentials
5351
) -> str:
5452
"""
55-
Verifies that a customer is allowed to access the API.
53+
Extracts the API key from the request.
54+
API key can be passed in the following ways:
55+
1. X-API-KEY header
56+
2. api_key query param
57+
3. Basic auth header
58+
4. Digest auth header
5659
5760
Args:
58-
api_key (str, optional): The API key extracted from X-API-KEY header. Defaults to Security(auth_header).
61+
request (Request): FastAPI request object
62+
api_key (str): The API key extracted from X-API-KEY header
63+
authorization (HTTPAuthorizationCredentials): The credentials extracted from the Authorization header
5964
6065
Raises:
6166
HTTPException: 401 if the user is unauthorized.
6267
6368
Returns:
64-
str: The tenant id.
69+
str: api key
6570
"""
6671
api_key = api_key or request.query_params.get("api_key", None)
6772
if not api_key:
@@ -97,6 +102,27 @@ def verify_api_key(
97102
api_key = credentials
98103
else:
99104
raise HTTPException(status_code=401, detail="Missing API Key")
105+
return api_key
106+
107+
108+
def verify_api_key(
109+
request: Request,
110+
api_key: str = Security(auth_header),
111+
authorization: HTTPAuthorizationCredentials = Security(http_basic),
112+
) -> str:
113+
"""
114+
Verifies that a customer is allowed to access the API.
115+
116+
Args:
117+
api_key (str, optional): The API key extracted from X-API-KEY header. Defaults to Security(auth_header).
118+
119+
Raises:
120+
HTTPException: 401 if the user is unauthorized.
121+
122+
Returns:
123+
str: The tenant id.
124+
"""
125+
api_key = __extract_api_key(request, api_key, authorization)
100126

101127
tenant_api_key = get_api_key(api_key)
102128
if not tenant_api_key:
@@ -185,6 +211,8 @@ def verify_api_key_single_tenant(
185211
):
186212
return SINGLE_TENANT_UUID
187213

214+
api_key = __extract_api_key(request, api_key, authorization)
215+
188216
tenant_api_key = get_api_key(api_key)
189217
if not tenant_api_key:
190218
raise HTTPException(status_code=401, detail="Invalid API Key")

keep/providers/dynatrace_provider/dynatrace_provider.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import base64
55
import dataclasses
66
import datetime
7+
import json
78
import logging
89
import os
910
import random
@@ -215,8 +216,8 @@ def format_alert(event: dict) -> AlertDto:
215216
severity=event.get("ProblemSeverity", None),
216217
lastReceived=datetime.datetime.now().isoformat(),
217218
fatigueMeter=random.randint(0, 100),
218-
description=event.get(
219-
"ImpactedEntities"
219+
description=json.dumps(
220+
event.get("ImpactedEntities", {})
220221
), # was asked by a user (should be configurable)
221222
source=["dynatrace"],
222223
impact=event.get("ProblemImpact"),
@@ -233,17 +234,17 @@ def format_alert(event: dict) -> AlertDto:
233234
)
234235
# else, problem from the problem API
235236
else:
236-
event.pop("problemId")
237+
_id = event.pop("problemId")
237238
name = event.pop("displayId")
238239
status = event.pop("status")
239240
severity = event.pop("severityLevel", None)
240241
description = event.pop("title")
241242
impact = event.pop("impactLevel")
242243
tags = event.pop("entityTags")
243244
impacted_entities = event.pop("impactedEntities", [])
244-
url = event.pop("ProblemURL")
245+
url = event.pop("ProblemURL", None)
245246
alert_dto = AlertDto(
246-
id=id,
247+
id=_id,
247248
name=name,
248249
status=status,
249250
severity=severity,
@@ -336,7 +337,7 @@ def setup_webhook(
336337
"notifyClosedProblems": True,
337338
"notifyEventMergesEnabled": True,
338339
# all the fields - https://docs.dynatrace.com/docs/observe-and-explore/notifications-and-alerting/problem-notifications/webhook-integration#example-json-with-placeholders
339-
"payload": '{\n"State":"{State}",\n"ProblemID":"{ProblemID}",\n"ProblemTitle":"{ProblemTitle}",\n"ImpactedEntities": {ImpactedEntities},\n "PID": "{PID}",\n "ProblemDetailsJSON": {ProblemDetailsJSON},\n "ProblemImpact" : "{ProblemImpact}",\n"ProblemSeverity": "{ProblemSeverity}",\n "ProblemURL": "{ProblemURL}",\n"State": "{State}",\n"Tags": "{Tags}"\n"ProblemDetails": "{ProblemDetailsText}"\n"NamesOfImpactedEntities": "{NamesOfImpactedEntities}"\n"ImpactedEntity": "{ImpactedEntity}"\n"ImpactedEntityNames": "{ImpactedEntityNames}"\n"ProblemDetailsJSONv2": {ProblemDetailsJSONv2}\n\n}',
340+
"payload": '{\n"State":"{State}",\n"ProblemID":"{ProblemID}",\n"ProblemTitle":"{ProblemTitle}",\n"ImpactedEntities": {ImpactedEntities},\n "PID": "{PID}",\n "ProblemDetailsJSON": {ProblemDetailsJSON},\n "ProblemImpact" : "{ProblemImpact}",\n"ProblemSeverity": "{ProblemSeverity}",\n "ProblemURL": "{ProblemURL}",\n"State": "{State}",\n"Tags": "{Tags}",\n"ProblemDetails": "{ProblemDetailsText}",\n"NamesOfImpactedEntities": "{NamesOfImpactedEntities}",\n"ImpactedEntity": "{ImpactedEntity}",\n"ImpactedEntityNames": "{ImpactedEntityNames}",\n"ProblemDetailsJSONv2": {ProblemDetailsJSONv2}\n}',
340341
},
341342
}
342343
actual_payload = [

0 commit comments

Comments
 (0)