-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathcertificate_basic_operations.cpp
More file actions
115 lines (104 loc) · 3.85 KB
/
certificate_basic_operations.cpp
File metadata and controls
115 lines (104 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @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.
*
*/
#include <azure/identity.hpp>
#include <azure/keyvault/certificates.hpp>
#include <chrono>
#include <iostream>
#include <pipeline_auth_helper.hpp>
#include <thread>
using namespace Azure::Security::KeyVault::Certificates;
using namespace std::chrono_literals;
int main()
{
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
auto credential = PipelineAuthHelper::GetSampleCredentials();
std::chrono::milliseconds defaultWait(10s);
// create client
CertificateClient certificateClient(keyVaultUrl, credential);
try
{
std::string certificateName = "Sample1";
KeyVaultCertificateWithPolicy certificate;
CertificateCreateOptions options;
// setup certificate create properties/policy
{
// create a lifetime action
LifetimeAction action;
action.LifetimePercentage = 80;
action.Action = CertificatePolicyAction::AutoRenew;
// etu properties
options.Properties.Enabled = true;
options.Properties.Name = certificateName;
// setup policy
options.Policy.Subject = "CN=sample1";
options.Policy.ValidityInMonths = 12;
options.Policy.Enabled = true;
options.Policy.ContentType = CertificateContentType::Pkcs12;
options.Policy.IssuerName = "Self";
// add a lifetime action
options.Policy.LifetimeActions.emplace_back(action);
}
// create a certificate
{
// start the create process
auto response = certificateClient.StartCreateCertificate(certificateName, options);
// wait for complete to get the certificate
auto pollResponse = response.PollUntilDone(defaultWait).Value;
// check the status of the poll response
if (!pollResponse.Error && pollResponse.Status.Value() == "completed")
{
// get the certificate
certificate = certificateClient.GetCertificate(certificateName).Value;
std::cout << "Created certificate with policy. Certificate name : " << certificate.Name();
}
else
{
std::cout << "Create certificate with policy result : " << pollResponse.Status.Value();
}
}
// update certificate
{
std::cout << "Certificate is enabled : "
<< (certificate.Properties.Enabled.Value() ? "true" : "false");
CertificateProperties updateOptions;
updateOptions = certificate.Properties;
updateOptions.Enabled = false;
auto updatedCertificate
= certificateClient
.UpdateCertificateProperties(
certificateName, certificate.Properties.Version, updateOptions)
.Value;
std::cout << "After update certificate is enabled : "
<< (updatedCertificate.Properties.Enabled.HasValue()
&& 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;
}