azure - 通过基于证书的服务主体凭据访问 Azure 存储;azure.identity 与 azure.common.credentials
问题描述
我已经尝试尝试通过 Python SDK 使用服务主体凭据访问 Azure Blob 存储,并且有一些我认为社区可以提供帮助的困惑。
#1azure.common.credentials
与azure.identity
------------------------------------------------------------ --
我注意到 Azure 中有两个不同的 python 包具有凭据类。
- azure.common.credentials
- azure.identity
两者有什么区别,什么时候应该用一个来对付另一个?更具体地说,当尝试使用 Azure 服务主体时,
**azure.identity**
两者都提供,**ClientSecretCredential & CertificateCredential**
因此我们可以使用共享密钥或 SSL 证书。**azure.common.credentials**
包只提供**ServicePrincipalCredentials**
需要共享密钥的类,并且没有对应的证书凭证。
我错过了什么吗?我正在寻找使用基于证书的服务主体。
#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)
解决方案
我承认,目前的情况具有误导性,这是截至今天的一些细节(在此处监控情况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-xxx
,azure-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 团队工作)
推荐阅读
- react-native - Jitsi-Meet SDK - 获取来电推送提醒 React Native
- sql-server - TSQL:全年/每月的连续周期
- java - 在 XML 中,必须填写和添加附件的字段之一也是强制性的
- c++ - 用指针创建静态数组?
- javascript - 删除子元素jquery
- javascript - 修改模板内容失败
- java - jdbc:前端java,后端Oracle 10g express版
- angular - 打字稿版本 3.7.2 和 Angular
- combinations - 在概率上相同的项目被视为不同的项目?
- node.js - 如何将 vuejs - express 应用程序部署到本地 Ubuntu?