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

  • IoT Needs To Get Serious About Security
  • Strengthening Cybersecurity: The Role of Digital Certificates and PKI in Authentication
  • Best Practices To Secure Data Transmission
  • 19 Most Common OpenSSL Commands for 2023

Trending

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)
  • RAG Done Right: When to Use SQL, Search, and Vector Retrieval and How To Combine Them
  • A Comprehensive Guide to Prompt Engineering
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Secure Your gRPC Services With SSL/TLS

Secure Your gRPC Services With SSL/TLS

Learn how to easily set up TLS for your gRPC client and server application.

By 
Hakan Altındağ user avatar
Hakan Altındağ
·
Mar. 21, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

This tutorial will walk you through the process of protecting your gRPC services with encryption based on SSL/TLS. The tutorial will provide examples written in Java, but can easily be converted to Scala and Kotlin.

What is gRPC?

gRPC is a high-performance, open source RPC framework initially developed by Google. It helps in eliminating boilerplate code and helps in connecting polyglot services in and across data centers.

See here for more: https://grpc.io/

Maven Dependencies

The following maven dependencies will be used for the examples:

Java
 




xxxxxxxxxx
1
27


 
1
<dependency>
2
    <groupId>io.grpc</groupId>
3
    <artifactId>grpc-netty</artifactId>
4
</dependency>
5
<dependency>
6
    <groupId>io.grpc</groupId>
7
    <artifactId>grpc-protobuf</artifactId>
8
</dependency>
9
<dependency>
10
    <groupId>io.grpc</groupId>
11
    <artifactId>grpc-stub</artifactId>
12
</dependency>
13

          
14
<dependency>
15
    <groupId>com.google.protobuf</groupId>
16
    <artifactId>protobuf-java</artifactId>
17
</dependency>
18

          
19
<dependency>
20
    <groupId>javax.annotation</groupId>
21
    <artifactId>javax.annotation-api</artifactId>
22
</dependency>
23

          
24
<dependency>
25
    <groupId>io.github.hakky54</groupId>
26
    <artifactId>sslcontext-kickstart-for-netty</artifactId>
27
</dependency>



Example SSL Configuration

Server

Java
 




xxxxxxxxxx
1
43


 
1
import io.grpc.Server;
2
import io.grpc.netty.GrpcSslContexts;
3
import io.grpc.netty.NettyServerBuilder;
4
import io.netty.handler.ssl.SslContext;
5
import nl.altindag.grpc.server.service.HelloServiceImpl;
6
import nl.altindag.ssl.SSLFactory;
7
import nl.altindag.ssl.util.NettySslUtils;
8

          
9
import java.io.IOException;
10

          
11
public class App {
12

          
13
    private Server server;
14

          
15
    private void start() throws IOException {
16
        SSLFactory sslFactory = SSLFactory.builder()
17
                .withIdentityMaterial("server/identity.jks", "secret".toCharArray())
18
                .withTrustMaterial("server/truststore.jks", "secret".toCharArray())
19
                .withNeedClientAuthentication()
20
                .build();
21

          
22
        SslContext sslContext = GrpcSslContexts.configure(NettySslUtils.forServer(sslFactory)).build();
23

          
24
        server = NettyServerBuilder.forPort(8443)
25
                .addService(new HelloServiceImpl())
26
                .sslContext(sslContext)
27
                .build()
28
                .start();
29
    }
30

          
31
    private void blockUntilShutdown() throws InterruptedException {
32
        if (server != null) {
33
            server.awaitTermination();
34
        }
35
    }
36

          
37
    public static void main(String[] args) throws IOException, InterruptedException {
38
        final App server = new App();
39
        server.start();
40
        server.blockUntilShutdown();
41
    }
42

          
43
}


Client

Java
 




xxxxxxxxxx
1
51


 
1
package nl.altindag.grpc.client;
2

          
3
import io.grpc.Channel;
4
import io.grpc.ManagedChannel;
5
import io.grpc.netty.GrpcSslContexts;
6
import io.grpc.netty.NettyChannelBuilder;
7
import io.netty.handler.ssl.SslContext;
8
import nl.altindag.grpc.HelloRequest;
9
import nl.altindag.grpc.HelloResponse;
10
import nl.altindag.grpc.HelloServiceGrpc;
11
import nl.altindag.ssl.SSLFactory;
12
import nl.altindag.ssl.util.NettySslUtils;
13

          
14
import javax.net.ssl.SSLException;
15

          
16
public class App {
17

          
18
    private final HelloServiceGrpc.HelloServiceBlockingStub stub;
19

          
20
    public App(Channel channel) {
21
        stub = HelloServiceGrpc.newBlockingStub(channel);
22
    }
23

          
24
    public void hello(String name) {
25
        HelloRequest request = HelloRequest.newBuilder().setName(name).build();
26
        HelloResponse response = stub.hello(request);
27
        System.out.println(response.getMessage());
28
    }
29

          
30

          
31
    public static void main(String[] args) throws SSLException {
32
        SSLFactory sslFactory = SSLFactory.builder()
33
                .withIdentityMaterial("client/identity.jks", "secret".toCharArray())
34
                .withTrustMaterial("client/truststore.jks", "secret".toCharArray())
35
                .withDefaultTrustMaterial()
36
                .build();
37

          
38
        SslContext sslContext = GrpcSslContexts.configure(NettySslUtils.forClient(sslFactory)).build();
39

          
40
        ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 8443)
41
                .sslContext(sslContext)
42
                .build();
43

          
44
        App app = new App(channel);
45
        app.hello("John");
46

          
47
        channel.shutdown();
48
    }
49

          
50
}
51

          



Conclusion

In this tutorial, we saw how we could set up and configure SSL/TLS for gRPC on a client as well as server-side.

As usual, you'll find the sources over on GitHub.

security gRPC TLS

Published at DZone with permission of Hakan Altındağ. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • IoT Needs To Get Serious About Security
  • Strengthening Cybersecurity: The Role of Digital Certificates and PKI in Authentication
  • Best Practices To Secure Data Transmission
  • 19 Most Common OpenSSL Commands for 2023

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