Exemplo n.º 1
0
def connect_to_thrift(conf):
  """
  Connect to a thrift endpoint as determined by the 'conf' parameter.
  Note that this does *not* open the transport.

  Returns a tuple of (service, protocol, transport)
  """
  if conf.use_ssl:
    sock = TSSLSocket(conf.host, conf.port, validate=conf.validate, ca_certs=conf.ca_certs, keyfile=conf.keyfile, certfile=conf.certfile)
  else:
    sock = TSocket(conf.host, conf.port)
  if conf.timeout_seconds:
    # Thrift trivia: You can do this after the fact with
    # _grab_transport_from_wrapper(self.wrapped.transport).setTimeout(seconds*1000)
    sock.setTimeout(conf.timeout_seconds * 1000.0)
  if conf.use_sasl:
    def sasl_factory():
      saslc = sasl.Client()
      saslc.setAttr("host", str(conf.host))
      saslc.setAttr("service", str(conf.kerberos_principal))
      if conf.mechanism == 'PLAIN':
        saslc.setAttr("username", str(conf.username))
	saslc.setAttr("password", str(conf.password)) # defaults to hue for a non-empty string unless using ldap
      saslc.init()
      return saslc

    transport = TSaslClientTransport(sasl_factory, conf.mechanism, sock)
  elif conf.transport == 'framed':
    transport = TFramedTransport(sock)
  else:
    transport = TBufferedTransport(sock)

  protocol = TBinaryProtocol(transport)
  service = conf.klass(protocol)
  return service, protocol, transport
Exemplo n.º 2
0
 def _refresh_thrift_client(self):
     """Refresh the Thrift socket, transport, and client."""
     socket = TSSLSocket(self.host, self.port, False, KEY_FILE, KEY_FILE,
                         KEY_FILE)
     if self.timeout is not None:
         socket.setTimeout(self.timeout)
     self.transport = self._transport_class(socket)
     protocol = self._protocol_class(self.transport)
     self.client = Hbase.Client(protocol)
Exemplo n.º 3
0
 def _refresh_thrift_client(self):
     """Refresh the Thrift socket, transport, and client."""
     socket = TSSLSocket(self.host, self.port, False, KEY_FILE, KEY_FILE,
         KEY_FILE)
     if self.timeout is not None:
         socket.setTimeout(self.timeout)
     self.transport = self._transport_class(socket)
     protocol = self._protocol_class(self.transport)
     self.client = Hbase.Client(protocol)
Exemplo n.º 4
0
def connect_to_thrift(conf):
    """
  Connect to a thrift endpoint as determined by the 'conf' parameter.
  Note that this does *not* open the transport.

  Returns a tuple of (service, protocol, transport)
  """

    if conf.use_ssl:
        sock = TSSLSocket(conf.host,
                          conf.port,
                          validate=conf.validate,
                          ca_certs=conf.ca_certs,
                          keyfile=conf.keyfile,
                          certfile=conf.certfile)
    else:
        sock = TSocket(conf.host, conf.port)
    if conf.timeout_seconds:
        # Thrift trivia: You can do this after the fact with
        # _grab_transport_from_wrapper(self.wrapped.transport).setTimeout(seconds*1000)
        sock.setTimeout(conf.timeout_seconds * 1000.0)

    if conf.transport_mode == 'http':
        sock = THttpClient(conf.http_url, cert_validate=conf.validate)
        if conf.use_sasl and conf.mechanism != 'PLAIN':
            sock.set_kerberos_auth()
        else:
            sock.set_basic_auth(conf.username, conf.password)
        transport = TBufferedTransport(sock)

    elif conf.use_sasl:

        def sasl_factory():
            saslc = sasl.Client()
            saslc.setAttr("host", str(conf.host))
            saslc.setAttr("service", str(conf.kerberos_principal))
            if conf.mechanism == 'PLAIN':
                saslc.setAttr("username", str(conf.username))
                saslc.setAttr(
                    "password", str(conf.password)
                )  # defaults to hue for a non-empty string unless using ldap
            saslc.init()
            return saslc

        transport = TSaslClientTransport(sasl_factory, conf.mechanism, sock)
    elif conf.transport_mode == 'framed':
        transport = TFramedTransport(sock)
    else:
        transport = TBufferedTransport(sock)

    protocol = TBinaryProtocol(transport)
    if conf.multiple:
        protocol = TMultiplexedProtocol(protocol, conf.service_name)
    service = conf.klass(protocol)
    return service, protocol, transport
Exemplo n.º 5
0
    def __init__(self,
                 host=None,
                 port=10000,
                 ssl=True,
                 authMechanism=None,
                 user=None,
                 password=None,
                 database=None,
                 configuration=None,
                 timeout=None):
        authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP'])
        if authMechanism not in authMechanisms:
            raise NotImplementedError(
                'authMechanism is either not supported or not implemented')
        #Must set a password for thrift, even if it doesn't need one
        #Open issue with python-sasl
        if authMechanism == 'PLAIN' and (password is None
                                         or len(password) == 0):
            password = '******'
        socket = TSSLSocket(host, port, False) if ssl else TSocket(host, port)
        socket.setTimeout(timeout)
        if authMechanism == 'NOSASL':
            transport = TBufferedTransport(socket)
        else:
            sasl_mech = 'PLAIN'
            saslc = sasl.Client()
            saslc.setAttr("username", user)
            saslc.setAttr("password", password)
            if authMechanism == 'KERBEROS':
                krb_host, krb_service = self._get_krb_settings(
                    host, configuration)
                sasl_mech = 'GSSAPI'
                saslc.setAttr("host", krb_host)
                saslc.setAttr("service", krb_service)

            saslc.init()
            transport = TSaslClientTransport(saslc, sasl_mech, socket)

        self.client = TCLIService.Client(TBinaryProtocol(transport))
        transport.open()
        res = self.client.OpenSession(
            TOpenSessionReq(username=user,
                            password=password,
                            configuration=configuration))
        self.session = res.sessionHandle
        if database is not None:
            with self.cursor() as cur:
                query = "USE {0}".format(database)
                cur.execute(query)
Exemplo n.º 6
0
def connect_to_thrift(conf):
    """
  Connect to a thrift endpoint as determined by the 'conf' parameter.
  Note that this does *not* open the transport.

  Returns a tuple of (service, protocol, transport)
  """
    if conf.use_ssl:
        sock = TSSLSocket(conf.host,
                          conf.port,
                          validate=conf.validate,
                          ca_certs=conf.ca_certs,
                          keyfile=conf.keyfile,
                          certfile=conf.certfile)
    else:
        sock = TSocket(conf.host, conf.port)
    if conf.timeout_seconds:
        # Thrift trivia: You can do this after the fact with
        # _grab_transport_from_wrapper(self.wrapped.transport).setTimeout(seconds*1000)
        sock.setTimeout(conf.timeout_seconds * 1000.0)
    if conf.use_sasl:

        def sasl_factory():
            saslc = sasl.Client()
            saslc.setAttr("host", str(conf.host))
            saslc.setAttr("service", str(conf.kerberos_principal))
            if conf.mechanism == 'PLAIN':
                saslc.setAttr("username", str(conf.username))
                saslc.setAttr("password", 'hue')  # Just a non empty string
            saslc.init()
            return saslc

        transport = TSaslClientTransport(sasl_factory, conf.mechanism, sock)
    else:
        transport = TBufferedTransport(sock)

    protocol = TBinaryProtocol(transport)
    service = conf.klass(protocol)
    return service, protocol, transport