eladkal commented on code in PR #52182:
URL: https://github.com/apache/airflow/pull/52182#discussion_r2253483689
##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/base_azure.py:
##########
@@ -111,22 +117,56 @@ 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
- 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
- )
- 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(
- managed_identity_client_id=managed_identity_client_id,
- workload_identity_tenant_id=workload_identity_tenant_id,
- )
+ 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")
+ use_azure_identity_object =
conn.extra_dejson.get("use_azure_identity_object", False)
+ credential: (
+ ServicePrincipalCredentials
+ | AzureIdentityCredentialAdapter
+ | ClientSecretCredential
+ | DefaultAzureCredential
+ )
+ if all([conn.login, conn.password, tenant]):
+ self.log.info("Getting credentials using specific credentials and
subscription_id.")
+ if use_azure_identity_object:
+ credential = ClientSecretCredential(
+ client_id=conn.login, # type: ignore[arg-type]
+ client_secret=conn.password, # type: ignore[arg-type]
+ tenant_id=tenant, # type: ignore[arg-type]
+ )
+ else:
+ credential = ServicePrincipalCredentials(
+ client_id=conn.login, secret=conn.password, tenant=tenant
+ )
+ 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")
+ if use_azure_identity_object:
+ credential = get_sync_default_azure_credential(
+ managed_identity_client_id=managed_identity_client_id,
+ workload_identity_tenant_id=workload_identity_tenant_id,
+ )
+ else:
+ credential = AzureIdentityCredentialAdapter(
+ managed_identity_client_id=managed_identity_client_id,
+ workload_identity_tenant_id=workload_identity_tenant_id,
+ )
+ return credential
Review Comment:
There are too many if/else here which makes it really hard to understand the
logic.
Can you check if there is a simpler way to write this (you can use private
until functions if needed)
--
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]