diff --git a/services/tunnelbroker/src/Constants.h b/services/tunnelbroker/src/Constants.h index 973e03369..634f4d8b9 100644 --- a/services/tunnelbroker/src/Constants.h +++ b/services/tunnelbroker/src/Constants.h @@ -1,62 +1,64 @@ #pragma once #include #include #include namespace comm { namespace network { // AWS DynamoDB const size_t DYNAMODB_MAX_BATCH_ITEMS = 25; const size_t DYNAMODB_BACKOFF_FIRST_RETRY_DELAY = 50; const size_t DYNAMODB_MAX_BACKOFF_TIME = 10000; // 10 seconds const std::string DEVICE_SESSIONS_TABLE_NAME = "tunnelbroker-device-sessions"; const std::string DEVICE_SESSIONS_VERIFICATION_MESSAGES_TABLE_NAME = "tunnelbroker-verification-messages"; const std::string DEVICE_PUBLIC_KEY_TABLE_NAME = "tunnelbroker-public-keys"; const std::string MESSAGES_TABLE_NAME = "tunnelbroker-messages"; // Sessions const size_t SIGNATURE_REQUEST_LENGTH = 64; const size_t SESSION_ID_LENGTH = 64; const size_t SESSION_RECORD_TTL = 30 * 24 * 3600; // 30 days const size_t SESSION_SIGN_RECORD_TTL = 24 * 3600; // 24 hours const std::regex SESSION_ID_FORMAT_REGEX( "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); // AMQP (RabbitMQ) const std::string AMQP_FANOUT_EXCHANGE_NAME = "allBrokers"; // Message broker queue message TTL const size_t AMQP_MESSAGE_TTL = 300 * 1000; // 5 min // queue TTL in case of no consumers (tunnelbroker is down) const size_t AMQP_QUEUE_TTL = 24 * 3600 * 1000; // 24 hours // routing message headers name const std::string AMQP_HEADER_FROM_DEVICEID = "fromDeviceID"; const std::string AMQP_HEADER_TO_DEVICEID = "toDeviceID"; const std::string AMQP_HEADER_MESSAGEID = "messageID"; const int64_t AMQP_SHORTEST_RECONNECTION_ATTEMPT_INTERVAL = 1000 * 60; // 1 min // DeviceID // DEVICEID_CHAR_LENGTH has to be kept in sync with deviceIDCharLength // which is defined in web/utils/device-id.js const size_t DEVICEID_CHAR_LENGTH = 64; const std::regex DEVICEID_FORMAT_REGEX( "^(ks|mobile|web):[a-zA-Z0-9]{" + std::to_string(DEVICEID_CHAR_LENGTH) + "}$"); // Config -const std::string CONFIG_FILE_PATH = - std::string(std::getenv("HOME")) + "/tunnelbroker/tunnelbroker.ini"; -const std::string DEV_CONFIG_FILE_PATH = - std::string(std::getenv("HOME")) + "/tunnelbroker/tunnelbroker-dev.ini"; +const std::string CONFIG_FILE_DIRECTORY_ENV_VARIABLE = + "TUNNELBROKER_CONFIG_FILE_DIRECTORY"; +const std::string DEFAULT_CONFIG_FILE_DIRECTORY = + std::string(std::getenv("HOME")) + "/tunnelbroker"; +const std::string CONFIG_FILE_NAME = "tunnelbroker.ini"; +const std::string SANDBOX_CONFIG_FILE_NAME = "tunnelbroker-sandbox.ini"; // DeliveryBroker const size_t DELIVERY_BROKER_MAX_QUEUE_SIZE = 100; // Database messages TTL const size_t MESSAGE_RECORD_TTL = 300 * 24 * 60 * 60; // 300 days } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/ConfigManager.cpp b/services/tunnelbroker/src/Tools/ConfigManager.cpp index d61e010d4..fa99894c7 100644 --- a/services/tunnelbroker/src/Tools/ConfigManager.cpp +++ b/services/tunnelbroker/src/Tools/ConfigManager.cpp @@ -1,118 +1,125 @@ #include "ConfigManager.h" #include "Constants.h" #include "GlobalTools.h" #include namespace comm { namespace network { namespace config { const std::string ConfigManager::OPTION_TUNNELBROKER_ID = "tunnelbroker.instance-id"; const std::string ConfigManager::OPTION_DEFAULT_KEYSERVER_ID = "keyserver.default_keyserver_id"; const std::string ConfigManager::OPTION_AMQP_URI = "amqp.uri"; const std::string ConfigManager::OPTION_AMQP_FANOUT_EXCHANGE = "amqp.fanout_exchange_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_TABLE = "dynamodb.sessions_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE = "dynamodb.sessions_verification_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE = "dynamodb.sessions_public_key_table_name"; const std::string ConfigManager::OPTION_DYNAMODB_MESSAGES_TABLE = "dynamodb.messages_table_name"; ConfigManager &ConfigManager::getInstance() { static ConfigManager instance; return instance; } void ConfigManager::load() { + char const *configFileDirectoryFromEnvironment = + std::getenv(CONFIG_FILE_DIRECTORY_ENV_VARIABLE.c_str()); + std::string configFilePath = DEFAULT_CONFIG_FILE_DIRECTORY; + + if (configFileDirectoryFromEnvironment != nullptr) { + configFilePath = std::string{configFileDirectoryFromEnvironment}; + } if (comm::network::tools::isSandbox()) { - loadConfigFile(comm::network::DEV_CONFIG_FILE_PATH); + loadConfigFile(configFilePath + "/" + SANDBOX_CONFIG_FILE_NAME); } else { - loadConfigFile(comm::network::CONFIG_FILE_PATH); + loadConfigFile(configFilePath + "/" + CONFIG_FILE_NAME); } } void ConfigManager::loadConfigFile(const std::string configFilePath) { try { std::ifstream fileStream; fileStream.open(configFilePath.c_str(), std::ifstream::in); if (!fileStream.is_open()) { throw std::runtime_error("Error: can not open file " + configFilePath); } boost::program_options::options_description description{ "Tunnelbroker options"}; description.add_options()( this->OPTION_TUNNELBROKER_ID.c_str(), boost::program_options::value()->required(), "Tunnelbroker unique identification"); description.add_options()( this->OPTION_DEFAULT_KEYSERVER_ID.c_str(), boost::program_options::value()->required(), "Default and only allowed keyserver deviceID"); description.add_options()( this->OPTION_AMQP_URI.c_str(), boost::program_options::value()->required(), "AMQP URI connection string"); description.add_options()( this->OPTION_AMQP_FANOUT_EXCHANGE.c_str(), boost::program_options::value()->default_value( AMQP_FANOUT_EXCHANGE_NAME), "AMQP Fanout exchange name"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_SESSIONS_TABLE_NAME), "DynamoDB table name for sessions"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_SESSIONS_VERIFICATION_MESSAGES_TABLE_NAME), "DynamoDB table name for sessions verification messages"); description.add_options()( this->OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE.c_str(), boost::program_options::value()->default_value( DEVICE_PUBLIC_KEY_TABLE_NAME), "DynamoDB table name for public keys"); description.add_options()( this->OPTION_DYNAMODB_MESSAGES_TABLE.c_str(), boost::program_options::value()->default_value( MESSAGES_TABLE_NAME), "DynamoDB table name for messages"); boost::program_options::parsed_options parsedDescription = boost::program_options::parse_config_file( fileStream, description, true); boost::program_options::store(parsedDescription, this->variablesMap); boost::program_options::notify(this->variablesMap); fileStream.close(); } catch (const std::exception &e) { throw std::runtime_error( "Got an exception at ConfigManager: " + std::string(e.what())); } } std::string ConfigManager::getParameter(std::string param) { if (!this->variablesMap.count(param) && !this->variablesMap[param].defaulted()) { throw std::runtime_error( "ConfigManager Error: config parameter " + param + " is not set."); } const std::string parameterValue = this->variablesMap[param].as(); if (parameterValue.empty()) { throw std::runtime_error( "ConfigManager Error: config parameter " + param + " can not be empty."); } return parameterValue; } } // namespace config } // namespace network } // namespace comm diff --git a/services/tunnelbroker/tunnelbroker-dev.ini b/services/tunnelbroker/tunnelbroker-sandbox.ini similarity index 100% rename from services/tunnelbroker/tunnelbroker-dev.ini rename to services/tunnelbroker/tunnelbroker-sandbox.ini