forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_keys_and_certificate.cpp
More file actions
134 lines (116 loc) · 5.14 KB
/
create_keys_and_certificate.cpp
File metadata and controls
134 lines (116 loc) · 5.14 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
#include <aws/core/Aws.h>
#include <aws/iot/IoTClient.h>
#include <aws/iot/model/CreateKeysAndCertificateRequest.h>
#include <iostream>
#include <fstream>
#include "iot_samples.h"
// snippet-start:[cpp.example_code.iot.CreateKeysAndCertificate]
//! Create keys and certificate for an Aws IoT device.
//! This routine will save certificates and keys to an output folder, if provided.
/*!
\param outputFolder: Location for storing output in files, ignored when string is empty.
\param certificateARNResult: A string to receive the ARN of the created certificate.
\param certificateID: A string to receive the ID of the created certificate.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::IoT::createKeysAndCertificate(const Aws::String &outputFolder,
Aws::String &certificateARNResult,
Aws::String &certificateID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::IoT::IoTClient client(clientConfiguration);
Aws::IoT::Model::CreateKeysAndCertificateRequest createKeysAndCertificateRequest;
Aws::IoT::Model::CreateKeysAndCertificateOutcome outcome =
client.CreateKeysAndCertificate(createKeysAndCertificateRequest);
if (outcome.IsSuccess()) {
std::cout << "Successfully created a certificate and keys" << std::endl;
certificateARNResult = outcome.GetResult().GetCertificateArn();
certificateID = outcome.GetResult().GetCertificateId();
std::cout << "Certificate ARN: " << certificateARNResult << ", certificate ID: "
<< certificateID << std::endl;
if (!outputFolder.empty()) {
std::cout << "Writing certificate and keys to the folder '" << outputFolder
<< "'." << std::endl;
std::cout << "Be sure these files are stored securely." << std::endl;
Aws::String certificateFilePath = outputFolder + "/certificate.pem.crt";
std::ofstream certificateFile(certificateFilePath);
if (!certificateFile.is_open()) {
std::cerr << "Error opening certificate file, '" << certificateFilePath
<< "'."
<< std::endl;
return false;
}
certificateFile << outcome.GetResult().GetCertificatePem();
certificateFile.close();
const Aws::IoT::Model::KeyPair &keyPair = outcome.GetResult().GetKeyPair();
Aws::String privateKeyFilePath = outputFolder + "/private.pem.key";
std::ofstream privateKeyFile(privateKeyFilePath);
if (!privateKeyFile.is_open()) {
std::cerr << "Error opening private key file, '" << privateKeyFilePath
<< "'."
<< std::endl;
return false;
}
privateKeyFile << keyPair.GetPrivateKey();
privateKeyFile.close();
Aws::String publicKeyFilePath = outputFolder + "/public.pem.key";
std::ofstream publicKeyFile(publicKeyFilePath);
if (!publicKeyFile.is_open()) {
std::cerr << "Error opening public key file, '" << publicKeyFilePath
<< "'."
<< std::endl;
return false;
}
publicKeyFile << keyPair.GetPublicKey();
}
}
else {
std::cerr << "Error creating keys and certificate: "
<< outcome.GetError().GetMessage() << std::endl;
}
return outcome.IsSuccess();
}
// snippet-end:[cpp.example_code.iot.CreateKeysAndCertificate]
/*
*
* main function
*
* Usage: 'run_create_keys_and_certificate <output_folder>'
*
*/
#ifndef EXCLUDE_ACTION_MAIN
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: run_create_keys_and_certificate <output_folder>'"
<< std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String outputFolder(argv[1]);
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws::String certificateARN;
Aws::String certificateID;
AwsDoc::IoT::createKeysAndCertificate(outputFolder, certificateARN,
certificateID, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_ACTION_MAIN