Skip to content

Commit

Permalink
Rename compile env name for gnmi write #10
Browse files Browse the repository at this point in the history
Why I did it
I will add a future env var ENABLE_NATIVE_WRITE which is another kind of gnmi write mode.
So, I need to rename TELEMETRY_WRITABLE and READ_WRITE_MODE to avoid conflict.

How I did it
Rename compile env var and build tag.

How to verify it
Run unit test for sonic-gnmi.
  • Loading branch information
ganglyu authored Sep 16, 2022
2 parents 14f9121 + bec56e5 commit 437fc35
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ SRC_FILES=$(shell find . -name '*.go' | grep -v '_test.go' | grep -v '/tests/')
TEST_FILES=$(wildcard *_test.go)
TELEMETRY_TEST_DIR = build/tests/gnmi_server
TELEMETRY_TEST_BIN = $(TELEMETRY_TEST_DIR)/server.test
ifeq ($(TELEMETRY_WRITABLE),y)
BLD_FLAGS := -tags readwrite
ifeq ($(ENABLE_TRANSLIB_WRITE),y)
BLD_FLAGS := -tags gnmi_translib_write
endif

GO_DEPS := vendor/.done
Expand Down
4 changes: 2 additions & 2 deletions gnmi_server/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build !readwrite
// +build !gnmi_translib_write

package gnmi

const READ_WRITE_MODE = false
const ENABLE_TRANSLIB_WRITE = false
4 changes: 2 additions & 2 deletions gnmi_server/constants_readwrite.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build readwrite
// +build gnmi_translib_write

package gnmi

const READ_WRITE_MODE = true
const ENABLE_TRANSLIB_WRITE = true
15 changes: 9 additions & 6 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
Port int64
LogLevel int
UserAuth AuthTypes
EnableTranslibWrite bool
}

var AuthLock sync.Mutex
Expand Down Expand Up @@ -141,11 +142,11 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
}
gnmipb.RegisterGNMIServer(srv.s, srv)
spb_jwt_gnoi.RegisterSonicJwtServiceServer(srv.s, srv)
if READ_WRITE_MODE {
if srv.config.EnableTranslibWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
}
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !READ_WRITE_MODE)
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !srv.config.EnableTranslibWrite)
return srv, nil
}

Expand Down Expand Up @@ -338,9 +339,6 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe
}

func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetResponse, error) {
if !READ_WRITE_MODE {
return nil, grpc.Errorf(codes.Unimplemented, "Telemetry is in read-only mode")
}
ctx, err := authenticate(s.config.UserAuth, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -390,7 +388,12 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe
/* Add to Set response results. */
results = append(results, &res)
}
err = dc.Set(req.GetDelete(), req.GetReplace(), req.GetUpdate())
if s.config.EnableTranslibWrite {
err = dc.Set(req.GetDelete(), req.GetReplace(), req.GetUpdate())
} else {
return nil, grpc.Errorf(codes.Unimplemented, "Telemetry is in read-only mode")
}


return &gnmipb.SetResponse{
Prefix: req.GetPrefix(),
Expand Down
6 changes: 3 additions & 3 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func createServer(t *testing.T, port int64) *Server {
}

opts := []grpc.ServerOption{grpc.Creds(credentials.NewTLS(tlsCfg))}
cfg := &Config{Port: port}
cfg := &Config{Port: port, EnableTranslibWrite: true}
s, err := NewServer(cfg, opts)
if err != nil {
t.Errorf("Failed to create gNMI server: %v", err)
Expand Down Expand Up @@ -633,7 +633,7 @@ func mergeStrMaps(sourceOrigin interface{}, updateOrigin interface{}) interface{
}

func TestGnmiSet(t *testing.T) {
if !READ_WRITE_MODE {
if !ENABLE_TRANSLIB_WRITE {
t.Skip("skipping test in read-only mode.")
}
s := createServer(t, 8081)
Expand Down Expand Up @@ -2351,7 +2351,7 @@ func TestCapabilities(t *testing.T) {
}

func TestGNOI(t *testing.T) {
if !READ_WRITE_MODE {
if !ENABLE_TRANSLIB_WRITE {
t.Skip("skipping test in read-only mode.")
}
s := createServer(t, 8086)
Expand Down
5 changes: 4 additions & 1 deletion telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ var (
allowNoClientCert = flag.Bool("allow_no_client_auth", false, "When set, telemetry server will request but not require a client certificate.")
jwtRefInt = flag.Uint64("jwt_refresh_int", 900, "Seconds before JWT expiry the token can be refreshed.")
jwtValInt = flag.Uint64("jwt_valid_int", 3600, "Seconds that JWT token is valid for.")
gnmi_translib_write = flag.Bool("gnmi_translib_write", gnmi.ENABLE_TRANSLIB_WRITE, "Enable gNMI translib write for management framework")
)

func main() {
flag.Var(userAuth, "client_auth", "Client auth mode(s) - none,cert,password")
flag.Parse()

var defUserAuth gnmi.AuthTypes
if gnmi.READ_WRITE_MODE {
if *gnmi_translib_write {
//In read/write mode we want to enable auth by default.
defUserAuth = gnmi.AuthTypes{"password": true, "cert": false, "jwt": true}
}else {
Expand All @@ -59,6 +60,7 @@ func main() {

cfg := &gnmi.Config{}
cfg.Port = int64(*port)
cfg.EnableTranslibWrite = bool(*gnmi_translib_write)
cfg.LogLevel = 3
var opts []grpc.ServerOption

Expand Down Expand Up @@ -134,6 +136,7 @@ func main() {
cfg := &gnmi.Config{}
cfg.Port = int64(*port)
cfg.UserAuth = userAuth
cfg.EnableTranslibWrite = bool(*gnmi_translib_write)

gnmi.GenerateJwtSecretKey()
}
Expand Down

0 comments on commit 437fc35

Please sign in to comment.