Esempio n. 1
0
    def addUpstreamServerRegistration(self, registration):
        """
    Parameters:
     - registration
    """
        self._logger.info(
            "thrift interface received a call to addUpstreamServerRegistration with a registration value of %s"
            % (registration))
        self._validateUpstreamServerRegistration(registration)
        self._validateNoConflictingUpstreamPath(registration)
        nodeName = ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(
            registration)
        nodeData = self._serializeUpstreamServerRegistration(registration)

        with self.lock:
            lock = self.kz.Lock(ezRPKazoo.KZLOCKFILE, "another contender")
            with lock:
                self._logger.info("addUpstreamServerRegistration registering")
                if not self.kz.exists(nodeName):
                    self._logger.info(
                        "Adding node in zookeeper\n\tNode Name: %s\n\tNode Data:%s"
                        % (nodeName, nodeData))
                    self.kz.create(nodeName, nodeData)
                else:
                    self.kz.set(nodeName, nodeData)
                    self._logger.info(
                        "Updating node in zookeeper\n\tNode Name: %s\n\tNode Data:%s"
                        % (nodeName, nodeData))
                self.kz.set(ezRPKazoo.KZWATCHLOC,
                            str(gConfig.current_milli_time()))
 def removeUpstreamServerRegistration(self, registration):
   """
   Parameters:
    - registration
   """
   self._logger.info("thrift interface received a call to removeUpstreamServerRegistration with a registration value of %s" % (registration))
   self._validateUpstreamServerRegistration(registration)
   self._validateNoConflictingUpstreamPath(registration)
   nodeName =  ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(registration)
   serverName = ezRPRegistration.get_ngx_server_name(registration)
   sslConfigNodeName = ezRPKazoo.KZSSLCONFLOC + '/' + serverName
   with self.lock:
       lock = self.kz.Lock(ezRPKazoo.KZLOCKFILE,"another contender")
       with lock:
           if not self.kz.exists(nodeName):
             raise RegistrationNotFoundException("no registration found for %s" % (nodeName))
           else:
             self._logger.info("removeUpstreamServerRegistration removing %s" % (nodeName))
             self.kz.delete(nodeName)
             if self.kz.exists(sslConfigNodeName) and self._isServerUnique(registration):
                 try:
                     #remove certs if server registration is unique
                     self.kz.delete(sslConfigNodeName)
                     self.ac.remove(serverName)
                     self._logger.info("removed certs for server %s" % serverName)
                 except EzRPCertStoreException as ex:
                     self._logger.exception('Exception in removing certs for server %s' % serverName)
             self.kz.set(ezRPKazoo.KZWATCHLOC, str(gConfig.current_milli_time()))
Esempio n. 3
0
 def _isServerUnique(self, registration):
     encodedServceNamePrefix = ezRPRegistration.getNodeName(
         registration).rsplit('_', 1)[0] + '_'
     allConfigs = self.kz.get_children(ezRPKazoo.KZCONFLOC)
     return False if (len(
         [x for x in allConfigs
          if x.startswith(encodedServceNamePrefix)]) > 1) else True
Esempio n. 4
0
    def _validateNoConflictingUpstreamPath(self, registration):

        # the node in zk has 3 parts base62 encoded parts separated by _
        # see getNodeName for details
        matchingPrefix = ezRPRegistration.getNodeName(registration).rsplit(
            '_', 1)[0] + '_'
        basePrefix = matchingPrefix.split('_')[0] + '_'
        with self.lock:
            allConfigs = self.kz.get_children(ezRPKazoo.KZCONFLOC)
        sameUserFacingConfigs = [
            x for x in allConfigs if x.startswith(basePrefix)
        ]

        # if there aren't any existing nodes in zk that have the same UserFacingPrefix
        # then we're OK to create one with any upstream path, and can just return here
        if len(sameUserFacingConfigs) > 0:
            # if one or more of the existing nodes with the same UserFacingPrefix has
            # a different upstream path, throw an invalid registration exception
            collidingConfigs = [
                x for x in sameUserFacingConfigs
                if not x.startswith(matchingPrefix)
            ]
            if len(collidingConfigs) > 0:
                raise RegistrationInvalidException(
                    "that UserFacingUrlPrefix is already registered with a different UpstreamPath"
                )
Esempio n. 5
0
    def _addUpstreamAndServer(self,upstream_number,registration):
        upstream_group_name = 'server'+str(upstream_number)
        upstream_name = registration.UpstreamHostAndPort
        upstream_timeout = registration.timeout
        upstream_timeout_tries = registration.timeoutTries
        upstream_upload_file_size = registration.uploadFileSize
        server_name = registration.UserFacingUrlPrefix.split('/',1)[0]
        loc = self.Location(registration.UserFacingUrlPrefix.split('/',1)[1], upstream_group_name, server_name, registration.UpstreamPath, upstream_name.split(':',1)[0], upstream_name.split(':',1)[1], upstream_timeout, upstream_timeout_tries, upstream_upload_file_size, registration.sticky, registration.disableChunkedTransferEncoding)

        name_to_resolve, port_to_use = upstream_name.split(':',1)
        try:
            #try to resolve the upstream name
            socket.gethostbyname(name_to_resolve)

            if upstream_group_name not in self._upstreams:
                self._upstreams[upstream_group_name] = {'location':loc.location,
                                                        'upstreams':[],
                                                        'sticky':bool(loc.sticky or False),
                                                        'timeout':int(loc.upstream_timeout or 0),
                                                        'timeout_tries':int(loc.upstream_timeout_tries or 0)
                                                       }
            self._upstreams[upstream_group_name]['upstreams'].append(upstream_name)

        except Exception as e:
            self._logger.error("Exception (%s) resolving upstream %s. Dropping that upstream path [%s:%s]. Location %s will not be configured unless it has other (valid) upstreams" % (str(e), name_to_resolve, loc.upstream_host, loc.upstream_port, loc.location))
            #remove from local upstream cache
            if upstream_group_name in self._upstreams:
                del self._upstreams[upstream_group_name]
            #remove from zookeeper
            try:
                self.kz.delete(ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(registration))
                self.kz.set(ezRPKazoo.KZWATCHLOC, str(gConfig.current_milli_time()))
            except NoNodeError:
                #node didn't exist before
                pass
            except Exception as e:
                self._logger.error('Exception in removing unresolved registration: %s' % str(e))
                raise

        self._logger.info('Configuring Location %s' % str(loc))

        if server_name not in self._servers:
            self._servers[server_name] = {}
        self._servers[server_name][loc.location] = loc

        self._serversWithSpecializedCerts.discard(server_name)
        if self.kz.exists(ezRPKazoo.KZSSLCONFLOC + '/' + server_name):
            if os.path.isfile(os.path.join(self._newSslDir, server_name + '.crt')) and \
               os.path.isfile(os.path.join(self._newSslDir, server_name + '.key')):
                self._serversWithSpecializedCerts.add(server_name)
            else:
                self._logger.error('Certs for configured %s server are not present in %s. Registration will use defaults' % (server_name, gConfig.ssl_server_certs))

        if server_name not in self._redirects:
            self._redirects[server_name] = {}
        if loc.location not in self._redirects[server_name]:
            self._redirects[server_name][loc.location] = []
        self._redirects[server_name][loc.location].append((loc.upstream_host,loc.upstream_port))
 def isUpstreamServerRegistered(self, registration): #UpstreamServerRegistration
   '''
       Does not verify timout and  timeoutTries match
   '''
   self._logger.info("thrift interface received a call to isUpstreamServerRegistered with a registration value of %s" % (registration))
   nodeName =  ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(registration)
   with self.lock:
       if self.kz.exists(nodeName):
           return True
   return False
Esempio n. 7
0
 def isUpstreamServerRegistered(self,
                                registration):  #UpstreamServerRegistration
     '''
     Does not verify timout and  timeoutTries match
 '''
     self._logger.info(
         "thrift interface received a call to isUpstreamServerRegistered with a registration value of %s"
         % (registration))
     nodeName = ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(
         registration)
     with self.lock:
         if self.kz.exists(nodeName):
             return True
     return False
  def _validateNoConflictingUpstreamPath(self,registration):

    # the node in zk has 3 parts base62 encoded parts separated by _
    # see getNodeName for details
    matchingPrefix = ezRPRegistration.getNodeName(registration).rsplit('_',1)[0]+'_'
    basePrefix = matchingPrefix.split('_')[0]+'_'
    with self.lock:
        allConfigs = self.kz.get_children(ezRPKazoo.KZCONFLOC)
    sameUserFacingConfigs = [x for x in allConfigs if x.startswith(basePrefix)]

    # if there aren't any existing nodes in zk that have the same UserFacingPrefix
    # then we're OK to create one with any upstream path, and can just return here
    if len(sameUserFacingConfigs) > 0:
        # if one or more of the existing nodes with the same UserFacingPrefix has
        # a different upstream path, throw an invalid registration exception
        collidingConfigs = [x for x in sameUserFacingConfigs if not x.startswith(matchingPrefix)]
        if len(collidingConfigs) > 0:
            raise RegistrationInvalidException("that UserFacingUrlPrefix is already registered with a different UpstreamPath")
  def addUpstreamServerRegistration(self, registration):
    """
    Parameters:
     - registration
    """
    self._logger.info("thrift interface received a call to addUpstreamServerRegistration with a registration value of %s" % (registration))
    self._validateUpstreamServerRegistration(registration)
    self._validateNoConflictingUpstreamPath(registration)
    nodeName = ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(registration)
    nodeData = self._serializeUpstreamServerRegistration(registration)

    with self.lock:
        lock = self.kz.Lock(ezRPKazoo.KZLOCKFILE,"another contender")
        with lock:
            self._logger.info("addUpstreamServerRegistration registering")
            if not self.kz.exists(nodeName):
              self._logger.info("Adding node in zookeeper\n\tNode Name: %s\n\tNode Data:%s" % (nodeName,nodeData))
              self.kz.create(nodeName,nodeData)
            else:
              self.kz.set(nodeName,nodeData)
              self._logger.info("Updating node in zookeeper\n\tNode Name: %s\n\tNode Data:%s" % (nodeName,nodeData))
            self.kz.set(ezRPKazoo.KZWATCHLOC, str(gConfig.current_milli_time()))
Esempio n. 10
0
 def removeUpstreamServerRegistration(self, registration):
     """
 Parameters:
  - registration
 """
     self._logger.info(
         "thrift interface received a call to removeUpstreamServerRegistration with a registration value of %s"
         % (registration))
     self._validateUpstreamServerRegistration(registration)
     self._validateNoConflictingUpstreamPath(registration)
     nodeName = ezRPKazoo.KZCONFLOC + '/' + ezRPRegistration.getNodeName(
         registration)
     serverName = ezRPRegistration.get_ngx_server_name(registration)
     sslConfigNodeName = ezRPKazoo.KZSSLCONFLOC + '/' + serverName
     with self.lock:
         lock = self.kz.Lock(ezRPKazoo.KZLOCKFILE, "another contender")
         with lock:
             if not self.kz.exists(nodeName):
                 raise RegistrationNotFoundException(
                     "no registration found for %s" % (nodeName))
             else:
                 self._logger.info(
                     "removeUpstreamServerRegistration removing %s" %
                     (nodeName))
                 self.kz.delete(nodeName)
                 if self.kz.exists(sslConfigNodeName
                                   ) and self._isServerUnique(registration):
                     try:
                         #remove certs if server registration is unique
                         self.kz.delete(sslConfigNodeName)
                         self.ac.remove(serverName)
                         self._logger.info("removed certs for server %s" %
                                           serverName)
                     except EzRPCertStoreException as ex:
                         self._logger.exception(
                             'Exception in removing certs for server %s' %
                             serverName)
                 self.kz.set(ezRPKazoo.KZWATCHLOC,
                             str(gConfig.current_milli_time()))
Esempio n. 11
0
    def _addUpstreamAndServer(self, upstream_number, registration):
        upstream_group_name = 'server' + str(upstream_number)
        upstream_name = registration.UpstreamHostAndPort
        upstream_timeout = registration.timeout
        upstream_timeout_tries = registration.timeoutTries
        upstream_upload_file_size = registration.uploadFileSize
        static_content_type = registration.contentServiceType
        server_name = registration.UserFacingUrlPrefix.split('/', 1)[0]
        loc = self.Location(
            registration.UserFacingUrlPrefix.split('/', 1)[1],
            upstream_group_name, server_name, registration.UpstreamPath,
            upstream_name.split(':', 1)[0],
            upstream_name.split(':', 1)[1], upstream_timeout,
            upstream_timeout_tries, upstream_upload_file_size,
            registration.sticky, registration.disableChunkedTransferEncoding,
            registration.authOperations,
            registration.validateUpstreamConnection, static_content_type)
        name_to_resolve, port_to_use = upstream_name.split(':', 1)
        try:
            #try to resolve the upstream name
            socket.gethostbyname(name_to_resolve)

            if upstream_group_name not in self._upstreams:
                self._upstreams[upstream_group_name] = {
                    'location': loc.location,
                    'upstreams': [],
                    'sticky': bool(loc.sticky or False),
                    'timeout': int(loc.upstream_timeout or 0),
                    'timeout_tries': int(loc.upstream_timeout_tries or 0)
                }
            self._upstreams[upstream_group_name]['upstreams'].append(
                upstream_name)

        except Exception as e:
            self._logger.error(
                "Exception (%s) resolving upstream %s. Dropping that upstream path [%s:%s]. Location %s will not be configured unless it has other (valid) upstreams"
                % (str(e), name_to_resolve, loc.upstream_host,
                   loc.upstream_port, loc.location))
            #remove from local upstream cache
            if upstream_group_name in self._upstreams:
                del self._upstreams[upstream_group_name]
            #remove from zookeeper
            try:
                self.kz.delete(ezRPKazoo.KZCONFLOC + '/' +
                               ezRPRegistration.getNodeName(registration))
                self.kz.set(ezRPKazoo.KZWATCHLOC,
                            str(gConfig.current_milli_time()))
            except NoNodeError:
                #node didn't exist before
                pass
            except Exception as e:
                self._logger.error(
                    'Exception in removing unresolved registration: %s' %
                    str(e))
                raise

        self._logger.info('Configuring Location %s' % str(loc))

        if server_name not in self._servers:
            self._servers[server_name] = {}
        self._servers[server_name][loc.location] = loc

        self._serversWithSpecializedCerts.discard(server_name)
        if self.kz.exists(ezRPKazoo.KZSSLCONFLOC + '/' + server_name):
            if os.path.isfile(os.path.join(self._newSslDir, server_name + '.crt')) and \
               os.path.isfile(os.path.join(self._newSslDir, server_name + '.key')):
                self._serversWithSpecializedCerts.add(server_name)
            else:
                self._logger.error(
                    'Certs for configured %s server are not present in %s. Registration will use defaults'
                    % (server_name, gConfig.ssl_server_certs))

        if server_name not in self._redirects:
            self._redirects[server_name] = {}
        if loc.location not in self._redirects[server_name]:
            self._redirects[server_name][loc.location] = []
        self._redirects[server_name][loc.location].append(
            (loc.upstream_host, loc.upstream_port))
 def _isServerUnique(self, registration):
    encodedServceNamePrefix = ezRPRegistration.getNodeName(registration).rsplit('_',1)[0]+'_'
    allConfigs = self.kz.get_children(ezRPKazoo.KZCONFLOC)
    return False if (len([x for x in allConfigs if x.startswith(encodedServceNamePrefix)]) > 1) else True