2008-11-24 13:51:03 -06:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
2009-01-22 15:00:37 -06:00
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
2008-11-24 13:51:03 -06:00
|
|
|
#
|
|
|
|
# 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-11-24 13:51:03 -06: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-11-24 13:51:03 -06:00
|
|
|
|
|
|
|
"""
|
2009-01-16 02:56:39 -06:00
|
|
|
RPC client and shared RPC client/server functionality.
|
2009-01-16 02:23:55 -06:00
|
|
|
|
|
|
|
This module adds some additional functionality on top of the ``xmlrpclib``
|
|
|
|
module in the Python standard library. For documentation on the
|
|
|
|
``xmlrpclib`` module, see:
|
|
|
|
|
|
|
|
http://docs.python.org/library/xmlrpclib.html
|
2009-01-16 02:56:39 -06:00
|
|
|
|
|
|
|
Also see the `ipaserver.rpcserver` module.
|
2008-11-24 13:51:03 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
from types import NoneType
|
2012-01-17 04:19:00 -06:00
|
|
|
from decimal import Decimal
|
2009-01-19 22:10:42 -06:00
|
|
|
import threading
|
2010-08-31 15:59:27 -05:00
|
|
|
import sys
|
2009-02-19 16:20:37 -06:00
|
|
|
import os
|
2009-04-13 17:01:58 -05:00
|
|
|
import errno
|
2011-02-15 13:10:38 -06:00
|
|
|
import locale
|
2009-07-10 14:33:07 -05:00
|
|
|
from xmlrpclib import Binary, Fault, dumps, loads, ServerProxy, Transport, ProtocolError
|
2009-01-22 15:00:37 -06:00
|
|
|
import kerberos
|
2009-01-23 19:02:32 -06:00
|
|
|
from ipalib.backend import Connectible
|
2011-01-17 15:23:53 -06:00
|
|
|
from ipalib.errors import public_errors, PublicError, UnknownError, NetworkError, KerberosError, XMLRPCMarshallError
|
2009-04-23 07:51:59 -05:00
|
|
|
from ipalib import errors
|
2009-01-19 22:10:42 -06:00
|
|
|
from ipalib.request import context
|
2010-07-26 16:54:38 -05:00
|
|
|
from ipapython import ipautil, dnsclient
|
2009-02-19 16:20:37 -06:00
|
|
|
import httplib
|
2010-10-14 16:21:11 -05:00
|
|
|
import socket
|
2010-08-31 15:59:27 -05:00
|
|
|
from ipapython.nsslib import NSSHTTPS, NSSConnection
|
2010-05-31 06:40:17 -05:00
|
|
|
from nss.error import NSPRError
|
2010-07-26 16:54:38 -05:00
|
|
|
from urllib2 import urlparse
|
add session manager and cache krb auth
This patch adds a session manager and support for caching
authentication in the session. Major elements of the patch are:
* Add a session manager to support cookie based sessions which
stores session data in a memcached entry.
* Add ipalib/krb_utils.py which contains functions to parse ccache
names, format principals, format KRB timestamps, and a KRB_CCache
class which reads ccache entry and allows one to extract information
such as the principal, credentials, credential timestamps, etc.
* Move krb constants defined in ipalib/rpc.py to ipa_krb_utils.py so
that all kerberos items are co-located.
* Modify javascript in ipa.js so that the IPA.command() RPC call
checks for authentication needed error response and if it receives
it sends a GET request to /ipa/login URL to refresh credentials.
* Add session_auth_duration config item to constants.py, used to
configure how long a session remains valid.
* Add parse_time_duration utility to ipalib/util.py. Used to parse the
session_auth_duration config item.
* Update the default.conf.5 man page to document session_auth_duration
config item (also added documentation for log_manager config items
which had been inadvertantly omitted from a previous commit).
* Add SessionError object to ipalib/errors.py
* Move Kerberos protection in Apache config from /ipa to /ipa/xml and
/ipa/login
* Add SessionCCache class to session.py to manage temporary Kerberos
ccache file in effect for the duration of an RPC command.
* Adds a krblogin plugin used to implement the /ipa/login
handler. login handler sets the session expiration time, currently
60 minutes or the expiration of the TGT, whichever is shorter. It
also copies the ccache provied by mod_auth_kerb into the session
data. The json handler will later extract and validate the ccache
belonging to the session.
* Refactored the WSGI handlers so that json and xlmrpc could have
independent behavior, this also moves where create and destroy
context occurs, now done in the individual handler rather than the
parent class.
* The json handler now looks up the session data, validates the ccache
bound to the session, if it's expired replies with authenicated
needed error.
* Add documentation to session.py. Fully documents the entire process,
got questions, read the doc.
* Add exclusions to make-lint as needed.
2012-02-06 12:29:56 -06:00
|
|
|
from ipalib.krb_utils import KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN, KRB5KRB_AP_ERR_TKT_EXPIRED, \
|
|
|
|
KRB5_FCC_PERM, KRB5_FCC_NOFILE, KRB5_CC_FORMAT, KRB5_REALM_CANT_RESOLVE
|
2008-11-24 13:51:03 -06:00
|
|
|
|
2009-01-16 00:52:50 -06:00
|
|
|
def xml_wrap(value):
|
2008-11-24 13:51:03 -06:00
|
|
|
"""
|
|
|
|
Wrap all ``str`` in ``xmlrpclib.Binary``.
|
|
|
|
|
|
|
|
Because ``xmlrpclib.dumps()`` will itself convert all ``unicode`` instances
|
|
|
|
into UTF-8 encoded ``str`` instances, we don't do it here.
|
|
|
|
|
2009-01-16 02:23:55 -06:00
|
|
|
So in total, when encoding data for an XML-RPC packet, the following
|
2008-11-24 13:51:03 -06:00
|
|
|
transformations occur:
|
|
|
|
|
|
|
|
* All ``str`` instances are treated as binary data and are wrapped in
|
|
|
|
an ``xmlrpclib.Binary()`` instance.
|
|
|
|
|
|
|
|
* Only ``unicode`` instances are treated as character data. They get
|
|
|
|
converted to UTF-8 encoded ``str`` instances (although as mentioned,
|
|
|
|
not by this function).
|
|
|
|
|
2009-01-16 02:23:55 -06:00
|
|
|
Also see `xml_unwrap()`.
|
|
|
|
|
|
|
|
:param value: The simple scalar or simple compound value to wrap.
|
2008-11-24 13:51:03 -06:00
|
|
|
"""
|
|
|
|
if type(value) in (list, tuple):
|
2009-01-16 00:52:50 -06:00
|
|
|
return tuple(xml_wrap(v) for v in value)
|
2008-11-24 13:51:03 -06:00
|
|
|
if type(value) is dict:
|
|
|
|
return dict(
|
2009-01-16 00:52:50 -06:00
|
|
|
(k, xml_wrap(v)) for (k, v) in value.iteritems()
|
2008-11-24 13:51:03 -06:00
|
|
|
)
|
|
|
|
if type(value) is str:
|
|
|
|
return Binary(value)
|
2012-01-17 04:19:00 -06:00
|
|
|
if type(value) is Decimal:
|
|
|
|
# transfer Decimal as a string
|
|
|
|
return unicode(value)
|
2008-11-24 13:51:03 -06:00
|
|
|
assert type(value) in (unicode, int, float, bool, NoneType)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2009-01-16 00:52:50 -06:00
|
|
|
def xml_unwrap(value, encoding='UTF-8'):
|
2008-11-24 13:51:03 -06:00
|
|
|
"""
|
|
|
|
Unwrap all ``xmlrpc.Binary``, decode all ``str`` into ``unicode``.
|
|
|
|
|
2009-01-16 02:23:55 -06:00
|
|
|
When decoding data from an XML-RPC packet, the following transformations
|
2008-11-24 13:51:03 -06:00
|
|
|
occur:
|
|
|
|
|
|
|
|
* The binary payloads of all ``xmlrpclib.Binary`` instances are
|
|
|
|
returned as ``str`` instances.
|
|
|
|
|
2009-01-16 02:23:55 -06:00
|
|
|
* All ``str`` instances are treated as UTF-8 encoded Unicode strings.
|
2008-11-24 13:51:03 -06:00
|
|
|
They are decoded and the resulting ``unicode`` instance is returned.
|
|
|
|
|
2009-01-16 02:23:55 -06:00
|
|
|
Also see `xml_wrap()`.
|
|
|
|
|
|
|
|
:param value: The value to unwrap.
|
|
|
|
:param encoding: The Unicode encoding to use (defaults to ``'UTF-8'``).
|
2008-11-24 13:51:03 -06:00
|
|
|
"""
|
|
|
|
if type(value) in (list, tuple):
|
2009-01-16 00:52:50 -06:00
|
|
|
return tuple(xml_unwrap(v, encoding) for v in value)
|
2008-11-24 13:51:03 -06:00
|
|
|
if type(value) is dict:
|
|
|
|
return dict(
|
2009-01-16 00:52:50 -06:00
|
|
|
(k, xml_unwrap(v, encoding)) for (k, v) in value.iteritems()
|
2008-11-24 13:51:03 -06:00
|
|
|
)
|
|
|
|
if type(value) is str:
|
|
|
|
return value.decode(encoding)
|
|
|
|
if isinstance(value, Binary):
|
|
|
|
assert type(value.data) is str
|
|
|
|
return value.data
|
|
|
|
assert type(value) in (unicode, int, float, bool, NoneType)
|
|
|
|
return value
|
2009-01-16 00:52:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
def xml_dumps(params, methodname=None, methodresponse=False, encoding='UTF-8'):
|
2009-01-16 02:23:55 -06:00
|
|
|
"""
|
|
|
|
Encode an XML-RPC data packet, transparently wraping ``params``.
|
|
|
|
|
|
|
|
This function will wrap ``params`` using `xml_wrap()` and will
|
|
|
|
then encode the XML-RPC data packet using ``xmlrpclib.dumps()`` (from the
|
|
|
|
Python standard library).
|
|
|
|
|
|
|
|
For documentation on the ``xmlrpclib.dumps()`` function, see:
|
|
|
|
|
|
|
|
http://docs.python.org/library/xmlrpclib.html#convenience-functions
|
|
|
|
|
|
|
|
Also see `xml_loads()`.
|
|
|
|
|
|
|
|
:param params: A ``tuple`` or an ``xmlrpclib.Fault`` instance.
|
|
|
|
:param methodname: The name of the method to call if this is a request.
|
|
|
|
:param methodresponse: Set this to ``True`` if this is a response.
|
|
|
|
:param encoding: The Unicode encoding to use (defaults to ``'UTF-8'``).
|
|
|
|
"""
|
2009-01-16 00:52:50 -06:00
|
|
|
if type(params) is tuple:
|
|
|
|
params = xml_wrap(params)
|
|
|
|
else:
|
|
|
|
assert isinstance(params, Fault)
|
|
|
|
return dumps(params,
|
|
|
|
methodname=methodname,
|
|
|
|
methodresponse=methodresponse,
|
|
|
|
encoding=encoding,
|
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
|
2009-01-31 00:46:51 -06:00
|
|
|
|
2009-01-22 16:41:54 -06:00
|
|
|
def decode_fault(e, encoding='UTF-8'):
|
|
|
|
assert isinstance(e, Fault)
|
|
|
|
if type(e.faultString) is str:
|
|
|
|
return Fault(e.faultCode, e.faultString.decode(encoding))
|
|
|
|
return e
|
2009-01-16 00:52:50 -06:00
|
|
|
|
2009-01-31 00:46:51 -06:00
|
|
|
|
2009-01-22 16:41:54 -06:00
|
|
|
def xml_loads(data, encoding='UTF-8'):
|
2009-01-16 02:23:55 -06:00
|
|
|
"""
|
2009-01-16 02:56:39 -06:00
|
|
|
Decode the XML-RPC packet in ``data``, transparently unwrapping its params.
|
2009-01-16 02:23:55 -06:00
|
|
|
|
|
|
|
This function will decode the XML-RPC packet in ``data`` using
|
|
|
|
``xmlrpclib.loads()`` (from the Python standard library). If ``data``
|
|
|
|
contains a fault, ``xmlrpclib.loads()`` will itself raise an
|
|
|
|
``xmlrpclib.Fault`` exception.
|
|
|
|
|
|
|
|
Assuming an exception is not raised, this function will then unwrap the
|
|
|
|
params in ``data`` using `xml_unwrap()`. Finally, a
|
|
|
|
``(params, methodname)`` tuple is returned containing the unwrapped params
|
|
|
|
and the name of the method being called. If the packet contains no method
|
|
|
|
name, ``methodname`` will be ``None``.
|
|
|
|
|
|
|
|
For documentation on the ``xmlrpclib.loads()`` function, see:
|
|
|
|
|
|
|
|
http://docs.python.org/library/xmlrpclib.html#convenience-functions
|
|
|
|
|
|
|
|
Also see `xml_dumps()`.
|
|
|
|
|
|
|
|
:param data: The XML-RPC packet to decode.
|
|
|
|
"""
|
2009-01-22 16:41:54 -06:00
|
|
|
try:
|
|
|
|
(params, method) = loads(data)
|
|
|
|
return (xml_unwrap(params), method)
|
|
|
|
except Fault, e:
|
|
|
|
raise decode_fault(e)
|
2009-01-19 22:10:42 -06:00
|
|
|
|
|
|
|
|
2011-02-15 13:10:38 -06:00
|
|
|
class LanguageAwareTransport(Transport):
|
|
|
|
"""Transport sending Accept-Language header"""
|
|
|
|
def get_host_info(self, host):
|
|
|
|
(host, extra_headers, x509) = Transport.get_host_info(self, host)
|
|
|
|
|
2011-02-07 12:26:25 -06:00
|
|
|
try:
|
|
|
|
lang = locale.setlocale(locale.LC_ALL, '').split('.')[0].lower()
|
|
|
|
except locale.Error:
|
|
|
|
# fallback to default locale
|
|
|
|
lang = 'en_us'
|
2011-02-15 13:10:38 -06:00
|
|
|
|
|
|
|
if not isinstance(extra_headers, list):
|
|
|
|
extra_headers = []
|
|
|
|
|
|
|
|
extra_headers.append(
|
|
|
|
('Accept-Language', lang.replace('_', '-'))
|
|
|
|
)
|
2011-10-20 10:29:26 -05:00
|
|
|
extra_headers.append(
|
|
|
|
('Referer', 'https://%s/ipa/xml' % str(host))
|
|
|
|
)
|
2011-02-15 13:10:38 -06:00
|
|
|
|
|
|
|
return (host, extra_headers, x509)
|
|
|
|
|
|
|
|
class SSLTransport(LanguageAwareTransport):
|
2009-02-19 16:20:37 -06:00
|
|
|
"""Handles an HTTPS transaction to an XML-RPC server."""
|
|
|
|
|
|
|
|
def make_connection(self, host):
|
2010-08-31 15:59:27 -05:00
|
|
|
host, self._extra_headers, x509 = self.get_host_info(host)
|
|
|
|
host, self._extra_headers, x509 = self.get_host_info(host)
|
|
|
|
# Python 2.7 changed the internal class used in xmlrpclib from
|
|
|
|
# HTTP to HTTPConnection. We need to use the proper subclass
|
|
|
|
(major, minor, micro, releaselevel, serial) = sys.version_info
|
|
|
|
if major == 2 and minor < 7:
|
|
|
|
conn = NSSHTTPS(host, 443, dbdir="/etc/pki/nssdb")
|
|
|
|
else:
|
|
|
|
conn = NSSConnection(host, 443, dbdir="/etc/pki/nssdb")
|
2010-05-31 06:40:17 -05:00
|
|
|
conn.connect()
|
|
|
|
return conn
|
2009-02-19 16:20:37 -06:00
|
|
|
|
|
|
|
class KerbTransport(SSLTransport):
|
2009-01-22 15:00:37 -06:00
|
|
|
"""
|
|
|
|
Handles Kerberos Negotiation authentication to an XML-RPC server.
|
|
|
|
"""
|
2012-02-15 10:06:54 -06:00
|
|
|
flags = kerberos.GSS_C_MUTUAL_FLAG | kerberos.GSS_C_SEQUENCE_FLAG
|
2009-01-22 15:00:37 -06:00
|
|
|
|
2009-04-13 17:01:58 -05:00
|
|
|
def _handle_exception(self, e, service=None):
|
|
|
|
(major, minor) = ipautil.get_gsserror(e)
|
|
|
|
if minor[1] == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.ServiceError(service=service)
|
2009-04-13 17:01:58 -05:00
|
|
|
elif minor[1] == KRB5_FCC_NOFILE:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.NoCCacheError()
|
2009-04-13 17:01:58 -05:00
|
|
|
elif minor[1] == KRB5KRB_AP_ERR_TKT_EXPIRED:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.TicketExpired()
|
2009-04-13 17:01:58 -05:00
|
|
|
elif minor[1] == KRB5_FCC_PERM:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.BadCCachePerms()
|
2009-04-13 17:01:58 -05:00
|
|
|
elif minor[1] == KRB5_CC_FORMAT:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.BadCCacheFormat()
|
2009-04-13 17:01:58 -05:00
|
|
|
elif minor[1] == KRB5_REALM_CANT_RESOLVE:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.CannotResolveKDC()
|
2009-04-13 17:01:58 -05:00
|
|
|
else:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.KerberosError(major=major, minor=minor)
|
2009-04-13 17:01:58 -05:00
|
|
|
|
2009-01-22 15:00:37 -06:00
|
|
|
def get_host_info(self, host):
|
2009-02-19 16:20:37 -06:00
|
|
|
(host, extra_headers, x509) = SSLTransport.get_host_info(self, host)
|
2009-01-22 15:00:37 -06:00
|
|
|
|
|
|
|
# Set the remote host principal
|
|
|
|
service = "HTTP@" + host.split(':')[0]
|
|
|
|
|
|
|
|
try:
|
2012-02-15 10:06:54 -06:00
|
|
|
(rc, vc) = kerberos.authGSSClientInit(service, self.flags)
|
2009-01-22 15:00:37 -06:00
|
|
|
except kerberos.GSSError, e:
|
2009-04-13 17:01:58 -05:00
|
|
|
self._handle_exception(e)
|
2009-01-22 15:00:37 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
kerberos.authGSSClientStep(vc, "")
|
|
|
|
except kerberos.GSSError, e:
|
2009-04-13 17:01:58 -05:00
|
|
|
self._handle_exception(e, service=service)
|
2009-01-22 15:00:37 -06:00
|
|
|
|
2011-02-15 13:10:38 -06:00
|
|
|
if not isinstance(extra_headers, list):
|
|
|
|
extra_headers = []
|
|
|
|
|
|
|
|
for (h, v) in extra_headers:
|
|
|
|
if h == 'Authorization':
|
|
|
|
extra_headers.remove((h, v))
|
|
|
|
break
|
|
|
|
|
|
|
|
extra_headers.append(
|
2009-01-22 15:00:37 -06:00
|
|
|
('Authorization', 'negotiate %s' % kerberos.authGSSClientResponse(vc))
|
2011-02-15 13:10:38 -06:00
|
|
|
)
|
2009-01-22 15:00:37 -06:00
|
|
|
|
|
|
|
return (host, extra_headers, x509)
|
|
|
|
|
|
|
|
|
2012-02-15 10:06:54 -06:00
|
|
|
class DelegatedKerbTransport(KerbTransport):
|
|
|
|
"""
|
|
|
|
Handles Kerberos Negotiation authentication and TGT delegation to an
|
|
|
|
XML-RPC server.
|
|
|
|
"""
|
|
|
|
flags = kerberos.GSS_C_DELEG_FLAG | kerberos.GSS_C_MUTUAL_FLAG | \
|
|
|
|
kerberos.GSS_C_SEQUENCE_FLAG
|
|
|
|
|
2009-01-23 19:02:32 -06:00
|
|
|
class xmlclient(Connectible):
|
2009-01-19 22:10:42 -06:00
|
|
|
"""
|
2009-01-23 14:08:24 -06:00
|
|
|
Forwarding backend plugin for XML-RPC client.
|
|
|
|
|
|
|
|
Also see the `ipaserver.rpcserver.xmlserver` plugin.
|
2009-01-19 22:10:42 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(xmlclient, self).__init__()
|
|
|
|
self.__errors = dict((e.errno, e) for e in public_errors)
|
|
|
|
|
2010-07-26 16:54:38 -05:00
|
|
|
def reconstruct_url(self):
|
|
|
|
"""
|
|
|
|
The URL directly isn't stored in the ServerProxy. We can't store
|
|
|
|
it in the connection object itself but we can reconstruct it
|
|
|
|
from the ServerProxy.
|
|
|
|
"""
|
|
|
|
if not hasattr(self.conn, '_ServerProxy__transport'):
|
|
|
|
return None
|
2012-02-15 10:06:54 -06:00
|
|
|
if type(self.conn._ServerProxy__transport) in (KerbTransport, DelegatedKerbTransport):
|
2010-07-26 16:54:38 -05:00
|
|
|
scheme = "https"
|
|
|
|
else:
|
|
|
|
scheme = "http"
|
2011-09-30 03:09:55 -05:00
|
|
|
server = '%s://%s%s' % (scheme, ipautil.format_netloc(self.conn._ServerProxy__host), self.conn._ServerProxy__handler)
|
2010-07-26 16:54:38 -05:00
|
|
|
return server
|
|
|
|
|
|
|
|
def get_url_list(self):
|
|
|
|
"""
|
|
|
|
Create a list of urls consisting of the available IPA servers.
|
|
|
|
"""
|
|
|
|
# the configured URL defines what we use for the discovered servers
|
|
|
|
(scheme, netloc, path, params, query, fragment) = urlparse.urlparse(self.env.xmlrpc_uri)
|
|
|
|
servers = []
|
|
|
|
name = '_ldap._tcp.%s.' % self.env.domain
|
|
|
|
rs = dnsclient.query(name, dnsclient.DNS_C_IN, dnsclient.DNS_T_SRV)
|
|
|
|
for r in rs:
|
|
|
|
if r.dns_type == dnsclient.DNS_T_SRV:
|
|
|
|
rsrv = r.rdata.server.rstrip('.')
|
2011-09-30 03:09:55 -05:00
|
|
|
servers.append('https://%s%s' % (ipautil.format_netloc(rsrv), path))
|
2010-07-26 16:54:38 -05:00
|
|
|
servers = list(set(servers))
|
|
|
|
# the list/set conversion won't preserve order so stick in the
|
|
|
|
# local config file version here.
|
2011-09-19 11:37:27 -05:00
|
|
|
cfg_server = self.env.xmlrpc_uri
|
|
|
|
if cfg_server in servers:
|
|
|
|
# make sure the configured master server is there just once and
|
|
|
|
# it is the first one
|
|
|
|
servers.remove(cfg_server)
|
|
|
|
servers.insert(0, cfg_server)
|
|
|
|
else:
|
|
|
|
servers.insert(0, cfg_server)
|
|
|
|
|
2010-07-26 16:54:38 -05:00
|
|
|
return servers
|
|
|
|
|
2012-02-15 10:06:54 -06:00
|
|
|
def create_connection(self, ccache=None, verbose=False, fallback=True,
|
|
|
|
delegate=False):
|
2010-07-26 16:54:38 -05:00
|
|
|
servers = self.get_url_list()
|
|
|
|
serverproxy = None
|
|
|
|
for server in servers:
|
|
|
|
kw = dict(allow_none=True, encoding='UTF-8')
|
|
|
|
kw['verbose'] = verbose
|
|
|
|
if server.startswith('https://'):
|
2012-02-15 10:06:54 -06:00
|
|
|
if delegate:
|
|
|
|
kw['transport'] = DelegatedKerbTransport()
|
|
|
|
else:
|
|
|
|
kw['transport'] = KerbTransport()
|
2011-02-15 13:10:38 -06:00
|
|
|
else:
|
|
|
|
kw['transport'] = LanguageAwareTransport()
|
2010-07-26 16:54:38 -05:00
|
|
|
self.log.info('trying %s' % server)
|
|
|
|
serverproxy = ServerProxy(server, **kw)
|
|
|
|
if len(servers) == 1 or not fallback:
|
|
|
|
# if we have only 1 server to try then let the main
|
|
|
|
# requester handle any errors
|
|
|
|
return serverproxy
|
|
|
|
try:
|
|
|
|
command = getattr(serverproxy, 'ping')
|
2011-05-17 14:09:39 -05:00
|
|
|
try:
|
|
|
|
response = command()
|
|
|
|
except Fault, e:
|
|
|
|
e = decode_fault(e)
|
|
|
|
if e.faultCode in self.__errors:
|
|
|
|
error = self.__errors[e.faultCode]
|
|
|
|
raise error(message=e.faultString)
|
|
|
|
else:
|
|
|
|
raise UnknownError(
|
|
|
|
code=e.faultCode,
|
|
|
|
error=e.faultString,
|
|
|
|
server=server,
|
|
|
|
)
|
2010-07-26 16:54:38 -05:00
|
|
|
# We don't care about the response, just that we got one
|
|
|
|
break
|
|
|
|
except KerberosError, krberr:
|
|
|
|
# kerberos error on one server is likely on all
|
|
|
|
raise errors.KerberosError(major=str(krberr), minor='')
|
|
|
|
except Exception, e:
|
|
|
|
if not fallback:
|
|
|
|
raise e
|
|
|
|
serverproxy = None
|
|
|
|
|
|
|
|
if serverproxy is None:
|
|
|
|
raise NetworkError(uri='any of the configured servers', error=', '.join(servers))
|
|
|
|
return serverproxy
|
2009-01-22 15:00:37 -06:00
|
|
|
|
2009-01-23 19:02:32 -06:00
|
|
|
def destroy_connection(self):
|
|
|
|
pass
|
2009-01-22 15:00:37 -06:00
|
|
|
|
2009-01-19 22:10:42 -06:00
|
|
|
def forward(self, name, *args, **kw):
|
|
|
|
"""
|
|
|
|
Forward call to command named ``name`` over XML-RPC.
|
|
|
|
|
|
|
|
This method will encode and forward an XML-RPC request, and will then
|
|
|
|
decode and return the corresponding XML-RPC response.
|
|
|
|
|
|
|
|
:param command: The name of the command being forwarded.
|
|
|
|
:param args: Positional arguments to pass to remote command.
|
|
|
|
:param kw: Keyword arguments to pass to remote command.
|
|
|
|
"""
|
|
|
|
if name not in self.Command:
|
|
|
|
raise ValueError(
|
|
|
|
'%s.forward(): %r not in api.Command' % (self.name, name)
|
|
|
|
)
|
2010-07-26 16:54:38 -05:00
|
|
|
server = self.reconstruct_url()
|
|
|
|
self.info('Forwarding %r to server %r', name, server)
|
2009-01-23 19:02:32 -06:00
|
|
|
command = getattr(self.conn, name)
|
2010-03-26 04:56:53 -05:00
|
|
|
params = [args, kw]
|
2009-01-19 22:10:42 -06:00
|
|
|
try:
|
2009-01-22 16:41:54 -06:00
|
|
|
response = command(*xml_wrap(params))
|
2009-01-19 22:10:42 -06:00
|
|
|
return xml_unwrap(response)
|
|
|
|
except Fault, e:
|
2009-01-22 16:41:54 -06:00
|
|
|
e = decode_fault(e)
|
2009-01-22 15:00:37 -06:00
|
|
|
self.debug('Caught fault %d from server %s: %s', e.faultCode,
|
2010-07-26 16:54:38 -05:00
|
|
|
server, e.faultString)
|
2009-01-19 22:10:42 -06:00
|
|
|
if e.faultCode in self.__errors:
|
|
|
|
error = self.__errors[e.faultCode]
|
|
|
|
raise error(message=e.faultString)
|
|
|
|
raise UnknownError(
|
|
|
|
code=e.faultCode,
|
|
|
|
error=e.faultString,
|
2010-07-26 16:54:38 -05:00
|
|
|
server=server,
|
2009-01-19 22:10:42 -06:00
|
|
|
)
|
2010-05-31 06:40:17 -05:00
|
|
|
except NSPRError, e:
|
2010-07-26 16:54:38 -05:00
|
|
|
raise NetworkError(uri=server, error=str(e))
|
2009-07-10 14:33:07 -05:00
|
|
|
except ProtocolError, e:
|
2010-07-26 16:54:38 -05:00
|
|
|
raise NetworkError(uri=server, error=e.errmsg)
|
2010-10-14 16:21:11 -05:00
|
|
|
except socket.error, e:
|
|
|
|
raise NetworkError(uri=server, error=str(e))
|
2011-01-17 15:23:53 -06:00
|
|
|
except (OverflowError, TypeError), e:
|
|
|
|
raise XMLRPCMarshallError(error=str(e))
|