-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathcertificate_get_certificates.cpp
More file actions
170 lines (154 loc) · 6.03 KB
/
certificate_get_certificates.cpp
File metadata and controls
170 lines (154 loc) · 6.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
*
* @brief This sample provides examples 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.
*
*/
#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;
KeyVaultCertificateWithPolicy CreateCertificate(
std::string const& certificateName,
CertificateClient const& certificateClient);
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 certificateName1 = "SampleCertificate1";
std::string certificateName2 = "SampleCertificate2";
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)
{
CertificateCreateOptions options;
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
options.Properties.Enabled = true;
// 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
{
options.Properties.Name = certificateName;
// 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.HasValue()
&& pollResponse.Status.Value() == "completed")
{
// get the certificate
auto certificate = certificateClient.GetCertificate(certificateName).Value;
std::cout << "Created certificate with policy. Certificate name : " << certificate.Name();
return certificate;
}
else
{
throw std::runtime_error(
"Create certificate with policy result : " + pollResponse.Status.Value());
}
}
}