Show More
@@ -18,14 +18,17 b'' | |||
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | import time | |
|
21 | 22 | import logging |
|
22 | 23 | |
|
23 | 24 | from pyramid.exceptions import ConfigurationError |
|
24 | 25 | from zope.interface import implementer |
|
25 | 26 | |
|
26 | 27 | from rhodecode.authentication.interface import IAuthnPluginRegistry |
|
28 | from rhodecode.model.settings import SettingsModel | |
|
27 | 29 | from rhodecode.lib.utils2 import safe_str |
|
28 |
from rhodecode. |
|
|
30 | from rhodecode.lib.statsd_client import StatsdClient | |
|
31 | from rhodecode.lib import rc_cache | |
|
29 | 32 | |
|
30 | 33 | log = logging.getLogger(__name__) |
|
31 | 34 | |
@@ -38,7 +41,6 b' class AuthenticationPluginRegistry(objec' | |||
|
38 | 41 | |
|
39 | 42 | def __init__(self, settings): |
|
40 | 43 | self._plugins = {} |
|
41 | self._plugins_for_auth = None | |
|
42 | 44 | self._fallback_plugin = settings.get(self.fallback_plugin_key, None) |
|
43 | 45 | |
|
44 | 46 | def add_authn_plugin(self, config, plugin): |
@@ -64,44 +66,56 b' class AuthenticationPluginRegistry(objec' | |||
|
64 | 66 | if plugin.uid == plugin_uid: |
|
65 | 67 | return plugin |
|
66 | 68 | |
|
67 |
def |
|
|
68 | log.debug('Invalidating cached plugins for authentication') | |
|
69 | self._plugins_for_auth = None | |
|
70 | ||
|
71 | def get_plugins_for_authentication(self): | |
|
69 | def get_plugins_for_authentication(self, cache=True): | |
|
72 | 70 | """ |
|
73 | 71 | Returns a list of plugins which should be consulted when authenticating |
|
74 | 72 | a user. It only returns plugins which are enabled and active. |
|
75 | 73 | Additionally it includes the fallback plugin from the INI file, if |
|
76 | 74 | `rhodecode.auth_plugin_fallback` is set to a plugin ID. |
|
77 | 75 | """ |
|
78 | if self._plugins_for_auth is not None: | |
|
79 | return self._plugins_for_auth | |
|
76 | ||
|
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 | # auth_plugins setting from DB because it also represents the ordering. | |
|
85 | enabled_plugins = SettingsModel().get_auth_plugins() | |
|
86 |
raw_settings = SettingsModel().get_all_settings(cache= |
|
|
87 | for plugin_id in enabled_plugins: | |
|
88 | plugin = self.get_plugin(plugin_id) | |
|
89 | if plugin is not None and plugin.is_active( | |
|
90 | plugin_cached_settings=raw_settings): | |
|
84 | # Add all enabled and active plugins to the list. We iterate over the | |
|
85 | # auth_plugins setting from DB because it also represents the ordering. | |
|
86 | enabled_plugins = SettingsModel().get_auth_plugins() | |
|
87 | raw_settings = SettingsModel().get_all_settings(cache=False) | |
|
88 | for plugin_id in enabled_plugins: | |
|
89 | plugin = self.get_plugin(plugin_id) | |
|
90 | if plugin is not None and plugin.is_active( | |
|
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 | |
|
93 | plugin._settings = plugin._propagate_settings(raw_settings) | |
|
94 |
|
|
|
97 | # Add the fallback plugin from ini file. | |
|
98 | if fallback_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. | |
|
97 | if self._fallback_plugin: | |
|
98 | log.warn( | |
|
99 | 'Using fallback authentication plugin from INI file: "%s"', | |
|
100 | self._fallback_plugin) | |
|
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) | |
|
108 | start = time.time() | |
|
109 | plugins = _get_auth_plugins('rhodecode_auth_plugins', 'v1', self._fallback_plugin) | |
|
110 | ||
|
111 | compute_time = time.time() - start | |
|
112 | log.debug('cached method:%s took %.4fs', _get_auth_plugins.func_name, compute_time) | |
|
105 | 113 | |
|
106 | self._plugins_for_auth = plugins | |
|
107 | return self._plugins_for_auth | |
|
114 | statsd = StatsdClient.statsd | |
|
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 | 218 | return region, cache_key |
|
219 | 219 | |
|
220 | 220 | def invalidate_settings_cache(self): |
|
221 | from rhodecode.authentication.base import get_authn_registry | |
|
222 | ||
|
223 | 221 | region, cache_key = self.get_cache_region() |
|
224 | 222 | log.debug('Invalidation cache region %s for cache_key: %s', region, cache_key) |
|
225 | 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 | 225 | def get_all_settings(self, cache=False, from_request=True): |
|
233 | 226 | # defines if we use GLOBAL, or PER_REPO |
@@ -260,6 +253,7 b' class SettingsModel(BaseModel):' | |||
|
260 | 253 | start = time.time() |
|
261 | 254 | result = _get_all_settings('rhodecode_settings', cache_key) |
|
262 | 255 | compute_time = time.time() - start |
|
256 | log.debug('cached method:%s took %.4fs', _get_all_settings.func_name, compute_time) | |
|
263 | 257 | |
|
264 | 258 | statsd = StatsdClient.statsd |
|
265 | 259 | if statsd: |
General Comments 0
You need to be logged in to leave comments.
Login now