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