// 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 #include #include #include "acm_samples.h" // snippet-start:[cpp.example_code.acm.ImportCertificate] //! Import an AWS Certificate Manager (ACM) certificate. /*! \param certificateFile: Path to certificate to import. \param privateKeyFile: Path to file containing a private key. \param certificateChainFile: Path to file containing a PEM encoded certificate chain. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::ACM::importCertificate(const Aws::String &certificateFile, const Aws::String &privateKeyFile, const Aws::String &certificateChainFile, const Aws::Client::ClientConfiguration &clientConfiguration) { std::ifstream certificateInStream(certificateFile.c_str()); if (!certificateInStream) { std::cerr << "Error: The certificate file '" << certificateFile << "' does not exist." << std::endl; return false; } std::ifstream privateKeyInstream(privateKeyFile.c_str()); if (!privateKeyInstream) { std::cerr << "Error: The private key file '" << privateKeyFile << "' does not exist." << std::endl; return false; } std::ifstream certificateChainInStream(certificateChainFile.c_str()); if (!certificateChainInStream) { std::cerr << "Error: The certificate chain file '" << certificateChainFile << "' does not exist." << std::endl; return false; } Aws::String certificate; certificate.assign(std::istreambuf_iterator(certificateInStream), std::istreambuf_iterator()); Aws::String privateKey; privateKey.assign(std::istreambuf_iterator(privateKeyInstream), std::istreambuf_iterator()); Aws::String certificateChain; certificateChain.assign(std::istreambuf_iterator(certificateChainInStream), std::istreambuf_iterator()); Aws::ACM::ACMClient acmClient(clientConfiguration); Aws::ACM::Model::ImportCertificateRequest request; request.WithCertificate(Aws::Utils::ByteBuffer((unsigned char *) certificate.c_str(), certificate.size())) .WithPrivateKey(Aws::Utils::ByteBuffer((unsigned char *) privateKey.c_str(), privateKey.size())) .WithCertificateChain(Aws::Utils::ByteBuffer((unsigned char *) certificateChain.c_str(), certificateChain.size())); Aws::ACM::Model::ImportCertificateOutcome outcome = acmClient.ImportCertificate(request); if (!outcome.IsSuccess()) { std::cerr << "Error: ImportCertificate: " << outcome.GetError().GetMessage() << std::endl; return false; } else { std::cout << "Success: Certificate associated with ARN '" << outcome.GetResult().GetCertificateArn() << "' imported." << std::endl; return true; } } // snippet-end:[cpp.example_code.acm.ImportCertificate] /* * * main function * * Usage: 'run_import_certificate ' * */ #ifndef TESTING_BUILD int main(int argc, char **argv) { if (argc != 4) { std::cout << "Usage: 'run_import_certificate '" << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String certificateFile = argv[1]; Aws::String privateKeyFile = argv[2]; Aws::String certificateChainFile = argv[3]; Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; AwsDoc::ACM::importCertificate(certificateFile, privateKeyFile, certificateChainFile, clientConfig); } Aws::ShutdownAPI(options); return 0; } #endif // TESTING_BUILD