def _executemany(self, *args, **kwargs): if not kwargs: return 0 params = UserDictCase(kwargs) # First break all the incoming keyword arg lists into individual # hashes: all_kwargs = [] for key in list(params.keys()): if len(all_kwargs) < len(params[key]): for i in range(len(params[key])): all_kwargs.append({}) i = 0 for val in params[key]: all_kwargs[i][key] = val i = i + 1 self._real_cursor.executemany(self.sql, all_kwargs) self.description = self._real_cursor.description rowcount = self._real_cursor.rowcount return rowcount
def _execute_(self, args, kwargs): """ Oracle specific execution of the query. """ # TODO: args appears unused, raise exception if we see any? # Only copy the arguments we're interested in _p = UserDictCase(kwargs) params = {} # Check that all required parameters were provided: # NOTE: bindnames() is Oracle specific: for k in self._real_cursor.bindnames(): if not _p.has_key(k): # Raise the fault ourselves raise sql_base.SQLError(1008, 'Not all variables bound', k) params[k] = to_string(_p[k]) # cx_Oracle expects the first arg to be the statement and no # positional args: self._real_cursor.execute(*(None, ), **params) self.description = self._real_cursor.description return self._real_cursor.rowcount
def __cacheClientSessionToken(self, headers): """pull session token from headers and push to caching daemon. """ log_debug(1) # Get the server ID if not headers.has_key('X-RHN-Server-ID'): log_debug(3, "Client server ID not found in headers") # XXX: no client server ID in headers, should we care? #raise rhnFault(1000, _("Client Server ID not found in headers!")) return serverId = 'X-RHN-Server-ID' self.clientServerId = headers[serverId] token = UserDictCase() # The session token contains everything that begins with # "x-rhn-auth" prefix = "x-rhn-auth" l = len(prefix) tokenKeys = [x for x in headers.keys() if x[:l].lower() == prefix] for k in tokenKeys: if k.lower() == 'x-rhn-auth-channels': # Multivalued header #values = headers.getHeaderValues(k) values = self._get_header(k) token[k] = [x.split(':') for x in values] else: # Single-valued header token[k] = headers[k] # Dump the proxy's clock skew in the dict serverTime = float(token['X-RHN-Auth-Server-Time']) token["X-RHN-Auth-Proxy-Clock-Skew"] = time.time() - serverTime # Save the token self.proxyAuth.set_client_token(self.clientServerId, token) return token
def __init__(self): #start_init = time.time() self.filesuploaded = False self.options = rhnConfig.initCFG('server') print self.options mytime = time.time() self.test_username = username or ("test_username_%.3f" % mytime) self.test_password = password or ("test_password_%.3f" % mytime) self.test_email = email or ("%s@test_domain.com" % self.test_username) self.channel_arch = 'unittestarch' self.roles = ['org_admin'] rhnFlags.set('outputTransportOptions', UserDictCase()) self._init_db() self._init_org() self._init_user(self.roles) self._init_server() self._init_channels() self._init_up2date()
def __init__(self, transfer=0, encoding=0, refreshCallback=None, progressCallback=None, use_datetime=None, timeout=None): self._use_builtin_types = False self._transport_flags = {'transfer': 0, 'encoding': 0} self.set_transport_flags(transfer=transfer, encoding=encoding) self._headers = UserDictCase() self.verbose = 0 self.connection = None self.method = "POST" self._lang = None self.refreshCallback = refreshCallback self.progressCallback = progressCallback self.bufferSize = 16384 self.headers_in = None self.response_status = None self.response_reason = None self._redirected = None self._use_datetime = use_datetime self.timeout = timeout
def __checkAuthSessionTokenCache(self, token, channel): """ Authentication / authorize the channel """ log_debug(2, token, channel) self.clientServerId = token['X-RHN-Server-ID'] cachedToken = self.proxyAuth.get_client_token(self.clientServerId) if not cachedToken: # maybe client logged in through different load-balanced proxy # try to update the cache an try again cachedToken = self.proxyAuth.update_client_token_if_valid( self.clientServerId, token) if not cachedToken: msg = _("Invalid session key - server ID not found in cache: %s") \ % self.clientServerId log_error(msg) raise rhnFault(33, msg) self.cachedClientInfo = UserDictCase(cachedToken) clockSkew = self.cachedClientInfo["X-RHN-Auth-Proxy-Clock-Skew"] del self.cachedClientInfo["X-RHN-Auth-Proxy-Clock-Skew"] # Add the server id self.authChannels = self.cachedClientInfo['X-RHN-Auth-Channels'] del self.cachedClientInfo['X-RHN-Auth-Channels'] self.cachedClientInfo['X-RHN-Server-ID'] = self.clientServerId log_debug(4, 'Retrieved token from cache: %s' % self.cachedClientInfo) # Compare the two things if not _dictEquals(token, self.cachedClientInfo, ['X-RHN-Auth-Channels']): # Maybe the client logged in through a different load-balanced # proxy? Check validity of the token the client passed us. updatedToken = self.proxyAuth.update_client_token_if_valid( self.clientServerId, token) # fix up the updated token the same way we did above if updatedToken: self.cachedClientInfo = UserDictCase(updatedToken) clockSkew = self.cachedClientInfo[ "X-RHN-Auth-Proxy-Clock-Skew"] del self.cachedClientInfo["X-RHN-Auth-Proxy-Clock-Skew"] self.authChannels = self.cachedClientInfo[ 'X-RHN-Auth-Channels'] del self.cachedClientInfo['X-RHN-Auth-Channels'] self.cachedClientInfo['X-RHN-Server-ID'] = \ self.clientServerId log_debug( 4, 'Retrieved token from cache: %s' % self.cachedClientInfo) if not updatedToken or not _dictEquals( token, self.cachedClientInfo, ['X-RHN-Auth-Channels']): log_debug(3, "Session tokens different") raise rhnFault(33) # Invalid session key # Check the expiration serverTime = float(token['X-RHN-Auth-Server-Time']) offset = float(token['X-RHN-Auth-Expire-Offset']) if time.time() > serverTime + offset + clockSkew: log_debug(3, "Session token has expired") raise rhnFault(34) # Session key has expired # Only autherized channels are the ones stored in the cache. authChannels = [x[0] for x in self.authChannels] log_debug(4, "Auth channels: '%s'" % authChannels) # Check the authorization if channel not in authChannels: log_debug(4, "Not subscribed to channel %s; unauthorized" % channel) raise rhnFault(35, _('Unauthorized channel access requested.'))
def __local_GET_handler(self, req): """ GETs: authenticate user, and service local GETs. if not a local fetch, return None """ log_debug(2, 'request method: %s' % req.method) # Early test to check if this is a request the proxy can handle # Can we serve this request? if req.method != "GET" or not CFG.PKG_DIR: # Don't know how to handle this return None # Tiny-url kickstart requests (for server kickstarts, aka not profiles) # have been name munged and we've already sent a HEAD request to the # Satellite to get a checksum for the rpm so we can find it in the # squid cache. # Original url looks like /ty/bSWE7qIq/Packages/policycoreutils-2.0.83 # -19.39.el6.x86_64.rpm which gets munged to be /ty-cksm/ddb43838ad58 # d74dc95badef543cd96459b8bb37ff559339de58ec8dbbd1f18b/Packages/polic # ycoreutils-2.0.83-19.39.el6.x86_64.rpm args = req.path_info.split('/') # urlparse returns a ParseResult, index 2 is the path if re.search('^' + URI_PREFIX_KS_CHECKSUM, urlparse(self.rhnParent)[2]): # We *ONLY* locally cache RPMs for kickstarts if len(args) < 3 or args[2] != 'Packages': return None req_type = 'tinyurl' reqident = args[1] reqaction = 'getPackage' reqparams = [args[-1]] self.cachedClientInfo = UserDictCase() elif (len(args) > 3 and args[1] == 'dist'): # This is a kickstart request req_type = 'ks-dist' reqident, reqaction, reqparams = self._split_ks_url(req) self.cachedClientInfo = UserDictCase() else: # Some other type of request (req_type, reqident, reqaction, reqparams) = self._split_url(req) if req_type is None or (req_type not in [ '$RHN', 'GET-REQ', 'tinyurl', 'ks-dist' ]): # not a traditional RHN GET (i.e., it is an arbitrary get) # XXX: there has to be a more elegant way to do this return None # --- LOCAL GET: localFlist = CFG.PROXY_LOCAL_FLIST or [] if reqaction not in localFlist: # Not an action we know how to handle return None # kickstarts don't auth... if req_type in ['$RHN', 'GET-REQ']: # --- AUTH. CHECK: # Check client authentication. If not authenticated, throw # an exception. token = self.__getSessionToken() self.__checkAuthSessionTokenCache(token, reqident) # Is this channel local? for ch in self.authChannels: channel, _version, _isBaseChannel, isLocalChannel = ch[:4] if channel == reqident and str(isLocalChannel) == '1': # Local channel break else: # Not a local channel return None # We have a match; we'll try to serve packages from the local # repository log_debug(3, "Retrieve from local repository.") log_debug(3, req_type, reqident, reqaction, reqparams) result = self.__callLocalRepository(req_type, reqident, reqaction, reqparams) if result is None: log_debug(3, "Not available locally; will try higher up the chain.") else: # Signal that we have to XMLRPC encode the response in apacheHandler rhnFlags.set("NeedEncoding", 1) return result
def __init__(self): self.server = UserDictCase() Packages.__init__(self) History.__init__(self) Hardware.__init__(self) SuseData.__init__(self)
def _querySatelliteForChecksum(self, req): """ Sends a HEAD request to the satellite for the purpose of obtaining the checksum for the requested resource. A (status, checksum) tuple is returned. If status is not apache.OK, checksum will be None. If status is OK, and a checksum is not returned, the old BZ 158236 behavior will be used. """ scheme = SCHEME_HTTP if req.server.port == 443: scheme = SCHEME_HTTPS log_debug(6, "Using scheme: %s" % scheme) # Initiate a HEAD request to the satellite to retrieve the MD5 sum. # Actually, we make the request through our own proxy first, so # that we don't accidentally bypass necessary authentication # routines. Since it's a HEAD request, the proxy will forward it # directly to the satellite like it would a POST request. host = "127.0.0.1" port = req.connection.local_addr[1] connection = self._createConnection(host, port, scheme) if not connection: # Couldn't form the connection. Log an error and revert to the # old BZ 158236 behavior. In order to be as robust as possible, # we won't fail here. log_error('HEAD req - Could not create connection to %s://%s:%s' % (scheme, host, str(port))) return (apache.OK, None) # We obtained the connection successfully. Construct the URL that # we'll connect to. pingURL = "%s://%s:%s%s" % (scheme, host, str(port), req.uri) log_debug(6, "Ping URI: %s" % pingURL) hdrs = UserDictCase() for k in req.headers_in.keys(): if k.lower() != 'range': # we want checksum of whole file hdrs[k] = req.headers_in[k] log_debug(9, "Using existing headers_in", hdrs) connection.request("HEAD", pingURL, None, hdrs) log_debug(6, "Connection made, awaiting response.") # Get the response. response = connection.getresponse() log_debug(6, "Received response status: %s" % response.status) connection.close() if (response.status != apache.HTTP_OK) and ( response.status != apache.HTTP_PARTIAL_CONTENT): # Something bad happened. Return back back to the client. log_debug( 1, "HEAD req - Received error code in reponse: %s" % (str(response.status))) return (response.status, None) # The request was successful. Dig the MD5 checksum out of the headers. responseHdrs = response.msg if not responseHdrs: # No headers?! This shouldn't happen at all. But if it does, # revert to the old # BZ 158236 behavior. log_error("HEAD response - No HTTP headers!") return (apache.OK, None) if not responseHdrs.has_key(HEADER_CHECKSUM): # No checksum was provided. This could happen if a newer # proxy is talking to an older satellite. To keep things # running smoothly, we'll just revert to the BZ 158236 # behavior. log_debug(1, "HEAD response - No X-RHN-Checksum field provided!") return (apache.OK, None) checksum = responseHdrs[HEADER_CHECKSUM] return (apache.OK, checksum)
def __init__(self, req): rhnHandler.__init__(self) dumper.XML_Dumper.__init__(self) self.headers_out = UserDictCase() self._raw_stream = req self._raw_stream.content_type = 'application/octet-stream' self.compress_level = 0 # State machine self._headers_sent = 0 self._is_closed = 0 self._compressed_stream = None self.functions = [ 'arches', 'arches_extra', 'channel_families', 'channels', 'get_comps', 'get_modules', 'channel_packages_short', 'packages_short', 'packages', 'source_packages', 'errata', 'blacklist_obsoletes', 'product_names', 'get_rpm', 'kickstartable_trees', 'get_ks_file', 'orgs', 'support_information', 'suse_products', 'suse_product_channels', 'suse_upgrade_paths', 'suse_product_extensions', 'suse_product_repositories', 'scc_repositories', 'suse_subscriptions', 'cloned_channels', ] self.system_id = None self._channel_family_query_template = """ select cfm.channel_family_id, 0 quantity from rhnChannelFamilyMembers cfm, rhnChannel c, rhnChannelFamily cf where cfm.channel_id = c.id and c.label in (%s) and cfm.channel_family_id = cf.id and cf.label != 'rh-public' and (cf.org_id in (%s) or cf.org_id is null) union select id channel_family_id, NULL quantity from rhnChannelFamily where label = 'rh-public' """ self._channel_family_query_public = """ select id channel_family_id, 0 quantity from rhnChannelFamily where org_id in (%s) or org_id is null """ self._channel_family_query = None
def add_hardware(self, hardware): """ add new hardware """ log_debug(4, hardware) if not hardware: return -1 if type(hardware) == type({}): hardware = UserDictCase(hardware) if not isinstance(hardware, UserDictCase): log_error("argument type is not hash: %s" % hardware) raise TypeError("This function requires a hash as an argument") # validation is important hw_class = hardware.get("class") if hw_class is None: return -1 hw_class = hw_class.lower() class_type = None if hw_class in [ "video", "audio", "audio_hd", "usb", "other", "hd", "floppy", "mouse", "modem", "network", "cdrom", "scsi", "unspec", "scanner", "tape", "capture", "raid", "socket", "keyboard", "printer", "firewire", "ide" ]: class_type = HardwareDevice elif hw_class == "cpu": class_type = CPUDevice elif hw_class == "memory": class_type = MemoryInformation elif hw_class == "dmi": class_type = DMIInformation elif hw_class == "installinfo": class_type = InstallInformation elif hw_class == "netinterfaces": class_type = NetIfaceInformation elif hw_class == "fqdn": class_type = FQDNInformation elif hw_class == "sysinfo": # special case: we got info about a virtual host # where this system is running on SystemInformation(hardware, self) return 0 elif hw_class == "machineinfo": MachineInformation(self.server["id"], self.server["name"], hardware) return 0 else: log_error("UNKNOWN CLASS TYPE `%s'" % hw_class) # Same trick: try-except and raise the exception so that Traceback # can send the e-mail try: raise KeyError("Unknown class type `%s' for hardware '%s'" % (hw_class, hardware)) except: Traceback(mail=1) return # create the new device new_dev = class_type(hardware) if class_type in self.__hardware: _l = self.__hardware[class_type] else: _l = self.__hardware[class_type] = [] _l.append(new_dev) self.__changed = 1 return 0
def __init__(self, uri, transport=None, encoding=None, verbose=0, proxy=None, username=None, password=None, refreshCallback=None, progressCallback=None, timeout=None): # establish a "logical" server connection # # First parse the proxy information if available # if proxy != None: (ph, pp, pu, pw) = get_proxy_info(proxy) if pp is not None: proxy = "%s:%s" % (ph, pp) else: proxy = ph # username and password will override whatever was passed in the # URL if pu is not None and username is None: username = pu if pw is not None and password is None: password = pw self._uri = sstr(uri) self._refreshCallback = None self._progressCallback = None self._bufferSize = None self._proxy = proxy self._username = username self._password = password self._timeout = timeout if len(__version__.split()) > 1: self.rpc_version = __version__.split()[1] else: self.rpc_version = __version__ self._reset_host_handler_and_type() if transport is None: self._allow_redirect = 1 transport = self.default_transport(self._type, proxy, username, password, timeout) else: # # dont allow redirect on unknow transports, that should be # set up independantly # self._allow_redirect = 0 self._redirected = None self.use_handler_path = 1 self._transport = transport self._trusted_cert_files = [] self._lang = None self._encoding = encoding self._verbose = verbose self.set_refresh_callback(refreshCallback) self.set_progress_callback(progressCallback) # referer, which redirect us to new handler self.send_handler = None self._headers = UserDictCase()
def set_info(self, name, value): """ set a certain value for the userinfo field. This is BUTT ugly. """ log_debug(3, name, value) # translation from what the client send us to real names of the fields # in the tables. mapping = { "first_name": "first_names", "position": "title", "title": "prefix" } if not name: return -1 name = name.lower() if type(value) == type(""): value = value.strip() # We have to watch over carefully for different field names # being sent from rhn_register changed = 0 # translation if name in mapping.keys(): name = mapping[name] # Some fields can not have null string values if name in [ "first_names", "last_name", "prefix", # personal_info "address1", "city", "country" ]: # site_info # we require something of it if len(str(value)) == 0: return -1 # fields in personal_info (and some in site) if name in [ "last_name", "first_names", "company", "phone", "fax", "email", "title" ]: self.info[name] = value[:128] changed = 1 elif name == "prefix": values = ["Mr.", "Mrs.", "Ms.", "Dr.", "Hr.", "Sr.", " "] # Now populate a dictinary of valid values valids = UserDictCase() for v in values: # initialize from good values, with and w/o the dot valids[v] = v valids[v[:-1]] = v # commonly encountered values valids["Miss"] = "Miss" valids["Herr"] = "Hr." valids["Sig."] = "Sr." valids["Sir"] = "Mr." # Now check it out if valids.has_key(value): self.info["prefix"] = valids[value] changed = 1 else: log_error("Unknown prefix value `%s'. Assumed `Mr.' instead" % value) self.info["prefix"] = "Mr." changed = 1 # fields in site if name in ["phone", "fax", "zip"]: self.site[name] = value[:32] changed = 1 elif name in [ "city", "country", "alt_first_names", "alt_last_name", "address1", "address2", "email", "last_name", "first_names" ]: if name == "last_name": self.site["alt_last_name"] = value changed = 1 elif name == "first_names": self.site["alt_first_names"] = value changed = 1 else: self.site[name] = value[:128] changed = 1 elif name in ["state"]: # stupid people put stupid things in here too self.site[name] = value[:60] changed = 1 if not changed: log_error("SET_INFO: Unknown info `%s' = `%s'" % (name, value)) return 0
def __init__(self): self.server = UserDictCase() Packages.__init__(self) History.__init__(self) Hardware.__init__(self) SolarisPatches.__init__(self)
#start_init = time.time() self.filesuploaded = False self.options = rhnConfig.initCFG( 'server' ) print self.options mytime = time.time() self.test_username = username or ("test_username_%.3f" % mytime) self.test_password = password or ("test_password_%.3f" % mytime) self.test_email = email or ("%s@test_domain.com" % self.test_username) self.channel_arch = 'unittestarch' self.roles = ['org_admin'] rhnFlags.set( 'outputTransportOptions', UserDictCase() ) self._init_db() self._init_org() self._init_user(self.roles) self._init_server() self._init_channels() self._init_up2date() #Sets up database connection def _init_db( self ): rhnSQL.initDB() #creates an org def _init_org( self ): self.org_id, self.org_name, self.org_password = misc_functions.create_new_org()