Skip to content

Commit e2482d3

Browse files
committed
test: add grpc tls test
1 parent 10d932d commit e2482d3

File tree

5 files changed

+488
-0
lines changed

5 files changed

+488
-0
lines changed

go.mod

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module github.com/cloudwego/kitex-tests
2+
3+
go 1.17
4+
5+
require (
6+
github.com/apache/thrift v0.13.0
7+
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b
8+
github.com/cloudwego/fastpb v0.0.4
9+
github.com/cloudwego/kitex v0.8.1-0.20240108100713-a6d5d904434d
10+
github.com/cloudwego/netpoll v0.5.1
11+
google.golang.org/grpc v1.36.1
12+
google.golang.org/protobuf v1.28.1
13+
)
14+
15+
require (
16+
github.com/bytedance/sonic v1.10.2 // indirect
17+
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
18+
github.com/chenzhuoyu/iasm v0.9.1 // indirect
19+
github.com/choleraehyq/pid v0.0.17 // indirect
20+
github.com/cloudwego/configmanager v0.2.0 // indirect
21+
github.com/cloudwego/dynamicgo v0.1.6 // indirect
22+
github.com/cloudwego/frugal v0.1.12 // indirect
23+
github.com/cloudwego/localsession v0.0.2 // indirect
24+
github.com/cloudwego/thriftgo v0.3.5 // indirect
25+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
26+
github.com/fatih/structtag v1.2.0 // indirect
27+
github.com/golang/protobuf v1.5.2 // indirect
28+
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
29+
github.com/iancoleman/strcase v0.2.0 // indirect
30+
github.com/jhump/protoreflect v1.8.2 // indirect
31+
github.com/json-iterator/go v1.1.12 // indirect
32+
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
33+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
34+
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
35+
github.com/modern-go/reflect2 v1.0.2 // indirect
36+
github.com/oleiade/lane v1.0.1 // indirect
37+
github.com/pmezard/go-difflib v1.0.0 // indirect
38+
github.com/stretchr/testify v1.8.2 // indirect
39+
github.com/tidwall/gjson v1.9.3 // indirect
40+
github.com/tidwall/match v1.1.1 // indirect
41+
github.com/tidwall/pretty v1.2.0 // indirect
42+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
43+
golang.org/x/arch v0.2.0 // indirect
44+
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
45+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
46+
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect
47+
golang.org/x/text v0.6.0 // indirect
48+
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect
49+
gopkg.in/yaml.v3 v3.0.1 // indirect
50+
)
51+
52+
replace (
53+
github.com/apache/thrift => github.com/apache/thrift v0.13.0
54+
github.com/cloudwego/kitex => github.com/felix021/kitex v0.8.1-0.20240111074411-c4fd7c65edf8
55+
)

grpc/tls/cert/gen.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /bin/bash
2+
# Copyright 2024 CloudWeGo Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -e
17+
18+
rm -f *.pem
19+
rm -f *.srl
20+
21+
# 1. Generate CA's private key and self-signed certificate
22+
openssl req -x509 -newkey rsa:4096 -days 365 -nodes -keyout ca-key.pem -out ca-cert.pem -subj "/C=FR/ST=Occitanie/L=Toulouse/O=Tech School/OU=Education/CN=*.techschool.guru/emailAddress=techschool.guru@gmail.com"
23+
24+
echo "CA's self-signed certificate"
25+
openssl x509 -in ca-cert.pem -noout -text
26+
27+
# 2. Generate web server's private key and certificate signing request (CSR)
28+
openssl req -newkey rsa:4096 -nodes -keyout server-key.pem -out server-req.pem -subj "/C=FR/ST=Ile de France/L=Paris/O=PC Book/OU=Computer/CN=*.pcbook.com/emailAddress=pcbook@gmail.com"
29+
30+
# 3. Use CA's private key to sign web server's CSR and get back the signed certificate
31+
openssl x509 -req -in server-req.pem -days 60 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem
32+
33+
echo "Server's signed certificate"
34+
openssl x509 -in server-cert.pem -noout -text
35+
36+
# 4. Generate client's private key and certificate signing request (CSR)
37+
openssl req -newkey rsa:4096 -nodes -keyout client-key.pem -out client-req.pem -subj "/C=FR/ST=Alsace/L=Strasbourg/O=PC Client/OU=Computer/CN=*.pcclient.com/emailAddress=pcclient@gmail.com"
38+
39+
# 5. Use CA's private key to sign client's CSR and get back the signed certificate
40+
openssl x509 -req -in client-req.pem -days 60 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem
41+
42+
echo "Client's signed certificate"
43+
openssl x509 -in client-cert.pem -noout -text

grpc/tls/grpc_server.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2024 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tls
16+
17+
import (
18+
"context"
19+
"crypto/tls"
20+
"crypto/x509"
21+
"fmt"
22+
"io"
23+
"io/ioutil"
24+
"net"
25+
"strings"
26+
27+
"google.golang.org/grpc"
28+
"google.golang.org/grpc/credentials"
29+
30+
grpc_demo "github.com/cloudwego/kitex-tests/grpc_gen/protobuf/grpc_demo_2"
31+
)
32+
33+
func RunGRPCTLSServer(hostport string) (*grpc.Server, error) {
34+
tlsCredentials, err := serverLoadTLSCredentials()
35+
if err != nil {
36+
return nil, err
37+
}
38+
cred := grpc.Creds(tlsCredentials)
39+
40+
svr := grpc.NewServer(cred)
41+
ms := &GrpcServiceA{}
42+
grpc_demo.RegisterServiceAServer(svr, ms)
43+
listener, err := net.Listen("tcp", hostport)
44+
if err != nil {
45+
return nil, err
46+
}
47+
go svr.Serve(listener)
48+
return svr, nil
49+
}
50+
51+
type GrpcServiceA struct {
52+
grpc_demo.UnimplementedServiceAServer
53+
}
54+
55+
func (s *GrpcServiceA) CallUnary(ctx context.Context, req *grpc_demo.Request) (*grpc_demo.Reply, error) {
56+
res := &grpc_demo.Reply{Message: req.Name + " Hello!"}
57+
return res, nil
58+
}
59+
60+
func (s *GrpcServiceA) CallClientStream(stream grpc_demo.ServiceA_CallClientStreamServer) error {
61+
var msgs []string
62+
for {
63+
req, err := stream.Recv()
64+
if err != nil {
65+
if err == io.EOF {
66+
break
67+
}
68+
return err
69+
}
70+
msgs = append(msgs, req.Name)
71+
}
72+
return stream.SendAndClose(&grpc_demo.Reply{Message: "all message: " + strings.Join(msgs, ", ")})
73+
}
74+
func (s *GrpcServiceA) CallServerStream(req *grpc_demo.Request, stream grpc_demo.ServiceA_CallServerStreamServer) error {
75+
resp := &grpc_demo.Reply{}
76+
for i := 0; i < 3; i++ {
77+
resp.Message = fmt.Sprintf("%v-%d", req.Name, i)
78+
err := stream.Send(resp)
79+
if err != nil {
80+
return err
81+
}
82+
}
83+
return nil
84+
}
85+
func (s *GrpcServiceA) CallBidiStream(stream grpc_demo.ServiceA_CallBidiStreamServer) error {
86+
for {
87+
recv, err := stream.Recv()
88+
if err != nil {
89+
if err == io.EOF {
90+
break
91+
}
92+
return err
93+
}
94+
resp := &grpc_demo.Reply{}
95+
resp.Message = recv.Name
96+
err = stream.Send(resp)
97+
if err != nil {
98+
return err
99+
}
100+
}
101+
return nil
102+
}
103+
104+
func serverLoadTLSCredentials() (credentials.TransportCredentials, error) {
105+
// Load certificate of the CA who signed client's certificate
106+
pemClientCA, err := ioutil.ReadFile("cert/ca-cert.pem")
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
certPool := x509.NewCertPool()
112+
if !certPool.AppendCertsFromPEM(pemClientCA) {
113+
return nil, fmt.Errorf("failed to add client CA's certificate")
114+
}
115+
116+
// Load server's certificate and private key
117+
serverCert, err := tls.LoadX509KeyPair("cert/server-cert.pem", "cert/server-key.pem")
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
// Create the credentials and return it
123+
config := &tls.Config{
124+
Certificates: []tls.Certificate{serverCert},
125+
ClientAuth: tls.RequireAndVerifyClientCert, // mTLS
126+
ClientCAs: certPool,
127+
}
128+
129+
return credentials.NewTLS(config), nil
130+
}

0 commit comments

Comments
 (0)