Show More
@@ -1,114 +1,118 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 | log = logging.getLogger(__name__) |
|
34 | log = logging.getLogger(__name__) | |
34 |
|
35 | |||
|
36 | # Legacy plugins are stored with this prefix in 'auth_plugins'. | |||
|
37 | legacy_plugin_prefix = 'py:' | |||
|
38 | ||||
35 |
|
39 | |||
36 | # TODO: Currently this is only used to discover the authentication plugins. |
|
40 | # TODO: Currently this is only used to discover the authentication plugins. | |
37 | # Later on this may be used in a generic way to look up and include all kinds |
|
41 | # Later on this may be used in a generic way to look up and include all kinds | |
38 | # of supported enterprise plugins. Therefore this has to be moved and |
|
42 | # of supported enterprise plugins. Therefore this has to be moved and | |
39 | # refactored to a real 'plugin look up' machinery. |
|
43 | # refactored to a real 'plugin look up' machinery. | |
40 | # TODO: When refactoring this think about splitting it up into distinct |
|
44 | # TODO: When refactoring this think about splitting it up into distinct | |
41 | # discover, load and include phases. |
|
45 | # discover, load and include phases. | |
42 | def _discover_plugins(config, entry_point='enterprise.plugins1'): |
|
46 | def _discover_plugins(config, entry_point='enterprise.plugins1'): | |
43 | _discovered_plugins = {} |
|
47 | _discovered_plugins = {} | |
44 |
|
48 | |||
45 | for ep in iter_entry_points(entry_point): |
|
49 | for ep in iter_entry_points(entry_point): | |
46 | plugin_id = 'egg:{}#{}'.format(ep.dist.project_name, ep.name) |
|
50 | plugin_id = 'egg:{}#{}'.format(ep.dist.project_name, ep.name) | |
47 | log.debug('Plugin discovered: "%s"', plugin_id) |
|
51 | log.debug('Plugin discovered: "%s"', plugin_id) | |
48 | module = ep.load() |
|
52 | module = ep.load() | |
49 | plugin = module(plugin_id=plugin_id) |
|
53 | plugin = module(plugin_id=plugin_id) | |
50 | config.include(plugin.includeme) |
|
54 | config.include(plugin.includeme) | |
51 |
|
55 | |||
52 | return _discovered_plugins |
|
56 | return _discovered_plugins | |
53 |
|
57 | |||
54 |
|
58 | |||
55 |
def _discover_legacy_plugins(config, prefix= |
|
59 | def _discover_legacy_plugins(config, prefix=legacy_plugin_prefix): | |
56 | """ |
|
60 | """ | |
57 | Function that imports the legacy plugins stored in the 'auth_plugins' |
|
61 | Function that imports the legacy plugins stored in the 'auth_plugins' | |
58 | setting in database which are using the specified prefix. Normally 'py:' is |
|
62 | setting in database which are using the specified prefix. Normally 'py:' is | |
59 | used for the legacy plugins. |
|
63 | used for the legacy plugins. | |
60 | """ |
|
64 | """ | |
61 | auth_plugins = SettingsModel().get_setting_by_name('auth_plugins') |
|
65 | auth_plugins = SettingsModel().get_setting_by_name('auth_plugins') | |
62 | enabled_plugins = auth_plugins.app_settings_value |
|
66 | enabled_plugins = auth_plugins.app_settings_value | |
63 | legacy_plugins = [id_ for id_ in enabled_plugins if id_.startswith(prefix)] |
|
67 | legacy_plugins = [id_ for id_ in enabled_plugins if id_.startswith(prefix)] | |
64 |
|
68 | |||
65 | log.debug('Importing these legacy authentication plugins {}'.format( |
|
69 | log.debug('Importing these legacy authentication plugins {}'.format( | |
66 | legacy_plugins)) |
|
70 | legacy_plugins)) | |
67 |
|
71 | |||
68 | for plugin_id in legacy_plugins: |
|
72 | for plugin_id in legacy_plugins: | |
69 | module_name = plugin_id.split(prefix, 1)[-1] |
|
73 | module_name = plugin_id.split(prefix, 1)[-1] | |
70 | try: |
|
74 | try: | |
71 | log.debug('Import %s', module_name) |
|
75 | log.debug('Import %s', module_name) | |
72 | module = importlib.import_module(module_name) |
|
76 | module = importlib.import_module(module_name) | |
73 | plugin = module.plugin_factory(plugin_id=plugin_id) |
|
77 | plugin = module.plugin_factory(plugin_id=plugin_id) | |
74 | config.include(plugin.includeme) |
|
78 | config.include(plugin.includeme) | |
75 | except ImportError as e: |
|
79 | except ImportError as e: | |
76 | log.error( |
|
80 | log.error( | |
77 | 'Error while importing legacy authentication plugin ' |
|
81 | 'Error while importing legacy authentication plugin ' | |
78 | '"{}": {}'.format(plugin_id, e.message)) |
|
82 | '"{}": {}'.format(plugin_id, e.message)) | |
79 |
|
83 | |||
80 |
|
84 | |||
81 | def includeme(config): |
|
85 | def includeme(config): | |
82 | # Set authentication policy. |
|
86 | # Set authentication policy. | |
83 | authn_policy = SessionAuthenticationPolicy() |
|
87 | authn_policy = SessionAuthenticationPolicy() | |
84 | config.set_authentication_policy(authn_policy) |
|
88 | config.set_authentication_policy(authn_policy) | |
85 |
|
89 | |||
86 | # Create authentication plugin registry and add it to the pyramid registry. |
|
90 | # Create authentication plugin registry and add it to the pyramid registry. | |
87 | authn_registry = AuthenticationPluginRegistry(config.get_settings()) |
|
91 | authn_registry = AuthenticationPluginRegistry(config.get_settings()) | |
88 | config.add_directive('add_authn_plugin', authn_registry.add_authn_plugin) |
|
92 | config.add_directive('add_authn_plugin', authn_registry.add_authn_plugin) | |
89 | config.registry.registerUtility(authn_registry) |
|
93 | config.registry.registerUtility(authn_registry) | |
90 |
|
94 | |||
91 | # Create authentication traversal root resource. |
|
95 | # Create authentication traversal root resource. | |
92 | authn_root_resource = root_factory() |
|
96 | authn_root_resource = root_factory() | |
93 | config.add_directive('add_authn_resource', |
|
97 | config.add_directive('add_authn_resource', | |
94 | authn_root_resource.add_authn_resource) |
|
98 | authn_root_resource.add_authn_resource) | |
95 |
|
99 | |||
96 | # Add the authentication traversal route. |
|
100 | # Add the authentication traversal route. | |
97 | config.add_route('auth_home', |
|
101 | config.add_route('auth_home', | |
98 | ADMIN_PREFIX + '/auth*traverse', |
|
102 | ADMIN_PREFIX + '/auth*traverse', | |
99 | factory=root_factory) |
|
103 | factory=root_factory) | |
100 | # Add the authentication settings root views. |
|
104 | # Add the authentication settings root views. | |
101 | config.add_view('rhodecode.authentication.views.AuthSettingsView', |
|
105 | config.add_view('rhodecode.authentication.views.AuthSettingsView', | |
102 | attr='index', |
|
106 | attr='index', | |
103 | request_method='GET', |
|
107 | request_method='GET', | |
104 | route_name='auth_home', |
|
108 | route_name='auth_home', | |
105 | context=AuthnRootResource) |
|
109 | context=AuthnRootResource) | |
106 | config.add_view('rhodecode.authentication.views.AuthSettingsView', |
|
110 | config.add_view('rhodecode.authentication.views.AuthSettingsView', | |
107 | attr='auth_settings', |
|
111 | attr='auth_settings', | |
108 | request_method='POST', |
|
112 | request_method='POST', | |
109 | route_name='auth_home', |
|
113 | route_name='auth_home', | |
110 | context=AuthnRootResource) |
|
114 | context=AuthnRootResource) | |
111 |
|
115 | |||
112 | # Auto discover authentication plugins and include their configuration. |
|
116 | # Auto discover authentication plugins and include their configuration. | |
113 | _discover_plugins(config) |
|
117 | _discover_plugins(config) | |
114 | _discover_legacy_plugins(config) |
|
118 | _discover_legacy_plugins(config) |
General Comments 0
You need to be logged in to leave comments.
Login now