2008-09-24 00:12:35 +00:00
|
|
|
# Authors:
|
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
|
#
|
2010-12-09 13:59:11 +01: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-24 00:12:35 +00: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 13:59:11 +01:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-09-24 00:12:35 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Base classes for all backed-end plugins.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-05-23 18:35:57 +02:00
|
|
|
import logging
|
2009-01-23 15:49:16 -07:00
|
|
|
import threading
|
2012-02-15 10:26:42 -05:00
|
|
|
import os
|
2015-07-31 10:15:01 +02:00
|
|
|
|
|
|
|
|
from ipalib import plugable
|
|
|
|
|
from ipalib.errors import PublicError, InternalError, CommandError
|
|
|
|
|
from ipalib.request import context, Connection, destroy_context
|
2008-09-24 00:12:35 +00:00
|
|
|
|
2017-05-23 18:35:57 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2008-10-30 01:11:33 -06:00
|
|
|
|
2008-09-24 00:12:35 +00:00
|
|
|
class Backend(plugable.Plugin):
|
2008-09-24 00:44:41 +00:00
|
|
|
"""
|
|
|
|
|
Base class for all backend plugins.
|
|
|
|
|
"""
|
|
|
|
|
|
2009-01-23 15:49:16 -07:00
|
|
|
|
|
|
|
|
class Connectible(Backend):
|
2009-01-24 22:44:58 -07: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 10:58:43 +00:00
|
|
|
def __init__(self, api, shared_instance=False):
|
|
|
|
|
Backend.__init__(self, api)
|
2010-01-05 13:38:20 +01:00
|
|
|
if shared_instance:
|
|
|
|
|
self.id = self.name
|
|
|
|
|
else:
|
|
|
|
|
self.id = '%s_%s' % (self.name, str(id(self)))
|
|
|
|
|
|
2009-01-23 15:49:16 -07:00
|
|
|
def connect(self, *args, **kw):
|
|
|
|
|
"""
|
|
|
|
|
Create thread-local connection.
|
|
|
|
|
"""
|
2010-01-05 13:38:20 +01:00
|
|
|
if hasattr(context, self.id):
|
2015-08-24 12:40:33 +02:00
|
|
|
raise Exception(
|
2015-11-20 13:47:34 +01:00
|
|
|
"{0} is already connected ({1} in {2})".format(
|
|
|
|
|
self.name,
|
|
|
|
|
self.id,
|
|
|
|
|
threading.currentThread().getName()
|
2009-01-23 15:49:16 -07:00
|
|
|
)
|
|
|
|
|
)
|
2009-01-23 18:02:32 -07:00
|
|
|
conn = self.create_connection(*args, **kw)
|
2010-01-05 13:38:20 +01:00
|
|
|
setattr(context, self.id, Connection(conn, self.disconnect))
|
2009-01-23 18:02:32 -07:00
|
|
|
assert self.conn is conn
|
2017-05-23 18:35:57 +02:00
|
|
|
logger.debug('Created connection context.%s', self.id)
|
2009-01-23 18:02:32 -07:00
|
|
|
|
|
|
|
|
def create_connection(self, *args, **kw):
|
2010-01-05 13:38:20 +01:00
|
|
|
raise NotImplementedError('%s.create_connection()' % self.id)
|
2009-01-23 18:02:32 -07:00
|
|
|
|
|
|
|
|
def disconnect(self):
|
2010-01-05 13:38:20 +01:00
|
|
|
if not hasattr(context, self.id):
|
2015-08-24 12:40:33 +02:00
|
|
|
raise Exception(
|
2015-11-20 13:47:34 +01:00
|
|
|
"{0} is not connected ({1} in {2})".format(
|
|
|
|
|
self.name,
|
|
|
|
|
self.id,
|
|
|
|
|
threading.currentThread().getName()
|
2009-01-23 18:02:32 -07:00
|
|
|
)
|
2009-01-23 15:49:16 -07:00
|
|
|
)
|
2009-01-23 18:02:32 -07:00
|
|
|
self.destroy_connection()
|
2010-01-05 13:38:20 +01:00
|
|
|
delattr(context, self.id)
|
2017-05-23 18:35:57 +02:00
|
|
|
logger.debug('Destroyed connection context.%s', self.id)
|
2009-01-23 18:02:32 -07:00
|
|
|
|
|
|
|
|
def destroy_connection(self):
|
2010-01-05 13:38:20 +01:00
|
|
|
raise NotImplementedError('%s.destroy_connection()' % self.id)
|
2009-01-23 15:49:16 -07:00
|
|
|
|
|
|
|
|
def isconnected(self):
|
|
|
|
|
"""
|
|
|
|
|
Return ``True`` if thread-local connection on `request.context` exists.
|
|
|
|
|
"""
|
2010-01-05 13:38:20 +01:00
|
|
|
return hasattr(context, self.id)
|
2009-01-23 15:49:16 -07:00
|
|
|
|
|
|
|
|
def __get_conn(self):
|
|
|
|
|
"""
|
|
|
|
|
Return thread-local connection.
|
|
|
|
|
"""
|
2010-01-05 13:38:20 +01:00
|
|
|
if not hasattr(context, self.id):
|
2015-11-20 13:47:34 +01:00
|
|
|
raise AttributeError(
|
|
|
|
|
"{0} is not connected ({1} in {2})".format(
|
|
|
|
|
self.name,
|
|
|
|
|
self.id,
|
|
|
|
|
threading.currentThread().getName()
|
|
|
|
|
)
|
2009-01-23 15:49:16 -07:00
|
|
|
)
|
2010-01-05 13:38:20 +01:00
|
|
|
return getattr(context, self.id).conn
|
2009-01-23 15:49:16 -07:00
|
|
|
conn = property(__get_conn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Executioner(Backend):
|
|
|
|
|
|
2009-01-27 16:28:50 -07:00
|
|
|
def create_context(self, ccache=None, client_ip=None):
|
2009-10-20 11:59:07 -04:00
|
|
|
"""
|
|
|
|
|
client_ip: The IP address of the remote client.
|
|
|
|
|
"""
|
2012-02-15 10:26:42 -05:00
|
|
|
|
|
|
|
|
if ccache is not None:
|
|
|
|
|
os.environ["KRB5CCNAME"] = ccache
|
|
|
|
|
|
2009-01-27 16:28:50 -07:00
|
|
|
if self.env.in_server:
|
2016-10-27 15:31:25 +02:00
|
|
|
self.Backend.ldap2.connect(ccache=ccache,
|
|
|
|
|
size_limit=None,
|
|
|
|
|
time_limit=None)
|
2009-01-27 16:28:50 -07:00
|
|
|
else:
|
2016-05-25 12:31:03 +02:00
|
|
|
self.Backend.rpcclient.connect()
|
2009-10-20 11:59:07 -04:00
|
|
|
if client_ip is not None:
|
|
|
|
|
setattr(context, "client_ip", client_ip)
|
2009-01-27 16:28:50 -07:00
|
|
|
|
2009-07-10 16:43:47 -04:00
|
|
|
def destroy_context(self):
|
|
|
|
|
destroy_context()
|
|
|
|
|
|
2009-03-13 00:53:05 -06:00
|
|
|
def execute(self, _name, *args, **options):
|
2009-01-23 15:49:16 -07:00
|
|
|
try:
|
2009-03-13 00:53:05 -06:00
|
|
|
if _name not in self.Command:
|
|
|
|
|
raise CommandError(name=_name)
|
2017-12-15 17:00:04 +01:00
|
|
|
return self.Command[_name](*args, **options)
|
2018-07-12 11:21:34 -03:00
|
|
|
except PublicError: # pylint: disable=try-except-raise
|
2017-12-15 17:00:04 +01:00
|
|
|
raise
|
2015-08-24 12:40:33 +02:00
|
|
|
except Exception as e:
|
2017-05-23 18:35:57 +02:00
|
|
|
logger.exception(
|
2009-01-23 15:49:16 -07:00
|
|
|
'non-public: %s: %s', e.__class__.__name__, str(e)
|
|
|
|
|
)
|
2017-12-15 17:00:04 +01:00
|
|
|
raise InternalError()
|
|
|
|
|
finally:
|
|
|
|
|
destroy_context()
|