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