|
1 | 1 | ## gtls
|
2 | 2 |
|
3 |
| -gtls provides grpc secure connectivity, supporting both server-only authentication and client-server authentication. |
| 3 | +`gtls` provides grpc secure connectivity by tls, supporting both one-way secure connection and mutual tls connection. |
4 | 4 |
|
5 |
| -#### Example of use |
| 5 | +### Example of use |
6 | 6 |
|
7 |
| -#### grpc server |
| 7 | +#### One-way secure connection |
| 8 | + |
| 9 | +**grpc server example** |
8 | 10 |
|
9 | 11 | ```go
|
10 | 12 | import "github.com/zhufuyi/sponge/pkg/grpc/gtls"
|
11 | 13 |
|
12 | 14 | func main() {
|
13 |
| - // one-way authentication (server-side authentication) |
14 |
| - //credentials, err := gtls.GetServerTLSCredentials(certfile.Path("/one-way/server.crt"), certfile.Path("/one-way/server.key")) |
15 |
| - |
16 |
| - // two-way authentication |
17 |
| - credentials, err := gtls.GetServerTLSCredentialsByCA( |
18 |
| - certfile.Path("two-way/ca.pem"), |
19 |
| - certfile.Path("two-way/server/server.pem"), |
20 |
| - certfile.Path("two-way/server/server.key"), |
21 |
| - ) |
22 |
| - if err != nil { |
23 |
| - panic(err) |
24 |
| - } |
| 15 | + // one-way connection |
| 16 | + credentials, err := gtls.GetServerTLSCredentials( |
| 17 | + certfile.Path("/one-way/server.crt"), |
| 18 | + certfile.Path("/one-way/server.key"), |
| 19 | + ) |
| 20 | + // check err |
| 21 | + |
| 22 | + server := grpc.NewServer(grpc.Creds(credentials)) |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +<br> |
25 | 27 |
|
26 |
| - // interceptor |
27 |
| - opts := []grpc.ServerOption{ |
28 |
| - grpc.Creds(credentials), |
29 |
| - } |
| 28 | +**grpc client example** |
30 | 29 |
|
31 |
| - server := grpc.NewServer(opts...) |
| 30 | +```go |
| 31 | +import "github.com/zhufuyi/sponge/pkg/grpc/gtls" |
| 32 | + |
| 33 | +func main() { |
| 34 | + // one-way connection |
| 35 | + credentials, err := gtls.GetClientTLSCredentials( |
| 36 | + "localhost", |
| 37 | + certfile.Path("/one-way/server.crt"), |
| 38 | + ) |
| 39 | + // check err |
32 | 40 |
|
33 |
| - // ...... |
| 41 | + conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithTransportCredentials(credentials)) |
| 42 | + // check err |
34 | 43 | }
|
35 | 44 | ```
|
36 | 45 |
|
37 | 46 | <br>
|
38 | 47 |
|
39 |
| -#### grpc client |
| 48 | +#### Mutual tls connection |
| 49 | + |
| 50 | +**grpc server example** |
40 | 51 |
|
41 | 52 | ```go
|
42 | 53 | import "github.com/zhufuyi/sponge/pkg/grpc/gtls"
|
43 | 54 |
|
44 | 55 | func main() {
|
45 |
| - // one-way authentication |
46 |
| - //credentials, err := gtls.GetClientTLSCredentials("localhost", certfile.Path("/one-way/server.crt")) |
47 |
| - |
48 |
| - // two-way authentication |
49 |
| - credentials, err := gtls.GetClientTLSCredentialsByCA( |
50 |
| - "localhost", |
51 |
| - certfile.Path("two-way/ca.pem"), |
52 |
| - certfile.Path("two-way/client/client.pem"), |
53 |
| - certfile.Path("two-way/client/client.key"), |
54 |
| - ) |
55 |
| - if err != nil { |
56 |
| - panic(err) |
57 |
| - } |
| 56 | + // two-way secure connection |
| 57 | + credentials, err := gtls.GetServerTLSCredentialsByCA( |
| 58 | + certfile.Path("two-way/ca.pem"), |
| 59 | + certfile.Path("two-way/server/server.pem"), |
| 60 | + certfile.Path("two-way/server/server.key"), |
| 61 | + ) |
| 62 | + // check err |
| 63 | + |
| 64 | + server := grpc.NewServer(grpc.Creds(credentials)) |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +<br> |
58 | 69 |
|
59 |
| - conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithTransportCredentials(credentials)) |
60 |
| - if err != nil { |
61 |
| - panic(err) |
62 |
| - } |
| 70 | +**grpc client example** |
63 | 71 |
|
64 |
| - // ...... |
| 72 | +```go |
| 73 | +import "github.com/zhufuyi/sponge/pkg/grpc/gtls" |
| 74 | + |
| 75 | +func main() { |
| 76 | + // two-way secure connection |
| 77 | + credentials, err := gtls.GetClientTLSCredentialsByCA( |
| 78 | + "localhost", |
| 79 | + certfile.Path("two-way/ca.pem"), |
| 80 | + certfile.Path("two-way/client/client.pem"), |
| 81 | + certfile.Path("two-way/client/client.key"), |
| 82 | + ) |
| 83 | + // check err |
| 84 | + |
| 85 | + conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithTransportCredentials(credentials)) |
| 86 | + // check err |
65 | 87 | }
|
66 |
| -``` |
| 88 | +``` |
0 commit comments