首页 > 解决方案 > 通过基于证书的服务主体凭据访问 Azure 存储;azure.identity 与 azure.common.credentials

问题描述

我已经尝试尝试通过 Python SDK 使用服务主体凭据访问 Azure Blob 存储,并且有一些我认为社区可以提供帮助的困惑。

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

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

 - azure.common.credentials
 - azure.identity

两者有什么区别,什么时候应该用一个来对付另一个?更具体地说,当尝试使用 Azure 服务主体时,

我错过了什么吗?我正在寻找使用基于证书的服务主体

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

我用于访问 Azure 存储的测试代码与 ServicePrincipalCredentials类一起成功运行。但是使用带有异常消息的 ClientSecretCredential 类失败:'ClientSecretCredential' object has no attribute '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)

当尝试使用证书而不是秘密时,这里是创建凭证实例的代码片段;其余代码相同。

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

标签: azureazure-service-principal

解决方案


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

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

这将很快改变,到 2020 年夏季,mgmt SDK 应该支持azure-identity开箱即用。

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

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


推荐阅读