Package up2date_client :: Module rhnserver
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.rhnserver

  1   
  2  #   rhn-client-tools 
  3  # 
  4  # Copyright (c) 2006--2016 Red Hat, Inc. 
  5  # 
  6  #   This program is free software; you can redistribute it and/or modify 
  7  #   it under the terms of the GNU General Public License as published by 
  8  #   the Free Software Foundation; version 2 of the License. 
  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, write to the Free Software 
 17  #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 18  #   02110-1301  USA 
 19  # 
 20  # In addition, as a special exception, the copyright holders give 
 21  # permission to link the code of portions of this program with the 
 22  # OpenSSL library under certain conditions as described in each 
 23  # individual source file, and distribute linked combinations 
 24  # including the two. 
 25  # You must obey the GNU General Public License in all respects 
 26  # for all of the code used other than OpenSSL.  If you modify 
 27  # file(s) with this exception, you may extend this exception to your 
 28  # version of the file(s), but you are not obligated to do so.  If you 
 29  # do not wish to do so, delete this exception statement from your 
 30  # version.  If you delete this exception statement from all source 
 31  # files in the program, then also delete it here. 
 32   
 33   
 34  from rhn.tb import raise_with_tb 
 35  from up2date_client import rpcServer 
 36  from up2date_client import up2dateErrors 
 37  from up2date_client import capabilities 
 38  import sys 
 39  import OpenSSL 
 40   
 41  try: # python2 
 42      import xmlrpclib 
 43  except ImportError: # python3 
 44      import xmlrpc.client as xmlrpclib 
 45   
46 -class _DoCallWrapper(object):
47 48 """ 49 A callable object that will handle multiple levels of attributes, 50 and catch exceptions. 51 """ 52
53 - def __init__(self, server, method_name):
54 self._server = server 55 self._method_name = method_name
56
57 - def __getattr__(self, method_name):
58 """ Recursively build up the method name to pass to the server. """ 59 return _DoCallWrapper(self._server, 60 "%s.%s" % (self._method_name, method_name))
61
62 - def __call__(self, *args, **kwargs):
63 """ Call the method. Catch faults and translate them. """ 64 method = getattr(self._server, self._method_name) 65 66 try: 67 return rpcServer.doCall(method, *args, **kwargs) 68 except xmlrpclib.Fault: 69 raise_with_tb(self.__exception_from_fault(sys.exc_info()[1])) 70 except OpenSSL.SSL.Error: 71 # TODO This should probably be moved to rhnlib and raise an 72 # exception that subclasses OpenSSL.SSL.Error 73 # TODO Is there a better way to detect cert failures? 74 error = str(sys.exc_info()[1]) 75 error = error.strip("[()]") 76 pieces = error.split(',') 77 message = "" 78 if len(pieces) > 2: 79 message = pieces[2] 80 elif len(pieces) == 2: 81 message = pieces[1] 82 message = message.strip(" '") 83 if message == 'certificate verify failed': 84 raise_with_tb(up2dateErrors.SSLCertificateVerifyFailedError()) 85 else: 86 raise_with_tb(up2dateErrors.NetworkError(message))
87
88 - def __exception_from_fault(self, fault):
89 if fault.faultCode == -3: 90 # This username is already taken, or the password is incorrect. 91 exception = up2dateErrors.AuthenticationOrAccountCreationError(fault.faultString) 92 elif fault.faultCode == -2: 93 # Invalid username and password combination. 94 exception = up2dateErrors.AuthenticationOrAccountCreationError(fault.faultString) 95 elif fault.faultCode == -110: 96 # Account is disabled 97 exception = up2dateErrors.AuthenticationOrAccountCreationError(fault.faultString) 98 elif fault.faultCode == -1: 99 exception = up2dateErrors.UnknownMethodException(fault.faultString) 100 elif fault.faultCode == -13: 101 # Username is too short. 102 exception = up2dateErrors.LoginMinLengthError(fault.faultString) 103 elif fault.faultCode == -14: 104 # too short password 105 exception = up2dateErrors.PasswordMinLengthError( 106 fault.faultString) 107 elif fault.faultCode == -15: 108 # bad chars in username 109 exception = up2dateErrors.ValidationError(fault.faultString) 110 elif fault.faultCode == -16: 111 # Invalid product registration code. 112 # TODO Should this really be a validation error? 113 exception = up2dateErrors.ValidationError(fault.faultString) 114 elif fault.faultCode == -19: 115 # invalid 116 exception = up2dateErrors.NoBaseChannelError(fault.faultString) 117 elif fault.faultCode == -31: 118 # No entitlement 119 exception = up2dateErrors.InsuffMgmntEntsError(fault.faultString) 120 elif fault.faultCode == -36: 121 # rhnException.py says this means "Invalid action." 122 # TODO find out which is right 123 exception = up2dateErrors.PasswordError(fault.faultString) 124 elif abs(fault.faultCode) == 49: 125 exception = up2dateErrors.AbuseError(fault.faultString) 126 elif abs(fault.faultCode) == 60: 127 exception = up2dateErrors.AuthenticationTicketError(fault.faultString) 128 elif abs(fault.faultCode) == 74: 129 exception = up2dateErrors.RegistrationDeniedError() 130 elif abs(fault.faultCode) == 105: 131 exception = up2dateErrors.RhnUuidUniquenessError(fault.faultString) 132 elif fault.faultCode == 99: 133 exception = up2dateErrors.DelayError(fault.faultString) 134 elif abs(fault.faultCode) == 91: 135 exception = up2dateErrors.InsuffMgmntEntsError(fault.faultString) 136 elif fault.faultCode == -106: 137 # Invalid username. 138 exception = up2dateErrors.ValidationError(fault.faultString) 139 elif fault.faultCode == -600: 140 # Invalid username. 141 exception = up2dateErrors.InvalidRegistrationNumberError(fault.faultString) 142 elif fault.faultCode == -601: 143 # No entitlements associated with given hardware info 144 exception = up2dateErrors.NotEntitlingError(fault.faultString) 145 elif fault.faultCode == -602: 146 # No entitlements associated with reg num 147 exception = up2dateErrors.NotEntitlingError(fault.faultString) 148 elif fault.faultCode == -2001 or fault.faultCode == -700: 149 exception = up2dateErrors.AuthenticationOrAccountCreationError( 150 fault.faultString) 151 elif fault.faultCode == -701: 152 exception = up2dateErrors.PasswordMaxLengthError( 153 fault.faultString) 154 elif fault.faultCode == -61: 155 exception = up2dateErrors.ActivationKeyUsageLimitError( 156 fault.faultString) 157 elif fault.faultCode == -5: 158 exception = up2dateErrors.UnableToCreateUser( 159 fault.faultString) 160 else: 161 exception = up2dateErrors.CommunicationError(fault.faultString) 162 163 return exception
164 165
166 -class RhnServer(object):
167 168 """ 169 An rpc server object that calls doCall for you, and catches lower 170 level exceptions 171 """ 172
173 - def __init__(self, serverOverride=None, timeout=None, rpcServerOverride=None):
174 if rpcServerOverride is None: 175 self._server = rpcServer.getServer(serverOverride=serverOverride, 176 timeout=timeout) 177 else: 178 self._server = rpcServerOverride 179 self._capabilities = None
180
181 - def __get_capabilities(self):
182 if self._capabilities is None: 183 headers = self._server.get_response_headers() 184 if headers is None: 185 self.registration.welcome_message() 186 headers = self._server.get_response_headers() 187 self._capabilities = capabilities.Capabilities() 188 self._capabilities.populate(headers) 189 return self._capabilities
190 191 capabilities = property(__get_capabilities) 192
193 - def add_header(self, key, value):
194 self._server.add_header(key, value)
195
196 - def __getattr__(self, method_name):
197 """ Return a callable object that will do the work for us. """ 198 return _DoCallWrapper(self._server, method_name)
199