2008-09-23 19:12:35 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-09-23 19:12:35 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-09-23 19:12:35 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
Base classes for all backed-end plugins.
|
|
|
|
"""
|
|
|
|
|
2017-05-23 11:35:57 -05:00
|
|
|
import logging
|
2009-01-23 16:49:16 -06:00
|
|
|
import threading
|
2012-02-15 09:26:42 -06:00
|
|
|
import os
|
2015-07-31 03:15:01 -05:00
|
|
|
|
|
|
|
from ipalib import plugable
|
|
|
|
from ipalib.errors import PublicError, InternalError, CommandError
|
|
|
|
from ipalib.request import context, Connection, destroy_context
|
2008-09-23 19:12:35 -05:00
|
|
|
|
2017-05-23 11:35:57 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2008-10-30 02:11:33 -05:00
|
|
|
|
2008-09-23 19:12:35 -05:00
|
|
|
class Backend(plugable.Plugin):
|
2008-09-23 19:44:41 -05:00
|
|
|
"""
|
|
|
|
Base class for all backend plugins.
|
|
|
|
"""
|
|
|
|
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
class Connectible(Backend):
|
2009-01-24 23:44:58 -06:00
|
|
|
"""
|
|
|
|
Base class for backend plugins that create connections.
|
|
|
|
|
|
|
|
In addition to the nicety of providing a standard connection API, all
|
|
|
|
backend plugins that create connections should use this base class so that
|
|
|
|
`request.destroy_context()` can properly close all open connections.
|
|
|
|
"""
|
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
def __init__(self, api, shared_instance=False):
|
|
|
|
Backend.__init__(self, api)
|
2010-01-05 06:38:20 -06:00
|
|
|
if shared_instance:
|
|
|
|
self.id = self.name
|
|
|
|
else:
|
|
|
|
self.id = '%s_%s' % (self.name, str(id(self)))
|
|
|
|
|
2009-01-23 16:49:16 -06:00
|
|
|
def connect(self, *args, **kw):
|
|
|
|
"""
|
|
|
|
Create thread-local connection.
|
|
|
|
"""
|
2010-01-05 06:38:20 -06:00
|
|
|
if hasattr(context, self.id):
|
2015-08-24 05:40:33 -05:00
|
|
|
raise Exception(
|
2015-11-20 06:47:34 -06:00
|
|
|
"{0} is already connected ({1} in {2})".format(
|
|
|
|
self.name,
|
|
|
|
self.id,
|
2022-02-21 04:21:06 -06:00
|
|
|
threading.current_thread().name,
|
2009-01-23 16:49:16 -06:00
|
|
|
)
|
|
|
|
)
|
2009-01-23 19:02:32 -06:00
|
|
|
conn = self.create_connection(*args, **kw)
|
2010-01-05 06:38:20 -06:00
|
|
|
setattr(context, self.id, Connection(conn, self.disconnect))
|
2009-01-23 19:02:32 -06:00
|
|
|
assert self.conn is conn
|
2017-05-23 11:35:57 -05:00
|
|
|
logger.debug('Created connection context.%s', self.id)
|
2009-01-23 19:02:32 -06:00
|
|
|
|
|
|
|
def create_connection(self, *args, **kw):
|
2010-01-05 06:38:20 -06:00
|
|
|
raise NotImplementedError('%s.create_connection()' % self.id)
|
2009-01-23 19:02:32 -06:00
|
|
|
|
|
|
|
def disconnect(self):
|
2010-01-05 06:38:20 -06:00
|
|
|
if not hasattr(context, self.id):
|
2015-08-24 05:40:33 -05:00
|
|
|
raise Exception(
|
2015-11-20 06:47:34 -06:00
|
|
|
"{0} is not connected ({1} in {2})".format(
|
|
|
|
self.name,
|
|
|
|
self.id,
|
2022-02-21 04:21:06 -06:00
|
|
|
threading.current_thread().name,
|
2009-01-23 19:02:32 -06:00
|
|
|
)
|
2009-01-23 16:49:16 -06:00
|
|
|
)
|
2009-01-23 19:02:32 -06:00
|
|
|
self.destroy_connection()
|
2010-01-05 06:38:20 -06:00
|
|
|
delattr(context, self.id)
|
2017-05-23 11:35:57 -05:00
|
|
|
logger.debug('Destroyed connection context.%s', self.id)
|
2009-01-23 19:02:32 -06:00
|
|
|
|
|
|
|
def destroy_connection(self):
|
2010-01-05 06:38:20 -06:00
|
|
|
raise NotImplementedError('%s.destroy_connection()' % self.id)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
def isconnected(self):
|
|
|
|
"""
|
|
|
|
Return ``True`` if thread-local connection on `request.context` exists.
|
|
|
|
"""
|
2010-01-05 06:38:20 -06:00
|
|
|
return hasattr(context, self.id)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2022-02-18 07:44:37 -06:00
|
|
|
def __get_conn(self): # pylint: disable=unused-private-member, #4756
|
2009-01-23 16:49:16 -06:00
|
|
|
"""
|
|
|
|
Return thread-local connection.
|
|
|
|
"""
|
2010-01-05 06:38:20 -06:00
|
|
|
if not hasattr(context, self.id):
|
2015-11-20 06:47:34 -06:00
|
|
|
raise AttributeError(
|
|
|
|
"{0} is not connected ({1} in {2})".format(
|
|
|
|
self.name,
|
|
|
|
self.id,
|
2022-02-21 04:21:06 -06:00
|
|
|
threading.current_thread().name,
|
2015-11-20 06:47:34 -06:00
|
|
|
)
|
2009-01-23 16:49:16 -06:00
|
|
|
)
|
2010-01-05 06:38:20 -06:00
|
|
|
return getattr(context, self.id).conn
|
2009-01-23 16:49:16 -06:00
|
|
|
conn = property(__get_conn)
|
|
|
|
|
|
|
|
|
|
|
|
class Executioner(Backend):
|
|
|
|
|
2009-01-27 17:28:50 -06:00
|
|
|
def create_context(self, ccache=None, client_ip=None):
|
2009-10-20 10:59:07 -05:00
|
|
|
"""
|
|
|
|
client_ip: The IP address of the remote client.
|
|
|
|
"""
|
2012-02-15 09:26:42 -06:00
|
|
|
|
|
|
|
if ccache is not None:
|
|
|
|
os.environ["KRB5CCNAME"] = ccache
|
|
|
|
|
2009-01-27 17:28:50 -06:00
|
|
|
if self.env.in_server:
|
2016-10-27 08:31:25 -05:00
|
|
|
self.Backend.ldap2.connect(ccache=ccache,
|
|
|
|
size_limit=None,
|
|
|
|
time_limit=None)
|
2009-01-27 17:28:50 -06:00
|
|
|
else:
|
2016-05-25 05:31:03 -05:00
|
|
|
self.Backend.rpcclient.connect()
|
2009-10-20 10:59:07 -05:00
|
|
|
if client_ip is not None:
|
|
|
|
setattr(context, "client_ip", client_ip)
|
2009-01-27 17:28:50 -06:00
|
|
|
|
2009-07-10 15:43:47 -05:00
|
|
|
def destroy_context(self):
|
|
|
|
destroy_context()
|
|
|
|
|
2009-03-13 01:53:05 -05:00
|
|
|
def execute(self, _name, *args, **options):
|
2009-01-23 16:49:16 -06:00
|
|
|
try:
|
2009-03-13 01:53:05 -05:00
|
|
|
if _name not in self.Command:
|
|
|
|
raise CommandError(name=_name)
|
2017-12-15 10:00:04 -06:00
|
|
|
return self.Command[_name](*args, **options)
|
2022-02-21 06:36:10 -06:00
|
|
|
except PublicError:
|
2017-12-15 10:00:04 -06:00
|
|
|
raise
|
2015-08-24 05:40:33 -05:00
|
|
|
except Exception as e:
|
2017-05-23 11:35:57 -05:00
|
|
|
logger.exception(
|
2009-01-23 16:49:16 -06:00
|
|
|
'non-public: %s: %s', e.__class__.__name__, str(e)
|
|
|
|
)
|
2017-12-15 10:00:04 -06:00
|
|
|
raise InternalError()
|
|
|
|
finally:
|
|
|
|
destroy_context()
|