-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
There was a problem hiding this 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. |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
…m certificate (Azure#15863) * Add certificate key/cert parsing samples * Thanks, Charles! * Better wording Co-authored-by: Charles Lowell <chlowe@microsoft.com>
This adds two samples,
parse_certificate.py
andparse_certificate_async.py
, to the samples folder inazure-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.