SIGN IN SIGN UP

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.

0 0 42 Java
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
/**
* Before running this C++ code example, set up your development environment,
* including your credentials.
*
* For more information, see the following documentation topic:
2022-10-14 09:27:14 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html.
*
2022-10-12 14:52:10 -04:00
* 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.
*
* Purpose
*
* Demonstrates listing all access keys associated with an IAM user.
*
*/
2021-11-15 13:15:43 +00:00
// snippet-start:[iam.cpp.list_access_keys.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/ListAccessKeysRequest.h>
#include <aws/iam/model/ListAccessKeysResult.h>
#include <iomanip>
#include <iostream>
#include "iam_samples.h"
2021-11-15 13:15:43 +00:00
// snippet-end:[iam.cpp.list_access_keys.inc]
2022-10-12 14:52:10 -04:00
//! Lists all access keys associated with an IAM user.
/*!
\sa listAccessKeys()
\param userName: The user's name.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
2022-10-12 14:52:10 -04:00
// snippet-start:[iam.cpp.list_access_keys.code]
bool AwsDoc::IAM::listAccessKeys(const Aws::String &userName,
const Aws::Client::ClientConfiguration &clientConfig) {
Aws::IAM::IAMClient iam(clientConfig);
Aws::IAM::Model::ListAccessKeysRequest request;
request.SetUserName(userName);
2021-11-15 13:15:43 +00:00
bool done = false;
bool header = false;
2022-10-12 14:52:10 -04:00
while (!done) {
auto outcome = iam.ListAccessKeys(request);
2022-10-12 14:52:10 -04:00
if (!outcome.IsSuccess()) {
std::cerr << "Failed to list access keys for user " << userName
<< ": " << outcome.GetError().GetMessage() << std::endl;
return false;
}
2022-10-12 14:52:10 -04:00
if (!header) {
std::cout << std::left << std::setw(32) << "UserName" <<
std::setw(30) << "KeyID" << std::setw(20) << "Status" <<
std::setw(20) << "CreateDate" << std::endl;
header = true;
}
const auto &keys = outcome.GetResult().GetAccessKeyMetadata();
const Aws::String DATE_FORMAT = "%Y-%m-%d";
2022-10-12 14:52:10 -04:00
for (const auto &key: keys) {
Aws::String statusString =
Aws::IAM::Model::StatusTypeMapper::GetNameForStatusType(
key.GetStatus());
std::cout << std::left << std::setw(32) << key.GetUserName() <<
std::setw(30) << key.GetAccessKeyId() << std::setw(20) <<
statusString << std::setw(20) <<
key.GetCreateDate().ToGmtString(DATE_FORMAT.c_str()) << std::endl;
}
2022-10-12 14:52:10 -04:00
if (outcome.GetResult().GetIsTruncated()) {
request.SetMarker(outcome.GetResult().GetMarker());
}
2022-10-12 14:52:10 -04:00
else {
done = true;
}
}
return true;
}
// snippet-end:[iam.cpp.list_access_keys.code]
/*
*
* main function
*
* Usage: 'run_list_access_keys <user_name>'
*
2021-11-15 13:15:43 +00:00
*/
#ifndef TESTING_BUILD
2021-11-15 13:15:43 +00:00
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: run_list_access_keys <user_name>" << std::endl;
2021-11-15 13:15:43 +00:00
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String userName(argv[1]);
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc::IAM::listAccessKeys(userName, clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD