* two samples

* samples

* typas, types, typis .... typos

* PR comments
This commit is contained in:
George Arama 2021-10-25 18:30:43 -07:00 committed by GitHub
parent a4f2e510e5
commit a9094d0933
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 898 additions and 5 deletions

View File

@ -86,9 +86,9 @@ endif()
# add_subdirectory(test/perf)
# endif()
# if(BUILD_SAMPLES)
# add_subdirectory(test/samples)
# endif()
if(BUILD_SAMPLES)
add_subdirectory(samples)
endif()
az_vcpkg_export(
azure-security-keyvault-certificates

View File

@ -365,7 +365,7 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Certificat
* page of certificates.
*/
CertificatePropertiesPagedResponse GetPropertiesOfCertificates(
GetPropertiesOfCertificatesOptions const& options,
GetPropertiesOfCertificatesOptions const& options = GetPropertiesOfCertificatesOptions(),
Azure::Core::Context const& context = Azure::Core::Context()) const;
/**

View File

@ -0,0 +1,8 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required (VERSION 3.13)
add_subdirectory(certificate-basic-operations)
add_subdirectory(certificate-GetCertificates)
add_subdirectory(certificate-ImportCertificate)

View File

@ -0,0 +1,15 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required (VERSION 3.13)
project (certificate-GetCertificates LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable (
certificate-GetCertificates
certificate-GetCertificates.cpp
)
target_link_libraries(certificate-GetCertificates PRIVATE azure-security-keyvault-certificates azure-identity )

View File

@ -0,0 +1,139 @@
# Creating, get properties, get versions, delete, get deleted and purge certificates
This sample demonstrates how to:
* create certificates
* get properties of certificates
* get properties of certificate versions
* delete a certificate
* get deleted certificates
* purge
in Azure Key Vault.
To get started, you'll need a URI to an Azure Key Vault.
### Creating a CertificateClient
To create a new `CertificateClient` to create, get, update, or delete certificates, you need the endpoint to an Azure Key Vault and credentials.
Key Vault Certificate client for C++ currently supports the `ClientSecretCredential` for authenticating.
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
```cpp Snippet:CertificateSample2CreateCredential
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential = std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
```
Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application.
```cpp Snippet:CertificateSample2Client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
```
## Creating a Certificate
Call StartCreateCertificate to create a new certificate, with specified properties and policy.
```cpp Snippet:CertificateSample2Create
std::string certificateName = "Sample1";
auto params = CertificateCreateParameters();
...
// start the create process
auto response = certificateClient.StartCreateCertificate(certificateName, params);
auto result = response.PollUntilDone(defaultWait);
// check that the operation completed
while (!response.IsCompleted())
{
response.UpdateProperties();
std::this_thread::sleep_for(defaultWait);
}
```
## Getting properties of Certificates
Call GetPropertiesOfCertificates to retrieve information about certificates from Key Vault.
```cpp Snippet:CertificateSample2GetProperties
// get properties of certificates
for (auto certificates = certificateClient.GetPropertiesOfCertificates();
certificates.HasPage();
certificates.MoveToNextPage())
{
// go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << certificates.Items.size() << " certificates.";
for (auto oneCertificate : certificates.Items)
{
std::cout << "Certificate name : " << oneCertificate.Name;
}
}
```
## Creating a new certificate version
Repeat the create certificate procedure, for an existing certificate it will create a new version of it.
## Getting the versions of a certificate
To get information about certificate versions call GetPropertiesOfCertificateVersions.
```cpp Snippet:CertificateSample2GetProperties
// get properties of all the versions of a certificate
for (auto certificateVersions
= certificateClient.GetPropertiesOfCertificateVersions(certificateName1);
certificateVersions.HasPage();
certificateVersions.MoveToNextPage())
{
// go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << certificateVersions.Items.size()
<< " certificate versions for certificate " << certificateName1;
}
```
## Deleting the certificates
Now we will delete the certificates. Since this is a long running operation we need to wait for the operation to finish
```cpp Snippet:CertificateSample2Delete
// delete the certificates
auto response1 = certificateClient.StartDeleteCertificate(certificateName1);
auto response2 = certificateClient.StartDeleteCertificate(certificateName2);
response1.PollUntilDone(defaultWait);
response2.PollUntilDone(defaultWait);
```
## Getting the deleted certificates
After the certificates are deleted , but not yet purged we can call GetDeletedCertificates
```cpp Snippet:CertificatesSample2GetDeleted
// get properties of deleted certificates
for (auto deletedCertificates = certificateClient.GetDeletedCertificates();
deletedCertificates.HasPage();
deletedCertificates.MoveToNextPage())
{
// go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << deletedCertificates.Items.size() << " deleted certificates.";
}
```
## Purging the deleted certificates
If the Azure Key Vault is soft delete-enabled and you want to permanently delete the certificate before its `ScheduledPurgeDate`, the certificate needs to be purged.
```cpp Snippet:certificateSample2Purge
// purge the certificates
{
certificateClient.PurgeDeletedCertificate(certificateName1);
certificateClient.PurgeDeletedCertificate(certificateName2);
}
```

View File

@ -0,0 +1,175 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
*
* @brief This sample provides explamles of handling paged operations.
* @details This sample provides the code implementation to use the Key Vault Certificates SDK
* client for C++ to create, get properties of certificates, get properties of certificate versions,
* delete , get deleted certificates, purge
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_KEYVAULT_URL: To the Key Vault account URL.
* - AZURE_TENANT_ID: Tenant ID for the Azure account.
* - AZURE_CLIENT_ID: The Client ID to authenticate the request.
* - AZURE_CLIENT_SECRET: The client secret.
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_certificates.hpp>
#include <chrono>
#include <iostream>
using namespace Azure::Security::KeyVault::Certificates;
using namespace std::chrono_literals;
KeyVaultCertificateWithPolicy CreateCertificate(
std::string const& certificateName,
CertificateClient const& certificateClient);
int main()
{
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential
= std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
std::chrono::milliseconds defaultWait(10s);
// create client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
try
{
std::string certificateName1 = "Sample1";
std::string certificateName2 = "Sample2";
KeyVaultCertificateWithPolicy certificate1;
KeyVaultCertificateWithPolicy certificate2;
// create and get two certificates
{
// create certificates
certificate1 = CreateCertificate(certificateName1, certificateClient);
certificate2 = CreateCertificate(certificateName2, certificateClient);
// get properties of certificates
for (auto certificates = certificateClient.GetPropertiesOfCertificates();
certificates.HasPage();
certificates.MoveToNextPage())
{ // go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << certificates.Items.size() << " certificates.";
for (auto oneCertificate : certificates.Items)
{
std::cout << "Certificate name : " << oneCertificate.Name;
}
}
}
// certificate versions, and get versions
{
// create new version of certificate
CreateCertificate(certificateName1, certificateClient);
// get properties of all the versions of a certificate
for (auto certificateVersions
= certificateClient.GetPropertiesOfCertificateVersions(certificateName1);
certificateVersions.HasPage();
certificateVersions.MoveToNextPage())
{ // go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << certificateVersions.Items.size()
<< " certificate versions for certificate " << certificateName1;
}
}
// delete the certificates, and get deleted
{
// delete the certificates
auto response1 = certificateClient.StartDeleteCertificate(certificateName1);
auto response2 = certificateClient.StartDeleteCertificate(certificateName2);
response1.PollUntilDone(defaultWait);
response2.PollUntilDone(defaultWait);
// get properties of deleted certificates
for (auto deletedCertificates = certificateClient.GetDeletedCertificates();
deletedCertificates.HasPage();
deletedCertificates.MoveToNextPage())
{ // go through every certificate of each page returned
// the number of results returned for in a page is not guaranteed
// it can be anywhere from 0 to 25
std::cout << "Found " << deletedCertificates.Items.size() << " deleted certificates.";
}
}
// purge the certificates
{
certificateClient.PurgeDeletedCertificate(certificateName1);
certificateClient.PurgeDeletedCertificate(certificateName2);
}
}
catch (Azure::Core::Credentials::AuthenticationException const& e)
{
std::cout << "Authentication Exception happened:" << std::endl << e.what() << std::endl;
return 1;
}
catch (Azure::Core::RequestFailedException const& e)
{
std::cout << "Key Vault Certificate Client Exception happened:" << std::endl
<< e.Message << std::endl;
return 1;
}
return 0;
}
KeyVaultCertificateWithPolicy CreateCertificate(
std::string const& certificateName,
CertificateClient const& certificateClient)
{
CertificateCreateParameters params;
std::chrono::milliseconds defaultWait(10s);
// setup certificate create properties/policy
{
// create a lifetime action
LifetimeAction action;
action.LifetimePercentage = 80;
action.Action = CertificatePolicyAction::AutoRenew;
// setup properties
params.Properties.Enabled = true;
// setup policy
params.Policy.Subject = "CN=sample1";
params.Policy.ValidityInMonths = 12;
params.Policy.Enabled = true;
params.Policy.ContentType = CertificateContentType::Pkcs12;
params.Policy.IssuerName = "Self";
// add a lifetime action
params.Policy.LifetimeActions.emplace_back(action);
}
// create a certificate
{
params.Properties.Name = certificateName;
// start the create process
auto response = certificateClient.StartCreateCertificate(certificateName, params);
auto result = response.PollUntilDone(defaultWait);
// check that the operation completed
while (!response.IsCompleted())
{
response.UpdateProperties();
std::this_thread::sleep_for(defaultWait);
}
// get the certificate
auto certificate = certificateClient.GetCertificate(certificateName).Value;
std::cout << "Created certificate with policy. Certificate name : " << certificate.Name();
return certificate;
}
}

View File

@ -0,0 +1,15 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required (VERSION 3.13)
project (certificate-ImportCertificate LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable (
certificate-ImportCertificate
certificate-ImportCertificate.cpp
)
target_link_libraries(certificate-ImportCertificate PRIVATE azure-security-keyvault-certificates azure-identity )

View File

@ -0,0 +1,92 @@
# Importing certificates
This sample demonstrates how to import a certificate in Azure Key Vault.
To get started, you'll need a URI to an Azure Key Vault.
### Creating a CertificateClient
To create a new `CertificateClient` to create, get, update, or delete certificates, you need the endpoint to an Azure Key Vault and credentials.
Key Vault Certificate client for C++ currently supports the `ClientSecretCredential` for authenticating.
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
```cpp Snippet:CertificateSample3CreateCredential
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential = std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
```
Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application.
```cpp Snippet:CertificateSample3Client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
```
## Importing a PEM certificate
You will need the certificate content in PEM format to perform this operation. One sample is provided in certificate-ImportCertificate.hpp as the pemCertificate string.
Once the import parameters are setup we can call Import certificate and get back the newly imported certificate.
```cpp Snippet:CertificateSample3ImportPEM
// prepare the parameters
ImportCertificateOptions params;
params.Value = pemCertificate;
params.Policy.Enabled = true;
params.Policy.KeyType = CertificateKeyType::Rsa;
params.Policy.KeySize = 2048;
params.Policy.ContentType = CertificateContentType::Pem;
params.Policy.Exportable = true;
// call import API
auto imported = certificateClient.ImportCertificate(pemName, params).Value;
// get some value from the certificate
std::cout << "Imported pem certificate with name " << imported.Name();
```
## Importing a PKCS certificate
You will need the certificate content in PKCS format to perform this operation. One sample is provided in certificate-ImportCertificate.hpp as the pkcsBase64 string.
Once the import parameters are setup we can call Import certificate and get back the newly imported certificate
```cpp Snippet:CertificateSample3ImportPKCS
// prepare the parameters
ImportCertificateOptions params;
params.Value = pkcsBase64;
params.Policy.Enabled = true;
params.Policy.KeyType = CertificateKeyType::Rsa;
params.Policy.KeySize = 2048;
params.Policy.ContentType = CertificateContentType::Pkcs12;
params.Policy.Exportable = true;
// call the import API
auto imported = certificateClient.ImportCertificate(pkcsName, params).Value;
// read something from the certificate
std::cout << "Imported pkcs certificate with name " << imported.Name();
```
## Deleting the certificates
Call StartDeleteCertificate to delete a certificate. This is a long running operation.
```cpp Snippet:CertificateSample1Delete
// delete the certificates
auto response1 = certificateClient.StartDeleteCertificate(pemName);
auto response2 = certificateClient.StartDeleteCertificate(pkcsName);
```
## Purging the deleted certificates
If the Azure Key Vault is soft delete-enabled and you want to permanently delete the certificate before its `ScheduledPurgeDate`, the certificate needs to be purged.
```cpp Snippet:CertificateSample3PurgeCertificate
response1.PollUntilDone(defaultWait);
response2.PollUntilDone(defaultWait);
// purge the certificates
certificateClient.PurgeDeletedCertificate(pkcsName);
certificateClient.PurgeDeletedCertificate(pemName);
```

View File

@ -0,0 +1,222 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @brief This sample provides the code implementation to use the Key Vault Certificates SDK client
* for C++ to import a certificate.
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_KEYVAULT_URL: To the Key Vault account URL.
* - AZURE_TENANT_ID: Tenant ID for the Azure account.
* - AZURE_CLIENT_ID: The Client ID to authenticate the request.
* - AZURE_CLIENT_SECRET: The client secret.
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <azure/core.hpp>
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_certificates.hpp>
#include <chrono>
#include <iostream>
using namespace Azure::Security::KeyVault::Certificates;
using namespace std::chrono_literals;
std::string GetPemCertificate();
std::string GetPkcsCertificate();
int main()
{
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential
= std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
std::chrono::milliseconds defaultWait(10s);
// create client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
try
{
// certificate names
std::string const pemName = "Pem1";
std::string const pkcsName = "Pkcs1";
// import pem certificate
{
// prepare the parameters
ImportCertificateOptions params;
params.Value = GetPemCertificate();
params.Policy.Enabled = true;
params.Policy.KeyType = CertificateKeyType::Rsa;
params.Policy.KeySize = 2048;
params.Policy.ContentType = CertificateContentType::Pem;
params.Policy.Exportable = true;
// call import API
auto imported = certificateClient.ImportCertificate(pemName, params).Value;
// get some value from the certificate
std::cout << "Imported pem certificate with name " << imported.Name();
}
// import pkcs certificate
{
// prepare the parameters
ImportCertificateOptions params;
params.Value = GetPkcsCertificate();
params.Policy.Enabled = true;
params.Policy.KeyType = CertificateKeyType::Rsa;
params.Policy.KeySize = 2048;
params.Policy.ContentType = CertificateContentType::Pkcs12;
params.Policy.Exportable = true;
// call the import API
auto imported = certificateClient.ImportCertificate(pkcsName, params).Value;
// read something from the certificate
std::cout << "Imported pkcs certificate with name " << imported.Name();
}
// delete the certificates, and get deleted
{
// delete the certificates
auto response1 = certificateClient.StartDeleteCertificate(pemName);
auto response2 = certificateClient.StartDeleteCertificate(pkcsName);
response1.PollUntilDone(defaultWait);
response2.PollUntilDone(defaultWait);
// purge the certificates
certificateClient.PurgeDeletedCertificate(pkcsName);
certificateClient.PurgeDeletedCertificate(pemName);
}
}
catch (Azure::Core::Credentials::AuthenticationException const& e)
{
std::cout << "Authentication Exception happened:" << std::endl << e.what() << std::endl;
return 1;
}
catch (Azure::Core::RequestFailedException const& e)
{
std::cout << "Key Vault Certificate Client Exception happened:" << std::endl
<< e.Message << std::endl;
return 1;
}
return 0;
}
/* cSpell:disable */
std::string GetPemCertificate()
{
static std::string pemCertificate
= "-----BEGIN CERTIFICATE-----\n"
"MIIDqzCCApMCFC+MROpib4t03Wqzgkcod1lad6JtMA0GCSqGSIb3DQEBCwUAMI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QDzh5sy7q9RA+GqprC8\n"
"8aUmkaTMXNahPPPJoLOflJ/+QlOt6YZUIn55vmicVsvFzr9hbxdTW7aQS91iAu05\n"
"swEyltsR0my7FXsHZnN4SBct2FimAzMLTWQr10vLLRoSR5CNpUdoXGWFOAa3LKrZ\n"
"aPJEM1hA3h2XDfZ7Gtxjg4ypIQKBgQDnRl9pGwd83MkoxT4CiZvNbvdBg4lXlHcA\n"
"JoZ9OfoOey+7WRsOFsMvQapXf+JlvixP0ldECXZyxifswvfmiR2oqYTeRbITderg\n"
"mwjDjN571Ui0ls5HwCBE+/iZoNmQI5INAPqsQMXwW0rx4YNXHblsJ0qT+3yFNWOF\n"
"m6STMH8Y+QKBgFai8JivB1nICrleMdQWF43gFIPLp2OXPpeFf0GPa1fWGtTtFifK\n"
"WbpP/gFYc4f8pGMyVVcHcqxlAO5EYka7ovpvZqIxfRMVcj5QuVWaN/zMUcVFsBwe\n"
"PTvHjSRL+FF2ejuaCAxdipRZOTJjRqivyDhxF72EB3zcr8pd5PfWLe1hAoGASJRO\n"
"JvcDj4zeWDwmLLewvHTBhb7Y4DJIcjSk6jHCpr7ECQB6vB4qnO73nUQV8aYP0/EH\n"
"z+NEV9qV9vhswd1wAFlKyFKJAxBzaI9e3becrrINghb9n4jM17lXmCbhgBmZoRkY\n"
"kew18itERspl5HYAlc9y2SQIPOm3VNu2dza1/EkCgYEAlTMyL6arbtJJsygzVn8l\n"
"gKHuURwp1cxf6hUuXKJ56xI/I1OZjMidZM0bYSznmK9SGNxlfNbIV8vNhQfiwR6t\n"
"HyGypSRP+h9MS9E66boXyINaOClZqiCn0pI9aiIpl3D6EbT6e7+zKljT0XmZJduK\n"
"BkRGMfUngiT8oVyaMtZWYPM=\n"
"-----END PRIVATE KEY-----\n";
return pemCertificate;
};
std::string GetPkcsCertificate()
{
static std::string pkcsCertificate
= "MIIJ6QIBAzCCCa8GCSqGSIb3DQEHAaCCCaAEggmcMIIJmDCCBE8GCSqGSIb3DQEH"
"BqC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pUFNOo"
"jpzdtv9qGuifnqtjp/1BlJOYTtzgAbq7YIoNw74oWS2j9qf4N+MdxIQIWp5EUmKc"
"PLn+J1KhHwtkO3hqPBKPV5lA0xL1s/OCUCP1oPnhz+VKCm2tj9lRhzmLbRdntbLv"
"D8ZsMSUwIwYJKoZIhvcNAQkVMRYEFBbpBK9fRSneUhgx9SL/t04nnPfiMDEwITAJ"
"BgUrDgMCGgUABBQ3xckfQUCgNMIXxUvrEUKgdeV8lQQIAPCuS/4UMrICAggA";
return pkcsCertificate;
};
/* cSpell:enable */

View File

@ -0,0 +1,15 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required (VERSION 3.13)
project (certificate-basic-operations LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable (
certificate-basic-operations
certificate-basic-operations.cpp
)
target_link_libraries(certificate-basic-operations PRIVATE azure-security-keyvault-certificates azure-identity )

View File

@ -0,0 +1,97 @@
# Creating, getting, updating, and deleting certificates
This sample demonstrates how to :
* create a certificate
* get a certificate
* update a certificate
* delete a certificate
* purge a certificate
in Azure Key Vault.
To get started, you'll need a URI to an Azure Key Vault.
## Creating a CertificateClient
To create a new `CertificateClient` to create, get, update, or delete certificates, you need the endpoint to an Azure Key Vault and credentials.
Key Vault Certificate client for C++ currently supports the `ClientSecretCredential` for authenticating.
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
```cpp Snippet:CertificateSample1CreateCredential
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential = std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
```
Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application.
```cpp Snippet:CertificateSample1Client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
```
## Creating a Certificate
Call StartCreateCertificate to create a new certificate, with specified properties and policy.
```cpp Snippet:CertificateSample1Create
std::string certificateName = "Sample1";
CertificateCreateParameters params;
...
// start the create process
auto response = certificateClient.StartCreateCertificate(certificateName, params);
auto result = response.PollUntilDone(defaultWait);
// check that the operation completed
while (!response.IsCompleted())
{
response.UpdateProperties();
std::this_thread::sleep_for(defaultWait);
}
```
## Getting a Certificate
Call GetCertificate to retrieve a certificate from Key Vault.
```cpp Snippet:CertificateSample1Get
// get the certificate
certificate = certificateClient.GetCertificate(certificateName).Value;
std::cout << "Created certificate with policy. Certificate name : " << certificate.Name();
```
## Updating certificate properties
Call UpdateCertificateProperties to change one of the certificate properties.
```cpp Snippet:CertificateSample1UpdateCertificateProperties
CertificateUpdateOptions updateOptions;
updateOptions.Properties = certificate.Properties;
updateOptions.Properties.Enabled = false;
auto updatedCertificate = certificateClient.UpdateCertificateProperties(updateOptions).Value;
std::cout << "After update certificate is enabled : "
<< (updatedCertificate.Properties.Enabled.Value() ? "true" : "false");
```
## Deleting a Certificate
Call StartDeleteCertificate to delete a certificate. This is a long running operation.
```cpp Snippet:CertificateSample1Delete
auto response = certificateClient.StartDeleteCertificate(certificateName);
```
## Purging a deleted certificate
If the Azure Key Vault is soft delete-enabled and you want to permanently delete the certificate before its `ScheduledPurgeDate`, the certificate needs to be purged.
```cpp Snippet:CertificateSample1Purge
auto result = response.PollUntilDone(defaultWait);
certificateClient.PurgeDeletedCertificate(certificateName);
```

View File

@ -0,0 +1,115 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @brief This sample provides the code implementation to use the Key Vault Certificates SDK client
* for C++ to create, get, update, delete and purge a certificate.
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_KEYVAULT_URL: To the Key Vault account URL.
* - AZURE_TENANT_ID: Tenant ID for the Azure account.
* - AZURE_CLIENT_ID: The Client ID to authenticate the request.
* - AZURE_CLIENT_SECRET: The client secret.
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_certificates.hpp>
#include <chrono>
#include <iostream>
using namespace Azure::Security::KeyVault::Certificates;
using namespace std::chrono_literals;
int main()
{
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential
= std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
std::chrono::milliseconds defaultWait(10s);
// create client
CertificateClient certificateClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
try
{
std::string certificateName = "Sample1";
KeyVaultCertificateWithPolicy certificate;
CertificateCreateParameters params;
// setup certificate create properties/policy
{
// create a lifetime action
LifetimeAction action;
action.LifetimePercentage = 80;
action.Action = CertificatePolicyAction::AutoRenew;
// etu properties
params.Properties.Enabled = true;
params.Properties.Name = certificateName;
// setup policy
params.Policy.Subject = "CN=sample1";
params.Policy.ValidityInMonths = 12;
params.Policy.Enabled = true;
params.Policy.ContentType = CertificateContentType::Pkcs12;
params.Policy.IssuerName = "Self";
// add a lifetime action
params.Policy.LifetimeActions.emplace_back(action);
}
// create a certificate
{
// start the create process
auto response = certificateClient.StartCreateCertificate(certificateName, params);
auto result = response.PollUntilDone(defaultWait);
// check that the operation completed
while (!response.IsCompleted())
{
response.UpdateProperties();
std::this_thread::sleep_for(defaultWait);
}
// get the certificate
certificate = certificateClient.GetCertificate(certificateName).Value;
std::cout << "Created certificate with policy. Certificate name : " << certificate.Name();
}
// update certificate
{
std::cout << "Certificate is enabled : "
<< (certificate.Properties.Enabled.Value() ? "true" : "false");
CertificateUpdateOptions updateOptions;
updateOptions.Properties = certificate.Properties;
updateOptions.Properties.Enabled = false;
auto updatedCertificate = certificateClient.UpdateCertificateProperties(updateOptions).Value;
std::cout << "After update certificate is enabled : "
<< (updatedCertificate.Properties.Enabled.Value() ? "true" : "false");
}
// delete the certificate
{
auto response = certificateClient.StartDeleteCertificate(certificateName);
auto result = response.PollUntilDone(defaultWait);
certificateClient.PurgeDeletedCertificate(certificateName);
}
}
catch (Azure::Core::Credentials::AuthenticationException const& e)
{
std::cout << "Authentication Exception happened:" << std::endl << e.what() << std::endl;
return 1;
}
catch (Azure::Core::RequestFailedException const& e)
{
std::cout << "Key Vault Certificate Client Exception happened:" << std::endl
<< e.Message << std::endl;
return 1;
}
return 0;
}

View File

@ -15,7 +15,7 @@
{
"name": "azure-core-cpp",
"default-features": false,
"version>=": "1.1.0"
"version>=": "1.2.0"
},
{
"name": "vcpkg-cmake",