diff --git a/services/tunnelbroker/src/Tools/AwsTools.h b/services/tunnelbroker/src/Tools/AwsTools.h --- a/services/tunnelbroker/src/Tools/AwsTools.h +++ b/services/tunnelbroker/src/Tools/AwsTools.h @@ -10,7 +10,7 @@ namespace comm { namespace network { -Aws::String getAwsRegion(); +Aws::String getAwsRegionFromCredentials(); } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/AwsTools.cpp b/services/tunnelbroker/src/Tools/AwsTools.cpp --- a/services/tunnelbroker/src/Tools/AwsTools.cpp +++ b/services/tunnelbroker/src/Tools/AwsTools.cpp @@ -1,19 +1,38 @@ #include "AwsTools.h" #include "Constants.h" +#include + +#include + namespace comm { namespace network { -Aws::String getAwsRegion() { - auto profileName = Aws::Auth::GetConfigProfileName(); - if (Aws::Config::HasCachedConfigProfile(profileName)) { - auto profile = Aws::Config::GetCachedConfigProfile(profileName); - Aws::String profileRegion = profile.GetRegion(); - if (!profileRegion.empty()) { - return profileRegion; - } +Aws::String getAwsRegionFromCredentials() { + const std::string regionParameter = "default.region"; + const std::string awsCredentialsPath = "~/.aws/credentials"; + std::ifstream fileStream; + fileStream.open(awsCredentialsPath, std::ifstream::in); + if (!fileStream.is_open()) { + throw std::runtime_error( + "Error: can not open AWS credentials file " + awsCredentialsPath); } - return {}; + + boost::program_options::options_description optionDescription{ + "AWS credentials options"}; + optionDescription.add_options()( + regionParameter.c_str(), + boost::program_options::value(), + "AWS region"); + + boost::program_options::parsed_options parsedDescription = + boost::program_options::parse_config_file( + fileStream, optionDescription, true); + boost::program_options::variables_map variablesMap; + boost::program_options::store(parsedDescription, variablesMap); + boost::program_options::notify(variablesMap); + fileStream.close(); + return variablesMap[regionParameter].as(); } } // namespace network