org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters Java Examples

The following examples show how to use org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractThriftServerImpl.java    From ikasoa with MIT License 5 votes vote down vote up
/**
 * 获取一个服务传输类型
 * <p>
 * 如果使用非Socket传输类型,需要重写此方法.
 * 
 * @return TServerTransport 服务传输类型
 */
@Override
public TServerTransport getTransport() throws TTransportException {
	if (ObjectUtil.isNull(serverSocket)) {
		TSSLTransportParameters params = getServerConfiguration().getSslTransportParameters();
		serverSocket = ObjectUtil.isNull(params) ? new TServerSocket(getServerPort())
				: TSSLTransportFactory.getServerSocket(getServerPort(), 0, null, params);
	}
	return serverSocket;
}
 
Example #2
Source File: ServerTest.java    From ikasoa with MIT License 5 votes vote down vote up
@Test
public void testSSLThriftServer() {
	int serverPort = ServerUtil.getNewPort();
	String keystoreStr = getSslUrlFileString("/ssl/.keystore");
	String truststoreStr = getSslUrlFileString("/ssl/.truststore");
	if (StringUtil.orIsEmpty(keystoreStr, truststoreStr))
		// 如果没有密钥就跳过测试
		return;
	// 服务端设置
	ThriftServerConfiguration serverConfiguration = new ThriftServerConfiguration();
	TSSLTransportParameters sslServerTransportParameters = new TSSLTransportParameters();
	sslServerTransportParameters.setKeyStore(keystoreStr, TestConstants.SSL_KEY_PASS, null, null);
	serverConfiguration.setSslTransportParameters(sslServerTransportParameters);
	// 客户端设置
	ThriftClientConfiguration clientConfiguration = new ThriftClientConfiguration();
	TSSLTransportParameters sslClientTransportParameters = new TSSLTransportParameters();
	sslClientTransportParameters.setTrustStore(truststoreStr, TestConstants.SSL_KEY_PASS, "SunX509", "JKS");
	clientConfiguration.setSslTransportParameters(sslClientTransportParameters);
	// 因为创建和回收连接会增加测试执行时间,所以这里不使用Socket连接池
	clientConfiguration.setSocketPool(new NoSocketPoolImpl());

	Factory factory = new GeneralFactory(serverConfiguration, clientConfiguration);
	ThriftServer thriftServer = factory.getThriftServer(serverName, serverPort, processor);
	thriftServer.run();
	waiting();
	try (ThriftClient thriftClient = factory.getThriftClient(TestConstants.LOCAL_HOST, serverPort);
			TTransport transport = thriftClient.getTransport()) {
		// SSL方式不需要transport.open()
		assertEquals(TestConstants.TEST_STRING,
				new ThriftSimpleService.Client(thriftClient.getProtocol(transport)).get(TestConstants.TEST_STRING));
	} catch (Exception e) {
		fail();
	} finally {
		thriftServer.stop();
	}
}
 
Example #3
Source File: DBConn.java    From Doradus with Apache License 2.0 5 votes vote down vote up
private TSocket createTLSSocket(String host) throws TTransportException {
    TSSLTransportParameters sslParams = new TSSLTransportParameters("SSL", m_dbtls_cipher_suites);
    if (!Utils.isEmpty(m_keystore)) {
        sslParams.setKeyStore(m_keystore, m_keystorepassword);
    }
    if (!Utils.isEmpty(m_truststore)) {
        sslParams.setTrustStore(m_truststore, m_truststorepassword);
    }
    return TSSLTransportFactory.getClientSocket(host, m_dbport, m_db_timeout_millis, sslParams);
}
 
Example #4
Source File: CustomTThreadPoolServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public TServer buildTServer(Args args)
{
    final InetSocketAddress addr = args.addr;
    TServerTransport serverTransport;
    try
    {
        final ClientEncryptionOptions clientEnc = DatabaseDescriptor.getClientEncryptionOptions();
        if (clientEnc.enabled)
        {
            logger.info("enabling encrypted thrift connections between client and server");
            TSSLTransportParameters params = new TSSLTransportParameters(clientEnc.protocol, clientEnc.cipher_suites);
            params.setKeyStore(clientEnc.keystore, clientEnc.keystore_password);
            if (clientEnc.require_client_auth)
            {
                params.setTrustStore(clientEnc.truststore, clientEnc.truststore_password);
                params.requireClientAuth(true);
            }
            TServerSocket sslServer = TSSLTransportFactory.getServerSocket(addr.getPort(), 0, addr.getAddress(), params);
            SSLServerSocket sslServerSocket = (SSLServerSocket) sslServer.getServerSocket();
            sslServerSocket.setEnabledProtocols(SSLFactory.ACCEPTED_PROTOCOLS);
            serverTransport = new TCustomServerSocket(sslServer.getServerSocket(), args.keepAlive, args.sendBufferSize, args.recvBufferSize);
        }
        else
        {
            serverTransport = new TCustomServerSocket(addr, args.keepAlive, args.sendBufferSize, args.recvBufferSize, args.listenBacklog);
        }
    }
    catch (TTransportException e)
    {
        throw new RuntimeException(String.format("Unable to create thrift socket to %s:%s", addr.getAddress(), addr.getPort()), e);
    }
    // ThreadPool Server and will be invocation per connection basis...
    TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport)
                                                             .minWorkerThreads(DatabaseDescriptor.getRpcMinThreads())
                                                             .maxWorkerThreads(DatabaseDescriptor.getRpcMaxThreads())
                                                             .inputTransportFactory(args.inTransportFactory)
                                                             .outputTransportFactory(args.outTransportFactory)
                                                             .inputProtocolFactory(args.tProtocolFactory)
                                                             .outputProtocolFactory(args.tProtocolFactory)
                                                             .processor(args.processor);
    ExecutorService executorService = new ThreadPoolExecutor(serverArgs.minWorkerThreads,
                                                             serverArgs.maxWorkerThreads,
                                                             60,
                                                             TimeUnit.SECONDS,
                                                             new SynchronousQueue<Runnable>(),
                                                             new NamedThreadFactory("Thrift"));
    return new CustomTThreadPoolServer(serverArgs, executorService);
}