From a860ec838920ad6def4ca0dcad71e08e3560617c Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 5 Jan 2022 22:24:55 -0800 Subject: [PATCH] Framework update --- build | 2 +- src/RESTAPI/RESTAPI_map_handler.cpp | 8 +-- src/RESTAPI/RESTAPI_map_list_handler.cpp | 2 +- src/RESTObjects/RESTAPI_SecurityObjects.cpp | 4 +- src/RESTObjects/RESTAPI_SecurityObjects.h | 38 +++++++---- src/framework/MicroService.h | 75 ++++++++++++++------- src/framework/orm.h | 51 +++++++++++--- 7 files changed, 126 insertions(+), 54 deletions(-) diff --git a/build b/build index 0fa6a7b..a46c9d2 100644 --- a/build +++ b/build @@ -1 +1 @@ -90 \ No newline at end of file +91 \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_map_handler.cpp b/src/RESTAPI/RESTAPI_map_handler.cpp index 58bf094..1505d3f 100644 --- a/src/RESTAPI/RESTAPI_map_handler.cpp +++ b/src/RESTAPI/RESTAPI_map_handler.cpp @@ -36,7 +36,7 @@ namespace OpenWifi{ return NotFound(); } - if(UserInfo_.userinfo.Id!=Existing.creator) { + if(UserInfo_.userinfo.id!=Existing.creator) { return UnAuthorized("You must be the creator of the map to delete it"); } @@ -66,7 +66,7 @@ namespace OpenWifi{ return BadRequest( RESTAPI::Errors::NameMustBeSet); } - NewObject.creator = UserInfo_.userinfo.Id; + NewObject.creator = UserInfo_.userinfo.id; if(DB_.CreateRecord(NewObject)) { @@ -96,14 +96,14 @@ namespace OpenWifi{ return BadRequest( RESTAPI::Errors::NameMustBeSet); } - if(Existing.creator != UserInfo_.userinfo.Id) { + if(Existing.creator != UserInfo_.userinfo.id) { if(Existing.visibility == ProvObjects::PRIVATE) { return UnAuthorized(RESTAPI::Errors::InsufficientAccessRights, ACCESS_DENIED); } if(Existing.visibility == ProvObjects::SELECT) { for(const auto &i:Existing.access.list) { for(const auto &j:i.users.list) { - if(j==UserInfo_.userinfo.Id) { + if(j==UserInfo_.userinfo.id) { } } } diff --git a/src/RESTAPI/RESTAPI_map_list_handler.cpp b/src/RESTAPI/RESTAPI_map_list_handler.cpp index b819d34..258fe5c 100644 --- a/src/RESTAPI/RESTAPI_map_list_handler.cpp +++ b/src/RESTAPI/RESTAPI_map_list_handler.cpp @@ -10,7 +10,7 @@ namespace OpenWifi{ void RESTAPI_map_list_handler::DoGet() { if(GetBoolParameter("myMaps",false)) { - auto where = StorageService()->MapDB().OP("creator",ORM::EQ,UserInfo_.userinfo.Id); + auto where = StorageService()->MapDB().OP("creator",ORM::EQ,UserInfo_.userinfo.id); std::vector Maps; StorageService()->MapDB().GetRecords(QB_.Offset,QB_.Limit,Maps,where); return MakeJSONObjectArray("list", Maps, *this); diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.cpp b/src/RESTObjects/RESTAPI_SecurityObjects.cpp index 58403b2..c368ee2 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.cpp +++ b/src/RESTObjects/RESTAPI_SecurityObjects.cpp @@ -224,7 +224,7 @@ namespace OpenWifi::SecurityObjects { } void UserInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"Id",Id); + field_to_json(Obj,"id",id); field_to_json(Obj,"name",name); field_to_json(Obj,"description", description); field_to_json(Obj,"avatar", avatar); @@ -258,7 +258,7 @@ namespace OpenWifi::SecurityObjects { bool UserInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"Id",Id); + field_from_json(Obj,"id",id); field_from_json(Obj,"name",name); field_from_json(Obj,"description",description); field_from_json(Obj,"avatar",avatar); diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.h b/src/RESTObjects/RESTAPI_SecurityObjects.h index fd01baa..15a9b4b 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.h +++ b/src/RESTObjects/RESTAPI_SecurityObjects.h @@ -11,10 +11,14 @@ #include #include "framework/OpenWifiTypes.h" #include "Poco/JSON/Object.h" +#include "Poco/Data/LOB.h" +#include "Poco/Data/LOBStream.h" namespace OpenWifi { namespace SecurityObjects { + typedef std::string USER_ID_TYPE; + struct AclTemplate { bool Read_ = true; bool ReadWrite_ = true; @@ -102,32 +106,32 @@ namespace OpenWifi { }; struct UserInfo { - std::string Id; + std::string id; std::string name; std::string description; std::string avatar; std::string email; - bool validated = false; + bool validated = false; std::string validationEmail; - uint64_t validationDate = 0; - uint64_t creationDate = 0; + uint64_t validationDate = 0; + uint64_t creationDate = 0; std::string validationURI; - bool changePassword = false; - uint64_t lastLogin = 0; + bool changePassword = false; + uint64_t lastLogin = 0; std::string currentLoginURI; - uint64_t lastPasswordChange = 0; - uint64_t lastEmailCheck = 0; - bool waitingForEmailCheck = false; + uint64_t lastPasswordChange = 0; + uint64_t lastEmailCheck = 0; + bool waitingForEmailCheck = false; std::string locale; NoteInfoVec notes; std::string location; std::string owner; - bool suspended = false; - bool blackListed = false; - USER_ROLE userRole; + bool suspended = false; + bool blackListed = false; + USER_ROLE userRole; UserLoginLoginExtensions userTypeProprietaryInfo; std::string securityPolicy; - uint64_t securityPolicyChange = 0 ; + uint64_t securityPolicyChange = 0 ; std::string currentPassword; OpenWifi::Types::StringVec lastPasswords; std::string oauthType; @@ -280,5 +284,13 @@ namespace OpenWifi { bool from_json(Poco::JSON::Object::Ptr &Obj); }; + struct Avatar { + std::string id; + std::string type; + uint64_t created=0; + std::string name; + Poco::Data::LOB avatar; + }; + } } diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index c0aaff2..bc25689 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -68,6 +68,7 @@ using namespace std::chrono_literals; #include "Poco/PatternFormatter.h" #include "Poco/FileChannel.h" #include "Poco/SimpleFileChannel.h" +#include "Poco/Util/PropertyFileConfiguration.h" #include "cppkafka/cppkafka.h" @@ -1035,8 +1036,29 @@ namespace OpenWifi { static const std::string uSERVICE_SUBCRIBER{ "owsub"}; static const std::string uSERVICE_INSTALLER{ "owinst"}; + template class RecordCache { + public: + explicit RecordCache( KeyType Record::* Q) : + MemberOffset(Q){ + }; + inline auto update(const Record &R) { + return Cache_.update(R.*MemberOffset, R); + } + inline auto get(const KeyType &K) { + return Cache_.get(K); + } + inline auto remove(const KeyType &K) { + return Cache_.remove(K); + } + inline auto remove(const Record &R) { + return Cache_.remove(R.*MemberOffset); + } + private: + KeyType Record::* MemberOffset; + Poco::ExpireLRUCache Cache_{Size,Expiry}; + }; - class MyErrorHandler : public Poco::ErrorHandler { + class MyErrorHandler : public Poco::ErrorHandler { public: explicit MyErrorHandler(Poco::Util::Application &App) : App_(App) {} inline void exception(const Poco::Exception & E) { @@ -1473,7 +1495,7 @@ namespace OpenWifi { int Count=0; }; - static RESTAPI_RateLimiter *instance() { + static auto instance() { static auto instance_ = new RESTAPI_RateLimiter; return instance_; } @@ -1520,7 +1542,7 @@ namespace OpenWifi { }; - inline RESTAPI_RateLimiter * RESTAPI_RateLimiter() { return RESTAPI_RateLimiter::instance(); } + inline auto RESTAPI_RateLimiter() { return RESTAPI_RateLimiter::instance(); } class RESTAPIHandler : public Poco::Net::HTTPRequestHandler { public: @@ -2224,7 +2246,7 @@ namespace OpenWifi { inline void initialize(Poco::Util::Application & self) override; - static KafkaManager *instance() { + static auto instance() { static auto instance_ = new KafkaManager; return instance_; } @@ -2318,7 +2340,7 @@ namespace OpenWifi { } }; - inline KafkaManager * KafkaManager() { return KafkaManager::instance(); } + inline auto KafkaManager() { return KafkaManager::instance(); } class AuthClient : public SubSystemServer { public: @@ -2327,7 +2349,7 @@ namespace OpenWifi { { } - static AuthClient *instance() { + static auto instance() { static auto instance_ = new AuthClient; return instance_; } @@ -2337,6 +2359,7 @@ namespace OpenWifi { } inline void Stop() override { + std::lock_guard G(Mutex_); Cache_.clear(); } @@ -2366,6 +2389,7 @@ namespace OpenWifi { return false; } Expired = false; + std::lock_guard G(Mutex_); Cache_.update(SessionToken, UInfo); return true; } @@ -2378,6 +2402,7 @@ namespace OpenWifi { } inline bool IsAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, bool & Expired, bool Sub = false) { + std::lock_guard G(Mutex_); auto User = Cache_.get(SessionToken); if(!User.isNull()) { if(IsTokenExpired(User->webtoken)) { @@ -2392,10 +2417,10 @@ namespace OpenWifi { } private: - Poco::ExpireLRUCache Cache_{1024,1200000 }; + Poco::ExpireLRUCache Cache_{512,1200000 }; }; - inline AuthClient * AuthClient() { return AuthClient::instance(); } + inline auto AuthClient() { return AuthClient::instance(); } class ALBRequestHandler: public Poco::Net::HTTPRequestHandler /// Return a HTML document with the current date and time. @@ -2406,7 +2431,7 @@ namespace OpenWifi { { } - void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) + void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) override { Logger_.information(Poco::format("ALB-REQUEST(%s): New ALB request.",Request.clientAddress().toString())); Response.setChunkedTransferEncoding(true); @@ -2451,8 +2476,8 @@ namespace OpenWifi { { } - static ALBHealthCheckServer *instance() { - static ALBHealthCheckServer * instance = new ALBHealthCheckServer; + static auto instance() { + static auto instance = new ALBHealthCheckServer; return instance; } @@ -2470,7 +2495,7 @@ namespace OpenWifi { std::atomic_bool Running_=false; }; - inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } + inline auto ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t Id); @@ -2481,7 +2506,7 @@ namespace OpenWifi { class RESTAPI_ExtServer : public SubSystemServer { public: - static RESTAPI_ExtServer *instance() { + static auto instance() { static auto instance_ = new RESTAPI_ExtServer; return instance_; } @@ -2512,7 +2537,7 @@ namespace OpenWifi { } }; - inline RESTAPI_ExtServer * RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); }; + inline auto RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); }; class ExtRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: @@ -2564,7 +2589,7 @@ namespace OpenWifi { class RESTAPI_IntServer : public SubSystemServer { public: - static RESTAPI_IntServer *instance() { + static auto instance() { static auto instance_ = new RESTAPI_IntServer; return instance_; } @@ -2593,7 +2618,7 @@ namespace OpenWifi { } }; - inline RESTAPI_IntServer * RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); }; + inline auto RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); }; class IntRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: @@ -2648,8 +2673,6 @@ namespace OpenWifi { uint64_t LastUpdate=0; }; - - class SubSystemServer; typedef std::map MicroServiceMetaMap; typedef std::vector MicroServiceMetaVec; @@ -2707,7 +2730,7 @@ namespace OpenWifi { return Poco::Logger::get(Name); } - inline void Exit(int Reason); + static inline void Exit(int Reason); inline void BusMessageReceived(const std::string &Key, const std::string & Message); inline MicroServiceMetaVec GetServices(const std::string & Type); inline MicroServiceMetaVec GetServices(); @@ -2750,7 +2773,8 @@ namespace OpenWifi { inline int main(const ArgVec &args) override; static MicroService & instance() { return *instance_; } inline void InitializeLoggingSystem(); - + inline void SaveConfig() { PropConfigurationFile_->save(ConfigFileName_); } + inline auto UpdateConfig() { return PropConfigurationFile_; } private: static MicroService * instance_; bool HelpRequested_ = false; @@ -2775,7 +2799,7 @@ namespace OpenWifi { BusEventManager BusEventManager_; std::mutex InfraMutex_; std::default_random_engine RandomEngine_; - + Poco::Util::PropertyFileConfiguration * PropConfigurationFile_ = nullptr; std::string DAEMON_PROPERTIES_FILENAME; std::string DAEMON_ROOT_ENV_VAR; std::string DAEMON_CONFIG_ENV_VAR; @@ -2882,9 +2906,8 @@ namespace OpenWifi { inline void MicroService::LoadConfigurationFile() { std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR,"."); - Poco::Path ConfigFile; - - ConfigFile = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; + ConfigFileName_ = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; + Poco::Path ConfigFile(ConfigFileName_); if(!ConfigFile.isFile()) { @@ -2894,7 +2917,9 @@ namespace OpenWifi { std::exit(Poco::Util::Application::EXIT_CONFIG); } - loadConfiguration(ConfigFile.toString()); + // loadConfiguration(ConfigFile.toString()); + PropConfigurationFile_ = new Poco::Util::PropertyFileConfiguration(ConfigFile.toString()); + configPtr()->addWriteable(PropConfigurationFile_, PRIO_DEFAULT); } inline void MicroService::Reload() { diff --git a/src/framework/orm.h b/src/framework/orm.h index e283b92..3b3d38b 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -155,6 +155,20 @@ namespace ORM { return S; } + template class DBCache { + public: + DBCache(unsigned Size, unsigned Timeout) + { + + } + virtual void Create(const RecordType &R)=0; + virtual bool GetFromCache(const std::string &FieldName, const std::string &Value, RecordType &R)=0; + virtual void UpdateCache(const RecordType &R)=0; + virtual void Delete(const std::string &FieldName, const std::string &Value)=0; + private: + + }; + template class DB { public: DB( OpenWifi::DBType dbtype, @@ -162,21 +176,22 @@ namespace ORM { const FieldVec & Fields, const IndexVec & Indexes, Poco::Data::SessionPool & Pool, - Poco::Logger &L, - const char *Prefix): + Poco::Logger &L, + const char *Prefix, + DBCache * Cache=nullptr): Type_(dbtype), DBName_(TableName), Pool_(Pool), Logger_(L), - Prefix_(Prefix) + Prefix_(Prefix), + Cache_(Cache) { + assert(RecordTuple::length == Fields.size()); + bool first = true; int Place=0; - assert( RecordTuple::length == Fields.size()); - for(const auto &i:Fields) { - FieldNames_[i.Name] = Place; if(!first) { CreateFields_ += ", "; @@ -374,7 +389,11 @@ namespace ORM { Insert << ConvertParams(St) , Poco::Data::Keywords::use(RT); Insert.execute(); + + if(Cache_) + Cache_->Create(R); return true; + } catch (const Poco::Exception &E) { Logger_.log(E); } @@ -383,9 +402,13 @@ namespace ORM { template bool GetRecord( const char * FieldName, T Value, RecordType & R) { try { - assert( FieldNames_.find(FieldName) != FieldNames_.end() ); + if(Cache_) { + if(Cache_->GetFromCache(FieldName, Value, R)) + return true; + } + Poco::Data::Session Session = Pool_.get(); Poco::Data::Statement Select(Session); RecordTuple RT; @@ -397,6 +420,8 @@ namespace ORM { Poco::Data::Keywords::use(Value); if(Select.execute()==1) { Convert(RT,R); + if(Cache_) + Cache_->UpdateCache(R); return true; } return false; @@ -481,6 +506,8 @@ namespace ORM { Poco::Data::Keywords::use(RT), Poco::Data::Keywords::use(Value); Update.execute(); + if(Cache_) + Cache_->UpdateCache(R); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -536,6 +563,8 @@ namespace ORM { Delete << ConvertParams(St) , Poco::Data::Keywords::use(Value); Delete.execute(); + if(Cache_) + Cache_->Delete(FieldName, Value); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -785,10 +814,17 @@ namespace ORM { Poco::Logger & Logger() { return Logger_; } + bool DeleteRecordsFromCache(const char *FieldName, const std::string &Value ) { + if(Cache_) + Cache_->Delete(FieldName, Value); + return true; + } + protected: Poco::Data::SessionPool &Pool_; Poco::Logger &Logger_; std::string DBName_; + DBCache *Cache_= nullptr; private: OpenWifi::DBType Type_; std::string CreateFields_; @@ -797,7 +833,6 @@ namespace ORM { std::string UpdateFields_; std::vector IndexCreation_; std::map FieldNames_; - // Poco::Data::SessionPool &Pool_; std::string Prefix_; }; }