Package proxy :: Module rhnAuthCacheClient
[hide private]
[frames] | no frames]

Source Code for Module proxy.rhnAuthCacheClient

  1  # rhnAuthCacheClient.py 
  2  #------------------------------------------------------------------------------- 
  3  # Implements a client-side 'remote shelf' caching object used for 
  4  # authentication token caching. 
  5  # (Client, meaning, a client to the authCache daemon) 
  6  # 
  7  # Copyright (c) 2008--2017 Red Hat, Inc. 
  8  # 
  9  # This software is licensed to you under the GNU General Public License, 
 10  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 11  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 12  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 13  # along with this software; if not, see 
 14  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 15  # 
 16  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 17  # granted to use or replicate Red Hat trademarks that are incorporated 
 18  # in this software or its documentation. 
 19  #------------------------------------------------------------------------------- 
 20   
 21  ## language imports 
 22  import socket 
 23  import sys 
 24  from xmlrpclib import Fault 
 25   
 26  ## local imports 
 27  from spacewalk.common.rhnLog import log_debug, log_error 
 28  from spacewalk.common.rhnTB import Traceback 
 29  from spacewalk.common.rhnException import rhnFault 
 30  from spacewalk.common.rhnTranslate import _ 
 31  from rhnAuthProtocol import CommunicationError, send, recv 
 32   
 33  # 
 34  # Protocol description: 
 35  # 1. Send the size of the data as a long (4 bytes), in network order 
 36  # 2. Send the data 
 37  # 
 38   
 39  # Shamelessly stolen from xmlrpclib.xmlrpc 
 40   
 41   
42 -class _Method:
43 44 """ Bind XML-RPC to an RPC Server 45 46 Some magic to bind an XML-RPC method to an RPC server. 47 Supports "nested" methods (e.g. examples.getStateName). 48 """ 49 # pylint: disable=R0903 50
51 - def __init__(self, msend, name):
52 self.__send = msend 53 self.__name = name
54
55 - def __getattr__(self, name):
56 return _Method(self.__send, "%s.%s" % (self.__name, name))
57
58 - def __call__(self, *args):
59 return self.__send(self.__name, args)
60
61 - def __str__(self):
62 return "<_Method instance at %s>" % id(self)
63 64 __repr__ = __str__
65 66
67 -class Shelf:
68 69 """ Client authenication temp. db. 70 71 Main class that the client side (client to the caching daemon) has to 72 instantiate to expose the proper API. Basically, the API is a dictionary. 73 """ 74 # pylint: disable=R0903 75
76 - def __init__(self, server_addr):
77 log_debug(6, server_addr) 78 self.serverAddr = server_addr
79
80 - def __request(self, methodname, params):
81 # pylint: disable=R0915 82 log_debug(6, methodname, params) 83 # Init the socket 84 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 85 86 try: 87 sock.connect(self.serverAddr) 88 except socket.error, e: 89 sock.close() 90 methodname = None 91 log_error("Error connecting to the auth cache: %s" % str(e)) 92 Traceback("Shelf.__request", extra=""" 93 Error connecting to the the authentication cache daemon. 94 Make sure it is started on %s""" % str(self.serverAddr)) 95 # FIXME: PROBLEM: this rhnFault will never reach the client 96 raise rhnFault(1000, 97 _("Spacewalk Proxy error (issues connecting to auth cache). " 98 "Please contact your system administrator")), None, sys.exc_info()[2] 99 100 wfile = sock.makefile("w") 101 102 try: 103 send(wfile, methodname, None, *params) 104 except CommunicationError: 105 wfile.close() 106 sock.close() 107 Traceback("Shelf.__request", 108 extra="Encountered a CommunicationError") 109 raise 110 except socket.error: 111 wfile.close() 112 sock.close() 113 log_error("Error communicating to the auth cache: %s" % str(e)) 114 Traceback("Shelf.__request", extra="""\ 115 Error sending to the authentication cache daemon. 116 Make sure the authentication cache daemon is started""") 117 # FIXME: PROBLEM: this rhnFault will never reach the client 118 raise rhnFault(1000, 119 _("Spacewalk Proxy error (issues connecting to auth cache). " 120 "Please contact your system administrator")), None, sys.exc_info()[2] 121 122 wfile.close() 123 124 rfile = sock.makefile("r") 125 try: 126 params, methodname = recv(rfile) 127 except CommunicationError, e: 128 log_error(e.faultString) 129 rfile.close() 130 sock.close() 131 log_error("Error communicating to the auth cache: %s" % str(e)) 132 Traceback("Shelf.__request", extra="""\ 133 Error receiving from the authentication cache daemon. 134 Make sure the authentication cache daemon is started""") 135 # FIXME: PROBLEM: this rhnFault will never reach the client 136 raise rhnFault(1000, 137 _("Spacewalk Proxy error (issues communicating to auth cache). " 138 "Please contact your system administrator")), None, sys.exc_info()[2] 139 except Fault, e: 140 rfile.close() 141 sock.close() 142 # If e.faultCode is 0, it's another exception 143 if e.faultCode != 0: 144 # Treat is as a regular xmlrpc fault 145 raise 146 147 _dict = e.faultString 148 if not isinstance(_dict, type({})): 149 # Not the expected type 150 raise 151 152 if not _dict.has_key('name'): 153 # Doesn't look like a marshalled exception 154 raise 155 156 name = _dict['name'] 157 args = _dict.get('args') 158 # Look up the exception 159 if not hasattr(__builtins__, name): 160 # Unknown exception name 161 raise 162 163 # Instantiate the exception object 164 import new 165 _dict = {'args': args} 166 # pylint: disable=bad-option-value,nonstandard-exception 167 raise new.instance(getattr(__builtins__, name), _dict), None, sys.exc_info()[2] 168 169 return params[0]
170
171 - def __getattr__(self, name):
172 log_debug(6, name) 173 return _Method(self.__request, name)
174
175 - def __str__(self):
176 return "<Remote-Shelf instance at %s>" % id(self)
177 178 179 #------------------------------------------------------------------------------- 180 # test code 181 # pylint: disable=E0012, C0411, C0413, E1136, C0412 182 # pylint: disable=bad-option-value,unsupported-assignment-operation 183 if __name__ == '__main__': 184 from spacewalk.common.rhnConfig import initCFG 185 initCFG("proxy.broker") 186 s = Shelf(('localhost', 9999)) 187 s['1234'] = [1, 2, 3, 4, None, None] 188 s['blah'] = 'testing 1 2 3' 189 print 'Cached object s["1234"] = %s' % str(s['1234']) 190 print 'Cached object s["blah"] = %s' % str(s['blah']) 191 print s.has_key("asdfrasdf") 192 193 # print 194 # print 'And this will bomb (attempt to get non-existant data:' 195 # s["DOESN'T EXIST!!!"] 196 #------------------------------------------------------------------------------- 197