I'm trying to using the Azure Python SDK to drive some server configuration management, but I'm having difficulty working out how I'm supposed to use the API to upload and configure SSL certificates.
I can successfully interrogate my Azure account to discovering the App Services that are available with the WebSiteManagementClient
, and I can interrogate and manipulate DNS configurations using the DnsManagementClient
.
I am also able to manually add an SSL certificate to an Azure App Service using the instructions on the Azure website.
However, it isn't at all clear to me what API endpoints I should be using to install a custom SSL certificate.
If I've got a WebSiteManagementClient
named client
, then I can see that:
client.certificates.get_certificate()
allows me to get a specific certificate by name - butclient.certificates
doesn't appear to have an API to list all available certificates.client.certificates.create_or_update_certificate()
allows me to presumably idempotently create/update a certificate - but it requires aCertificateEnvelope
argument, and I can't see where that object should be created.- Assuming I manually upload a certificate, I can't work out what API endpoint I would use to install that certificate on a site. There are calls to
get_site_host_name_bindings
anddelete_site_host_name_binding
, but no obvious API to create the binding; there are dozens of calls toconfigure_...
andcreate_or_update_...
, but neither the naming of the API endpoints nor the API documentation is in any way illuminating as to which calls should be used.
Can anyone point me in the right direction? What Python API calls do I need to make to upload a certificate obtained from a third party, and install that certificate on an AppService under a specific domain?
Addendum
Here's some sample code, based on suggestions from @peter-pan-msft:
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')
This code raises:
KeyVaultErrorException: Operation returned an invalid status code 'Forbidden'
The values for the credentials have worked for other operations, including getting and creating keys in the key store. If I modify the credentials to be known bad values, I get:
KeyVaultErrorException: Operation returned an invalid status code 'Unauthorized'