Add 'verify' parameter to construct_grafana_url function (#156)
taylor_socfortress committed
Feb 14, 2024 at 18:08 UTC
fec4a30f1715d184f97d902e3884e6fad6b09e27
1 file changed
+13
-5
backend/app/connectors/grafana/utils/universal.py
+13
-5
@@ -14,6 +14,7 @@ async def construct_grafana_url(
14
connector_url: str,
15
username: str,
16
password: str,
17
+ verify: bool = False
18
) -> str:
19
"""
20
Constructs a Grafana URL with embedded credentials.
@@ -22,17 +23,23 @@ async def construct_grafana_url(
23
connector_url (str): The base URL of the Grafana instance.
24
username (str): Username for Grafana authentication.
25
password (str): Password for Grafana authentication.
26
+ verify (bool, optional): Whether to verify SSL certificates. Defaults to True.
27
28
Returns:
29
str: The complete Grafana URL with credentials.
30
"""
31
if "http://" in connector_url:
30
- return connector_url.replace("http://", f"http://{username}:{password}@")
32
+ url = connector_url.replace("http://", f"http://{username}:{password}@")
33
elif "https://" in connector_url:
32
- return connector_url.replace("https://", f"https://{username}:{password}@")
34
+ url = connector_url.replace("https://", f"https://{username}:{password}@")
35
else:
36
raise ValueError("Invalid connector URL format")
37
38
+ # Add the verify parameter to the URL
39
+ url += "?verify=" + str(verify).lower()
40
+
41
+ return url
42
+
43
44
async def verify_grafana_credentials(attributes: Dict[str, Any]) -> Dict[str, Any]:
45
"""
@@ -46,9 +53,9 @@ async def verify_grafana_credentials(attributes: Dict[str, Any]) -> Dict[str, An
53
username = attributes["connector_username"]
54
password = attributes["connector_password"]
55
49
- grafana_url = await construct_grafana_url(connector_url, username, password)
56
+ grafana_url = await construct_grafana_url(connector_url, username, password, verify=False)
57
51
- grafana_client = GrafanaApi.from_url(grafana_url, verify=False)
58
+ grafana_client = GrafanaApi.from_url(grafana_url)
59
try:
60
create_org = grafana_client.organization.create_organization(
61
organization={
@@ -119,8 +126,9 @@ async def create_grafana_client(connector_name: str) -> GrafanaApi:
126
attributes["connector_url"],
127
attributes["connector_username"],
128
attributes["connector_password"],
129
+ verify=False,
130
)
123
- return GrafanaApi.from_url(grafana_url, verify=False)
131
+ return GrafanaApi.from_url(grafana_url)
132
except Exception as e:
133
raise HTTPException(
134
status_code=500,