@@ -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 (
0 commit comments