【问题标题】:Accessing Azure Storage via certificate based service principal credentials; azure.identity vs azure.common.credentials通过基于证书的服务主体凭据访问 Azure 存储; azure.identity 与 azure.common.credentials
【发布时间】:2020-06-04 02:40:30
【问题描述】:

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

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

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

 - azure.common.credentials
 - azure.identity

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

  • **azure.identity** 同时提供**ClientSecretCredential & CertificateCredential**,因此我们可以使用共享密钥或 SSL 证书

  • **azure.common.credentials** 包仅提供需要共享密钥**ServicePrincipalCredentials** 类,并且没有对应的证书用于处理 凭据。 p>

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

#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

我的代码中的 sn-ps:

# 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)

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

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

【问题讨论】:

  • Azure.Identity 是较新的 SDK。你能添加失败的代码吗?
  • @juunas,感谢您对此进行调查。在描述中添加了代码sn-p。

标签: azure azure-service-principal


【解决方案1】:

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

  • 对于azure-storage-blobazure.common 用于存储 SDK 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.identity 一起用于共享密钥和基于证书的服务主体。
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2016-03-13
  • 2020-07-21
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
相关资源
最近更新 更多