##// END OF EJS Templates
auth-plugins: remove the in-object cache that cauused invalidation failures....
super-admin -
r4885:2768c2d4 default
parent child Browse files
Show More
@@ -18,14 +18,17 b''
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import time
21 import logging
22 import logging
22
23
23 from pyramid.exceptions import ConfigurationError
24 from pyramid.exceptions import ConfigurationError
24 from zope.interface import implementer
25 from zope.interface import implementer
25
26
26 from rhodecode.authentication.interface import IAuthnPluginRegistry
27 from rhodecode.authentication.interface import IAuthnPluginRegistry
28 from rhodecode.model.settings import SettingsModel
27 from rhodecode.lib.utils2 import safe_str
29 from rhodecode.lib.utils2 import safe_str
28 from rhodecode.model.settings import SettingsModel
30 from rhodecode.lib.statsd_client import StatsdClient
31 from rhodecode.lib import rc_cache
29
32
30 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
31
34
@@ -38,7 +41,6 b' class AuthenticationPluginRegistry(objec'
38
41
39 def __init__(self, settings):
42 def __init__(self, settings):
40 self._plugins = {}
43 self._plugins = {}
41 self._plugins_for_auth = None
42 self._fallback_plugin = settings.get(self.fallback_plugin_key, None)
44 self._fallback_plugin = settings.get(self.fallback_plugin_key, None)
43
45
44 def add_authn_plugin(self, config, plugin):
46 def add_authn_plugin(self, config, plugin):
@@ -64,44 +66,56 b' class AuthenticationPluginRegistry(objec'
64 if plugin.uid == plugin_uid:
66 if plugin.uid == plugin_uid:
65 return plugin
67 return plugin
66
68
67 def invalidate_plugins_for_auth(self):
69 def get_plugins_for_authentication(self, cache=True):
68 log.debug('Invalidating cached plugins for authentication')
69 self._plugins_for_auth = None
70
71 def get_plugins_for_authentication(self):
72 """
70 """
73 Returns a list of plugins which should be consulted when authenticating
71 Returns a list of plugins which should be consulted when authenticating
74 a user. It only returns plugins which are enabled and active.
72 a user. It only returns plugins which are enabled and active.
75 Additionally it includes the fallback plugin from the INI file, if
73 Additionally it includes the fallback plugin from the INI file, if
76 `rhodecode.auth_plugin_fallback` is set to a plugin ID.
74 `rhodecode.auth_plugin_fallback` is set to a plugin ID.
77 """
75 """
78 if self._plugins_for_auth is not None:
76
79 return self._plugins_for_auth
77 cache_namespace_uid = 'cache_auth_plugins'
78 region = rc_cache.get_or_create_region('cache_general', cache_namespace_uid)
80
79
81 plugins = []
80 @region.conditional_cache_on_arguments(condition=cache)
81 def _get_auth_plugins(name, key, fallback_plugin):
82 plugins = []
82
83
83 # Add all enabled and active plugins to the list. We iterate over the
84 # Add all enabled and active plugins to the list. We iterate over the
84 # auth_plugins setting from DB because it also represents the ordering.
85 # auth_plugins setting from DB because it also represents the ordering.
85 enabled_plugins = SettingsModel().get_auth_plugins()
86 enabled_plugins = SettingsModel().get_auth_plugins()
86 raw_settings = SettingsModel().get_all_settings(cache=True)
87 raw_settings = SettingsModel().get_all_settings(cache=False)
87 for plugin_id in enabled_plugins:
88 for plugin_id in enabled_plugins:
88 plugin = self.get_plugin(plugin_id)
89 plugin = self.get_plugin(plugin_id)
89 if plugin is not None and plugin.is_active(
90 if plugin is not None and plugin.is_active(
90 plugin_cached_settings=raw_settings):
91 plugin_cached_settings=raw_settings):
92
93 # inject settings into plugin, we can re-use the DB fetched settings here
94 plugin._settings = plugin._propagate_settings(raw_settings)
95 plugins.append(plugin)
91
96
92 # inject settings into plugin, we can re-use the DB fetched settings here
97 # Add the fallback plugin from ini file.
93 plugin._settings = plugin._propagate_settings(raw_settings)
98 if fallback_plugin:
94 plugins.append(plugin)
99 log.warn(
100 'Using fallback authentication plugin from INI file: "%s"',
101 fallback_plugin)
102 plugin = self.get_plugin(fallback_plugin)
103 if plugin is not None and plugin not in plugins:
104 plugin._settings = plugin._propagate_settings(raw_settings)
105 plugins.append(plugin)
106 return plugins
95
107
96 # Add the fallback plugin from ini file.
108 start = time.time()
97 if self._fallback_plugin:
109 plugins = _get_auth_plugins('rhodecode_auth_plugins', 'v1', self._fallback_plugin)
98 log.warn(
110
99 'Using fallback authentication plugin from INI file: "%s"',
111 compute_time = time.time() - start
100 self._fallback_plugin)
112 log.debug('cached method:%s took %.4fs', _get_auth_plugins.func_name, compute_time)
101 plugin = self.get_plugin(self._fallback_plugin)
102 if plugin is not None and plugin not in plugins:
103 plugin._settings = plugin._propagate_settings(raw_settings)
104 plugins.append(plugin)
105
113
106 self._plugins_for_auth = plugins
114 statsd = StatsdClient.statsd
107 return self._plugins_for_auth
115 if statsd:
116 elapsed_time_ms = round(1000.0 * compute_time) # use ms only
117 statsd.timing("rhodecode_auth_plugins_timing.histogram", elapsed_time_ms,
118 use_decimals=False)
119
120 return plugins
121
@@ -218,16 +218,9 b' class SettingsModel(BaseModel):'
218 return region, cache_key
218 return region, cache_key
219
219
220 def invalidate_settings_cache(self):
220 def invalidate_settings_cache(self):
221 from rhodecode.authentication.base import get_authn_registry
222
223 region, cache_key = self.get_cache_region()
221 region, cache_key = self.get_cache_region()
224 log.debug('Invalidation cache region %s for cache_key: %s', region, cache_key)
222 log.debug('Invalidation cache region %s for cache_key: %s', region, cache_key)
225 region.invalidate()
223 region.invalidate()
226 registry = get_current_registry()
227 if registry:
228 authn_registry = get_authn_registry(registry)
229 if authn_registry:
230 authn_registry.invalidate_plugins_for_auth()
231
224
232 def get_all_settings(self, cache=False, from_request=True):
225 def get_all_settings(self, cache=False, from_request=True):
233 # defines if we use GLOBAL, or PER_REPO
226 # defines if we use GLOBAL, or PER_REPO
@@ -260,6 +253,7 b' class SettingsModel(BaseModel):'
260 start = time.time()
253 start = time.time()
261 result = _get_all_settings('rhodecode_settings', cache_key)
254 result = _get_all_settings('rhodecode_settings', cache_key)
262 compute_time = time.time() - start
255 compute_time = time.time() - start
256 log.debug('cached method:%s took %.4fs', _get_all_settings.func_name, compute_time)
263
257
264 statsd = StatsdClient.statsd
258 statsd = StatsdClient.statsd
265 if statsd:
259 if statsd:
General Comments 0
You need to be logged in to leave comments. Login now