Over a million developers have joined DZone.
Gold Partner

How to Secure an Apache Thrift Service

Make sure your Apache Thrift services are secure with this awesome tutorial.

· Database Zone

The Database Zone is brought to you in partnership with Clusterpoint.  Read about ACID-compliant Transactions Mechanism within NoSQL database from Clusterpoint.

A previous post explained how to create a Thrift service and consume it in different modes such as blocking, non blocking etc. Now if you are thinking of using Apache Thrift for client-server data exchange over a public network, the chances are that you may run in to the requirement of securing the data transmitted over Thrift. This post explains how SSL can be used to secure communication between a Thrift server and a Thrift client.


1. I will be using the same service definition (arithmetic.thrift) and generated codes plus the service implementation (ArithmeticServiceImpl) from earlier post.

2. You need to have a key store with server private key at server-side and a trust store containing server’s public key at client side. For this example let’s create a key store and trust store using JDK keytool.

Creating Key Store

1. Go to Java installation bin directory in command line and execute following.

keytool -genkeypair -alias certificatekey -keyalg RSA -validity 7 -keystore keystore.jks

 2. Give a suitable password and answers to the prompts. After that it will create the key store keystore.jks containing generated private/ public key pair.

3. Export the certificate (cret.cer) containing the public key from the key store using following command.

keytool -export -alias certificatekey -keystore keystore.jks -rfc -file cert.cer


Create Trust Store

1. Now let’s create the trust store (truststore.jks) and import the certificate to it. This can be done using single command line as given below.

keytool -import -alias certificatekey -file cert.cer -keystore truststore.jks

Again give a password and say yes to the prompt asking whether to trust this certificate. Now the certificate setup is complete. Let’s create the secure Thrift server and client to consume it.

Secure Thrift Server

Code for the secure server is given below. It uses TSSLTransportFactory to obtain a secure socket. Key store location is set as a parameter. Change the “path to the keystore.jks” and “keystore.jks password” parameters to suitable values in the code.

public class SecureServer {

    private void start() {
        try {
            TSSLTransportFactory.TSSLTransportParameters params =
                    new TSSLTransportFactory.TSSLTransportParameters();
            params.setKeyStore("path to keystore.jks", "keystore.jks password");

            TServerSocket serverTransport = TSSLTransportFactory.getServerSocket(
                    7911, 10000, InetAddress.getByName("localhost"), params);
            ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());

            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).
            System.out.println("Starting server on port 7911 ...");
        } catch (TTransportException e) {
        } catch (UnknownHostException e) {


    public static void main(String[] args) {
        SecureServer srv = new SecureServer();

Secure Thrift Client

Client code is given below.  As with the server replace “path to trustore.jks” and “truststore.jks password” parameters to actual values.

public class SecureClient {

    private void invoke() {
        TTransport transport;
        try {

            TSSLTransportFactory.TSSLTransportParameters params =
                    new TSSLTransportFactory.TSSLTransportParameters();
            params.setTrustStore("path to truststore.jks", "truststore.jks password");

            transport = TSSLTransportFactory.getClientSocket("localhost", 7911, 10000, params);
            TProtocol protocol = new TBinaryProtocol(transport);

            ArithmeticService.Client client = new ArithmeticService.Client(protocol);

            long addResult = client.add(100, 200);
            System.out.println("Add result: " + addResult);
            long multiplyResult = client.multiply(20, 40);
            System.out.println("Multiply result: " + multiplyResult);

        } catch (TTransportException e) {
        } catch (TException e) {

 That’s it. You have now secured your Thrift service with SSL and created a secure client to talk with your secure service.

Apart from this method you can also use TServlet transport to expose the Thrift service as Servlet and expose your Servlet securely to the outside world. I will describe this method in an upcoming post.

Source: http://chamibuddhika.wordpress.com/2011/10/03/securing-a-thrift-service/

The Database Zone is brought to you in partnership with Clusterpoint.  Read about ACID-compliant Transactions Mechanism within NoSQL database from Clusterpoint.

java,database,apache thrift,apache,server

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}