std::string get_caller_id()

in api/src/gmsa_service.cpp [2510:2561]


std::string get_caller_id( std::string region, Aws::Auth::AWSCredentials credentials )
{
    std::string callerId = "";
    Aws::SDKOptions options;
    try
    {
        Aws::InitAPI( options );
        {
            Aws::Client::ClientConfiguration clientConfig;
            clientConfig.region = region;
            auto provider = Aws::MakeShared<Aws::Auth::SimpleAWSCredentialsProvider>( "alloc-tag",
                                                                                      credentials );
            auto creds = provider->GetAWSCredentials();
            if ( creds.IsEmpty() )
            {
                std::cerr << Util::getCurrentTime() << '\t'
                          << "ERROR: Failed authentication invalid creds" << std::endl;
                return std::string( "" );
            }
            std::smatch arn_match;

            Aws::STS::STSClient stsClient( credentials,
                                           Aws::MakeShared<Aws::STS::STSEndpointProvider>(
                                               Aws::STS::STSClient::ALLOCATION_TAG ),
                                           clientConfig );
            Aws::STS::Model::GetCallerIdentityRequest request;

            auto outcome = stsClient.GetCallerIdentity( request );

            if ( !outcome.IsSuccess() )
            {
                const Aws::STS::STSError& err = outcome.GetError();
                std::cerr << Util::getCurrentTime() << '\t'
                          << "ERROR: retrieving caller info failed:" << err.GetExceptionName()
                          << ": " << err.GetMessage() << std::endl;
                return std::string( "" );
            }
            callerId = outcome.GetResult().GetAccount();
        }
    }
    catch ( ... )
    {
        std::cerr << Util::getCurrentTime() << '\t'
                  << "ERROR: retrieving caller id "
                     "failed"
                  << std::endl;
        return std::string( "" );
    }
    std::cerr << Util::getCurrentTime() << '\t' << "INFO: successfully retrieved callerId"
              << std::endl;
    return callerId;
}