Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.
|
|
// 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 <iostream>
|
||
|
|
#include <aws/core/Aws.h>
|
||
|
|
#include <aws/secretsmanager/SecretsManagerClient.h>
|
||
|
|
#include <aws/secretsmanager/model/GetSecretValueRequest.h>
|
||
|
|
#include "secretsmanager_samples.h"
|
||
|
|
|
||
|
|
// snippet-start:[cpp.example_code.secrets_manager.get_secret_value]
|
||
|
|
//! Retrieve an AWS Secrets Manager encrypted secret.
|
||
|
|
/*!
|
||
|
|
\param secretID: The ID for the secret.
|
||
|
|
\return bool: Function succeeded.
|
||
|
|
*/
|
||
|
|
bool AwsDoc::SecretsManager::getSecretValue(const Aws::String &secretID,
|
||
|
|
const Aws::Client::ClientConfiguration &clientConfiguration) {
|
||
|
|
Aws::SecretsManager::SecretsManagerClient secretsManagerClient(clientConfiguration);
|
||
|
|
|
||
|
|
Aws::SecretsManager::Model::GetSecretValueRequest request;
|
||
|
|
request.SetSecretId(secretID);
|
||
|
|
|
||
|
|
Aws::SecretsManager::Model::GetSecretValueOutcome getSecretValueOutcome = secretsManagerClient.GetSecretValue(
|
||
|
|
request);
|
||
|
|
if (getSecretValueOutcome.IsSuccess()) {
|
||
|
|
std::cout << "Secret is: "
|
||
|
|
<< getSecretValueOutcome.GetResult().GetSecretString() << std::endl;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
std::cerr << "Failed with Error: " << getSecretValueOutcome.GetError()
|
||
|
|
<< std::endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
return getSecretValueOutcome.IsSuccess();
|
||
|
|
}
|
||
|
|
// snippet-end:[cpp.example_code.secrets_manager.get_secret_value]
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
* main function
|
||
|
|
*
|
||
|
|
* Usage: 'Usage: run_get_secret_value <secret_id>'
|
||
|
|
*
|
||
|
|
* Prerequisites: A SecretsManager secret.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
#ifndef TESTING_BUILD
|
||
|
|
|
||
|
|
int main(int argc, char **argv) {
|
||
|
|
if (argc < 2) {
|
||
|
|
std::cout << "Usage: run_get_secret_value <secret_id>";
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
Aws::SDKOptions options;
|
||
|
|
Aws::InitAPI(options);
|
||
|
|
{
|
||
|
|
Aws::String secretId(argv[1]);
|
||
|
|
|
||
|
|
Aws::Client::ClientConfiguration clientConfig;
|
||
|
|
// Optional: Set to the AWS Region (overrides config file).
|
||
|
|
// clientConfig.region = "us-east-1";
|
||
|
|
|
||
|
|
AwsDoc::SecretsManager::getSecretValue(secretId, clientConfig);
|
||
|
|
}
|
||
|
|
|
||
|
|
Aws::ShutdownAPI(options);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif // TESTING_BUILD
|