Skip to content

Commit 4625652

Browse files
committed
Add support for Azure Workload Identity
1 parent e8bf6c3 commit 4625652

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

prometrix/auth.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def azure_authorization(cls, config: PrometheusConfig) -> bool:
1616
if not isinstance(config, AzurePrometheusConfig):
1717
return False
1818
return (config.azure_client_id != "" and config.azure_tenant_id != "") and (
19-
config.azure_client_secret != "" or config.azure_use_managed_id != ""
19+
config.azure_client_secret != "" or # Service Principal Auth
20+
config.azure_use_managed_id != "" or # Managed Identity Auth
21+
config.azure_use_workload_id != "" # Workload Identity Auth
2022
)
2123

2224
@classmethod
@@ -48,15 +50,33 @@ def _get_azure_metadata_endpoint(cls, config: PrometheusConfig):
4850
@no_type_check
4951
@classmethod
5052
def _post_azure_token_endpoint(cls, config: PrometheusConfig):
51-
return requests.post(
52-
url=config.azure_token_endpoint,
53-
headers={"Content-Type": "application/x-www-form-urlencoded"},
54-
data={
53+
# Try Azure Workload Identity
54+
with open("/var/run/secrets/azure/tokens/azure-identity-token", "r") as token_file:
55+
token = token_file.read()
56+
data = {
57+
"grant_type": "client_credentials",
58+
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
59+
"client_assertion": token,
60+
"client_id": config.azure_client_id,
61+
"scope": f"{config.azure_resource}/.default",
62+
}
63+
# Fallback to Azure Service Principal
64+
if not token:
65+
if config.azure_use_workload_id:
66+
return {
67+
"ok": False,
68+
"reason": f"Could not open token file from {token_file}",
69+
}
70+
data = {
5571
"grant_type": "client_credentials",
5672
"client_id": config.azure_client_id,
5773
"client_secret": config.azure_client_secret,
5874
"resource": config.azure_resource,
59-
},
75+
}
76+
return requests.post(
77+
url=config.azure_token_endpoint,
78+
headers={"Content-Type": "application/x-www-form-urlencoded"},
79+
data=data,
6080
)
6181

6282
@classmethod
@@ -67,7 +87,7 @@ def request_new_token(cls, config: PrometheusConfig) -> bool:
6787
try:
6888
if config.azure_use_managed_id:
6989
res = cls._get_azure_metadata_endpoint(config)
70-
else:
90+
else: # Service Principal and Workload Identity
7191
res = cls._post_azure_token_endpoint(config)
7292
except Exception:
7393
logging.exception(

prometrix/models/prometheus_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class AzurePrometheusConfig(PrometheusConfig):
6464
azure_metadata_endpoint: str
6565
azure_token_endpoint: str
6666
azure_use_managed_id: Optional[str]
67+
azure_use_workload_id: Optional[str]
6768
azure_client_id: Optional[str]
6869
azure_tenant_id: Optional[str]
6970
azure_client_secret: Optional[str]

0 commit comments

Comments
 (0)