caching.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  114. {
  115. public:
  116. CPermissionsCache()
  117. {
  118. m_cacheTimeout = 300;
  119. m_transactionalEnabled = false;
  120. m_secMgr = NULL;
  121. m_lastManagedFileScopesRefresh = 0;
  122. m_defaultPermission = SecAccess_Unknown;
  123. }
  124. virtual ~CPermissionsCache();
  125. //finds cached permissions for a number of resources and sets them in
  126. //and also returns status in the boolean array passed in
  127. //
  128. virtual int lookup( ISecUser& sec_user, IArrayOf<ISecResource>& resources, bool* found );
  129. //fetch permissions from resources passed in and store them in the cache
  130. //
  131. virtual void add ( ISecUser& sec_user, IArrayOf<ISecResource>& resources );
  132. virtual void removePermissions( ISecUser& sec_user);
  133. virtual void remove (SecResourceType rtype, const char* resourcename);
  134. virtual bool lookup( ISecUser& sec_user);
  135. virtual ISecUser* getCachedUser( ISecUser& sec_user);
  136. virtual void add (ISecUser& sec_user);
  137. virtual void removeFromUserCache(ISecUser& sec_user);
  138. void setCacheTimeout(int timeout) { m_cacheTimeout = timeout; }
  139. const int getCacheTimeout() { return m_cacheTimeout; }
  140. bool isCacheEnabled() { return m_cacheTimeout > 0; }
  141. void setTransactionalEnabled(bool enable) { m_transactionalEnabled = enable; }
  142. bool isTransactionalEnabled() { return m_transactionalEnabled;}
  143. void flush();
  144. bool addManagedFileScopes(IArrayOf<ISecResource>& scopes);
  145. void removeManagedFileScopes(IArrayOf<ISecResource>& scopes);
  146. void removeAllManagedFileScopes();
  147. bool queryPermsManagedFileScope(ISecUser& sec_user, const char * fullScope, StringBuffer& managedScope, int * accessFlags);
  148. void setSecManager(ISecManager * secMgr) { m_secMgr = secMgr; }
  149. int queryDefaultPermission(ISecUser& user);
  150. private:
  151. typedef std::map<string, CResPermissionsCache*> MapResPermissionsCache;
  152. typedef std::map<string, CachedUser*> MapUserCache;
  153. CPermissionsCache(const CPermissionsCache&);
  154. MapResPermissionsCache m_resPermissionsMap; //user specific resource permissions cache
  155. Monitor m_cachemonitor; //for thread safety
  156. int m_cacheTimeout; //cleanup cycle period
  157. bool m_transactionalEnabled;
  158. MapUserCache m_userCache;
  159. Monitor m_userCacheMonitor;
  160. //Managed File Scope support
  161. int m_defaultPermission;
  162. map<string, ISecResource*> m_managedFileScopesMap;
  163. Monitor m_managedFileScopesCacheMonitor;
  164. ISecManager * m_secMgr;
  165. time_t m_lastManagedFileScopesRefresh;
  166. };
  167. time_t getThreadCreateTime();
  168. #endif