我正在尝试使用Azure Python SDK来推动某些服务器配置管理,但我很难弄清楚我应该如何使用API来上传和配置SSL证书。
我可以成功查询我的Azure帐户,以发现WebSiteManagementClient
提供的应用服务,并且可以使用DnsManagementClient
查询和操作DNS配置。
我还可以使用the instructions on the Azure website手动将SSL证书添加到Azure应用服务。
但是,我并不清楚我应该使用哪些API端点来安装自定义SSL证书。
如果我有一个名为WebSiteManagementClient
的{{1}},那么我可以看到:
client
允许我按名称获取特定证书 - 但client.certificates.get_certificate()
似乎没有API列出所有可用证书。client.certificates
允许我大概是幂等地创建/更新证书 - 但它需要一个client.certificates.create_or_update_certificate()
参数,我无法看到应该在哪里创建该对象。CertificateEnvelope
和get_site_host_name_bindings
,但没有明显的API来创建绑定;有几十次调用delete_site_host_name_binding
和configure_...
,但API端点的命名和API文档都没有说明应该使用哪些调用。有人能指出我正确的方向吗?我需要做什么Python API来上传从第三方获得的证书,并在特定域下的AppService上安装该证书?
以下是一些示例代码,基于@ peter-pan-msft的建议:
create_or_update_...
此代码提出:
creds = ServicePrincipalCredentials(
client_id=UUID('<client>'),
secret='<secret>',
tenant=UUID('<tenant>'),
resource='https://vault.azure.net'
)
kv = KeyVaultClient(
credentials=creds
)
KEY_VAULT_URI = 'https://<vault>.vault.azure.net/'
with open('example.pfx', 'rb') as f:
data = f.read()
# Try to get the certificates
for cert in kv.get_certificates(KEY_VAULT_URI):
print(cert)
# or...
kv.import_certificate(KEY_VAULT_URI, 'cert name', data, 'password')
凭据的值适用于其他操作,包括在密钥库中获取和创建密钥。如果我将凭证修改为已知错误值,我会得到:
KeyVaultErrorException: Operation returned an invalid status code 'Forbidden'
答案 0 :(得分:1)
如果您遵循 App Service walkthrough for importing certificates from Key Vault,它会告诉您您的应用需要读取权限才能访问保险库中的证书。但是,要在执行过程中最初将证书导入 Key Vault,您还需要授予服务主体证书导入权限。尝试在没有导入权限的情况下导入证书会产生与您所看到的类似的“禁止”错误。
还有用于在 Python 中使用 Key Vault 的新包替代了 azure-keyvault
:
azure-identity 是应该与这些一起用于身份验证的包。
以下是使用 azure-keyvault-certificates
导入证书的示例:
from azure.identity import DefaultAzureCredential
from azure.keyvault.certificates import CertificateClient
KEY_VAULT_URI = 'https://<vault>.vault.azure.net/'
credential = DefaultAzureCredential()
client = CertificateClient(KEY_VAULT_URI, credential)
with open('example.pfx', 'rb') as f:
data = f.read()
client.import_certificate("cert-name", data.encode(), password="password")
您可以通过设置与 ServicePrincipalCredentials
、client_id
和 secret
对应的环境变量来提供用于 tenant
的相同凭据:
export AZURE_CLIENT_ID="<client>"
export AZURE_CLIENT_SECRET="<secret>"
export AZURE_TENANT_ID="<tenant>"
(我使用 Python 开发 Azure SDK)
答案 1 :(得分:0)
根据您的描述,根据我的理解,我认为您希望上传证书并在Azure App Service上使用它。
根据我对Azure Python SDK的经验,似乎没有任何Python API可以直接将证书上传到Azure App Service。但是,有一种解决方法可以通过将证书导入Azure Key Vault并从Azure App Service使用它来实现。有关详细信息,请参阅下面的docuemtn列表。
Key Valut
的{{3}} REST API。相关的Azure Python API是来自Import Certificate
的方法import_certificate
,您可以参考here以获取密钥保险柜以了解如何使用它。Create Or Update
,请参考create_or_update
。希望它有所帮助。
正如here的KeyVault的Azure Python SDK参考所述,如下所示。
访问政策
某些操作需要您的凭据的正确访问策略。
如果您收到“未经授权”错误,请使用Azure门户,Azure CLI或密钥保管库管理SDK本身为此凭据添加正确的访问策略
以下是通过Access Policy
为证书操作设置访问策略的步骤。
获取应用程序的Azure AD服务主体,命令azure ad sp show --search <your-application-display-name>
,然后复制Service Principal Names
(spn),如xxxx-xxxx-xxxxx-xxxx-xxxx
。
设置证书操作的策略,命令azure keyvault set-policy brucechen --spn <your-applicaiton-spn> --perms-to-certificates <perms-to-certificates, such as [\"all\"]>
。 <perms-to-certificates>
的解释如下。
表示证书操作的JSON编码的字符串数组;每个字符串可以是[all,get,list,delete,create,import,update,managecontacts,getissuers,listissuers,setissuers,deleteissuer
之一