Skip to content

Commit b179e06

Browse files
committed
initial refactoring
initial changes working version aws test added eks managed prom support coralogix prom support and fix prom bug fixed bug added all aws config changes
1 parent 2d53179 commit b179e06

19 files changed

+780
-138
lines changed

poetry.lock

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ kubernetes = "^26.1.0"
3030
prometheus-api-client = "^0.5.3"
3131
numpy = "^1.24.2"
3232
alive-progress = "^3.1.2"
33+
botocore = "^1.31.10"
34+
boto3 = "^1.28.10"
3335

3436

3537
[tool.poetry.group.dev.dependencies]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .models import AWSPrometheusConfig, AzurePrometheusConfig, PrometheusConfig
2+
from .utils import CustomPrometheusConnect, AWSPrometheusConnect

robusta_krr/common/prometheus/auth.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import logging
2+
import os
3+
from typing import Dict
4+
5+
import requests
6+
7+
from ..prometheus.models import (
8+
AzurePrometheusConfig,
9+
CoralogixPrometheusConfig,
10+
PrometheusConfig,
11+
)
12+
13+
14+
class PrometheusAuthorization:
15+
bearer_token: str = ""
16+
azure_authorization: bool = (
17+
os.environ.get("AZURE_CLIENT_ID", "") != "" and os.environ.get("AZURE_TENANT_ID", "") != ""
18+
) and (os.environ.get("AZURE_CLIENT_SECRET", "") != "" or os.environ.get("AZURE_USE_MANAGED_ID", "") != "")
19+
20+
@classmethod
21+
def get_authorization_headers(cls, config: PrometheusConfig) -> Dict:
22+
if isinstance(config, CoralogixPrometheusConfig):
23+
return {"token": config.prometheus_token}
24+
elif config.prometheus_auth:
25+
return {"Authorization": config.prometheus_auth.get_secret_value()}
26+
elif cls.azure_authorization:
27+
return {"Authorization": (f"Bearer {cls.bearer_token}")}
28+
else:
29+
return {}
30+
31+
@classmethod
32+
def request_new_token(cls, config: PrometheusConfig) -> bool:
33+
if cls.azure_authorization and isinstance(config, AzurePrometheusConfig):
34+
try:
35+
if config.azure_use_managed_id:
36+
res = requests.get(
37+
url=config.azure_metadata_endpoint,
38+
headers={
39+
"Metadata": "true",
40+
},
41+
data={
42+
"api-version": "2018-02-01",
43+
"client_id": config.azure_client_id,
44+
"resource": config.azure_resource,
45+
},
46+
)
47+
else:
48+
res = requests.post(
49+
url=config.azure_token_endpoint,
50+
headers={"Content-Type": "application/x-www-form-urlencoded"},
51+
data={
52+
"grant_type": "client_credentials",
53+
"client_id": config.azure_client_id,
54+
"client_secret": config.azure_client_secret,
55+
"resource": config.azure_resource,
56+
},
57+
)
58+
except Exception:
59+
logging.exception("Unexpected error when trying to generate azure access token.")
60+
return False
61+
62+
if not res.ok:
63+
logging.error(f"Could not generate an azure access token. {res.reason}")
64+
return False
65+
66+
cls.bearer_token = res.json().get("access_token")
67+
logging.info("Generated new azure access token.")
68+
return True
69+
70+
return False

0 commit comments

Comments
 (0)