You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Russell Haley <rh...@starfishmedical.com> on 2024/11/30 00:50:48 UTC

SSL/TLS with CPP Server

Hello,

My name is Russell and I am new to Thirft and the Thirft mail community.

I have successfully used Thrift to create some prototypes of remote systems passing messages using the C++ libraries. I am compiling Boost and Thrift myself and link statically into the application. Currently I have implemented the TSimpleServer. The example uses unencrypted communications as far as I can tell and I would like to implement TLS/SSL communication. I see examples of a Java SSL server on the internet but haven't seen anything concrete in C++. My attempts to duplicate the Java code in C++ were unsuccessful. Is there a reference implementation somewhere?

I tried JetBrains AI and it generated the following code which I modified to fit my application:

#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TSSLServerSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>

// Function to start an Apache Thrift server using TLS
void StartTLSThriftServer(const std::string &host, int port,
                          const std::string &certPath, const std::string &keyPath) {
    // Create SSL/TLS configuration
    apache::thrift::transport::TSSLSocketFactory sslSocketFactory;
    sslSocketFactory.loadCertificate(certPath.c_str());
    sslSocketFactory.loadPrivateKey(keyPath.c_str());

    // Setup server transport
    auto serverTransport = std::make_shared<apache::thrift::transport::TSSLServerSocket>(port, &sslSocketFactory);

    // Setup protocol factory
    auto protocolFactory = std::make_shared<apache::thrift::protocol::TBinaryProtocolFactory>();

    // Set up the handler and processor (assuming a pre-defined handler)
    auto handler = std::make_shared<YourServiceHandler>(); // Replace with your service handler
    auto processor = std::make_shared<YourServiceProcessor>(handler); // Replace with your service processor

    // Create and run the server
    apache::thrift::server::TSimpleServer server(processor, serverTransport, protocolFactory);
    std::cout << "Starting the Apache Thrift server with TLS on " << host << ":" << port << std::endl;
    server.serve();
}

When I compile, I get the following error:

/home/osboxes/starfish/dataman_service/src/main.cpp: In function ‘void StartTLSThriftServer(const string&, int, const string&, const string&)’:
/home/osboxes/starfish/dataman_service/src/main.cpp:305:23: error: aggregate ‘apache::thrift::transport::TSSLSocketFactory sslSocketFactory’ has incomplete type and cannot be defined
  305 |     TSSLSocketFactory sslSocketFactory;
      |                       ^~~~~~~~~~~~~~~~


Any ideas where I should go with this? (Or should I look at using nginx as a proxy?)

I also have a question about performance that I will ask on a different thread.

Thanks,
Russell



Re: SSL/TLS with CPP Server

Posted by Mario Emmenlauer <ma...@emmenlauer.de.INVALID>.
Hi,

On 30.11.24 01:50, Russell Haley wrote:
> Hello,
> 
> My name is Russell and I am new to Thirft and the Thirft mail community.
> 
> I have successfully used Thrift to create some prototypes of remote systems passing messages using the C++ libraries. I am compiling Boost and Thrift myself and link statically into the application. Currently I have implemented the TSimpleServer. The example uses unencrypted communications as far as I can tell and I would like to implement TLS/SSL communication. I see examples of a Java SSL server on the internet but haven't seen anything concrete in C++. My attempts to duplicate the Java code in C++ were unsuccessful. Is there a reference implementation somewhere?
> 
> I tried JetBrains AI and it generated the following code which I modified to fit my application:
> 
> #include <thrift/server/TSimpleServer.h>
> #include <thrift/transport/TSSLServerSocket.h>
> #include <thrift/protocol/TBinaryProtocol.h>
> 
> // Function to start an Apache Thrift server using TLS
> void StartTLSThriftServer(const std::string &host, int port,
>                            const std::string &certPath, const std::string &keyPath) {
>      // Create SSL/TLS configuration
>      apache::thrift::transport::TSSLSocketFactory sslSocketFactory;
>      sslSocketFactory.loadCertificate(certPath.c_str());
>      sslSocketFactory.loadPrivateKey(keyPath.c_str());
> 
>      // Setup server transport
>      auto serverTransport = std::make_shared<apache::thrift::transport::TSSLServerSocket>(port, &sslSocketFactory);
> 
>      // Setup protocol factory
>      auto protocolFactory = std::make_shared<apache::thrift::protocol::TBinaryProtocolFactory>();
> 
>      // Set up the handler and processor (assuming a pre-defined handler)
>      auto handler = std::make_shared<YourServiceHandler>(); // Replace with your service handler
>      auto processor = std::make_shared<YourServiceProcessor>(handler); // Replace with your service processor
> 
>      // Create and run the server
>      apache::thrift::server::TSimpleServer server(processor, serverTransport, protocolFactory);
>      std::cout << "Starting the Apache Thrift server with TLS on " << host << ":" << port << std::endl;
>      server.serve();
> }
> 
> When I compile, I get the following error:
> 
> /home/osboxes/starfish/dataman_service/src/main.cpp: In function ‘void StartTLSThriftServer(const string&, int, const string&, const string&)’:
> /home/osboxes/starfish/dataman_service/src/main.cpp:305:23: error: aggregate ‘apache::thrift::transport::TSSLSocketFactory sslSocketFactory’ has incomplete type and cannot be defined
>    305 |     TSSLSocketFactory sslSocketFactory;
>        |                       ^~~~~~~~~~~~~~~~
> 
> 
> Any ideas where I should go with this? (Or should I look at using nginx as a proxy?)

It could be the code is just missing the right includes. Can you try
to include <thrift/transport/TSSLSocket.h>?

All the best,

     Mario


--
BioDataAnalysis GmbH, Mario Emmenlauer      Tel. Buero: +49-89-74677203
Balanstr. 43                   mailto: memmenlauer * biodataanalysis.de
D-81669 München                          http://www.biodataanalysis.de/