Programmer-RD-AI commented on code in PR #56228:
URL: https://github.com/apache/airflow/pull/56228#discussion_r2392140917


##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/base_azure.py:
##########
@@ -111,22 +119,74 @@ def get_conn(self) -> Any:
             self.log.info("Getting connection using a JSON config.")
             return get_client_from_json_dict(client_class=self.sdk_client, 
config_dict=key_json)
 
-        credentials: ServicePrincipalCredentials | 
AzureIdentityCredentialAdapter
+        credentials = self.get_credential(conn=conn)
+
+        return self.sdk_client(
+            credentials=credentials,
+            subscription_id=subscription_id,
+        )
+
+    def get_credential(self, *, conn: Connection | None = None) -> Any:
+        """
+        Get Azure credential object for the connection.
+
+        Azure Identity based credential object (``ClientSecretCredential``, 
``DefaultAzureCredential``) can be used to get OAuth token using ``get_token`` 
method.
+        Older Credential objects (``ServicePrincipalCredentials``, 
``AzureIdentityCredentialAdapter``) are supported for backward compatibility.
+
+        :return: The Azure credential object
+        """
+        if not conn:
+            conn = self.get_connection(self.conn_id)
+        tenant = conn.extra_dejson.get("tenantId")
+        credential: (
+            ServicePrincipalCredentials
+            | AzureIdentityCredentialAdapter
+            | ClientSecretCredential
+            | DefaultAzureCredential
+        )
         if all([conn.login, conn.password, tenant]):
-            self.log.info("Getting connection using specific credentials and 
subscription_id.")
-            credentials = ServicePrincipalCredentials(
-                client_id=conn.login, secret=conn.password, tenant=tenant
-            )
+            credential = self._get_client_secret_credential(conn)
         else:
-            self.log.info("Using DefaultAzureCredential as credential")
-            managed_identity_client_id = 
conn.extra_dejson.get("managed_identity_client_id")
-            workload_identity_tenant_id = 
conn.extra_dejson.get("workload_identity_tenant_id")
-            credentials = AzureIdentityCredentialAdapter(
+            credential = self._get_default_azure_credential(conn)
+        return credential
+
+    def _get_client_secret_credential(self, conn: Connection):
+        self.log.info("Getting credentials using specific credentials and 
subscription_id.")
+        extra_dejson = conn.extra_dejson
+        tenant = extra_dejson.get("tenantId")
+        use_azure_identity_object = 
extra_dejson.get("use_azure_identity_object", False)
+        if use_azure_identity_object:
+            return ClientSecretCredential(
+                client_id=conn.login,  # type: ignore[arg-type]
+                client_secret=conn.password,  # type: ignore[arg-type]
+                tenant_id=tenant,  # type: ignore[arg-type]
+            )
+        return ServicePrincipalCredentials(client_id=conn.login, 
secret=conn.password, tenant=tenant)
+
+    def _get_default_azure_credential(self, conn: Connection):
+        self.log.info("Using DefaultAzureCredential as credential")
+        extra_dejson = conn.extra_dejson
+        managed_identity_client_id = 
extra_dejson.get("managed_identity_client_id")
+        workload_identity_tenant_id = 
extra_dejson.get("workload_identity_tenant_id")
+        use_azure_identity_object = 
extra_dejson.get("use_azure_identity_object", False)
+        if use_azure_identity_object:
+            return get_sync_default_azure_credential(
                 managed_identity_client_id=managed_identity_client_id,
                 workload_identity_tenant_id=workload_identity_tenant_id,
             )
-
-        return self.sdk_client(
-            credentials=credentials,
-            subscription_id=subscription_id,
+        return AzureIdentityCredentialAdapter(
+            managed_identity_client_id=managed_identity_client_id,
+            workload_identity_tenant_id=workload_identity_tenant_id,
         )
+
+    def get_token(self, *scopes, **kwargs) -> AccessToken:
+        """Request an access token for `scopes`."""
+        credential = self.get_credential()
+        if isinstance(credential, AzureIdentityCredentialAdapter) or 
isinstance(
+            credential, AzureIdentityCredentialAdapter
+        ):
+            raise AttributeError(
+                "The azure credential does not support get_token method. "
+                "Please set `use_azure_identity_object: True` in the 
connection extra field to use credential that support get_token method."
+            )
+        return credential.get_token(*scopes, **kwargs)

Review Comment:
   *i mean in the docstrings



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to