-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[KeyVault] README improvements #7664
Changes from 6 commits
157cb7d
3b29666
dc8f7d7
9e4d8ea
2e44031
83fcd05
787383c
b0d1712
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ Use the client library for Azure Key Vault Certificates in your Node.js applicat | |
- Get all certificates. | ||
- Get all deleted certificates. | ||
|
||
[Source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-certificates) | [Package (npm)](https://www.npmjs.com/package/@azure/keyvault-certificates) | [API Reference Documentation](https://docs.microsoft.com/javascript/api/@azure/keyvault-certificates) | [Product documentation](https://azure.microsoft.com/en-us/services/key-vault/) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-certificates/samples) | ||
[Source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-certificates) | [Package (npm)](https://www.npmjs.com/package/@azure/keyvault-certificates) | [API Reference Documentation](https://docs.microsoft.com/javascript/api/@azure/keyvault-certificates) | [Product documentation](https://azure.microsoft.com/en-us/services/key-vault/) | [Samples](./samples) | ||
|
||
## Getting started | ||
|
||
|
@@ -149,9 +149,11 @@ The following sections provide code snippets that cover some of the common | |
tasks using Azure Key Vault Certificates. The scenarios that are covered here consist of: | ||
|
||
- [Creating and setting a certificate](#creating-and-setting-a-certificate). | ||
- [Getting a certificate](#get-a-certificate). | ||
- [Getting the versions of a certificate](#list-all-versions-of-a-certificate). | ||
- [Listing all the available certificates](#list-all-certificates). | ||
- [Getting a KeyVault certificate](#getting-a-keyvault-certificate). | ||
- [Getting the full information of a certificate](#getting-the-full-information-of-a-certificate). | ||
- [Certificates in PEM format](#certificates-in-pem-format). | ||
- [List all versions of a certificate](#list-all-versions-of-a-certificate). | ||
- [List all certificates](#list-all-certificates). | ||
- [Updating a certificate](#updating-a-certificate). | ||
- [Deleting a certificate](#deleting-a-certificate). | ||
- [Iterating lists of certificates](#iterating-lists-of-certificates). | ||
|
@@ -301,7 +303,7 @@ async function main() { | |
main(); | ||
``` | ||
|
||
### Get a certificate | ||
### Getting a KeyVault certificate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the product is "Key Vault", not "KeyVault". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lots of instances all over docs that should change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! I'll do these changes. |
||
|
||
The simplest way to read certificates back from the vault is to get a | ||
certificate by name. `getCertificate` will retrieve the most recent | ||
|
@@ -339,32 +341,94 @@ async function main() { | |
main(); | ||
``` | ||
|
||
### List all versions of a certificate | ||
### Getting the full information of a certificate | ||
|
||
`listPropertiesOfCertificateVersions` will list versions of the given certificate. | ||
Azure's KeyVault's design makes sharp distinctions between Keys, | ||
Secrets and Certificates. The KeyVault service's Certificates | ||
features were designed making use of it's Keys and Secrets capabilities. | ||
Let's evaluate the composition of a KeyVault Certificate: | ||
|
||
```javascript | ||
const { DefaultAzureCredential } = require("@azure/identity"); | ||
const { CertificateClient } = require("@azure/keyvault-certificates"); | ||
> When a Key Vault certificate is created, an addressable key | ||
> and secret are also created with the same name. The Key Vault | ||
> key allows key operations and the Key Vault secret allows retrieval | ||
> of the certificate value as a secret. A Key Vault certificate | ||
> also contains public x509 certificate metadata. | ||
> _Source: [Composition of a Certificate][Composition-of-a-Certificate]._ | ||
|
||
const credential = new DefaultAzureCredential(); | ||
Knowing that the private key is stored in a KeyVault Secret, | ||
with the public certificate included, we can retrieve it | ||
by using the [KeyVault Secrets client][KeyVault-Secrets-client]. | ||
|
||
const vaultName = "<YOUR KEYVAULT NAME>"; | ||
const url = `https://${vaultName}.vault.azure.net`; | ||
```ts | ||
// Using the same credential object we used before, | ||
// and the same keyVaultUrl, | ||
// let's create a SecretClient | ||
const secretClient = new SecretClient(keyVaultUrl, credential); | ||
|
||
const client = new CertificateClient(url, credential); | ||
// Assuming you've already created a KeyVault certificate, | ||
// and that certificateName contains the name of your certificate | ||
const certificateSecret = await secretClient.getSecret(certificateName); | ||
|
||
const certificateName = "MyCertificateName"; | ||
// Here we can find both the private key and the public certificate, in PKCS 12 format: | ||
const PKCS12Certificate = certificateSecret.value!; | ||
|
||
async function main() { | ||
for await (let certificateProperties of client.listPropertiesOfCertificateVersions(certificateName)) { | ||
console.log("version: ", certificateProperties.version); | ||
} | ||
} | ||
// You can write this into a file: | ||
fs.writeFileSync("myCertificate.p12", PKCS12Certificate); | ||
``` | ||
|
||
main(); | ||
Note that, by default, the content type of the certificates | ||
is [PKCS 12][PKCS_12]. By specifying the content type | ||
of your certificate, you'll be able to retrieve it in PEM format. | ||
Before showing how to create PEM certificates, | ||
let's first explore how to retrieve a PEM secret key | ||
from a PKCS 12 certificate first. | ||
|
||
Using `openssl`, you can retrieve the public certificate in | ||
PEM format by using the following command: | ||
|
||
openssl pkcs12 -in myCertificate.p12 -out myCertificate.crt.pem -clcerts -nokeys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a code block using triple backticks. Doesn't need a language i.e. can just be preformatted text. |
||
|
||
You can also use `openssl` to retrieve the private key, as follows: | ||
|
||
openssl pkcs12 -in myCertificate.p12 -out myCertificate.key.pem -nocerts -nodes | ||
|
||
Note that in both cases, openssl will ask you for the | ||
password used to create the certificate. The sample code we've used | ||
so far hasn't specified a password, so you can append ` -passin 'pass:' ` | ||
to the end of each command. | ||
|
||
### Certificates in PEM format | ||
|
||
If you want to work with certificates in PEM format, | ||
you can tell Azure's KeyVault service to create and manage your | ||
certificates in PEM format by providing the `contentType` property | ||
at the moment of creating the certificates. | ||
|
||
The following example shows how to create and retrieve | ||
the public and the private parts of a PEM formatted certificate | ||
using the KeyVault clients for Certificates and Secrets: | ||
|
||
```ts | ||
// Creating the certificate | ||
const certificateName = "MyCertificate"; | ||
const createPoller = await client.beginCreateCertificate(certificateName, { | ||
issuerName: "Self", | ||
subject: "cn=MyCert", | ||
contentType: "application/x-pem-file" // Here you specify you want to work with PEM certificates. | ||
}); | ||
const keyVaultCertificate = await createPoller.pollUntilDone(); | ||
|
||
// Getting the PEM formatted private key and public certificate: | ||
const certificateSecret = await secretClient.getSecret(certificateName); | ||
const PEMPair = certificateSecret.value!; | ||
|
||
console.log(PEMPair); | ||
``` | ||
|
||
Keep in mind that your public certificate will | ||
be in the same blob of content as your private key. | ||
You can use the PEM headers to extract them accordingly. | ||
|
||
### List all certificates | ||
|
||
`listPropertiesOfCertificates` will list all certificates in the Key Vault. | ||
|
@@ -582,21 +646,21 @@ main(); | |
|
||
## Troubleshooting | ||
|
||
### Enable logs | ||
|
||
You can set the following environment variable to get the debug logs when using this library. | ||
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`: | ||
|
||
- Getting debug logs from the Key Vault Certificates SDK | ||
```javascript | ||
import { setLogLevel } from "@azure/logger"; | ||
|
||
```bash | ||
export DEBUG=azure* | ||
setLogLevel("info"); | ||
``` | ||
|
||
## Next steps | ||
|
||
Please take a look at the | ||
[samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/keyvault/keyvault-certificates/samples) | ||
directory for detailed examples on how to use this library. | ||
You can find more code samples through the following links: | ||
|
||
- [KeyVault Certificates Samples (JavaScript)](./samples/javascript) | ||
- [KeyVault Certificates Samples (TypeScript)](./samples/typescript) | ||
- [KeyVault Certificates Test Cases](./test/) | ||
|
||
## Testing | ||
|
||
|
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.
Do we consider these "samples" grade (meaning consumers can read them and get started)? Might be worth just removing it.
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.
Yep, most of our tests are thought as samples. In fact, our samples are almost copies of our tests.