DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • Detecting Advanced Persistent Threats Using Behavioral Analytics and Log Correlation
  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  • Catching Data Perimeter Drift Before It Reaches Production

Trending

  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • Why Good Models Fail After Deployment
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. How to Secure an Apache Thrift Service

How to Secure an Apache Thrift Service

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

By 
Buddhika  Chamith user avatar
Buddhika Chamith
·
Feb. 13, 12 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
20.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Prerequisites

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).
                    processor(processor));
            System.out.println("Starting server on port 7911 ...");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {

        }
    }

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


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);

            transport.close();
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        }
    }

 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/

Thrift (protocol) security Apache Thrift

Opinions expressed by DZone contributors are their own.

Related

  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • Detecting Advanced Persistent Threats Using Behavioral Analytics and Log Correlation
  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  • Catching Data Perimeter Drift Before It Reaches Production

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook