azure - 通过基于证书的服务主体凭据访问 Azure 存储; azure.identity 与 azure.common.credentials

标签 azure azure-service-principal

我尝试过通过 Python SDK 使用服务主体凭据来访问 Azure Blob 存储,但遇到了一些困惑,我认为社区可以提供帮助。

#1 azure.common.credentialsazure.identity---------------- ----------------------------------

我注意到 Azure 中的两个不同的 python 包具有凭据类。

 - azure.common.credentials
 - azure.identity

两者之间有什么区别,什么时候应该使用其中一种来对抗另一种?更具体地说,当尝试使用 Azure 服务主体时,

  • **azure.identity** 提供了 **ClientSecretCredential 和 CertificateCredential**,因此我们可以使用共享 key 或 SSL 证书

  • **azure.common.credentials** 包仅提供需要共享 key **ServicePrincipalCredentials** 类>,并且没有对应的证书凭证。

我错过了什么吗?我希望使用基于证书的服务主体

#2 ServicePrincipalCredentials 有效,但 ClientSecretCredential 失败 -------------------------------- ----------------

我用于访问 Azure 存储的测试代码可以成功地使用 ServicePrincipalCredentials 类。 但 ClientSecretCredential 类失败,并显示异常消息:'ClientSecretCredential' 对象没有属性 'signed_session'"

感谢任何有助于理解原因的帮助。除了将凭证实例化为上述两个类之一之外,代码中没有任何区别。

上面的#2 问题很重要,主要是因为#1。我希望使用基于证书的身份验证,但在 azure.common.credentials 下找不到支持类。

Python 环境详细信息:

>python3 --version
Python 3.6.9

>pip3 freeze | grep -i azure
azure-common==1.1.25
azure-core==1.5.0
azure-identity==1.3.1
azure-mgmt-resource==9.0.0
azure-mgmt-storage==10.0.0
azure-storage-blob==12.3.1
msrestazure==0.6.3

我的代码片段:

# for credential classes
from azure.identity import ClientSecretCredential
from azure.identity import CertificateCredential

# for storage & other resource mgmt classes
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient

tenant_id = params['tenant-id']
client_id = params['client-id']
client_secret = params['secret']
subscription_id = params['subscription-id']

creds = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)

# create resource group
resource_client = ResourceManagementClient(creds, subscription_id)

# create storage group, access keys etc
storage_client = StorageManagementClient(creds, subscription_id)

当尝试使用证书而不是 secret 时,以下是用于创建凭证实例的代码片段;其余代码相同。

    client_keycert_path = params['cert-path']
    creds = CertificateCredential(tenant_id =tenant_id, client_id = client_id, certificate_path = client_keycert_path)

最佳答案

我承认,目前的情况具有误导性,这是今天的一些详细信息(在此处监控情况https://github.com/Azure/azure-sdk-for-python/issues/9310):

  • 对于 azure-storage-blobazure.common 用于存储 SDK <= 2.x,azure-identity 用于用于存储 SDK >= v12.x。
  • 对于任何以 azure-mgmt-xxx 开头的软件包,azure-common 仍然是官方方式。检查此问题以获取有关如何编写使用 azure-identity 的管理代码的解决方法 ( https://github.com/Azure/azure-sdk-for-python/issues/9310 )

这种情况很快就会改变,到 2020 年夏季,mgmt SDK 应该支持开箱即用的 azure-identity

希望这对您有所帮助,如果还有其他问题,也请随时在 Github 上提出问题: https://github.com/Azure/azure-sdk-for-python/issues

(我在 MS 的 Azure SDK 团队工作)

关于azure - 通过基于证书的服务主体凭据访问 Azure 存储; azure.identity 与 azure.common.credentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61944440/

相关文章:

azure - 从后台作业获取Token()

python - plotly graph_objects 可以与 Azure 数据资源管理器一起使用吗?

azure : Can not install 3rd party setup due to access denied on registry

azure - 如何使用 powershell 列出服务主体权限

asp.net - Azure启动任务中的aspnet_compiler

azure - 是否可以使用 Azure Devops API 访问组织范围的工件源?

azure - 是否可以使用单个服务主体来访问 Azure 中的多个资源?

azure - 从 Azure 管道任务访问 Microsoft.Azure.WebSites 资源提供程序(Microsoft Azure 应用服务)的服务主体

azure - 如何授予 Function App 访问 Azure 存储帐户的权限?

c# - 在 C# 中访问 Azure 中系统管理标识的应用程序 ID