caching.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. //removes entries older than tstamp passed in
  60. //
  61. virtual void removeStaleEntries(time_t tstamp);
  62. virtual bool needsCleanup(time_t now, unsigned timeout)
  63. {
  64. return m_tLastCleanup < (now - timeout);
  65. }
  66. private:
  67. //type definitions
  68. //define mapping from resource name to pair<timeout, permission>
  69. //
  70. //typedef map<string, ResPermCacheEntry> MapResAccess;
  71. typedef map<SecCacheKeyEntry, ResPermCacheEntry> MapResAccess;
  72. //define mapping from timeout to resource name (used for cleanup)
  73. //
  74. typedef multimap<time_t, SecCacheKeyEntry> MapTimeStamp;
  75. //attributes
  76. time_t m_tLastCleanup; //last time the cache was cleaned up
  77. MapResAccess m_resAccessMap; //map of resource to pair<timeout, permission>
  78. MapTimeStamp m_timestampMap; //map of timeout to resource name
  79. string m_user;
  80. class CPermissionsCache* m_pParentCache;
  81. };
  82. class CachedUser
  83. {
  84. private:
  85. Owned<ISecUser> m_user;
  86. time_t m_timestamp;
  87. public:
  88. CachedUser(ISecUser* user)
  89. {
  90. if(!user)
  91. throw MakeStringException(-1, "can't create CachedUser, NULL user pointer");
  92. m_user.setown(user);
  93. time(&m_timestamp);
  94. }
  95. time_t getTimestamp()
  96. {
  97. return m_timestamp;
  98. }
  99. ISecUser* queryUser()
  100. {
  101. return m_user.get();
  102. }
  103. void setTimeStamp(time_t timestamp)
  104. {
  105. m_timestamp = timestamp;
  106. }
  107. };
  108. // main cache that stores all user-specific caches (defined by CResPermissionsCache above)
  109. //
  110. #define DEFAULT_CACHE_TIMEOUT_SECONDS 10
  111. class CPermissionsCache : public CInterface
  112. {
  113. public:
  114. CPermissionsCache(const char * _secMgrClass = nullptr)
  115. {
  116. m_cacheTimeout = 300;
  117. m_transactionalEnabled = false;
  118. m_secMgr = NULL;
  119. m_lastManagedFileScopesRefresh = 0;
  120. m_defaultPermission = SecAccess_Unknown;
  121. m_secMgrClass.set(_secMgrClass);
  122. m_transactionalCacheTimeout = DEFAULT_CACHE_TIMEOUT_SECONDS;
  123. }
  124. virtual ~CPermissionsCache();
  125. //Returns an owned reference to a shared cache of a given Sec Mgr class type.
  126. //Call this method with a unique class string ("LDAP", "MyOtherSecMgr")
  127. //to create a cache shared amongst security managers of the same class
  128. static CPermissionsCache* getInstance(const char * _secMgrClass);
  129. //finds cached permissions for a number of resources and sets them in
  130. //and also returns status in the boolean array passed in
  131. //
  132. virtual int lookup( ISecUser& sec_user, IArrayOf<ISecResource>& resources, bool* found );
  133. //fetch permissions from resources passed in and store them in the cache
  134. //
  135. virtual void add ( ISecUser& sec_user, IArrayOf<ISecResource>& resources );
  136. virtual void removePermissions( ISecUser& sec_user);
  137. virtual void remove (SecResourceType rtype, const char* resourcename);
  138. virtual bool lookup( ISecUser& sec_user);
  139. virtual ISecUser* getCachedUser( ISecUser& sec_user);
  140. virtual void add (ISecUser& sec_user);
  141. virtual void removeFromUserCache(ISecUser& sec_user);
  142. void setCacheTimeout(int timeoutSeconds)
  143. {
  144. m_cacheTimeout = timeoutSeconds;
  145. if(m_cacheTimeout == 0 && isTransactionalEnabled())//ensure transactional time is updated
  146. setTransactionalCacheTimeout(DEFAULT_CACHE_TIMEOUT_SECONDS); //Transactional timeout is set to 10 seconds for long transactions that might take over 10 seconds.
  147. else
  148. setTransactionalCacheTimeout(timeoutSeconds);
  149. }
  150. const int getCacheTimeout() { return m_cacheTimeout; }
  151. bool isCacheEnabled() { return m_cacheTimeout > 0; }
  152. void setTransactionalEnabled(bool enable)
  153. {
  154. m_transactionalEnabled = enable;
  155. if(getCacheTimeout() == 0 && enable)//ensure transactional time is updated
  156. setTransactionalCacheTimeout(DEFAULT_CACHE_TIMEOUT_SECONDS); //Transactional timeout is set to 10 seconds for long transactions that might take over 10 seconds.
  157. else
  158. setTransactionalCacheTimeout(getCacheTimeout());
  159. }
  160. void setTransactionalCacheTimeout(int timeoutSeconds) { m_transactionalCacheTimeout = timeoutSeconds; }
  161. const int getTransactionalCacheTimeout() { return m_transactionalCacheTimeout; }
  162. bool isTransactionalEnabled() { return m_transactionalEnabled;}
  163. void flush();
  164. bool addManagedFileScopes(IArrayOf<ISecResource>& scopes);
  165. void removeManagedFileScopes(IArrayOf<ISecResource>& scopes);
  166. void removeAllManagedFileScopes();
  167. bool queryPermsManagedFileScope(ISecUser& sec_user, const char * fullScope, StringBuffer& managedScope, SecAccessFlags * accessFlags);
  168. void setSecManager(ISecManager * secMgr) { m_secMgr = secMgr; }
  169. SecAccessFlags queryDefaultPermission(ISecUser& user);
  170. private:
  171. typedef std::map<string, CResPermissionsCache*> MapResPermissionsCache;
  172. typedef std::map<string, CachedUser*> MapUserCache;
  173. MapResPermissionsCache m_resPermissionsMap; //user specific resource permissions cache
  174. mutable ReadWriteLock m_resPermCacheRWLock; //guards m_resPermissionsMap
  175. int m_cacheTimeout; //cleanup cycle period
  176. bool m_transactionalEnabled;
  177. int m_transactionalCacheTimeout;
  178. MapUserCache m_userCache;
  179. mutable ReadWriteLock m_userCacheRWLock; //guards m_userCache
  180. StringAttr m_secMgrClass;
  181. //Managed File Scope support
  182. SecAccessFlags m_defaultPermission;
  183. map<string, ISecResource*> m_managedFileScopesMap;
  184. mutable ReadWriteLock m_scopesRWLock;//guards m_managedFileScopesMap
  185. ISecManager * m_secMgr;
  186. time_t m_lastManagedFileScopesRefresh;
  187. };
  188. time_t getThreadCreateTime();
  189. #endif