-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserver.go
661 lines (582 loc) · 17.3 KB
/
server.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package gnmi
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"sync"
"github.com/Azure/sonic-mgmt-common/translib"
"github.com/sonic-net/sonic-gnmi/common_utils"
spb "github.com/sonic-net/sonic-gnmi/proto"
spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
spb_jwt_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"
log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
gnmi_extpb "github.com/openconfig/gnmi/proto/gnmi_ext"
gnoi_system_pb "github.com/openconfig/gnoi/system"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
var (
supportedEncodings = []gnmipb.Encoding{gnmipb.Encoding_JSON, gnmipb.Encoding_JSON_IETF, gnmipb.Encoding_PROTO}
)
// Server manages a single gNMI Server implementation. Each client that connects
// via Subscribe or Get will receive a stream of updates based on the requested
// path. Set request is processed by server too.
type Server struct {
s *grpc.Server
lis net.Listener
config *Config
cMu sync.Mutex
clients map[string]*Client
// SaveStartupConfig points to a function that is called to save changes of
// configuration to a file. By default it points to an empty function -
// the configuration is not saved to a file.
SaveStartupConfig func() error
// ReqFromMaster point to a function that is called to verify if the request
// comes from a master controller.
ReqFromMaster func(req *gnmipb.SetRequest, masterEID *uint128) error
masterEID uint128
// UnimplementedSystemServer is embedded to satisfy SystemServer interface requirements
gnoi_system_pb.UnimplementedSystemServer
}
type AuthTypes map[string]bool
// Config is a collection of values for Server
type Config struct {
// Port for the Server to listen on. If 0 or unset the Server will pick a port
// for this Server.
Port int64
LogLevel int
Threshold int
UserAuth AuthTypes
EnableTranslibWrite bool
EnableNativeWrite bool
ZmqPort string
IdleConnDuration int
ConfigTableName string
}
var AuthLock sync.Mutex
var maMu sync.Mutex
func (i AuthTypes) String() string {
if i["none"] {
return ""
}
b := new(bytes.Buffer)
for key, value := range i {
if value {
fmt.Fprintf(b, "%s ", key)
}
}
return b.String()
}
func (i AuthTypes) Any() bool {
if i["none"] {
return false
}
for _, value := range i {
if value {
return true
}
}
return false
}
func (i AuthTypes) Enabled(mode string) bool {
if i["none"] {
return false
}
if value, exist := i[mode]; exist && value {
return true
}
return false
}
func (i AuthTypes) Set(mode string) error {
modes := strings.Split(mode, ",")
for _, m := range modes {
m = strings.Trim(m, " ")
if m == "none" || m == "" {
i["none"] = true
return nil
}
if _, exist := i[m]; !exist {
return fmt.Errorf("Expecting one or more of 'cert', 'password' or 'jwt'")
}
i[m] = true
}
return nil
}
func (i AuthTypes) Unset(mode string) error {
modes := strings.Split(mode, ",")
for _, m := range modes {
m = strings.Trim(m, " ")
if _, exist := i[m]; !exist {
return fmt.Errorf("Expecting one or more of 'cert', 'password' or 'jwt'")
}
i[m] = false
}
return nil
}
// New returns an initialized Server.
func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
if config == nil {
return nil, errors.New("config not provided")
}
common_utils.InitCounters()
s := grpc.NewServer(opts...)
reflection.Register(s)
srv := &Server{
s: s,
config: config,
clients: map[string]*Client{},
SaveStartupConfig: saveOnSetDisabled,
// ReqFromMaster point to a function that is called to verify if
// the request comes from a master controller.
ReqFromMaster: ReqFromMasterDisabledMA,
masterEID: uint128{High: 0, Low: 0},
}
var err error
if srv.config.Port < 0 {
srv.config.Port = 0
}
srv.lis, err = net.Listen("tcp", fmt.Sprintf(":%d", srv.config.Port))
if err != nil {
return nil, fmt.Errorf("failed to open listener port %d: %v", srv.config.Port, err)
}
gnmipb.RegisterGNMIServer(srv.s, srv)
spb_jwt_gnoi.RegisterSonicJwtServiceServer(srv.s, srv)
if srv.config.EnableTranslibWrite || srv.config.EnableNativeWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
}
if srv.config.EnableTranslibWrite {
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
}
spb_gnoi.RegisterDebugServer(srv.s, srv)
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !srv.config.EnableTranslibWrite)
return srv, nil
}
// Serve will start the Server serving and block until closed.
func (srv *Server) Serve() error {
s := srv.s
if s == nil {
return fmt.Errorf("Serve() failed: not initialized")
}
return srv.s.Serve(srv.lis)
}
func (srv *Server) Stop() {
s := srv.s
if s == nil {
log.Errorf("Stop() failed: not initialized")
return
}
s.Stop()
}
// Address returns the port the Server is listening to.
func (srv *Server) Address() string {
addr := srv.lis.Addr().String()
return strings.Replace(addr, "[::]", "localhost", 1)
}
// Port returns the port the Server is listening to.
func (srv *Server) Port() int64 {
return srv.config.Port
}
func authenticate(config *Config, ctx context.Context) (context.Context, error) {
var err error
success := false
rc, ctx := common_utils.GetContext(ctx)
if !config.UserAuth.Any() {
//No Auth enabled
rc.Auth.AuthEnabled = false
return ctx, nil
}
rc.Auth.AuthEnabled = true
if config.UserAuth.Enabled("password") {
ctx, err = BasicAuthenAndAuthor(ctx)
if err == nil {
success = true
}
}
if !success && config.UserAuth.Enabled("jwt") {
_, ctx, err = JwtAuthenAndAuthor(ctx)
if err == nil {
success = true
}
}
if !success && config.UserAuth.Enabled("cert") {
ctx, err = ClientCertAuthenAndAuthor(ctx, config.ConfigTableName)
if err == nil {
success = true
}
}
//Allow for future authentication mechanisms here...
if !success {
return ctx, status.Error(codes.Unauthenticated, "Unauthenticated")
}
log.V(5).Infof("authenticate user %v, roles %v", rc.Auth.User, rc.Auth.Roles)
return ctx, nil
}
// Subscribe implements the gNMI Subscribe RPC.
func (s *Server) Subscribe(stream gnmipb.GNMI_SubscribeServer) error {
ctx := stream.Context()
ctx, err := authenticate(s.config, ctx)
if err != nil {
return err
}
pr, ok := peer.FromContext(ctx)
if !ok {
return grpc.Errorf(codes.InvalidArgument, "failed to get peer from ctx")
//return fmt.Errorf("failed to get peer from ctx")
}
if pr.Addr == net.Addr(nil) {
return grpc.Errorf(codes.InvalidArgument, "failed to get peer address")
}
/* TODO: authorize the user
msg, ok := credentials.AuthorizeUser(ctx)
if !ok {
log.Infof("denied a Set request: %v", msg)
return nil, status.Error(codes.PermissionDenied, msg)
}
*/
c := NewClient(pr.Addr)
c.setLogLevel(s.config.LogLevel)
c.setConnectionManager(s.config.Threshold)
s.cMu.Lock()
if oc, ok := s.clients[c.String()]; ok {
log.V(2).Infof("Delete duplicate client %s", oc)
oc.Close()
delete(s.clients, c.String())
}
s.clients[c.String()] = c
s.cMu.Unlock()
err = c.Run(stream)
s.cMu.Lock()
delete(s.clients, c.String())
s.cMu.Unlock()
log.Flush()
return err
}
// checkEncodingAndModel checks whether encoding and models are supported by the server. Return error if anything is unsupported.
func (s *Server) checkEncodingAndModel(encoding gnmipb.Encoding, models []*gnmipb.ModelData) error {
hasSupportedEncoding := false
for _, supportedEncoding := range supportedEncodings {
if encoding == supportedEncoding {
hasSupportedEncoding = true
break
}
}
if !hasSupportedEncoding {
return fmt.Errorf("unsupported encoding: %s", gnmipb.Encoding_name[int32(encoding)])
}
return nil
}
func ParseOrigin(paths []*gnmipb.Path) (string, error) {
origin := ""
if len(paths) == 0 {
return origin, nil
}
for i, path := range paths {
if i == 0 {
origin = path.Origin
} else {
if origin != path.Origin {
return "", status.Error(codes.Unimplemented, "Origin conflict in path")
}
}
}
return origin, nil
}
func IsNativeOrigin(origin string) bool {
return origin == "sonic-db"
}
// Get implements the Get RPC in gNMI spec.
func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetResponse, error) {
common_utils.IncCounter(common_utils.GNMI_GET)
ctx, err := authenticate(s.config, ctx)
if err != nil {
common_utils.IncCounter(common_utils.GNMI_GET_FAIL)
return nil, err
}
if req.GetType() != gnmipb.GetRequest_ALL {
common_utils.IncCounter(common_utils.GNMI_GET_FAIL)
return nil, status.Errorf(codes.Unimplemented, "unsupported request type: %s", gnmipb.GetRequest_DataType_name[int32(req.GetType())])
}
if err = s.checkEncodingAndModel(req.GetEncoding(), req.GetUseModels()); err != nil {
common_utils.IncCounter(common_utils.GNMI_GET_FAIL)
return nil, status.Error(codes.Unimplemented, err.Error())
}
target := ""
origin := ""
prefix := req.GetPrefix()
if prefix != nil {
target = prefix.GetTarget()
origin = prefix.Origin
}
paths := req.GetPath()
extensions := req.GetExtension()
encoding := req.GetEncoding()
log.V(2).Infof("GetRequest paths: %v", paths)
var dc sdc.Client
if target == "OTHERS" {
dc, err = sdc.NewNonDbClient(paths, prefix)
} else if _, ok, _, _ := sdc.IsTargetDb(target); ok {
dc, err = sdc.NewDbClient(paths, prefix)
} else {
if origin == "" {
origin, err = ParseOrigin(paths)
if err != nil {
return nil, err
}
}
if check := IsNativeOrigin(origin); check {
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, encoding, s.config.ZmqPort)
} else {
dc, err = sdc.NewTranslClient(prefix, paths, ctx, extensions)
}
}
if err != nil {
common_utils.IncCounter(common_utils.GNMI_GET_FAIL)
return nil, status.Error(codes.NotFound, err.Error())
}
defer dc.Close()
notifications := make([]*gnmipb.Notification, len(paths))
spbValues, err := dc.Get(nil)
if err != nil {
common_utils.IncCounter(common_utils.GNMI_GET_FAIL)
return nil, status.Error(codes.NotFound, err.Error())
}
for index, spbValue := range spbValues {
update := &gnmipb.Update{
Path: spbValue.GetPath(),
Val: spbValue.GetVal(),
}
notifications[index] = &gnmipb.Notification{
Timestamp: spbValue.GetTimestamp(),
Prefix: prefix,
Update: []*gnmipb.Update{update},
}
}
return &gnmipb.GetResponse{Notification: notifications}, nil
}
// saveOnSetEnabled saves configuration to a file
func SaveOnSetEnabled() error {
sc, err := ssc.NewDbusClient()
if err != nil {
log.V(0).Infof("Saving startup config failed to create dbus client: %v", err)
return err
}
if err := sc.ConfigSave("/etc/sonic/config_db.json"); err != nil {
log.V(0).Infof("Saving startup config failed: %v", err)
return err
} else {
log.V(1).Infof("Success! Startup config has been saved!")
}
return nil
}
// SaveOnSetDisabeld does nothing.
func saveOnSetDisabled() error { return nil }
func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetResponse, error) {
e := s.ReqFromMaster(req, &s.masterEID)
if e != nil {
return nil, e
}
common_utils.IncCounter(common_utils.GNMI_SET)
if s.config.EnableTranslibWrite == false && s.config.EnableNativeWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "GNMI is in read-only mode")
}
ctx, err := authenticate(s.config, ctx)
if err != nil {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, err
}
var results []*gnmipb.UpdateResult
/* Fetch the prefix. */
prefix := req.GetPrefix()
origin := ""
if prefix != nil {
origin = prefix.Origin
}
extensions := req.GetExtension()
encoding := gnmipb.Encoding_JSON_IETF
var dc sdc.Client
paths := req.GetDelete()
for _, path := range req.GetReplace() {
paths = append(paths, path.GetPath())
}
for _, path := range req.GetUpdate() {
paths = append(paths, path.GetPath())
}
if origin == "" {
origin, err = ParseOrigin(paths)
if err != nil {
return nil, err
}
}
if check := IsNativeOrigin(origin); check {
if s.config.EnableNativeWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "GNMI native write is disabled")
}
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, encoding, s.config.ZmqPort)
} else {
if s.config.EnableTranslibWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "Translib write is disabled")
}
/* Create Transl client. */
dc, err = sdc.NewTranslClient(prefix, nil, ctx, extensions)
}
if err != nil {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, status.Error(codes.NotFound, err.Error())
}
defer dc.Close()
/* DELETE */
for _, path := range req.GetDelete() {
log.V(2).Infof("Delete path: %v", path)
res := gnmipb.UpdateResult{
Path: path,
Op: gnmipb.UpdateResult_DELETE,
}
/* Add to Set response results. */
results = append(results, &res)
}
/* REPLACE */
for _, path := range req.GetReplace() {
log.V(2).Infof("Replace path: %v ", path)
res := gnmipb.UpdateResult{
Path: path.GetPath(),
Op: gnmipb.UpdateResult_REPLACE,
}
/* Add to Set response results. */
results = append(results, &res)
}
/* UPDATE */
for _, path := range req.GetUpdate() {
log.V(2).Infof("Update path: %v ", path)
res := gnmipb.UpdateResult{
Path: path.GetPath(),
Op: gnmipb.UpdateResult_UPDATE,
}
/* Add to Set response results. */
results = append(results, &res)
}
err = dc.Set(req.GetDelete(), req.GetReplace(), req.GetUpdate())
if err != nil {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
}
s.SaveStartupConfig()
return &gnmipb.SetResponse{
Prefix: req.GetPrefix(),
Response: results,
}, err
}
func (s *Server) Capabilities(ctx context.Context, req *gnmipb.CapabilityRequest) (*gnmipb.CapabilityResponse, error) {
ctx, err := authenticate(s.config, ctx)
if err != nil {
return nil, err
}
extensions := req.GetExtension()
/* Fetch the client capabitlities. */
var supportedModels []gnmipb.ModelData
dc, _ := sdc.NewTranslClient(nil, nil, ctx, extensions)
supportedModels = append(supportedModels, dc.Capabilities()...)
dc, _ = sdc.NewMixedDbClient(nil, nil, "", gnmipb.Encoding_JSON_IETF, s.config.ZmqPort)
supportedModels = append(supportedModels, dc.Capabilities()...)
suppModels := make([]*gnmipb.ModelData, len(supportedModels))
for index, model := range supportedModels {
suppModels[index] = &gnmipb.ModelData{
Name: model.Name,
Organization: model.Organization,
Version: model.Version,
}
}
sup_bver := spb.SupportedBundleVersions{
BundleVersion: translib.GetYangBundleVersion().String(),
BaseVersion: translib.GetYangBaseVersion().String(),
}
sup_msg, _ := proto.Marshal(&sup_bver)
ext := gnmi_extpb.Extension{}
ext.Ext = &gnmi_extpb.Extension_RegisteredExt{
RegisteredExt: &gnmi_extpb.RegisteredExtension{
Id: spb.SUPPORTED_VERSIONS_EXT,
Msg: sup_msg}}
exts := []*gnmi_extpb.Extension{&ext}
return &gnmipb.CapabilityResponse{SupportedModels: suppModels,
SupportedEncodings: supportedEncodings,
GNMIVersion: "0.7.0",
Extension: exts}, nil
}
type uint128 struct {
High uint64
Low uint64
}
func (lh *uint128) Compare(rh *uint128) int {
if rh == nil {
// For MA disabled case, EID supposed to be 0.
rh = &uint128{High: 0, Low: 0}
}
if lh.High > rh.High {
return 1
}
if lh.High < rh.High {
return -1
}
if lh.Low > rh.Low {
return 1
}
if lh.Low < rh.Low {
return -1
}
return 0
}
// ReqFromMasterEnabledMA returns true if the request is sent by the master
// controller.
func ReqFromMasterEnabledMA(req *gnmipb.SetRequest, masterEID *uint128) error {
// Read the election_id.
reqEID := uint128{High: 0, Low: 0}
hasMaExt := false
// It can be one of many extensions, so iterate through them to find it.
for _, e := range req.GetExtension() {
ma := e.GetMasterArbitration()
if ma == nil {
continue
}
hasMaExt = true
// The Master Arbitration descriptor has been found.
if ma.ElectionId == nil {
return status.Errorf(codes.InvalidArgument, "MA: ElectionId missing")
}
if ma.Role != nil {
// Role will be implemented later.
return status.Errorf(codes.Unimplemented, "MA: Role is not implemented")
}
reqEID = uint128{High: ma.ElectionId.High, Low: ma.ElectionId.Low}
// Use the election ID that is in the last extension, so, no 'break' here.
}
if !hasMaExt {
log.V(0).Infof("MA: No Master Arbitration in setRequest extension, masterEID %v is not updated", masterEID)
return nil
}
maMu.Lock()
defer maMu.Unlock()
switch masterEID.Compare(&reqEID) {
case 1: // This Election ID is smaller than the known Master Election ID.
return status.Errorf(codes.PermissionDenied, "Election ID is smaller than the current master. Rejected. Master EID: %v. Current EID: %v.", masterEID, reqEID)
case -1: // New Master Election ID received!
log.V(0).Infof("New master has been elected with %v\n", reqEID)
*masterEID = reqEID
}
return nil
}
// ReqFromMasterDisabledMA always returns true. It is used when Master Arbitration
// is disabled.
func ReqFromMasterDisabledMA(req *gnmipb.SetRequest, masterEID *uint128) error {
return nil
}