From 66f5db3000728b36a33aa17b93377bc9657233d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sterba?= <andre.sterba@stud.h-da.de> Date: Wed, 1 Jun 2022 21:41:18 +0200 Subject: [PATCH] Update nbi servers --- controller/api/initialise_test.go | 4 +-- .../northbound/server/auth_interceptor.go | 13 +++++++-- controller/northbound/server/core.go | 9 +++++- controller/northbound/server/csbi.go | 8 +++++ controller/northbound/server/nbi.go | 29 +++++-------------- controller/northbound/server/pnd.go | 7 +++++ controller/northbound/server/sbi.go | 8 +++++ .../northbound/server/test_util_test.go | 15 +++++----- 8 files changed, 57 insertions(+), 36 deletions(-) diff --git a/controller/api/initialise_test.go b/controller/api/initialise_test.go index 8638d200c..21fc21017 100644 --- a/controller/api/initialise_test.go +++ b/controller/api/initialise_test.go @@ -142,9 +142,7 @@ func bootstrapUnitTest() { jwtManager := rbacImpl.NewJWTManager("", (10000 * time.Hour)) - northbound := nbi.NewNBI(pndStore, userService, roleService) - northbound.Auth = nbi.NewAuthServer(jwtManager) - northbound.User = nbi.NewUserServer(jwtManager) + northbound := nbi.NewNBI(pndStore, userService, roleService, *jwtManager) cpb.RegisterCoreServiceServer(s, northbound.Core) ppb.RegisterPndServiceServer(s, northbound.Pnd) diff --git a/controller/northbound/server/auth_interceptor.go b/controller/northbound/server/auth_interceptor.go index d4f73ec7e..11338596b 100644 --- a/controller/northbound/server/auth_interceptor.go +++ b/controller/northbound/server/auth_interceptor.go @@ -1,10 +1,11 @@ package server import ( - rbacInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac" "context" "time" + rbacInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac" + csbipb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/csbi" apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac" "code.fbi.h-da.de/danet/gosdn/controller/rbac" @@ -23,9 +24,15 @@ type AuthInterceptor struct { } // NewAuthInterceptor receives a JWTManager and a rbacMand returns a new AuthInterceptor provding gRPC Interceptor functionality. -func NewAuthInterceptor(jwtManager *rbac.JWTManager) *AuthInterceptor { +func NewAuthInterceptor( + jwtManager *rbac.JWTManager, + userService rbacInterfaces.UserService, + roleService rbacInterfaces.RoleService, +) *AuthInterceptor { return &AuthInterceptor{ - jwtManager: jwtManager, + jwtManager: jwtManager, + userService: userService, + roleService: roleService, } } diff --git a/controller/northbound/server/core.go b/controller/northbound/server/core.go index 4ef33b9b1..9b6e5485f 100644 --- a/controller/northbound/server/core.go +++ b/controller/northbound/server/core.go @@ -16,7 +16,14 @@ import ( type core struct { pb.UnimplementedCoreServiceServer - pndStore networkdomain.PndStore + pndStore networkdomain.PndStore +} + +// NewCoreServer receives a pndStore and returns a new coreServer. +func NewCoreServer(pndStore networkdomain.PndStore) *core { + return &core{ + pndStore: pndStore, + } } func (s core) GetPnd(ctx context.Context, request *pb.GetPndRequest) (*pb.GetPndResponse, error) { diff --git a/controller/northbound/server/csbi.go b/controller/northbound/server/csbi.go index 07811a875..f895803a0 100644 --- a/controller/northbound/server/csbi.go +++ b/controller/northbound/server/csbi.go @@ -24,6 +24,14 @@ type csbi struct { pndStore networkdomain.PndStore } +// NewCsbiServer receives a pndStore and returns a new csbiServer. +func NewCsbiServer(pndStore networkdomain.PndStore) *csbi { + return &csbi{ + pndStore: pndStore, + } +} + + func (s csbi) Hello(ctx context.Context, syn *cpb.Syn) (*cpb.Ack, error) { labels := prometheus.Labels{"service": "csbi", "rpc": "hello"} start := metrics.StartHook(labels, grpcRequestsTotal) diff --git a/controller/northbound/server/nbi.go b/controller/northbound/server/nbi.go index 5e4321d98..91079e4e5 100644 --- a/controller/northbound/server/nbi.go +++ b/controller/northbound/server/nbi.go @@ -27,28 +27,13 @@ type NorthboundInterface struct { // NewNBI receives a PndStore and returns a new gRPC *NorthboundInterface func NewNBI(pnds networkdomain.PndStore, users rbacInterfaces.UserService, roles rbacInterfaces.RoleService, jwt rbac.JWTManager) *NorthboundInterface { return &NorthboundInterface{ - Pnd: &pndServer{ - pndStore: pnds, - }, - Core: &core{ - pndStore: pnds, - }, - Csbi: &csbi{ - pndStore: pnds, - }, - Sbi: &sbiServer{ - pndStore: pnds, - }, - Auth: &Auth{ - jwtManager: &jwt, - userService: users, - }, - User: &User{ - jwtManager: &jwt, - }, - Role: &Role{ - jwtManager: &jwt, - }, + Pnd: NewPndServer(pnds), + Core: NewCoreServer(pnds), + Csbi: NewCsbiServer(pnds), + Sbi: NewSbiServer(pnds), + Auth: NewAuthServer(&jwt, users), + User: NewUserServer(&jwt, users), + Role: NewRoleServer(&jwt, roles), } } diff --git a/controller/northbound/server/pnd.go b/controller/northbound/server/pnd.go index 35364eb5d..fae7680bd 100644 --- a/controller/northbound/server/pnd.go +++ b/controller/northbound/server/pnd.go @@ -28,6 +28,13 @@ type pndServer struct { pndStore networkdomain.PndStore } +// NewPndServer receives a pndStore and returns a new pndServer. +func NewPndServer(pndStore networkdomain.PndStore) *pndServer { + return &pndServer{ + pndStore: pndStore, + } +} + func (p pndServer) GetOnd(ctx context.Context, request *ppb.GetOndRequest) (*ppb.GetOndResponse, error) { labels := prometheus.Labels{"service": "pnd", "rpc": "get"} start := metrics.StartHook(labels, grpcRequestsTotal) diff --git a/controller/northbound/server/sbi.go b/controller/northbound/server/sbi.go index d948cb30f..057d82db1 100644 --- a/controller/northbound/server/sbi.go +++ b/controller/northbound/server/sbi.go @@ -28,6 +28,14 @@ type sbiServer struct { pndStore networkdomain.PndStore } +// NewSbiServer receives a pndStore and returns a new sbiServer. +func NewSbiServer(pndStore networkdomain.PndStore) *sbiServer { + return &sbiServer{ + pndStore: pndStore, + } +} + + func (s sbiServer) GetSchema(request *spb.GetSchemaRequest, stream spb.SbiService_GetSchemaServer) error { labels := prometheus.Labels{"service": "pnd", "rpc": "get schema"} start := metrics.StartHook(labels, grpcRequestsTotal) diff --git a/controller/northbound/server/test_util_test.go b/controller/northbound/server/test_util_test.go index 6eb5c982b..1a9db343a 100644 --- a/controller/northbound/server/test_util_test.go +++ b/controller/northbound/server/test_util_test.go @@ -6,6 +6,8 @@ import ( "log" "testing" + rbacInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac" + "code.fbi.h-da.de/danet/gosdn/controller/rbac" "code.fbi.h-da.de/danet/gosdn/controller/store" "github.com/google/uuid" @@ -23,9 +25,8 @@ const randomRoleName = "bertram" var adminRoleMap = map[string]string{pndID: "adminTestRole"} var userRoleMap = map[string]string{pndID: "userTestRole"} -var jwt *rbac.JWTManager -func clearAndCreateAuthTestSetup() error { +func clearAndCreateAuthTestSetup(userService rbacInterfaces.UserService, roleService rbacInterfaces.RoleService) error { //clear setup if changed storedUsers, err := userService.GetAll() if err != nil { @@ -50,12 +51,12 @@ func clearAndCreateAuthTestSetup() error { } // create dataset - err = createTestUsers() + err = createTestUsers(userService) if err != nil { return err } - err = createTestRoles() + err = createTestRoles(roleService) if err != nil { return err } @@ -63,7 +64,7 @@ func clearAndCreateAuthTestSetup() error { return nil } -func createTestUsers() error { +func createTestUsers(userService rbacInterfaces.UserService) error { randomRoleMap := map[string]string{pndID: randomRoleName} // Generate a salt that is 16 characters long with 3 digits, 0 symbols, @@ -93,7 +94,7 @@ func createTestUsers() error { return nil } -func createTestRoles() error { +func createTestRoles(roleService rbacInterfaces.RoleService) error { roles := []rbac.Role{ { RoleID: uuid.MustParse(adminRoleID), @@ -153,7 +154,7 @@ func patchLogger(t *testing.T) { // Creates a token to be used in auth interceptor tests. If validTokenRequired is set as true, the generated token will also // be attached to the provided user. Else the user won't have the token and can not be authorized. -func createTestUserToken(userName string, validTokenRequired bool) (string, error) { +func createTestUserToken(userName string, validTokenRequired bool, userService rbacInterfaces.UserService, jwt *rbac.JWTManager) (string, error) { token, err := jwt.GenerateToken(rbac.User{UserName: userName}) if err != nil { return token, err -- GitLab