##// END OF EJS Templates
authn: Make the legacy plugin import more robust if something fails during import.
johbo -
r134:5edcb8e3 default
parent child Browse files
Show More
@@ -1,118 +1,121 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2012-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
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 21 import logging
22 22 import importlib
23 23
24 24 from pkg_resources import iter_entry_points
25 25 from pyramid.authentication import SessionAuthenticationPolicy
26 26
27 27 from rhodecode.authentication.registry import AuthenticationPluginRegistry
28 28 from rhodecode.authentication.routes import root_factory
29 29 from rhodecode.authentication.routes import AuthnRootResource
30 30 from rhodecode.config.routing import ADMIN_PREFIX
31 31 from rhodecode.model.settings import SettingsModel
32 32
33 33
34 34 log = logging.getLogger(__name__)
35 35
36 36 # Legacy plugins are stored with this prefix in 'auth_plugins'.
37 37 legacy_plugin_prefix = 'py:'
38 38
39 39
40 40 # TODO: Currently this is only used to discover the authentication plugins.
41 41 # Later on this may be used in a generic way to look up and include all kinds
42 42 # of supported enterprise plugins. Therefore this has to be moved and
43 43 # refactored to a real 'plugin look up' machinery.
44 44 # TODO: When refactoring this think about splitting it up into distinct
45 45 # discover, load and include phases.
46 46 def _discover_plugins(config, entry_point='enterprise.plugins1'):
47 47 _discovered_plugins = {}
48 48
49 49 for ep in iter_entry_points(entry_point):
50 50 plugin_id = 'egg:{}#{}'.format(ep.dist.project_name, ep.name)
51 51 log.debug('Plugin discovered: "%s"', plugin_id)
52 52 module = ep.load()
53 53 plugin = module(plugin_id=plugin_id)
54 54 config.include(plugin.includeme)
55 55
56 56 return _discovered_plugins
57 57
58 58
59 59 def _discover_legacy_plugins(config, prefix=legacy_plugin_prefix):
60 60 """
61 61 Function that imports the legacy plugins stored in the 'auth_plugins'
62 62 setting in database which are using the specified prefix. Normally 'py:' is
63 63 used for the legacy plugins.
64 64 """
65 65 auth_plugins = SettingsModel().get_setting_by_name('auth_plugins')
66 66 enabled_plugins = auth_plugins.app_settings_value
67 67 legacy_plugins = [id_ for id_ in enabled_plugins if id_.startswith(prefix)]
68 68
69 69 log.debug('Importing these legacy authentication plugins {}'.format(
70 70 legacy_plugins))
71 71
72 72 for plugin_id in legacy_plugins:
73 73 module_name = plugin_id.split(prefix, 1)[-1]
74 74 try:
75 log.debug('Import %s', module_name)
76 75 module = importlib.import_module(module_name)
77 76 plugin = module.plugin_factory(plugin_id=plugin_id)
78 77 config.include(plugin.includeme)
79 78 except ImportError as e:
80 79 log.error(
81 'Error while importing legacy authentication plugin '
80 'ImportError while importing legacy authentication plugin '
81 '"{}": {}'.format(plugin_id, e.message))
82 except Exception as e:
83 log.error(
84 'Exception while importing legacy authentication plugin '
82 85 '"{}": {}'.format(plugin_id, e.message))
83 86
84 87
85 88 def includeme(config):
86 89 # Set authentication policy.
87 90 authn_policy = SessionAuthenticationPolicy()
88 91 config.set_authentication_policy(authn_policy)
89 92
90 93 # Create authentication plugin registry and add it to the pyramid registry.
91 94 authn_registry = AuthenticationPluginRegistry(config.get_settings())
92 95 config.add_directive('add_authn_plugin', authn_registry.add_authn_plugin)
93 96 config.registry.registerUtility(authn_registry)
94 97
95 98 # Create authentication traversal root resource.
96 99 authn_root_resource = root_factory()
97 100 config.add_directive('add_authn_resource',
98 101 authn_root_resource.add_authn_resource)
99 102
100 103 # Add the authentication traversal route.
101 104 config.add_route('auth_home',
102 105 ADMIN_PREFIX + '/auth*traverse',
103 106 factory=root_factory)
104 107 # Add the authentication settings root views.
105 108 config.add_view('rhodecode.authentication.views.AuthSettingsView',
106 109 attr='index',
107 110 request_method='GET',
108 111 route_name='auth_home',
109 112 context=AuthnRootResource)
110 113 config.add_view('rhodecode.authentication.views.AuthSettingsView',
111 114 attr='auth_settings',
112 115 request_method='POST',
113 116 route_name='auth_home',
114 117 context=AuthnRootResource)
115 118
116 119 # Auto discover authentication plugins and include their configuration.
117 120 _discover_plugins(config)
118 121 _discover_legacy_plugins(config)
General Comments 0
You need to be logged in to leave comments. Login now