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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • How to Merge HTML Documents in Java
  • How GitHub Copilot Helps You Write More Secure Code
  • Security by Design: Building Full-Stack Applications With DevSecOps
  • Designing Fault-Tolerant Messaging Workflows Using State Machine Architecture
  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.5K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!