2022-10-07 15:58:01 -04:00
// 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:
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 putting an inline permissions policy on an IAM role.
*
*/
2019-02-08 13:51:58 -08:00
# include <aws/core/Aws.h>
# include <aws/iam/IAMClient.h>
# include <aws/iam/model/PutRolePolicyRequest.h>
# include <iostream>
2022-10-07 15:58:01 -04:00
# include "iam_samples.h"
2019-02-08 13:51:58 -08:00
2022-10-07 15:58:01 -04:00
//! Puts an inline permissions policy on an IAM role.
/*!
2022-10-12 14:52:10 -04:00
\sa putRolePolicy()
2022-10-07 15:58:01 -04:00
\param roleName: The IAM role name.
\param policyName: The policy name.
\param policyDocument: The policy document JSON string.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
2022-10-20 15:00:56 -04:00
// snippet-start:[iam.cpp.put_role_policy.code]
2022-10-07 15:58:01 -04:00
bool AwsDoc : : IAM : : putRolePolicy (
2022-10-12 14:52:10 -04:00
const Aws : : String & roleName ,
const Aws : : String & policyName ,
const Aws : : String & policyDocument ,
const Aws : : Client : : ClientConfiguration & clientConfig ) {
2022-10-07 15:58:01 -04:00
Aws : : IAM : : IAMClient iamClient ( clientConfig ) ;
Aws : : IAM : : Model : : PutRolePolicyRequest request ;
2019-02-08 13:51:58 -08:00
2022-10-07 15:58:01 -04:00
request . SetRoleName ( roleName ) ;
request . SetPolicyName ( policyName ) ;
request . SetPolicyDocument ( policyDocument ) ;
2019-02-08 13:51:58 -08:00
2022-10-07 15:58:01 -04:00
Aws : : IAM : : Model : : PutRolePolicyOutcome outcome = iamClient . PutRolePolicy ( request ) ;
2022-10-12 14:52:10 -04:00
if ( ! outcome . IsSuccess ( ) ) {
std : : cerr < < " Error putting policy on role. " < <
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2019-02-08 14:17:38 -08:00
}
2022-10-12 14:52:10 -04:00
else {
2022-10-07 15:58:01 -04:00
std : : cout < < " Successfully put the role policy. " < < std : : endl ;
}
return outcome . IsSuccess ( ) ;
2019-02-08 13:51:58 -08:00
}
2022-10-20 15:00:56 -04:00
// snippet-end:[iam.cpp.put_role_policy.code]
2019-02-08 13:51:58 -08:00
2022-10-07 15:58:01 -04:00
/*
*
* main function
*
* Prerequisites: An existing IAM role.
*
* Usage: 'run_put_role_policy <roleName> <policyName>'
*
2019-02-08 13:51:58 -08:00
*/
2022-10-07 15:58:01 -04:00
# ifndef TESTING_BUILD
int main ( int argc , char * * argv )
2019-02-08 13:51:58 -08:00
{
2022-10-12 14:52:10 -04:00
if ( argc ! = 3 ) {
2022-10-07 15:58:01 -04:00
std : : cout < < " Usage: run_put_role_policy <roleName> <policyName> " < < std : : endl ;
return 1 ;
}
2019-02-08 14:17:38 -08:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2022-10-20 15:00:56 -04:00
// Set these configuration values before running the program.
Aws : : String roleName = argv [ 1 ] ; // An existing IAM role,
2022-10-07 15:58:01 -04:00
Aws : : String policyName = argv [ 2 ] ;
2019-02-08 13:51:58 -08:00
2022-10-20 15:00:56 -04:00
// Define a permissions policy that enables Amazon S3 ReadOnly access.
2019-02-08 14:17:38 -08:00
Aws : : String permissionsPolicy = R " ({
" Version " : " 2012-10-17 " ,
" Statement " : [
{
" Effect " : " Allow " ,
" Action " : [
" s3:Get* " ,
" s3:List* "
] ,
" Resource " : " * "
}
]
} ) " ;
2019-02-08 13:51:58 -08:00
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";
2022-10-12 14:52:10 -04:00
AwsDoc : : IAM : : putRolePolicy ( roleName , policyName , permissionsPolicy , clientConfig ) ;
2019-02-08 14:17:38 -08:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2019-02-08 13:51:58 -08:00
}
2022-10-07 15:58:01 -04:00
# endif // TESTING_BUILD