Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Key Vault] Add sample for parsing private key/public certificate from certificate #15863

Merged
merged 3 commits into from
Dec 18, 2020

Conversation

mccoyp
Copy link
Member

@mccoyp mccoyp commented Dec 18, 2020

This adds two samples, parse_certificate.py and parse_certificate_async.py, to the samples folder in azure-keyvault-certificates. They each use the cryptography package to parse the secret associated with a default certificate (and specify a dependency on version 3.3+). They also demonstrate certificate purging, though this can be removed if we think a purge permission dependency is overkill.

These are being added because we seem to get questions about fetching the private key for KV certificates fairly often.

@mccoyp mccoyp added KeyVault Client This issue points to a problem in the data-plane of the library. labels Dec 18, 2020
@mccoyp mccoyp requested review from chlowell and schaabs December 18, 2020 01:12
Copy link
Member

@chlowell chlowell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to these from samples/README.md

print("Certificate with name '{}' was created".format(created_certificate.name))

# Key Vault also creates a secret with the same name as the created certificate.
# This secret contains protected information about the certificate, such as its private key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be more precise: the secret contains the certificate's bytes, which include the private key only when the cert's policy marks it exportable (the service documents this here). We should mention that to make it clear there are by-design cases in which Key Vault won't release a cert's private key.

# Now we can extract the private key and public certificate from the secret using the cryptography
# package. `additional_certificates` will be empty since the secret only contains one certificate.
cert_bytes = base64.b64decode(certificate_secret.value)
private_key, public_certificate, additional_certificates = pkcs12.load_key_and_certificates(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key Vault supports PEM as well. If these bytes were PEM encoded, you'd want load_pem_private_key instead. I think it's sufficient to mention the encoding depends on the content type set on the certificate policy, and we're showing PKCS12 here because it's the default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I didn't want to provide a PEM example in fear of complicating things, but you're right that it's worth a mention even if we don't walk through an example.

Co-authored-by: Charles Lowell <chlowe@microsoft.com>
@mccoyp mccoyp merged commit 6d4efde into Azure:master Dec 18, 2020
@mccoyp mccoyp deleted the certs-sample branch December 18, 2020 21:40
rakshith91 pushed a commit to rakshith91/azure-sdk-for-python that referenced this pull request Jan 8, 2021
…m certificate (Azure#15863)

* Add certificate key/cert parsing samples

* Thanks, Charles!

* Better wording

Co-authored-by: Charles Lowell <chlowe@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. KeyVault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants