Skip to content

Commit

Permalink
[Key Vault] Align certificates with other languages (Azure#18767)
Browse files Browse the repository at this point in the history
Co-authored-by: Charles Lowell <[email protected]>
  • Loading branch information
mccoyp and chlowell authored May 27, 2021
1 parent 2d2c064 commit 440274e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 52 deletions.
4 changes: 2 additions & 2 deletions sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
- Updated msrest requirement to >=0.6.21

### Added
- Added method `parse_key_vault_certificate_id` that parses out a full ID returned by Key Vault, so users can easily
access the certificate's `name`, `vault_url`, and `version`.
- Added class `KeyVaultCertificateIdentifier` that parses out a full ID returned by Key Vault,
so users can easily access the certificate's `name`, `vault_url`, and `version`.


## 4.2.1 (2020-09-08)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
DeletedCertificate,
IssuerProperties,
LifetimeAction,
KeyVaultCertificate
KeyVaultCertificate,
KeyVaultCertificateIdentifier
)
from ._parse_id import parse_key_vault_certificate_id
from ._shared.client_base import ApiVersion
from ._shared import KeyVaultResourceId

__all__ = [
"ApiVersion",
Expand All @@ -44,14 +43,13 @@
"KeyCurveName",
"KeyType",
"KeyVaultCertificate",
"KeyVaultCertificateIdentifier",
"KeyUsageType",
"LifetimeAction",
"CertificateContentType",
"WellKnownIssuerNames",
"CertificateIssuer",
"IssuerProperties",
"parse_key_vault_certificate_id",
"KeyVaultResourceId"
"IssuerProperties"
]

from ._version import VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self, **kwargs):
# type: (**Any) -> None
self._attributes = kwargs.pop("attributes", None)
self._id = kwargs.pop("cert_id", None)
self._vault_id = parse_key_vault_id(self._id)
self._vault_id = KeyVaultCertificateIdentifier(self._id)
self._x509_thumbprint = kwargs.pop("x509_thumbprint", None)
self._tags = kwargs.pop("tags", None)

Expand Down Expand Up @@ -392,6 +392,45 @@ def cer(self):
return self._cer


class KeyVaultCertificateIdentifier(object):
"""Information about a KeyVaultCertificate parsed from a certificate ID.
:param str id: the full original identifier of a certificate
:raises ValueError: if the certificate ID is improperly formatted
Example:
.. literalinclude:: ../tests/test_parse_id.py
:start-after: [START parse_key_vault_certificate_id]
:end-before: [END parse_key_vault_certificate_id]
:language: python
:caption: Parse a certificate's ID
:dedent: 8
"""

def __init__(self, id): # pylint: disable=W0622
# type: (str) -> None
self._resource_id = parse_key_vault_id(id)

@property
def source_id(self):
# type: () -> str
return self._resource_id.source_id

@property
def vault_url(self):
# type: () -> str
return self._resource_id.vault_url

@property
def name(self):
# type: () -> str
return self._resource_id.name

@property
def version(self):
# type: () -> Optional[str]
return self._resource_id.version


class CertificateOperation(object):
# pylint:disable=too-many-instance-attributes
"""A certificate operation is returned in case of long running requests.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
KeyType,
KeyCurveName,
KeyUsageType,
KeyVaultCertificateIdentifier,
CertificateContentType,
LifetimeAction,
CertificateIssuer,
IssuerProperties,
parse_key_vault_certificate_id
IssuerProperties
)
import pytest

Expand Down Expand Up @@ -88,7 +88,7 @@ def _validate_certificate_operation(self, pending_cert_operation, vault, cert_na
self.assertIsNotNone(pending_cert_operation)
self.assertIsNotNone(pending_cert_operation.csr)
self.assertEqual(original_cert_policy.issuer_name, pending_cert_operation.issuer_name)
pending_id = parse_key_vault_certificate_id(pending_cert_operation.id)
pending_id = KeyVaultCertificateIdentifier(pending_cert_operation.id)
self.assertEqual(pending_id.vault_url.strip("/"), vault.strip("/"))
self.assertEqual(pending_id.name, cert_name)

Expand Down Expand Up @@ -349,7 +349,7 @@ def test_recover_and_purge(self, client, **kwargs):
client.begin_delete_certificate(certificate_name=cert_name).wait()

# validate all our deleted certificates are returned by list_deleted_certificates
deleted = [parse_key_vault_certificate_id(source_id=c.id).name for c in client.list_deleted_certificates()]
deleted = [KeyVaultCertificateIdentifier(id=c.id).name for c in client.list_deleted_certificates()]
self.assertTrue(all(c in deleted for c in certs.keys()))

# recover select certificates (test resources have a "livekvtest" prefix)
Expand All @@ -364,7 +364,7 @@ def test_recover_and_purge(self, client, **kwargs):
time.sleep(50)

# validate none of our deleted certificates are returned by list_deleted_certificates
deleted = [parse_key_vault_certificate_id(source_id=c.id).name for c in client.list_deleted_certificates()]
deleted = [KeyVaultCertificateIdentifier(id=c.id).name for c in client.list_deleted_certificates()]
self.assertTrue(not any(c in deleted for c in certs.keys()))

# validate the recovered certificates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
KeyType,
KeyCurveName,
KeyUsageType,
KeyVaultCertificateIdentifier,
CertificateContentType,
LifetimeAction,
CertificateIssuer,
IssuerProperties,
parse_key_vault_certificate_id
IssuerProperties
)
from azure.keyvault.certificates.aio import CertificateClient
import pytest
Expand Down Expand Up @@ -84,7 +84,7 @@ def _validate_certificate_operation(self, pending_cert_operation, vault, cert_na
self.assertIsNotNone(pending_cert_operation)
self.assertIsNotNone(pending_cert_operation.csr)
self.assertEqual(original_cert_policy.issuer_name, pending_cert_operation.issuer_name)
pending_id = parse_key_vault_certificate_id(pending_cert_operation.id)
pending_id = KeyVaultCertificateIdentifier(pending_cert_operation.id)
self.assertEqual(pending_id.vault_url.strip("/"), vault.strip("/"))
self.assertEqual(pending_id.name, cert_name)

Expand Down Expand Up @@ -350,7 +350,7 @@ async def test_recover_and_purge(self, client, **kwargs):
deleted_certificates = client.list_deleted_certificates()
deleted = []
async for c in deleted_certificates:
deleted.append(parse_key_vault_certificate_id(source_id=c.id).name)
deleted.append(KeyVaultCertificateIdentifier(id=c.id).name)
self.assertTrue(all(c in deleted for c in certs.keys()))

# recover select certificates (test resources have a "livekvtest" prefix)
Expand All @@ -368,7 +368,7 @@ async def test_recover_and_purge(self, client, **kwargs):
deleted_certificates = client.list_deleted_certificates()
deleted = []
async for c in deleted_certificates:
deleted.append(parse_key_vault_certificate_id(source_id=c.id).name)
deleted.append(KeyVaultCertificateIdentifier(id=c.id).name)
self.assertTrue(not any(c in deleted for c in certs.keys()))

# validate the recovered certificates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# -------------------------------------
from azure.keyvault.certificates import CertificateClient, CertificatePolicy, parse_key_vault_certificate_id
from azure.keyvault.certificates import CertificateClient, CertificatePolicy, KeyVaultCertificateIdentifier
from devtools_testutils import PowerShellPreparer

from _shared.test_case import KeyVaultTestCase
Expand All @@ -25,7 +25,7 @@ def test_parse_certificate_id_with_version(self, azure_keyvault_url):

# [START parse_key_vault_certificate_id]
cert = client.get_certificate(cert_name)
parsed_certificate_id = parse_key_vault_certificate_id(cert.id)
parsed_certificate_id = KeyVaultCertificateIdentifier(cert.id)

print(parsed_certificate_id.name)
print(parsed_certificate_id.vault_url)
Expand All @@ -40,7 +40,7 @@ def test_parse_certificate_id_with_version(self, azure_keyvault_url):

def test_parse_certificate_id_with_pending_version():
source_id = "https://keyvault-name.vault.azure.net/certificates/certificate-name/pending"
parsed_certificate_id = parse_key_vault_certificate_id(source_id)
parsed_certificate_id = KeyVaultCertificateIdentifier(source_id)

assert parsed_certificate_id.name == "certificate-name"
assert parsed_certificate_id.vault_url == "https://keyvault-name.vault.azure.net"
Expand All @@ -52,7 +52,7 @@ def test_parse_certificate_id_with_pending_version():

def test_parse_deleted_certificate_id():
source_id = "https://keyvault-name.vault.azure.net/deletedcertificates/deleted-certificate"
parsed_certificate_id = parse_key_vault_certificate_id(source_id)
parsed_certificate_id = KeyVaultCertificateIdentifier(source_id)

assert parsed_certificate_id.name == "deleted-certificate"
assert parsed_certificate_id.vault_url == "https://keyvault-name.vault.azure.net"
Expand Down

0 comments on commit 440274e

Please sign in to comment.