forked from networkservicemesh/networkservicemesh
/
socket.go
230 lines (190 loc) · 6.79 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package tools
import (
"context"
"net"
"sync"
"time"
"github.com/networkservicemesh/networkservicemesh/pkg/security"
"github.com/networkservicemesh/networkservicemesh/pkg/tools/jaeger"
"github.com/networkservicemesh/networkservicemesh/pkg/tools/spanhelper"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
// InsecureEnv environment variable, if "true" NSM will work in insecure mode
InsecureEnv = "INSECURE"
insecureDefault = false
dialTimeoutDefault = 15 * time.Second
)
// DialConfig represents configuration of grpc connection, one per instance
type DialConfig struct {
OpenTracing bool
SecurityProvider security.Provider
}
var cfg DialConfig
var once sync.Once
// GetConfig returns instance of DialConfig
func GetConfig() DialConfig {
once.Do(func() {
var err error
cfg, err = readDialConfig()
if err != nil {
logrus.Fatal(err)
}
})
return cfg
}
// InitConfig allows init global DialConfig, should be called before any GetConfig(), otherwise do nothing
func InitConfig(c DialConfig) {
once.Do(func() {
cfg = c
})
}
// NewServer checks DialConfig and calls grpc.NewServer with certain grpc.ServerOption
func NewServer(ctx context.Context, opts ...grpc.ServerOption) *grpc.Server {
span := spanhelper.FromContext(ctx, "NewServer")
defer span.Finish()
if GetConfig().SecurityProvider != nil {
securitySpan := spanhelper.FromContext(span.Context(), "GetCertificate")
tlscfg, err := GetConfig().SecurityProvider.GetTLSConfig(ctx)
if err != nil {
return nil
}
opts = append(opts, grpc.Creds(credentials.NewTLS(tlscfg)))
securitySpan.Finish()
}
if GetConfig().OpenTracing {
span.Logger().Infof("GRPC.NewServer with open tracing enabled")
opts = append(opts, openTracingOpts()...)
}
return grpc.NewServer(opts...)
}
func NewServerInsecure(opts ...grpc.ServerOption) *grpc.Server {
if GetConfig().OpenTracing {
logrus.Infof("GRPC.NewServer with open tracing enabled")
opts = append(opts, openTracingOpts()...)
}
return grpc.NewServer(opts...)
}
func openTracingOpts() []grpc.ServerOption {
return []grpc.ServerOption{
grpc.UnaryInterceptor(
CloneArgsServerInterceptor(
otgrpc.OpenTracingServerInterceptor(opentracing.GlobalTracer()))),
grpc.StreamInterceptor(
otgrpc.OpenTracingStreamServerInterceptor(opentracing.GlobalTracer())),
}
}
// DialContext allows to call DialContext using net.Addr
func DialContext(ctx context.Context, addr net.Addr, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).Network(addr.Network()).DialContextFunc()
return dialCtx(ctx, addr.String(), opts...)
}
// DialContextUnix establish connection with passed unix socket
func DialContextUnix(ctx context.Context, path string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).Unix().DialContextFunc()
return dialCtx(ctx, path, opts...)
}
// DialUnix establish connection with passed unix socket and set default timeout
func DialUnix(path string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).Unix().Timeout(dialTimeoutDefault).DialContextFunc()
return dialCtx(context.Background(), path, opts...)
}
// DialUnixInsecure establish connection with passed unix socket in insecure mode and set default timeout
func DialUnixInsecure(path string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).Unix().Insecure().Timeout(dialTimeoutDefault).DialContextFunc()
return dialCtx(context.Background(), path, opts...)
}
// DialContextTCP establish TCP connection with address
func DialContextTCP(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).TCP().DialContextFunc()
return dialCtx(ctx, address, opts...)
}
// DialTCP establish TCP connection with address and set default timeout
func DialTCP(address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).TCP().Timeout(dialTimeoutDefault).DialContextFunc()
return dialCtx(context.Background(), address, opts...)
}
// DialTCPInsecure establish TCP connection with address in insecure mode and set default timeout
func DialTCPInsecure(address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx := new(dialBuilder).TCP().Insecure().Timeout(dialTimeoutDefault).DialContextFunc()
return dialCtx(context.Background(), address, opts...)
}
type dialContextFunc func(ctx context.Context, target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error)
type dialBuilder struct {
opts []grpc.DialOption
t time.Duration
insecure bool
}
func (b *dialBuilder) TCP() *dialBuilder {
return b.Network("tcp")
}
func (b *dialBuilder) Unix() *dialBuilder {
return b.Network("unix")
}
func (b *dialBuilder) Insecure() *dialBuilder {
b.insecure = true
return b
}
func (b *dialBuilder) Network(network string) *dialBuilder {
b.opts = append(b.opts, grpc.WithContextDialer(func(ctx context.Context, target string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, target)
}))
return b
}
func (b *dialBuilder) Timeout(t time.Duration) *dialBuilder {
b.t = t
return b
}
func (b *dialBuilder) DialContextFunc() dialContextFunc {
return func(ctx context.Context, target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
if GetConfig().OpenTracing {
b.opts = append(b.opts, OpenTracingDialOptions()...)
}
if !b.insecure && GetConfig().SecurityProvider != nil {
tlscfg, err := GetConfig().SecurityProvider.GetTLSConfig(ctx)
if err != nil {
return nil, err
}
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(tlscfg)))
} else {
opts = append(opts, grpc.WithInsecure())
}
b.opts = append(b.opts, grpc.WithBlock())
if b.t != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, b.t)
defer cancel()
}
return grpc.DialContext(ctx, target, append(opts, b.opts...)...)
}
}
// OpenTracingDialOptions returns array of grpc.DialOption that should be passed to grpc.Dial to enable opentracing
func OpenTracingDialOptions() []grpc.DialOption {
return []grpc.DialOption{
grpc.WithUnaryInterceptor(
CloneArgsClientInterceptor(
otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer()))),
grpc.WithStreamInterceptor(
otgrpc.OpenTracingStreamClientInterceptor(opentracing.GlobalTracer())),
}
}
func readDialConfig() (DialConfig, error) {
rv := DialConfig{
OpenTracing: jaeger.IsOpentracingEnabled(),
}
insecure, err := IsInsecure()
if err != nil {
return DialConfig{}, err
}
if !insecure {
rv.SecurityProvider, err = security.NewSpireProvider(security.SpireAgentUnixAddr)
if err != nil {
return DialConfig{}, err
}
}
return rv, nil
}