Beispiel #1
0
    def setUp(self):
        if self.ssl:
            self.socket = TSSLSocket.TSSLSocket("localhost", self._port)
        else:
            self.socket = TSocket.TSocket("localhost", self._port)

        self.transport = TTransport.TBufferedTransport(self.socket)
        self.protocol = self.protocol_factory.getProtocol(self.transport)
        if isinstance(self, HeaderAcceleratedCompactTest):
            self.protocol.trans.set_protocol_id(
                THeaderProtocol.THeaderProtocol.T_COMPACT_PROTOCOL
            )
            self.protocol.reset_protocol()
        self.transport.open()
        self.client = ThriftTest.Client(self.protocol)
        if self.multiple:
            p = TMultiplexedProtocol.TMultiplexedProtocol(self.protocol, "ThriftTest")
            self.client = ThriftTest.Client(p)
            p = TMultiplexedProtocol.TMultiplexedProtocol(
                self.protocol, "SecondService"
            )
            self.client2 = SecondService.Client(p)
        else:
            self.client = ThriftTest.Client(self.protocol)
            self.client2 = None
Beispiel #2
0
    def getFileMetaData(self, deviceId):
        print "device ID -> " + str(deviceId)

        query = ('Select DeviceIp From Devices Where DeviceId=%s' % (deviceId))

        try:
            self.cur.execute(query)
        except Exception as e:
            print e

        devIp = self.cur.fetchone()[0]
        query = ('Select DevicePort From Devices Where DeviceId=%s' %
                 (deviceId))
        try:
            self.cur.execute(query)
        except Exception as e:
            print e
        devPort = self.cur.fetchone()[0]
        print devIp
        print devPort

        clientSocket = TSSLSocket.TSSLSocket(host=devIp,
                                             port=devPort,
                                             ca_certs=str(deviceId) + '.crt')
        clientTransport = TTransport.TFramedTransport(clientSocket)
        clientProtocol = TBinaryProtocol.TBinaryProtocol(clientTransport)
        client = clientService.Client(clientProtocol)
        clientTransport.open()

        result = client.getFileMetaData()

        clientTransport.close()

        print result
        return result
Beispiel #3
0
    def setUp(self):
        if self.ssl:
            self.socket = TSSLSocket.TSSLSocket("localhost", self._port)
        else:
            self.socket = TSocket.TSocket("localhost", self._port)

        if self.server_type in FRAMED_TYPES \
                and not isinstance(self, HeaderTest):
            self.transport = TTransport.TFramedTransport(self.socket)
        else:
            self.transport = TTransport.TBufferedTransport(self.socket)

        self.protocol = self.protocol_factory.getProtocol(self.transport)
        self.transport.open()
        self.client = ThriftTest.Client(self.protocol)
        if self.multiple:
            p = TMultiplexedProtocol.TMultiplexedProtocol(
                self.protocol, "ThriftTest")
            self.client = ThriftTest.Client(p)
            p = TMultiplexedProtocol.TMultiplexedProtocol(
                self.protocol, "SecondService")
            self.client2 = SecondService.Client(p)
        else:
            self.client = ThriftTest.Client(self.protocol)
            self.client2 = None
Beispiel #4
0
 def setUp(self):
     if options.http_path:
         self.transport = THttpClient.THttpClient(options.host,
                                                  port=options.port,
                                                  path=options.http_path)
     else:
         if options.ssl:
             from thrift.transport import TSSLSocket
             socket = TSSLSocket.TSSLSocket(options.host,
                                            options.port,
                                            validate=False)
         else:
             socket = TSocket.TSocket(options.host, options.port)
         # frame or buffer depending upon args
         if options.framed:
             self.transport = TTransport.TFramedTransport(socket)
         else:
             self.transport = TTransport.TBufferedTransport(socket)
         if options.zlib:
             self.transport = TZlibTransport.TZlibTransport(
                 self.transport, 9)
     self.transport.open()
     protocol = self.protocol_factory.getProtocol(self.transport)
     if options.multiple:
         p = TMultiplexedProtocol.TMultiplexedProtocol(
             protocol, "ThriftTest")
         self.client = ThriftTest.Client(p)
         p = TMultiplexedProtocol.TMultiplexedProtocol(
             protocol, "SecondService")
         self.client2 = SecondService.Client(p)
     else:
         self.client = ThriftTest.Client(protocol)
         self.client2 = None
Beispiel #5
0
 def setUp(self):
     if options.http_path:
         self.transport = THttpClient.THttpClient(options.host,
                                                  port=options.port,
                                                  path=options.http_path)
     else:
         if options.ssl:
             from thrift.transport import TSSLSocket
             socket = TSSLSocket.TSSLSocket(options.host,
                                            options.port,
                                            validate=False)
         else:
             socket = TSocket.TSocket(options.host, options.port)
         # frame or buffer depending upon args
         self.transport = TTransport.TBufferedTransport(socket)
         if options.trans == 'framed':
             self.transport = TTransport.TFramedTransport(socket)
         elif options.trans == 'buffered':
             self.transport = TTransport.TBufferedTransport(socket)
         elif options.trans == '':
             raise AssertionError('Unknown --transport option: %s' %
                                  options.trans)
         if options.zlib:
             self.transport = TZlibTransport.TZlibTransport(
                 self.transport, 9)
     self.transport.open()
     protocol = self.get_protocol(self.transport)
     self.client = ThriftTest.Client(protocol)
    def _get_transport(self):
        """Create a Transport.

       A non-kerberized impalad just needs a simple buffered transport. For
       the kerberized version, a sasl transport is created.

       If SSL is enabled, a TSSLSocket underlies the transport stack; otherwise a TSocket
       is used.
    """
        if self.use_ssl:
            # TSSLSocket needs the ssl module, which may not be standard on all Operating
            # Systems. Only attempt to import TSSLSocket if the user wants an SSL connection.
            from thrift.transport import TSSLSocket

        # sasl does not accept unicode strings, explicitly encode the string into ascii.
        host, port = self.impalad[0].encode('ascii',
                                            'ignore'), int(self.impalad[1])
        if self.use_ssl:
            if self.ca_cert is None:
                # No CA cert means don't try to verify the certificate
                sock = TSSLSocket.TSSLSocket(host, port, validate=False)
            else:
                sock = TSSLSocket.TSSLSocket(host,
                                             port,
                                             validate=True,
                                             ca_certs=self.ca_cert)
        else:
            sock = TSocket(host, port)
        if not (self.use_ldap or self.use_kerberos):
            return TBufferedTransport(sock)
        # Initializes a sasl client
        def sasl_factory():
            sasl_client = sasl.Client()
            sasl_client.setAttr("host", host)
            if self.use_ldap:
                sasl_client.setAttr("username", self.user)
                sasl_client.setAttr("password", self.ldap_password)
            else:
                sasl_client.setAttr("service", self.kerberos_service_name)
            sasl_client.init()
            return sasl_client

        # GSSASPI is the underlying mechanism used by kerberos to authenticate.
        if self.use_kerberos:
            return TSaslClientTransport(sasl_factory, "GSSAPI", sock)
        else:
            return TSaslClientTransport(sasl_factory, "PLAIN", sock)
Beispiel #7
0
    def __init__(self):
        try:
            self.socket = TSSLSocket.TSSLSocket('192.168.99.105',
                                                9930,
                                                validate=False)

        except Thrift.TException as tx:
            print('%s' % (tx.message))
Beispiel #8
0
 def ssl_socket_factory(host, port):
     """
     Returns a :class:`TSSLSocket` instance.
     """
     return TSSLSocket.TSSLSocket(host,
                                  port,
                                  ca_certs=ca_certs,
                                  validate=validate)
Beispiel #9
0
 def setup_client(self):
     socket = TSSLSocket.TSSLSocket(host=self.test_host,
                                    port=self.test_port,
                                    validate=False)
     self.client_transport = TTransport.TBufferedTransport(socket)
     protocol = TBinaryProtocol.TBinaryProtocol(self.client_transport)
     self.client = DemoService.Client(protocol)
     self.client_transport.open()
Beispiel #10
0
def get_transport(hostname, port):
    # Create a socket to the Airavata Server
    # TODO: validate server certificate
    transport = TSSLSocket.TSSLSocket(hostname, port, validate=False)

    # Use Buffered Protocol to speedup over raw sockets
    transport = TTransport.TBufferedTransport(transport)
    return transport
Beispiel #11
0
def create_transport(host, port, service, transport_type="buffered", user=None,
                     password=None, use_ssl=False, ssl_cert=None):
  """
  Create a new Thrift Transport based on the requested type.
  Supported transport types:
  - buffered, returns simple buffered transport
  - plain_sasl, return a SASL transport with the PLAIN mechanism
  - kerberos, return a SASL transport with the GSSAPI mechanism

  If use_ssl is True, the connection will use SSL, optionally using the file at ssl_cert
  as the CA cert.
  """
  port = int(port)
  if use_ssl:
    from thrift.transport import TSSLSocket
    if ssl_cert is None:
      sock = TSSLSocket.TSSLSocket(host, port, validate=False)
    else:
      sock = TSSLSocket.TSSLSocket(host, port, validate=True, ca_certs=ssl_cert)
  else:
    sock = TSocket(host, port)
  if transport_type.lower() == "buffered":
    return TBufferedTransport(sock)

  # Set defaults for LDAP connections
  if transport_type.lower() == "plain_sasl":
    if user is None: user = getpass.getuser()
    if password is None: password = ""

  # Initializes a sasl client
  from shell.thrift_sasl import TSaslClientTransport
  def sasl_factory():
    sasl_client = sasl.Client()
    sasl_client.setAttr("host", host)
    sasl_client.setAttr("service", service)
    if transport_type.lower() == "plain_sasl":
      sasl_client.setAttr("username", user)
      sasl_client.setAttr("password", password)
    sasl_client.init()
    return sasl_client
  if transport_type.lower() == "plain_sasl":
    return TSaslClientTransport(sasl_factory, "PLAIN", sock)
  else:
    # GSSASPI is the underlying mechanism used by kerberos to authenticate.
    return TSaslClientTransport(sasl_factory, "GSSAPI", sock)
Beispiel #12
0
 def __init__(self, ip, port):
     # Make SSL socket
     self.socket = TSSLSocket.TSSLSocket(ip, port, False)
     # Buffering is critical. Raw sockets are very slow
     self.transport = TTransport.TBufferedTransport(self.socket)
     # Wrap in a protocol
     self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
     self.sessionId = None
     self.streamId = None
Beispiel #13
0
 def _get_client(self, options):
     host, port = self._parse_host_port(options.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port) if options.ssl
               else TSocket.TSocket(host, port))
     if options.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(options, transport, socket=socket)
Beispiel #14
0
    def __init__(self, ip, ssl_port, tcp_port):
        # Make SSL socket
        self.ssl_socket = TSSLSocket.TSSLSocket(ip, ssl_port, False)
        self.ssl_transport = TTransport.TBufferedTransport(self.ssl_socket)
        self.ssl_protocol = TBinaryProtocol.TBinaryProtocol(self.ssl_transport)

        # Make TCP socket
        self.tcp_socket = TSocket.TSocket(ip, tcp_port)
        self.tcp_transport = TTransport.TBufferedTransport(self.tcp_socket)
        self.tcp_protocol = TBinaryProtocol.TBinaryProtocol(self.tcp_transport)
 def _get_client_by_host(self):
     config = self.config
     host, port = self._parse_host_port(config.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port)
               if config.ssl else TSocket.TSocket(host, port))
     if config.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(config, transport, socket=socket)
Beispiel #16
0
def get_transport(hostname, port):
    """Create a socket to the Airavata Server

    :param hostname: Hostname of Airavata server
    :param port:     Port of Airavata server
    :return: Transport object
    """
    # TODO: validate server certificate
    transport = TSSLSocket.TSSLSocket(hostname, port, validate=False)
    # Use Buffered Protocol to speedup over raw sockets
    transport = TTransport.TBufferedTransport(transport)
    return transport
Beispiel #17
0
def ssl_transport_factory(host, port, env, config_file):
    """
    SSL Thrift transport factory function.

    Params:
    * host .........: hostname of Cassandra node.
    * port .........: port number to connect to.
    * env ..........: environment variables. SSL factory will use, if passed,
                      SSL_CERTFILE and SSL_VALIDATE variables.
    * config_file ..: path to cqlsh config file (usually ~/.cqlshrc).
                      SSL factory will use, if set, certfile and validate
                      options in [ssl] section, as well as host to certfile
                      mapping in [certfiles] section.

    [certfiles] section is optional, 'validate' setting in [ssl] section is
    optional too. If validation is enabled then SSL certfile must be provided
    either in the config file or as an environment variable.
    Environment variables override any options set in cqlsh config file.
    """
    configs = ConfigParser.SafeConfigParser()
    configs.read(config_file)

    def get_option(section, option):
        try:
            return configs.get(section, option)
        except ConfigParser.Error:
            return None

    ssl_validate = env.get('SSL_VALIDATE')
    if ssl_validate is None:
        ssl_validate = get_option('ssl', 'validate')
    ssl_validate = ssl_validate is None or ssl_validate.lower() != 'false'

    ssl_certfile = env.get('SSL_CERTFILE')
    if ssl_certfile is None:
        ssl_certfile = get_option('certfiles', host)
    if ssl_certfile is None:
        ssl_certfile = get_option('ssl', 'certfile')
    if ssl_validate and ssl_certfile is None:
        sys.exit(
            "Validation is enabled; SSL transport factory requires a valid certfile "
            "to be specified. Please provide path to the certfile in [ssl] section "
            "as 'certfile' option in %s (or use [certfiles] section) or set SSL_CERTFILE "
            "environment variable." % (config_file, ))
    if not ssl_certfile is None:
        ssl_certfile = os.path.expanduser(ssl_certfile)

    tsocket = TSSLSocket.TSSLSocket(host,
                                    port,
                                    ca_certs=ssl_certfile,
                                    validate=ssl_validate)
    return TTransport.TFramedTransport(tsocket)
    def setUp(self):
        if self.ssl:
            self.socket = TSSLSocket.TSSLSocket("localhost", self._port)
        else:
            self.socket = TSocket.TSocket("localhost", self._port)

        if self.server_type in FRAMED_TYPES \
                and not isinstance(self, HeaderTest):
            self.transport = TTransport.TFramedTransport(self.socket)
        else:
            self.transport = TTransport.TBufferedTransport(self.socket)

        self.protocol = self.protocol_factory.getProtocol(self.transport)
        self.transport.open()
        self.client = ThriftTest.Client(self.protocol)
Beispiel #19
0
 def setUp(self):
     if options.trans == 'http':
         uri = '{0}://{1}:{2}{3}'.format(
             ('https' if options.ssl else 'http'), options.host,
             options.port,
             (options.http_path if options.http_path else '/'))
         if options.ssl:
             __cafile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys",
                                     "CA.pem")
             __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys",
                                       "client.crt")
             __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys",
                                      "client.key")
             self.transport = THttpClient.THttpClient(uri,
                                                      cafile=__cafile,
                                                      cert_file=__certfile,
                                                      key_file=__keyfile)
         else:
             self.transport = THttpClient.THttpClient(uri)
     else:
         if options.ssl:
             from thrift.transport import TSSLSocket
             socket = TSSLSocket.TSSLSocket(options.host,
                                            options.port,
                                            validate=False)
         else:
             socket = TSocket.TSocket(options.host, options.port,
                                      options.domain_socket)
         # frame or buffer depending upon args
         self.transport = TTransport.TBufferedTransport(socket)
         if options.trans == 'framed':
             self.transport = TTransport.TFramedTransport(socket)
         elif options.trans == 'buffered':
             self.transport = TTransport.TBufferedTransport(socket)
         elif options.trans == '':
             raise AssertionError('Unknown --transport option: %s' %
                                  options.trans)
         if options.zlib:
             self.transport = TZlibTransport.TZlibTransport(
                 self.transport, 9)
     self.transport.open()
     protocol = self.get_protocol(self.transport)
     self.client = ThriftTest.Client(protocol)
     # for multiplexed services:
     protocol2 = self.get_protocol2(self.transport)
     self.client2 = SecondService.Client(
         protocol2) if protocol2 is not None else None
    def get_instance(self):
        if self.http:
            self.transport = THttpClient.THttpClient(self.host, self.port,
                                                     self.uri)
        else:
            socket = TSSLSocket.TSSLSocket(
                self.host, self.port,
                validate=False) if self.ssl else TSocket.TSocket(
                    self.host, self.port)
        if self.framed:
            self.transport = TTransport.TFramedTransport(socket)
        else:
            self.transport = TTransport.TBufferedTransport(socket)
        protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = ThriftHiveMetastore.Client(protocol)
        self.transport.open()

        return self.client
Beispiel #21
0
    def get_thrift_client(self, use_ssl):
        socket = (
            TSSLSocket.TSSLSocket(
                host=self.cli_opts.host,
                port=self.cli_opts.openr_ctrl_port,
                # verify server
                cert_reqs=ssl.CERT_REQUIRED,
                ca_certs=self.cli_opts.ca_file,
                certfile=self.cli_opts.cert_file,
                keyfile=self.cli_opts.key_file,
                verify_name=self.cli_opts.acceptable_peer_name,
            ) if use_ssl else TSocket.TSocket(
                host=self.cli_opts.host, port=self.cli_opts.openr_ctrl_port))
        socket.setTimeout(self.cli_opts.timeout)
        transport = THeaderTransport.THeaderTransport(socket)
        protocol = THeaderProtocol.THeaderProtocol(transport)

        transport.open()
        return OpenrCtrl.Client(protocol)
Beispiel #22
0
 def __init__(
     self,
     host: str,
     ca_file: str,
     cert_file: str,
     key_file: str,
     acceptable_peer_name: str,
     port: int = consts.Consts.CTRL_PORT,
     timeout_ms: int = 5000,
 ) -> None:
     socket = TSSLSocket.TSSLSocket(
         host=host,
         port=port,
         cert_reqs=ssl.CERT_REQUIRED,
         ca_certs=ca_file,
         certfile=cert_file,
         keyfile=key_file,
         verify_name=acceptable_peer_name,
     )
     socket.setTimeout(timeout_ms)
     OpenrCtrlClient.__init__(self, host, THeaderTransport.THeaderTransport(socket))
Beispiel #23
0
    def main(self):
        self.set_proctitle(self.name)

        self.set_exit_handler()
        try:
            handler = StateServiceHandler(self)
            processor = StateService.Processor(handler)
            if self.certfile:
                transport = TSSLSocket.TSSLServerSocket(self.host,
                                                        self.port,
                                                        certfile=self.certfile)
            else:
                transport = TSocket.TServerSocket(self.host, self.port)
            tfactory = TTransport.TBufferedTransportFactory()
            pfactory = TBinaryProtocol.TBinaryProtocolFactory()
            # In order to accept multiple simultaneous clients, we use TThreadedServer
            server = TServer.TThreadedServer(processor, transport, tfactory,
                                             pfactory)
            server.serve()
        except Exception as exp:
            logger.error("Error while trying to launch TSCA module %s", exp)
def startServer(config):
    def catch_shutdown(signal, frame):
        click.echo(f"Caught SIGTERM. Shutting down. Signal: {signal} Frame: {frame}")
        handler.keyboard_interrupt_handler_playbooks()
        click.echo("SIGTERM was handled. Exiting with Exitcode: -1.")
        sys.exit(-1)

    signal.signal(signal.SIGTERM, catch_shutdown)
    click.echo("Start Cloud-Client-Portal Server")

    CONFIG_FILE = config
    with open(CONFIG_FILE, "r") as ymlfile:
        cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader)
        HOST = cfg["openstack_connection"]["host"]
        PORT = cfg["openstack_connection"]["port"]
        USE_SSL = cfg["openstack_connection"].get("use_ssl", True)
        if USE_SSL:
            CERTFILE = cfg["openstack_connection"]["certfile"]
        THREADS = cfg["openstack_connection"]["threads"]
    click.echo(f"Server is running on port {PORT}")
    handler = VirtualMachineHandler(CONFIG_FILE)
    processor = Processor(handler)
    if USE_SSL:
        click.echo("Use SSL")
        transport = TSSLSocket.TSSLServerSocket(
            host=HOST, port=PORT, certfile=CERTFILE, ssl_version=ssl.PROTOCOL_TLS_SERVER
        )
    else:
        click.echo("Does not use SSL")
        transport = TSocket.TServerSocket(host=HOST, port=PORT)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = TServer.TThreadPoolServer(
        processor, transport, tfactory, pfactory, daemon=True
    )
    server.setNumThreads(THREADS)
    click.echo(f"Started with {THREADS} threads!")

    server.serve()
Beispiel #25
0
    def connect(self):
        """Connect to the HostHandler."""
        self._logger.info(
            "Initialize SSLSocket using certfile=%s, keyfile=%s, capath=%s, ciphers=%s"
            % (self._certfile, self._keyfile, self._capath, self._ciphers))
        sock = TSSLSocket.TSSLSocket(host=self._host,
                                     port=self._port,
                                     validate=self._validate,
                                     certfile=self._certfile,
                                     keyfile=self._keyfile,
                                     capath=self._capath,
                                     ciphers=self._ciphers)

        if self._client_timeout:
            sock.setTimeout(self._client_timeout * 1000)
        self._transport = TTransport.TFramedTransport(sock)
        protocol = TCompactProtocol.TCompactProtocol(self._transport)
        mux_protocol = TMultiplexedProtocol.TMultiplexedProtocol(
            protocol, self._service_name)
        self._client = self._client_cls(mux_protocol)
        self._transport.open()
        self._logger.info("Connected to %s:%s. for service %s" %
                          (self._host, self._port, self._service_name))
Beispiel #26
0
    def _open_connection(self, address):
        """ Opens a connection with a server address.

        :param address: the address of the server to connect to

        """
        (url, port) = self._parse_address_for_hostname_and_port(address)
        if self._tls:
            verifier_type = self._get_verifier_type(
                self.cert_verification_mode)
            if self._proxy:
                proxy_host, proxy_port = self._proxy.split(":")
                self._transport = TProxySSLSocket(url,
                                                  port,
                                                  proxy_host,
                                                  proxy_port,
                                                  verifier_type,
                                                  ca_certs=self._tls_key_path)
            else:
                ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                if self._tls_key_path is not None:
                    ssl_context.load_cert_chain(self._tls_key_path,
                                                self._tls_key_path)
                ssl_context.verify_mode = verifier_type
                self._transport = TSSLSocket.TSSLSocket(
                    url, port, ssl_context=ssl_context)
        else:
            if self._proxy:
                proxy_host, proxy_port = self._proxy.split(":")
                self._transport = TProxySocket(proxy_host, proxy_port, url,
                                               port)
            else:
                self._transport = TSocket.TSocket(url, port)
        self._transport = TTransport.TFramedTransport(self._transport)
        self._transport.open()
        self._protocol = TFinagleProtocol(self._transport,
                                          client_id=self._client_id)
Beispiel #27
0
    def __init__(
        self,
        uri=None,
        user=None,
        password=None,
        host=None,
        port=6274,
        dbname=None,
        protocol='binary',
        sessionid=None,
        bin_cert_validate=None,
        bin_ca_certs=None,
    ):

        self.sessionid = None
        if sessionid is not None:
            if any([user, password, uri, dbname]):
                raise TypeError("Cannot specify sessionid with user, password,"
                                " dbname, or uri")
        if uri is not None:
            if not all([
                    user is None, password is None, host is None, port == 6274,
                    dbname is None, protocol == 'binary',
                    bin_cert_validate is None, bin_ca_certs is None
            ]):
                raise TypeError("Cannot specify both URI and other arguments")
            user, password, host, port, dbname, protocol, \
                bin_cert_validate, bin_ca_certs = _parse_uri(uri)
        if host is None:
            raise TypeError("`host` parameter is required.")
        if protocol != 'binary' and not all(
            [bin_cert_validate is None, bin_ca_certs is None]):
            raise TypeError("Cannot specify bin_cert_validate or bin_ca_certs,"
                            " without binary protocol")
        if protocol in ("http", "https"):
            if not host.startswith(protocol):
                # the THttpClient expects http[s]://localhost
                host = '{0}://{1}'.format(protocol, host)
            transport = THttpClient.THttpClient("{}:{}".format(host, port))
            proto = TJSONProtocol.TJSONProtocol(transport)
            socket = None
        elif protocol == "binary":
            if any([bin_cert_validate is not None, bin_ca_certs]):
                socket = TSSLSocket.TSSLSocket(host,
                                               port,
                                               validate=(bin_cert_validate),
                                               ca_certs=bin_ca_certs)
            else:
                socket = TSocket.TSocket(host, port)
            transport = TTransport.TBufferedTransport(socket)
            proto = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
        else:
            raise ValueError("`protocol` should be one of",
                             " ['http', 'https', 'binary'],",
                             " got {} instead".format(protocol))
        self._user = user
        self._password = password
        self._host = host
        self._port = port
        self._dbname = dbname
        self._transport = transport
        self._protocol = protocol
        self._socket = socket
        self._closed = 0
        self._tdf = None
        try:
            self._transport.open()
        except TTransportException as e:
            if e.NOT_OPEN:
                err = OperationalError("Could not connect to database")
                raise err from e
            else:
                raise
        self._client = Client(proto)
        try:
            # If a sessionid was passed, we should validate it
            if sessionid:
                self._session = sessionid
                self.get_tables()
                self.sessionid = sessionid
            else:
                self._session = self._client.connect(user, password, dbname)
        except TMapDException as e:
            raise _translate_exception(e) from e
        except TTransportException:
            raise ValueError(f"Connection failed with port {port} and "
                             f"protocol '{protocol}'. Try port 6274 for "
                             "protocol == binary or 6273, 6278 or 443 for "
                             "http[s]")

        # if OmniSci version <4.6, raise RuntimeError, as data import can be
        # incorrect for columnar date loads
        # Caused by https://github.com/omnisci/pymapd/pull/188
        semver = self._client.get_version()
        if Version(semver.split("-")[0]) < Version("4.6"):
            raise RuntimeError(f"Version {semver} of OmniSci detected. "
                               "Please use pymapd <0.11. See release notes "
                               "for more details.")
Beispiel #28
0
def main(options):
    # set up the protocol factory form the --protocol option
    prot_factories = {
        'binary': TBinaryProtocol.TBinaryProtocolFactory,
        'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
        'compact': TCompactProtocol.TCompactProtocolFactory,
        'json': TJSONProtocol.TJSONProtocolFactory,
    }
    pfactory_cls = prot_factories.get(options.proto, None)
    if pfactory_cls is None:
        raise AssertionError('Unknown --protocol option: %s' % options.proto)
    pfactory = pfactory_cls()
    try:
        pfactory.string_length_limit = options.string_limit
        pfactory.container_length_limit = options.container_limit
    except:
        # Ignore errors for those protocols that does not support length limit
        pass

    # get the server type (TSimpleServer, TNonblockingServer, etc...)
    if len(args) > 1:
        raise AssertionError(
            'Only one server type may be specified, not multiple types.')
    server_type = args[0]

    # Set up the handler and processor objects
    handler = TestHandler()
    processor = ThriftTest.Processor(handler)

    # Handle THttpServer as a special case
    if server_type == 'THttpServer':
        server = THttpServer.THttpServer(processor, ('', options.port),
                                         pfactory)
        server.serve()
        sys.exit(0)

    # set up server transport and transport factory

    abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys',
                                'server.pem')

    host = None
    if options.ssl:
        from thrift.transport import TSSLSocket
        transport = TSSLSocket.TSSLServerSocket(host,
                                                options.port,
                                                certfile=abs_key_path)
    else:
        transport = TSocket.TServerSocket(host, options.port)
    tfactory = TTransport.TBufferedTransportFactory()
    if options.trans == 'buffered':
        tfactory = TTransport.TBufferedTransportFactory()
    elif options.trans == 'framed':
        tfactory = TTransport.TFramedTransportFactory()
    elif options.trans == '':
        raise AssertionError('Unknown --transport option: %s' % options.trans)
    else:
        tfactory = TTransport.TBufferedTransportFactory()
    # if --zlib, then wrap server transport, and use a different transport factory
    if options.zlib:
        transport = TZlibTransport.TZlibTransport(transport)  # wrap  with zlib
        tfactory = TZlibTransport.TZlibTransportFactory()

    # do server-specific setup here:
    if server_type == "TNonblockingServer":
        server = TNonblockingServer.TNonblockingServer(
            processor, transport, inputProtocolFactory=pfactory)
    elif server_type == "TProcessPoolServer":
        import signal
        from thrift.server import TProcessPoolServer
        server = TProcessPoolServer.TProcessPoolServer(processor, transport,
                                                       tfactory, pfactory)
        server.setNumWorkers(5)

        def set_alarm():
            def clean_shutdown(signum, frame):
                for worker in server.workers:
                    if options.verbose > 0:
                        logging.info('Terminating worker: %s' % worker)
                    worker.terminate()
                if options.verbose > 0:
                    logging.info('Requesting server to stop()')
                try:
                    server.stop()
                except:
                    pass

            signal.signal(signal.SIGALRM, clean_shutdown)
            signal.alarm(4)

        set_alarm()
    else:
        # look up server class dynamically to instantiate server
        ServerClass = getattr(TServer, server_type)
        server = ServerClass(processor, transport, tfactory, pfactory)
    # enter server main loop
    server.serve()
Beispiel #29
0
def main(options):
    # common header allowed client types
    allowed_client_types = [
        THeaderTransport.THeaderClientType.HEADERS,
        THeaderTransport.THeaderClientType.FRAMED_BINARY,
        THeaderTransport.THeaderClientType.UNFRAMED_BINARY,
        THeaderTransport.THeaderClientType.FRAMED_COMPACT,
        THeaderTransport.THeaderClientType.UNFRAMED_COMPACT,
    ]

    # set up the protocol factory form the --protocol option
    prot_factories = {
        'accel':
        TBinaryProtocol.TBinaryProtocolAcceleratedFactory(),
        'multia':
        TBinaryProtocol.TBinaryProtocolAcceleratedFactory(),
        'accelc':
        TCompactProtocol.TCompactProtocolAcceleratedFactory(),
        'multiac':
        TCompactProtocol.TCompactProtocolAcceleratedFactory(),
        'binary':
        TPedanticSequenceIdProtocolFactory(
            TBinaryProtocol.TBinaryProtocolFactory()),
        'multi':
        TPedanticSequenceIdProtocolFactory(
            TBinaryProtocol.TBinaryProtocolFactory()),
        'compact':
        TCompactProtocol.TCompactProtocolFactory(),
        'multic':
        TCompactProtocol.TCompactProtocolFactory(),
        'header':
        THeaderProtocol.THeaderProtocolFactory(allowed_client_types),
        'multih':
        THeaderProtocol.THeaderProtocolFactory(allowed_client_types),
        'json':
        TJSONProtocol.TJSONProtocolFactory(),
        'multij':
        TJSONProtocol.TJSONProtocolFactory(),
    }
    pfactory = prot_factories.get(options.proto, None)
    if pfactory is None:
        raise AssertionError('Unknown --protocol option: %s' % options.proto)
    try:
        pfactory.string_length_limit = options.string_limit
        pfactory.container_length_limit = options.container_limit
    except Exception:
        # Ignore errors for those protocols that does not support length limit
        pass

    # get the server type (TSimpleServer, TNonblockingServer, etc...)
    if len(args) > 1:
        raise AssertionError(
            'Only one server type may be specified, not multiple types.')
    server_type = args[0]
    if options.trans == 'http':
        server_type = 'THttpServer'

    # Set up the handler and processor objects
    handler = TestHandler()
    processor = ThriftTest.Processor(handler)

    if options.proto.startswith('multi'):
        secondHandler = SecondHandler()
        secondProcessor = SecondService.Processor(secondHandler)

        multiplexedProcessor = TMultiplexedProcessor()
        multiplexedProcessor.registerDefault(processor)
        multiplexedProcessor.registerProcessor('ThriftTest', processor)
        multiplexedProcessor.registerProcessor('SecondService',
                                               secondProcessor)
        processor = multiplexedProcessor

    global server

    # Handle THttpServer as a special case
    if server_type == 'THttpServer':
        if options.ssl:
            __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys",
                                      "server.crt")
            __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys",
                                     "server.key")
            server = THttpServer.THttpServer(processor, ('', options.port),
                                             pfactory,
                                             cert_file=__certfile,
                                             key_file=__keyfile)
        else:
            server = THttpServer.THttpServer(processor, ('', options.port),
                                             pfactory)
        server.serve()
        sys.exit(0)

    # set up server transport and transport factory

    abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys',
                                'server.pem')

    host = None
    if options.ssl:
        from thrift.transport import TSSLSocket
        transport = TSSLSocket.TSSLServerSocket(host,
                                                options.port,
                                                certfile=abs_key_path)
    else:
        transport = TSocket.TServerSocket(host, options.port,
                                          options.domain_socket)
    tfactory = TTransport.TBufferedTransportFactory()
    if options.trans == 'buffered':
        tfactory = TTransport.TBufferedTransportFactory()
    elif options.trans == 'framed':
        tfactory = TTransport.TFramedTransportFactory()
    elif options.trans == '':
        raise AssertionError('Unknown --transport option: %s' % options.trans)
    else:
        tfactory = TTransport.TBufferedTransportFactory()
    # if --zlib, then wrap server transport, and use a different transport factory
    if options.zlib:
        transport = TZlibTransport.TZlibTransport(transport)  # wrap  with zlib
        tfactory = TZlibTransport.TZlibTransportFactory()

    # do server-specific setup here:
    if server_type == "TNonblockingServer":
        server = TNonblockingServer.TNonblockingServer(
            processor, transport, inputProtocolFactory=pfactory)
    elif server_type == "TProcessPoolServer":
        import signal
        from thrift.server import TProcessPoolServer
        server = TProcessPoolServer.TProcessPoolServer(processor, transport,
                                                       tfactory, pfactory)
        server.setNumWorkers(5)

        def set_alarm():
            def clean_shutdown(signum, frame):
                for worker in server.workers:
                    if options.verbose > 0:
                        logging.info('Terminating worker: %s' % worker)
                    worker.terminate()
                if options.verbose > 0:
                    logging.info('Requesting server to stop()')
                try:
                    server.stop()
                except Exception:
                    pass

            signal.signal(signal.SIGALRM, clean_shutdown)
            signal.alarm(4)

        set_alarm()
    else:
        # look up server class dynamically to instantiate server
        ServerClass = getattr(TServer, server_type)
        server = ServerClass(processor, transport, tfactory, pfactory)
    # enter server main loop
    server.serve()
Beispiel #30
0
        return challenge
    
    def balance_history(self, address):
        history_list = []

        for x in range(5):
            balance_history = BalanceTransfer()

            balance_history.amount = random.randrange(100)
            balance_history.receiverId = uuid.uuid4().hex
            balance_history.senderId = uuid.uuid4().hex
            balance_history.timestamp = random.randrange(99999999)

            history_list.append(balance_history)

        print('balance_hisotry(%s, %s)' % (address, history_list))
        return history_list


if __name__ == '__main__':
    handler = TransactionHandler()
    processor = Transaction.Processor(handler)
    transport = TSSLSocket.TSSLServerSocket(host='127.0.0.1', port=9090, certfile='.ssh/server.crt', keyfile='.ssh/server.key')
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    print('Starting the server...')
    server.serve()
    print('Done.')