Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.63k stars 2.84k forks source link

ItemPaged[CertificateProperties] KeyVault Certificate list_properties_of_certificates does not honor the include_pending optional parameter after the first page #38589

Open ahsonkhan opened 4 days ago

ahsonkhan commented 4 days ago

The call to fetch the first page sets the appropriate query parameters based on the input parameter value: https://github.com/Azure/azure-sdk-for-python/blob/e821a490762956edf8c285ae9cba21c9ca267a7b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_client.py#L641-L649

But subsequent pages do not have that value set. This is done in the generated code. That means, if the include_pending param is set to true, it will not return all the certificates (including pending ones), if the pending certificate happens to be listed in a page other than the first. https://github.com/Azure/azure-sdk-for-python/blob/e821a490762956edf8c285ae9cba21c9ca267a7b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_generated/_operations/_operations.py#L784-L816

Here's the swagger (not sure if this requires some fix to the swagger): https://github.com/Azure/azure-rest-api-specs/blob/4a4acecea9901c29e19ba50f2d4cf65b20115b69/specification/keyvault/data-plane/Microsoft.KeyVault/stable/7.5/certificates.json#L30-L83

Sample repro:

from azure.identity import DefaultAzureCredential
from azure.keyvault.certificates import CertificateClient

# Pre-req: Create 25 certificates first so a page is full (either through the portal or programmatically)

# Case 1: Create a certificate (either on the portal or programmatically) on the first page, and run this, right away.
# Works as expected.
# Case 2: Create a certificate (either on the portal or programmatically) on any subsequent page, and run this, right away.
# Doesn't work as expected.

credential = DefaultAzureCredential()

# TODO: Set to your own KeyVault URL
certificate_client = CertificateClient(vault_url="https://<keyvault-name>.vault.azure.net/", credential=credential)

countFalse = 0
print("Certificates in the key vault (includePending = false):")
certificates = certificate_client.list_properties_of_certificates()
for certificate in certificates:
    print(certificate.name)
    countFalse += 1

countTrue = 0
print("Certificates in the key vault (includePending = true):")
certificatesTrue = certificate_client.list_properties_of_certificates(include_pending=True)
for certificate in certificatesTrue:
    print(certificate.name)
    countTrue += 1

# Expected countFalse < countTrue in both cases, since there's a certificate pending.
# Case 1: In the case where the certificate gets created on the first page:
#  -> countFalse < countTrue
# Case 2: But, in the case where the certificate gets created on any other subsequent page:
#  -> countFalse = countTrue
print(f"countFalse = {countFalse} vs countTrue = {countTrue}")

The issue is pervasive across all the ItemPaged methods that follow this pattern within the KeyVault SDKs, but list_properties_of_certificates and list_deleted_certificates (along with list_role_assignments in KeyVault Administration) seem to be the only ones that have optional parameters which are settable by the SDK methods (unlike maxResults) and hence have an actual behavioral bug here.

It's possible that some other service SDKs have similar concerns here.

Related issues in other languages: https://github.com/Azure/azure-sdk-for-net/issues/47202 https://github.com/Azure/azure-sdk-for-cpp/issues/6235 https://github.com/Azure/azure-sdk-for-python/issues/38589 https://github.com/Azure/azure-sdk-for-go/issues/23772 https://github.com/Azure/azure-sdk-for-js/issues/31803 https://github.com/Azure/azure-sdk-for-java/issues/42988

lmazuel commented 3 days ago

As discussed offline, this is by design, see guidelines: https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#collections

lmazuel commented 3 days ago

Re-open, as we discovered that the service was doing the wrong thing, so it may require some custom code to make it work.