Add a method to Installer classes to validate argument input
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from ipsilon.util.log import Log
19 from ipsilon.util.plugin import PluginInstaller, PluginLoader
20 from ipsilon.util.plugin import PluginObject, PluginConfig
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23 from ipsilon.rest.common import RestPage
24 import cherrypy
25
26
27 class ProviderException(Exception, Log):
28
29     def __init__(self, message):
30         super(ProviderException, self).__init__(message)
31         self.message = message
32
33     def __str__(self):
34         return repr(self.message)
35
36
37 class AuthenticationError(ProviderException):
38
39     def __init__(self, message, code):
40         super(AuthenticationError, self).__init__(message)
41         self.code = code
42         self._debug('%s [%s]' % (message, code))
43
44
45 class InvalidRequest(ProviderException):
46
47     def __init__(self, message):
48         super(InvalidRequest, self).__init__(message)
49         self._debug(message)
50
51
52 class ProviderBase(PluginConfig, PluginObject):
53
54     def __init__(self, name, path, *pargs):
55         PluginConfig.__init__(self)
56         PluginObject.__init__(self, *pargs)
57         self.name = name
58         self._root = None
59         self.path = path
60         self.tree = None
61
62     def get_tree(self, site):
63         raise NotImplementedError
64
65     def register(self, root, site):
66
67         self._root = root
68         # init pages and admin interfaces
69         self.tree = self.get_tree(site)
70         self._debug('IdP Provider registered: %s' % self.name)
71
72     def on_enable(self):
73         self._root.add_subtree(self.name, self.tree)
74
75     def on_disable(self):
76         self._root.del_subtree(self.name)
77
78
79 class ProviderPageBase(Page):
80
81     def __init__(self, site, config):
82         super(ProviderPageBase, self).__init__(site)
83         self.plugin_name = config.name
84         self.cfg = config
85
86     def GET(self, *args, **kwargs):
87         raise cherrypy.HTTPError(501)
88
89     def POST(self, *args, **kwargs):
90         raise cherrypy.HTTPError(501)
91
92     def root(self, *args, **kwargs):
93         method = cherrypy.request.method
94
95         preop = getattr(self, 'pre_%s' % method, None)
96         if preop and callable(preop):
97             preop(*args, **kwargs)
98
99         op = getattr(self, method, self.GET)
100         if callable(op):
101             return op(*args, **kwargs)
102         else:
103             raise cherrypy.HTTPError(405)
104
105     def _debug(self, fact):
106         superfact = '%s: %s' % (self.plugin_name, fact)
107         super(ProviderPageBase, self)._debug(superfact)
108
109     def _audit(self, fact):
110         cherrypy.log('%s: %s' % (self.plugin_name, fact))
111
112
113 FACILITY = 'provider_config'
114
115
116 class ProviderInstaller(object):
117     def __init__(self):
118         self.facility = FACILITY
119         self.ptype = 'provider'
120         self.name = None
121
122     def unconfigure(self, opts):
123         return
124
125     def install_args(self, group):
126         raise NotImplementedError
127
128     def validate_args(self, args):
129         return
130
131     def configure(self, opts):
132         raise NotImplementedError
133
134
135 class LoadProviders(Log):
136
137     def __init__(self, root, site):
138         plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
139         plugins.get_plugin_data()
140         site[FACILITY] = plugins
141
142         available = plugins.available.keys()
143         self._debug('Available providers: %s' % str(available))
144
145         for item in plugins.available:
146             plugin = plugins.available[item]
147             plugin.register(root, site)
148
149         for item in plugins.enabled:
150             self._debug('Provider plugin in enabled list: %s' % item)
151             if item not in plugins.available:
152                 continue
153             plugins.available[item].enable()
154
155
156 class ProvidersInstall(object):
157
158     def __init__(self):
159         pi = PluginInstaller(ProvidersInstall, FACILITY)
160         self.plugins = pi.get_plugins()
161
162
163 class RestProviderBase(RestPage):
164
165     def __init__(self, site, config):
166         super(RestProviderBase, self).__init__(site)
167         self.plugin_name = config.name
168         self.cfg = config
169
170     @admin_protect
171     def GET(self, *args, **kwargs):
172         raise cherrypy.HTTPError(501)
173
174     @admin_protect
175     def POST(self, *args, **kwargs):
176         raise cherrypy.HTTPError(501)
177
178     @admin_protect
179     def DELETE(self, *args, **kwargs):
180         raise cherrypy.HTTPError(501)
181
182     @admin_protect
183     def PUT(self, *args, **kwargs):
184         raise cherrypy.HTTPError(501)
185
186     def root(self, *args, **kwargs):
187         method = cherrypy.request.method
188
189         preop = getattr(self, 'pre_%s' % method, None)
190         if preop and callable(preop):
191             preop(*args, **kwargs)
192
193         op = getattr(self, method, self.GET)
194         if callable(op):
195             return op(*args, **kwargs)
196         else:
197             raise cherrypy.HTTPError(405)
198
199     def _debug(self, fact):
200         superfact = '%s: %s' % (self.plugin_name, fact)
201         super(RestProviderBase, self)._debug(superfact)
202
203     def _audit(self, fact):
204         cherrypy.log('%s: %s' % (self.plugin_name, fact))