Skip to content

Commit e563366

Browse files
committed
added irsa support to prometrix
1 parent ef75402 commit e563366

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

prometrix/connect/aws_connect.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Any, Dict, Optional, List
33

44
import requests
5+
import boto3
56
from botocore.auth import S3SigV4Auth
67
from botocore.awsrequest import AWSRequest
78
from botocore.credentials import Credentials
@@ -12,19 +13,44 @@
1213

1314
class AWSPrometheusConnect(CustomPrometheusConnect):
1415
def __init__(
15-
self, access_key: str, secret_key: str, region: str, service_name: str, token: Optional[str] = None, **kwargs
16+
self,
17+
access_key: str,
18+
secret_key: str,
19+
region: str,
20+
service_name: str,
21+
token: Optional[str] = None,
22+
**kwargs,
1623
):
24+
"""
25+
AMP connector using S3SigV4Auth.
26+
Supports both static AWS keys (for backwards compatibility) and IRSA.
27+
"""
1728
super().__init__(**kwargs)
18-
self._credentials = Credentials(access_key, secret_key, token)
19-
self._sigv4auth = S3SigV4Auth(self._credentials, service_name, region)
29+
self.region = region
30+
self.service_name = service_name
31+
32+
if access_key and secret_key:
33+
# Backwards compatibility: use static keys
34+
self._credentials = Credentials(access_key, secret_key, token)
35+
else:
36+
# Preferred: IRSA or any AWS provider chain
37+
session = boto3.Session()
38+
creds = session.get_credentials()
39+
if not creds:
40+
raise RuntimeError("No AWS credentials found (neither static keys nor IRSA)")
41+
self._credentials = creds
42+
43+
def _build_auth(self) -> S3SigV4Auth:
44+
"""Builds fresh SigV4 auth with current credentials (handles rotation)."""
45+
frozen = self._credentials.get_frozen_credentials()
46+
return S3SigV4Auth(frozen, self.service_name, self.region)
2047

2148
def signed_request(
2249
self, method, url, data=None, params=None, verify=False, headers=None
2350
):
24-
request = AWSRequest(
25-
method=method, url=url, data=data, params=params, headers=headers
26-
)
27-
self._sigv4auth.add_auth(request)
51+
request = AWSRequest(method=method, url=url, data=data, params=params, headers=headers)
52+
auth = self._build_auth()
53+
auth.add_auth(request)
2854
return requests.request(
2955
method=method,
3056
url=url,

prometrix/models/prometheus_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class PrometheusConfig(BaseModel):
3030

3131

3232
class AWSPrometheusConfig(PrometheusConfig):
33-
access_key: str
34-
secret_access_key: str
33+
access_key: Optional[str] = None
34+
secret_access_key: Optional[str] = None
3535
token: Optional[str] = None
3636
service_name: str = "aps"
3737
aws_region: str

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[tool.poetry]
22
name = "prometrix"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["Avi Kotlicky <avi@robusta.dev>"]
55
readme = "README.md"
66
packages = [{include = "prometrix"}]
77
description = "A Python Prometheus client for all Prometheus instances."
88

9-
[project.urls]
9+
[tool.poetry.urls]
1010
"Homepage" = "https://github.com/robusta-dev/prometrix"
1111
"Bug Tracker" = "https://github.com/robusta-dev/prometrix/issues"
1212

0 commit comments

Comments
 (0)