2022-10-07 15:58:01 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
2022-10-07 15:58:01 -04: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-07 15:58:01 -04:00
*
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.
*
2022-10-07 15:58:01 -04:00
* 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>
2022-10-07 15:58:01 -04:00
# 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.
2022-10-07 15:58:01 -04:00
/*!
\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 ) {
2022-10-07 15:58:01 -04:00
Aws : : IAM : : IAMClient iam ( clientConfig ) ;
Aws : : IAM : : Model : : ListAccessKeysRequest request ;
request . SetUserName ( userName ) ;
2021-11-15 13:15:43 +00:00
2022-10-07 15:58:01 -04:00
bool done = false ;
bool header = false ;
2022-10-12 14:52:10 -04:00
while ( ! done ) {
2022-10-07 15:58:01 -04:00
auto outcome = iam . ListAccessKeys ( request ) ;
2022-10-12 14:52:10 -04:00
if ( ! outcome . IsSuccess ( ) ) {
2022-10-07 15:58:01 -04:00
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 ) {
2022-10-07 15:58:01 -04:00
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 ) {
2022-10-07 15:58:01 -04:00
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 ( ) ) {
2022-10-07 15:58:01 -04:00
request . SetMarker ( outcome . GetResult ( ) . GetMarker ( ) ) ;
}
2022-10-12 14:52:10 -04:00
else {
2022-10-07 15:58:01 -04:00
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
*/
2022-10-07 15:58:01 -04:00
# ifndef TESTING_BUILD
2021-11-15 13:15:43 +00:00
int main ( int argc , char * * argv )
{
if ( argc ! = 2 )
{
2022-10-07 15:58:01 -04:00
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 ] ) ;
2022-10-07 15:58:01 -04:00
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 ;
}
2022-10-07 15:58:01 -04:00
# endif // TESTING_BUILD