diff --git a/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.h b/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.h index d101b7201..76cb7814d 100644 --- a/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.h +++ b/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.h @@ -1,34 +1,33 @@ #pragma once #include "Constants.h" #include "DeliveryBrokerEntites.h" #include -#include #include namespace comm { namespace network { class DeliveryBroker { folly::ConcurrentHashMap> messagesMap; public: static DeliveryBroker &getInstance(); void push( const std::string messageID, const uint64_t deliveryTag, const std::string toDeviceID, const std::string fromDeviceID, const std::string payload); bool isEmpty(const std::string deviceID); DeliveryBrokerMessage pop(const std::string deviceID); void erase(const std::string deviceID); void deleteQueueIfEmpty(const std::string clientDeviceID); }; } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.h b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.h index 87f0c025c..fb2cda96f 100644 --- a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.h +++ b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.h @@ -1,43 +1,42 @@ #pragma once #include <_generated/tunnelbroker.grpc.pb.h> #include <_generated/tunnelbroker.pb.h> #include -#include #include namespace comm { namespace network { class TunnelBrokerServiceImpl final : public tunnelbroker::TunnelbrokerService::Service { public: TunnelBrokerServiceImpl(); virtual ~TunnelBrokerServiceImpl(); grpc::Status SessionSignature( grpc::ServerContext *context, const tunnelbroker::SessionSignatureRequest *request, tunnelbroker::SessionSignatureResponse *reply) override; grpc::Status NewSession( grpc::ServerContext *context, const tunnelbroker::NewSessionRequest *request, tunnelbroker::NewSessionResponse *reply) override; grpc::Status Send( grpc::ServerContext *context, const tunnelbroker::SendRequest *request, google::protobuf::Empty *reply) override; grpc::Status Get(grpc::ServerContext *context, const tunnelbroker::GetRequest *request, grpc::ServerWriter *stream) override; }; } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/ConfigManager.h b/services/tunnelbroker/src/Tools/ConfigManager.h index 93fff090d..f19763c05 100644 --- a/services/tunnelbroker/src/Tools/ConfigManager.h +++ b/services/tunnelbroker/src/Tools/ConfigManager.h @@ -1,33 +1,32 @@ #pragma once #include -#include #include namespace comm { namespace network { namespace config { class ConfigManager { private: boost::program_options::variables_map variablesMap; public: static const std::string OPTION_TUNNELBROKER_ID; static const std::string OPTION_DEFAULT_KEYSERVER_ID; static const std::string OPTION_AMQP_URI; static const std::string OPTION_AMQP_FANOUT_EXCHANGE; static const std::string OPTION_DYNAMODB_SESSIONS_TABLE; static const std::string OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE; static const std::string OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE; static const std::string OPTION_DYNAMODB_MESSAGES_TABLE; static ConfigManager &getInstance(); void load(const std::string configFilePath); std::string getParameter(std::string param); }; } // namespace config } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/CryptoTools.cpp b/services/tunnelbroker/src/Tools/CryptoTools.cpp index db395b8b7..b702c7290 100644 --- a/services/tunnelbroker/src/Tools/CryptoTools.cpp +++ b/services/tunnelbroker/src/Tools/CryptoTools.cpp @@ -1,47 +1,45 @@ #include "CryptoTools.h" #include #include #include #include #include #include #include -#include - namespace comm { namespace network { namespace crypto { bool rsaVerifyString( const std::string &publicKeyBase64, const std::string &message, const std::string &signatureBase64) { CryptoPP::RSA::PublicKey publicKey; std::string decodedSignature; try { publicKey.Load(CryptoPP::StringSource( publicKeyBase64, true, new CryptoPP::Base64Decoder()) .Ref()); CryptoPP::StringSource stringSource( signatureBase64, true, new CryptoPP::Base64Decoder( new CryptoPP::StringSink(decodedSignature))); CryptoPP::RSASSA_PKCS1v15_SHA_Verifier verifierSha256(publicKey); return verifierSha256.VerifyMessage( reinterpret_cast(message.c_str()), message.length(), reinterpret_cast(decodedSignature.c_str()), decodedSignature.length()); } catch (const std::exception &e) { LOG(ERROR) << "CryptoTools: " << "Got an exception " << e.what(); return false; } } } // namespace crypto } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/Tools.cpp b/services/tunnelbroker/src/Tools/Tools.cpp index d02578dd6..576fa3711 100644 --- a/services/tunnelbroker/src/Tools/Tools.cpp +++ b/services/tunnelbroker/src/Tools/Tools.cpp @@ -1,72 +1,71 @@ #include "Tools.h" #include "ConfigManager.h" #include "Constants.h" #include #include #include -#include #include #include namespace comm { namespace network { namespace tools { std::string generateRandomString(std::size_t length) { const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; thread_local std::random_device generator; std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1); std::string random_string; for (std::size_t i = 0; i < length; ++i) { random_string += CHARACTERS[distribution(generator)]; } return random_string; } bool validateDeviceID(std::string deviceID) { try { static const std::regex deviceIDKeyserverRegexp("^ks:.*"); if (std::regex_match(deviceID, deviceIDKeyserverRegexp)) { return ( deviceID == config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DEFAULT_KEYSERVER_ID)); } return std::regex_match(deviceID, DEVICEID_FORMAT_REGEX); } catch (const std::exception &e) { LOG(ERROR) << "Tools: " << "Got an exception at `validateDeviceID`: " << e.what(); return false; } } bool validateSessionID(std::string sessionID) { try { return std::regex_match(sessionID, SESSION_ID_FORMAT_REGEX); } catch (const std::exception &e) { LOG(ERROR) << "Tools: " << "Got an exception at `validateSessionId`: " << e.what(); return false; } } void checkIfNotEmpty(std::string fieldName, std::string stringToCheck) { if (stringToCheck.empty()) { throw std::runtime_error( "Error: Required text field " + fieldName + " is empty."); } } void checkIfNotZero(std::string fieldName, uint64_t numberToCheck) { if (numberToCheck == 0) { throw std::runtime_error( "Error: Required number " + fieldName + " is zero."); } } } // namespace tools } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/server.cpp b/services/tunnelbroker/src/server.cpp index 8802f16dc..80feedd69 100644 --- a/services/tunnelbroker/src/server.cpp +++ b/services/tunnelbroker/src/server.cpp @@ -1,55 +1,54 @@ #include "AmqpManager.h" #include "ConfigManager.h" #include "Constants.h" #include "GlobalTools.h" #include "TunnelbrokerServiceImpl.h" #include #include -#include #include #include namespace comm { namespace network { void RunServer() { TunnelBrokerServiceImpl service; grpc::EnableDefaultHealthCheckService(true); grpc::ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort( SERVER_LISTEN_ADDRESS, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); std::unique_ptr server(builder.BuildAndStart()); LOG(INFO) << "gRPC Server listening at :" << SERVER_LISTEN_ADDRESS; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); } void RunAmqpClient() { AmqpManager::getInstance().connect(); } } // namespace network } // namespace comm int main(int argc, char **argv) { comm::network::tools::InitLogging("tunnelbroker"); if (comm::network::tools::isSandbox()) { comm::network::config::ConfigManager::getInstance().load( comm::network::DEV_CONFIG_FILE_PATH); } else { comm::network::config::ConfigManager::getInstance().load( comm::network::CONFIG_FILE_PATH); } std::thread amqpThread(comm::network::RunAmqpClient); std::thread grpcThread(comm::network::RunServer); amqpThread.join(); grpcThread.join(); return 0; }