예제 #1
0
    def _connect(self, host, port):
        client = self.client
        self.logger.info('Connecting to %s:%s', host, port)

        self.logger.log(BLATHER, '    Using session_id: %r session_passwd: %s',
                        client._session_id, hexlify(client._session_passwd))

        with self._socket_error_handling():
            self._socket = self.handler.create_connection(
                (host, port), client._session_timeout / 1000.0)

        self._socket.setblocking(0)

        connect = Connect(0, client.last_zxid, client._session_timeout,
                          client._session_id or 0, client._session_passwd,
                          client.read_only)

        connect_result, zxid = self._invoke(client._session_timeout, connect)

        if connect_result.time_out <= 0:
            raise SessionExpiredError("Session has expired")

        if zxid:
            client.last_zxid = zxid

        # Load return values
        client._session_id = connect_result.session_id
        client._protocol_version = connect_result.protocol_version
        negotiated_session_timeout = connect_result.time_out
        connect_timeout = negotiated_session_timeout / len(client.hosts)
        read_timeout = negotiated_session_timeout * 2.0 / 3.0
        client._session_passwd = connect_result.passwd

        self.logger.log(
            BLATHER, 'Session created, session_id: %r session_passwd: %s\n'
            '    negotiated session timeout: %s\n'
            '    connect timeout: %s\n'
            '    read timeout: %s', client._session_id,
            hexlify(client._session_passwd), negotiated_session_timeout,
            connect_timeout, read_timeout)

        if self.sasl_server_principal:
            self._authenticate_with_sasl(host, connect_timeout / 1000.0)

        if connect_result.read_only:
            client._session_callback(KeeperState.CONNECTED_RO)
            self._ro_mode = iter(self._server_pinger())
        else:
            client._session_callback(KeeperState.CONNECTED)
            self._ro_mode = None

        for scheme, auth in client.auth_data:
            ap = Auth(0, scheme, auth)
            zxid = self._invoke(connect_timeout, ap, xid=AUTH_XID)
            if zxid:
                client.last_zxid = zxid

        return read_timeout, connect_timeout
예제 #2
0
    def _connect(self, host, port):
        client = self.client
        log.info('Connecting to %s:%s', host, port)

        if self.log_debug:
            log.debug('    Using session_id: %r session_passwd: %s',
                      client._session_id,
                      hexlify(client._session_passwd))

        with socket_error_handling():
            self._socket.connect((host, port))

        self._socket.setblocking(0)

        connect = Connect(0, client.last_zxid, client._session_timeout,
                          client._session_id or 0, client._session_passwd,
                          client.read_only)

        connect_result, zxid = self._invoke(client._session_timeout, connect)

        if connect_result.time_out <= 0:
            raise SessionExpiredError("Session has expired")

        if zxid:
            client.last_zxid = zxid

        # Load return values
        client._session_id = connect_result.session_id
        negotiated_session_timeout = connect_result.time_out
        connect_timeout = negotiated_session_timeout / len(client.hosts)
        read_timeout = negotiated_session_timeout * 2.0 / 3.0
        client._session_passwd = connect_result.passwd

        if self.log_debug:
            log.debug('Session created, session_id: %r session_passwd: %s\n'
                      '    negotiated session timeout: %s\n'
                      '    connect timeout: %s\n'
                      '    read timeout: %s', client._session_id,
                      hexlify(client._session_passwd),
                      negotiated_session_timeout, connect_timeout,
                      read_timeout)

        if connect_result.read_only:
            client._session_callback(KeeperState.CONNECTED_RO)
            self._ro_mode = iter(self._server_pinger())
        else:
            client._session_callback(KeeperState.CONNECTED)
            self._ro_mode = None

        for scheme, auth in client.auth_data:
            ap = Auth(0, scheme, auth)
            zxid = self._invoke(connect_timeout, ap, xid=-4)
            if zxid:
                client.last_zxid = zxid
        return read_timeout, connect_timeout
예제 #3
0
    def add_auth_async(self, scheme, credential):
        """Asynchronously send credentials to server. Takes the same
        arguments as :meth:`add_auth`.

        :rtype: :class:`~kazoo.interfaces.IAsyncResult`

        """
        if not isinstance(scheme, basestring):
            raise TypeError("Invalid type for scheme")
        if not isinstance(credential, basestring):
            raise TypeError("Invalid type for credential")
        self._call(Auth(0, scheme, credential), None)
        return True
예제 #4
0
    def add_auth_async(self, scheme, credential):
        """Asynchronously send credentials to server. Takes the same
        arguments as :meth:`add_auth`.

        :rtype: :class:`~kazoo.interfaces.IAsyncResult`

        """
        if not isinstance(scheme, basestring):
            raise TypeError("Invalid type for scheme")
        if not isinstance(credential, basestring):
            raise TypeError("Invalid type for credential")

        # we need this auth data to re-authenticate on reconnect
        self.auth_data.add((scheme, credential))

        async_result = self.handler.async_result()
        self._call(Auth(0, scheme, credential), async_result)
        return async_result
예제 #5
0
    def _connect(self, host, port):
        client = self.client
        self.logger.info('Connecting to %s:%s', host, port)

        self.logger.log(BLATHER, '    Using session_id: %r session_passwd: %s',
                        client._session_id, hexlify(client._session_passwd))

        with self._socket_error_handling():
            self._socket = self.handler.create_connection(
                (host, port), client._session_timeout / 1000.0)

        self._socket.setblocking(0)

        connect = Connect(0, client.last_zxid, client._session_timeout,
                          client._session_id or 0, client._session_passwd,
                          client.read_only)

        # save the client's last_zxid before it gets overwritten by the server's.
        # we'll need this to reset watches via SetWatches further below.
        last_zxid = client.last_zxid

        connect_result, zxid = self._invoke(client._session_timeout / 1000.0,
                                            connect)

        if connect_result.time_out <= 0:
            raise SessionExpiredError("Session has expired")

        if zxid:
            client.last_zxid = zxid

        # Load return values
        client._session_id = connect_result.session_id
        client._protocol_version = connect_result.protocol_version
        negotiated_session_timeout = connect_result.time_out
        connect_timeout = negotiated_session_timeout / len(client.hosts)
        read_timeout = negotiated_session_timeout * 2.0 / 3.0
        client._session_passwd = connect_result.passwd

        self.logger.log(
            BLATHER, 'Session created, session_id: %r session_passwd: %s\n'
            '    negotiated session timeout: %s\n'
            '    connect timeout: %s\n'
            '    read timeout: %s', client._session_id,
            hexlify(client._session_passwd), negotiated_session_timeout,
            connect_timeout, read_timeout)

        if connect_result.read_only:
            client._session_callback(KeeperState.CONNECTED_RO)
            self._ro_mode = iter(self._server_pinger())
        else:
            client._session_callback(KeeperState.CONNECTED)
            self._ro_mode = None

        for scheme, auth in client.auth_data:
            ap = Auth(0, scheme, auth)
            zxid = self._invoke(connect_timeout / 1000.0, ap, xid=AUTH_XID)
            if zxid:
                client.last_zxid = zxid

        # TODO: separate exist from data watches
        if client._data_watchers or client._child_watchers.keys():
            sw = SetWatches(last_zxid, client._data_watchers.keys(),
                            client._data_watchers.keys(),
                            client._child_watchers.keys())
            zxid = self._invoke(connect_timeout / 1000.0,
                                sw,
                                xid=SET_WATCHES_XID)
            if zxid:
                client.last_zxid = zxid

        return read_timeout, connect_timeout
예제 #6
0
    def _connect(self, host, hostip, port):
        client = self.client
        self.logger.info('Connecting to %s(%s):%s, use_ssl: %r', host, hostip,
                         port, self.client.use_ssl)

        self.logger.log(BLATHER, '    Using session_id: %r session_passwd: %s',
                        client._session_id, hexlify(client._session_passwd))

        with self._socket_error_handling():
            self._socket = self.handler.create_connection(
                address=(hostip, port),
                timeout=client._session_timeout / 1000.0,
                use_ssl=self.client.use_ssl,
                keyfile=self.client.keyfile,
                certfile=self.client.certfile,
                ca=self.client.ca,
                keyfile_password=self.client.keyfile_password,
                verify_certs=self.client.verify_certs,
            )

        self._socket.setblocking(0)

        connect = Connect(0, client.last_zxid, client._session_timeout,
                          client._session_id or 0, client._session_passwd,
                          client.read_only)

        connect_result, zxid = self._invoke(
            client._session_timeout / 1000.0 / len(client.hosts), connect)

        if connect_result.time_out <= 0:
            raise SessionExpiredError("Session has expired")

        if zxid:
            client.last_zxid = zxid

        # Load return values
        client._session_id = connect_result.session_id
        client._protocol_version = connect_result.protocol_version
        negotiated_session_timeout = connect_result.time_out
        connect_timeout = negotiated_session_timeout / len(client.hosts)
        read_timeout = negotiated_session_timeout * 2.0 / 3.0
        client._session_passwd = connect_result.passwd

        self.logger.log(
            BLATHER, 'Session created, session_id: %r session_passwd: %s\n'
            '    negotiated session timeout: %s\n'
            '    connect timeout: %s\n'
            '    read timeout: %s', client._session_id,
            hexlify(client._session_passwd), negotiated_session_timeout,
            connect_timeout, read_timeout)

        if connect_result.read_only:
            client._session_callback(KeeperState.CONNECTED_RO)
            self._ro_mode = iter(self._server_pinger())
        else:
            client._session_callback(KeeperState.CONNECTED)
            self._ro_mode = None

        if self.sasl_options is not None:
            self._authenticate_with_sasl(host, connect_timeout / 1000.0)

        # Get a copy of the auth data before iterating, in case it is
        # changed.
        client_auth_data_copy = copy.copy(client.auth_data)

        for scheme, auth in client_auth_data_copy:
            ap = Auth(0, scheme, auth)
            zxid = self._invoke(connect_timeout / 1000.0, ap, xid=AUTH_XID)
            if zxid:
                client.last_zxid = zxid

        return read_timeout, connect_timeout
예제 #7
0
    def _connect(self, host, port):
        client = self.client
        self.logger.info('Connecting to %s:%s, use_ssl: %r', host, port,
                         self.client.use_ssl)

        self.logger.log(BLATHER, '    Using session_id: %r session_passwd: %s',
                        client._session_id, hexlify(client._session_passwd))

        with self._socket_error_handling():
            self._socket = self.handler.create_connection(
                address=(host, port),
                timeout=client._session_timeout / 1000.0,
                use_ssl=self.client.use_ssl,
                keyfile=self.client.keyfile,
                certfile=self.client.certfile,
                ca=self.client.ca,
                keyfile_password=self.client.keyfile_password,
                verify_certs=self.client.verify_certs,
            )

        self._socket.setblocking(0)

        connect = Connect(0, client.last_zxid, client._session_timeout,
                          client._session_id or 0, client._session_passwd,
                          client.read_only)

        connect_result, zxid = self._invoke(client._session_timeout / 1000.0,
                                            connect)

        if connect_result.time_out <= 0:
            raise SessionExpiredError("Session has expired")

        if zxid:
            client.last_zxid = zxid

        # Load return values
        client._session_id = connect_result.session_id
        client._protocol_version = connect_result.protocol_version
        negotiated_session_timeout = connect_result.time_out
        connect_timeout = negotiated_session_timeout / len(client.hosts)
        read_timeout = negotiated_session_timeout * 2.0 / 3.0
        client._session_passwd = connect_result.passwd

        self.logger.log(
            BLATHER, 'Session created, session_id: %r session_passwd: %s\n'
            '    negotiated session timeout: %s\n'
            '    connect timeout: %s\n'
            '    read timeout: %s', client._session_id,
            hexlify(client._session_passwd), negotiated_session_timeout,
            connect_timeout, read_timeout)

        if connect_result.read_only:
            client._session_callback(KeeperState.CONNECTED_RO)
            self._ro_mode = iter(self._server_pinger())
        else:
            self._ro_mode = None

            # Get a copy of the auth data before iterating, in case it is
            # changed.
            client_auth_data_copy = copy.copy(client.auth_data)

            if client.use_sasl and self.sasl_cli is None:
                if PURESASL_AVAILABLE:
                    for scheme, auth in client_auth_data_copy:
                        if scheme == 'sasl':
                            username, password = auth.split(":")
                            self.sasl_cli = SASLClient(
                                host=client.sasl_server_principal,
                                service='zookeeper',
                                mechanism='DIGEST-MD5',
                                username=username,
                                password=password)
                            break

                    # As described in rfc
                    # https://tools.ietf.org/html/rfc2831#section-2.1
                    # sending empty challenge
                    self._send_sasl_request(challenge=b'',
                                            timeout=connect_timeout)
                else:
                    self.logger.warn('Pure-sasl library is missing while sasl'
                                     ' authentification is configured. Please'
                                     ' install pure-sasl library to connect '
                                     'using sasl. Now falling back '
                                     'connecting WITHOUT any '
                                     'authentification.')
                    client.use_sasl = False
                    client._session_callback(KeeperState.CONNECTED)
            else:
                client._session_callback(KeeperState.CONNECTED)
                for scheme, auth in client_auth_data_copy:
                    if scheme == "digest":
                        ap = Auth(0, scheme, auth)
                        zxid = self._invoke(connect_timeout / 1000.0,
                                            ap,
                                            xid=AUTH_XID)
                        if zxid:
                            client.last_zxid = zxid

        return read_timeout, connect_timeout