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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Hide Sensitive Information Before You Share a File
  • Spring Security JWT Validation Best Practices
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Designing a Polling Pipeline for Rate-Limited APIs Without Losing Data
  • AI-Powered CI Test Failure Triage
  • Genkit Flows on Kubernetes, Routed by a Decision Model: senro + jev in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 13, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
20.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • How to Hide Sensitive Information Before You Share a File
  • Spring Security JWT Validation Best Practices
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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