caching.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _CACHING_HPP__
  14. #define _CACHING_HPP__
  15. #pragma warning(disable:4786)
  16. #include "jliball.hpp"
  17. #include "seclib.hpp"
  18. #undef new
  19. #include <map>
  20. #include <string>
  21. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  22. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  23. #endif
  24. using std::pair;
  25. using std::map;
  26. using std::multimap;
  27. using std::string;
  28. //Define type of cache entry stored for each resource (in each user specific cache).
  29. //This is a pair of timestamp when this was fetched and the permission itself.
  30. //
  31. typedef pair<time_t, ISecResource*> ResPermCacheEntry;
  32. typedef pair<string, SecResourceType> SecCacheKeyEntry;
  33. //this a cache for a given user that stores permissions for individual resources
  34. //along with their timestamps when they were fetched. The cache is periodically
  35. //cleaned up to remove stale (older than 5 minutes) entries - triggered by a lookup
  36. //itself. Note that each user has an instance of this cache, which is stored in
  37. //another map (CPermissionsCache) as defined below.
  38. //
  39. //
  40. // CPermissionsCache(user) -> CResPermissionsCache|(resource) -> pair<timeout, permission>
  41. // |(timestamp)->resource
  42. class CResPermissionsCache
  43. {
  44. public:
  45. CResPermissionsCache(class CPermissionsCache* parentCache, const char* user)
  46. : m_pParentCache(parentCache), m_user(user)
  47. {
  48. time( &m_tLastCleanup );
  49. }
  50. virtual ~CResPermissionsCache();
  51. //finds cached permissions for a number of resources and sets them in
  52. //and also returns status in the boolean array passed in
  53. //
  54. virtual int lookup( IArrayOf<ISecResource>& resources, bool* found );
  55. //fetch permissions from resources passed in and store them in the cache
  56. //
  57. virtual void add( IArrayOf<ISecResource>& resources );
  58. virtual void remove(SecResourceType rtype, const char* resourcename);
  59. private:
  60. //removes entries older than tstamp passed in
  61. //
  62. virtual void removeStaleEntries(time_t tstamp);
  63. //type definitions
  64. //define mapping from resource name to pair<timeout, permission>
  65. //
  66. //typedef map<string, ResPermCacheEntry> MapResAccess;
  67. typedef map<SecCacheKeyEntry, ResPermCacheEntry> MapResAccess;
  68. //define mapping from timeout to resource name (used for cleanup)
  69. //
  70. typedef multimap<time_t, SecCacheKeyEntry> MapTimeStamp;
  71. //attributes
  72. time_t m_tLastCleanup; //last time the cache was cleaned up
  73. MapResAccess m_resAccessMap; //map of resource to pair<timeout, permission>
  74. MapTimeStamp m_timestampMap; //map of timeout to resource name
  75. string m_user;
  76. class CPermissionsCache* m_pParentCache;
  77. };
  78. class CachedUser
  79. {
  80. private:
  81. Owned<ISecUser> m_user;
  82. time_t m_timestamp;
  83. public:
  84. CachedUser(ISecUser* user)
  85. {
  86. if(!user)
  87. throw MakeStringException(-1, "can't create CachedUser, NULL user pointer");
  88. m_user.setown(user);
  89. const char* pw = user->credentials().getPassword();
  90. if(pw && *pw)
  91. {
  92. StringBuffer pbuf(pw), md5pbuf;
  93. md5_string(pbuf, md5pbuf);
  94. user->credentials().setPassword(md5pbuf.str());
  95. }
  96. time(&m_timestamp);
  97. }
  98. time_t getTimestamp()
  99. {
  100. return m_timestamp;
  101. }
  102. ISecUser* queryUser()
  103. {
  104. return m_user.get();
  105. }
  106. void setTimeStamp(time_t timestamp)
  107. {
  108. m_timestamp = timestamp;
  109. }
  110. };
  111. // main cache that stores all user-specific caches (defined by CResPermissionsCache above)
  112. //
  113. class CPermissionsCache : public CInterface, implements IInterface
  114. {
  115. public:
  116. IMPLEMENT_IINTERFACE
  117. CPermissionsCache(const char * _secMgrClass = nullptr)
  118. {
  119. m_cacheTimeout = 300;
  120. m_transactionalEnabled = false;
  121. m_secMgr = NULL;
  122. m_lastManagedFileScopesRefresh = 0;
  123. m_defaultPermission = SecAccess_Unknown;
  124. m_secMgrClass.set(_secMgrClass);
  125. }
  126. virtual ~CPermissionsCache();
  127. //Returns an owned reference to a shared cache of a given Sec Mgr class type.
  128. //Call this method with a unique class string ("LDAP", "MyOtherSecMgr")
  129. //to create a cache shared amongst security managers of the same class
  130. static CPermissionsCache* getInstance(const char * _secMgrClass);
  131. //finds cached permissions for a number of resources and sets them in
  132. //and also returns status in the boolean array passed in
  133. //
  134. virtual int lookup( ISecUser& sec_user, IArrayOf<ISecResource>& resources, bool* found );
  135. //fetch permissions from resources passed in and store them in the cache
  136. //
  137. virtual void add ( ISecUser& sec_user, IArrayOf<ISecResource>& resources );
  138. virtual void removePermissions( ISecUser& sec_user);
  139. virtual void remove (SecResourceType rtype, const char* resourcename);
  140. virtual bool lookup( ISecUser& sec_user);
  141. virtual ISecUser* getCachedUser( ISecUser& sec_user);
  142. virtual void add (ISecUser& sec_user);
  143. virtual void removeFromUserCache(ISecUser& sec_user);
  144. void setCacheTimeout(int timeout) { m_cacheTimeout = timeout; }
  145. const int getCacheTimeout() { return m_cacheTimeout; }
  146. bool isCacheEnabled() { return m_cacheTimeout > 0; }
  147. void setTransactionalEnabled(bool enable) { m_transactionalEnabled = enable; }
  148. bool isTransactionalEnabled() { return m_transactionalEnabled;}
  149. void flush();
  150. bool addManagedFileScopes(IArrayOf<ISecResource>& scopes);
  151. void removeManagedFileScopes(IArrayOf<ISecResource>& scopes);
  152. void removeAllManagedFileScopes();
  153. bool queryPermsManagedFileScope(ISecUser& sec_user, const char * fullScope, StringBuffer& managedScope, int * accessFlags);
  154. void setSecManager(ISecManager * secMgr) { m_secMgr = secMgr; }
  155. int queryDefaultPermission(ISecUser& user);
  156. private:
  157. typedef std::map<string, CResPermissionsCache*> MapResPermissionsCache;
  158. typedef std::map<string, CachedUser*> MapUserCache;
  159. MapResPermissionsCache m_resPermissionsMap; //user specific resource permissions cache
  160. mutable ReadWriteLock m_resPermCacheRWLock; //guards m_resPermissionsMap
  161. int m_cacheTimeout; //cleanup cycle period
  162. bool m_transactionalEnabled;
  163. MapUserCache m_userCache;
  164. mutable ReadWriteLock m_userCacheRWLock; //guards m_userCache
  165. StringAttr m_secMgrClass;
  166. //Managed File Scope support
  167. int m_defaultPermission;
  168. map<string, ISecResource*> m_managedFileScopesMap;
  169. mutable ReadWriteLock m_scopesRWLock;//guards m_managedFileScopesMap
  170. ISecManager * m_secMgr;
  171. time_t m_lastManagedFileScopesRefresh;
  172. };
  173. time_t getThreadCreateTime();
  174. #endif