A python example of realizing secure grpc communication Useful links and references: Here are some links that I found it can be helpful when I was trying to work out how to setup the python ssl communication.
Secure gRPC with TLS/SSL, This is golang implementation
certstrap, a convienient tool to generate openssl keys and certificate
gRPC authentication, the official guide of grpc
grpcio, python package, the source code and official document of grpc python
grpc golang ssl example, another golang example of ssl communication
What is the difference between .pem , .csr , .key and .crt?, a stackexchange question which explains the concepts and differences of different types of files
First, build a docker container that contains all required packages. In this example, I choose ubuntu:bionic as the basic image, and I installed other packages onto it.
FROM ubuntu:bionic RUN apt-get update RUN apt-get install python3 -y RUN apt-get install python3-pip -y RUN pip3 install grpcio ADD app /app/ EXPOSE 22222 The Dockerfile is shown above. In the app file, it contains 4 files. They are:
client.py, server.py, test_pb2.py, test_pb2_grpc.py
test_pb2.py and test_pb2_grpc.py is generated by compiling the test.protofile. The content for test.proto is shown below.
syntax = "proto3"; package lmjwtest; // service, encode a plain text service EncodeService { // request a service of encode rpc GetEncode(plaintext) returns (encodetext) {} } message plaintext { string pttransactionID = 1; string ptproperties = 2; string ptsenderID = 3; } message encodetext { string enctransactionID = 1; string encproperties = 2; string encsenderID = 3; } By using the grpcio-tools to compile the test.