Skip to content

Commit 65e3c8f

Browse files
authored
Add log for authentication and add unit test. #12
Signed-off-by: Gang Lv [email protected] Why I did it We need to enhance authentication and authorization for sonic-gnmi, and the first step is to add log. How I did it Add log for auth user and roles. How to verify it Run unit test.
2 parents 1b3b838 + 5b8e873 commit 65e3c8f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

gnmi_server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func authenticate(UserAuth AuthTypes, ctx context.Context) (context.Context, err
202202
if !success {
203203
return ctx, status.Error(codes.Unauthenticated, "Unauthenticated")
204204
}
205+
log.V(5).Infof("authenticate user %v, roles %v", rc.Auth.User, rc.Auth.Roles)
205206

206207
return ctx, nil
207208
}

gnmi_server/server_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"io/ioutil"
1717
"os"
1818
"os/exec"
19+
"os/user"
1920
"reflect"
2021
"testing"
2122
"time"
@@ -42,6 +43,7 @@ import (
4243
gclient "github.com/jipanyang/gnmi/client/gnmi"
4344
"github.com/jipanyang/gnxi/utils/xpath"
4445
gnoi_system_pb "github.com/openconfig/gnoi/system"
46+
"github.com/agiledragon/gomonkey"
4547
)
4648

4749
var clientTypes = []string{gclient.Type}
@@ -104,6 +106,25 @@ func createServer(t *testing.T, port int64) *Server {
104106
return s
105107
}
106108

109+
func createAuthServer(t *testing.T, port int64) *Server {
110+
certificate, err := testcert.NewCert()
111+
if err != nil {
112+
t.Errorf("could not load server key pair: %s", err)
113+
}
114+
tlsCfg := &tls.Config{
115+
ClientAuth: tls.RequestClientCert,
116+
Certificates: []tls.Certificate{certificate},
117+
}
118+
119+
opts := []grpc.ServerOption{grpc.Creds(credentials.NewTLS(tlsCfg))}
120+
cfg := &Config{Port: port, UserAuth: AuthTypes{"password": true, "cert": true, "jwt": true}}
121+
s, err := NewServer(cfg, opts)
122+
if err != nil {
123+
t.Errorf("Failed to create gNMI server: %v", err)
124+
}
125+
return s
126+
}
127+
107128
// runTestGet requests a path from the server by Get grpc call, and compares if
108129
// the return code and response value are expected.
109130
func runTestGet(t *testing.T, ctx context.Context, gClient pb.GNMIClient, pathTarget string,
@@ -2509,8 +2530,57 @@ func TestBulkSet(t *testing.T) {
25092530
if !ok {
25102531
t.Fatal("got a non-grpc error from grpc call")
25112532
}
2533+
})
2534+
}
2535+
2536+
type loginCreds struct {
2537+
Username, Password string
2538+
}
25122539

2540+
func (c *loginCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
2541+
return map[string]string{
2542+
"username": c.Username,
2543+
"password": c.Password,
2544+
}, nil
2545+
}
2546+
2547+
func (c *loginCreds) RequireTransportSecurity() bool {
2548+
return true
2549+
}
2550+
2551+
func TestAuthCapabilities(t *testing.T) {
2552+
mock1 := gomonkey.ApplyFunc(UserPwAuth, func(username string, passwd string) (bool, error) {
2553+
return true, nil
25132554
})
2555+
defer mock1.Reset()
2556+
2557+
s := createAuthServer(t, 8089)
2558+
go runServer(t, s)
2559+
2560+
currentUser, _ := user.Current()
2561+
tlsConfig := &tls.Config{InsecureSkipVerify: true}
2562+
cred := &loginCreds{Username: currentUser.Username, Password: "dummy"}
2563+
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithPerRPCCredentials(cred)}
2564+
2565+
targetAddr := "127.0.0.1:8089"
2566+
conn, err := grpc.Dial(targetAddr, opts...)
2567+
if err != nil {
2568+
t.Fatalf("Dialing to %q failed: %v", targetAddr, err)
2569+
}
2570+
defer conn.Close()
2571+
2572+
gClient := pb.NewGNMIClient(conn)
2573+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2574+
defer cancel()
2575+
2576+
var req pb.CapabilityRequest
2577+
resp, err := gClient.Capabilities(ctx, &req)
2578+
if err != nil {
2579+
t.Fatalf("Failed to get Capabilities: %v", err)
2580+
}
2581+
if len(resp.SupportedModels) == 0 {
2582+
t.Fatalf("No Supported Models found!")
2583+
}
25142584

25152585
}
25162586

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/Azure/sonic-mgmt-common v0.0.0-00010101000000-000000000000
77
github.com/Workiva/go-datastructures v1.0.50
8+
github.com/agiledragon/gomonkey v2.0.2+incompatible
89
github.com/c9s/goprocinfo v0.0.0-20191125144613-4acdd056c72d
910
github.com/dgrijalva/jwt-go v3.2.1-0.20210802184156-9742bd7fca1c+incompatible
1011
github.com/go-redis/redis v6.15.6+incompatible

0 commit comments

Comments
 (0)