Skip to content

Commit 6421490

Browse files
committed
fix bug
1 parent 262d53f commit 6421490

File tree

4 files changed

+84
-44
lines changed

4 files changed

+84
-44
lines changed

pkg/grpc/grpccli/dail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Dial(ctx context.Context, endpoint string, opts ...Option) (*grpc.ClientCon
2727
clientOptions = append(clientOptions, grpc.WithResolvers(
2828
discovery.NewBuilder(
2929
o.discovery,
30-
discovery.WithInsecure(!o.isSecure()),
30+
discovery.WithInsecure(o.discoveryInsecure),
3131
)))
3232
}
3333

pkg/grpc/grpccli/option.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type options struct {
4444
enableCircuitBreaker bool // whether to turn on circuit breaker
4545
discovery registry.Discovery // if not nil means use service discovery
4646

47+
discoveryInsecure bool
48+
4749
// custom setting
4850
dialOptions []grpc.DialOption // custom options
4951
unaryInterceptors []grpc.UnaryClientInterceptor // custom unary interceptor
@@ -58,7 +60,8 @@ func defaultOptions() *options {
5860
keyFile: "",
5961
caFile: "",
6062

61-
enableLog: false,
63+
enableLog: false,
64+
discoveryInsecure: true,
6265

6366
dialOptions: nil,
6467
unaryInterceptors: nil,
@@ -134,6 +137,13 @@ func WithEnableCircuitBreaker() Option {
134137
}
135138
}
136139

140+
// WithDiscoveryInsecure setting discovery insecure
141+
func WithDiscoveryInsecure(b bool) Option {
142+
return func(o *options) {
143+
o.discoveryInsecure = b
144+
}
145+
}
146+
137147
func (o *options) isSecure() bool {
138148
if o.secureType == secureOneWay || o.secureType == secureTwoWay {
139149
return true

pkg/grpc/grpccli/option_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,15 @@ func TestWithTimeout(t *testing.T) {
9393
opt := WithTimeout(testData)
9494
o := new(options)
9595
o.apply(opt)
96-
assert.Equal(t, testData, o.timeout)
96+
assert.Equal(t, testData, o.requestTimeout)
97+
}
98+
99+
func TestWithDiscoveryInsecure(t *testing.T) {
100+
var testData bool
101+
opt := WithDiscoveryInsecure(testData)
102+
o := new(options)
103+
o.apply(opt)
104+
assert.Equal(t, testData, o.discoveryInsecure)
97105
}
98106

99107
func TestWithUnaryInterceptors(t *testing.T) {

pkg/grpc/gtls/README.md

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,88 @@
11
## gtls
22

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.
44

5-
#### Example of use
5+
### Example of use
66

7-
#### grpc server
7+
#### One-way secure connection
8+
9+
**grpc server example**
810

911
```go
1012
import "github.com/zhufuyi/sponge/pkg/grpc/gtls"
1113

1214
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>
2527

26-
// interceptor
27-
opts := []grpc.ServerOption{
28-
grpc.Creds(credentials),
29-
}
28+
**grpc client example**
3029

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
3240

33-
// ......
41+
conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithTransportCredentials(credentials))
42+
// check err
3443
}
3544
```
3645

3746
<br>
3847

39-
#### grpc client
48+
#### Mutual tls connection
49+
50+
**grpc server example**
4051

4152
```go
4253
import "github.com/zhufuyi/sponge/pkg/grpc/gtls"
4354

4455
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>
5869

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**
6371

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
6587
}
66-
```
88+
```

0 commit comments

Comments
 (0)