@@ -20,7 +20,9 @@ package driver
2020
2121import (
2222 "context"
23+ "encoding/json"
2324 "errors"
25+ "fmt"
2426 "net"
2527 "sync"
2628
@@ -102,7 +104,7 @@ func (c *CSIDriver) Start(l net.Listener) error {
102104
103105 // Create a new grpc server
104106 c .server = grpc .NewServer (
105- grpc .UnaryInterceptor (c .authInterceptor ),
107+ grpc .UnaryInterceptor (c .callInterceptor ),
106108 )
107109
108110 // Register Mock servers
@@ -162,22 +164,49 @@ func (c *CSIDriver) SetDefaultCreds() {
162164 }
163165}
164166
165- func (c * CSIDriver ) authInterceptor (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
167+ func (c * CSIDriver ) callInterceptor (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
168+ err := c .authInterceptor (req )
169+ if err != nil {
170+ logGRPC (info .FullMethod , req , nil , err )
171+ return nil , err
172+ }
173+ rsp , err := handler (ctx , req )
174+ logGRPC (info .FullMethod , req , rsp , err )
175+ return rsp , err
176+ }
177+
178+ func (c * CSIDriver ) authInterceptor (req interface {}) error {
166179 if c .creds != nil {
167180 authenticated , authErr := isAuthenticated (req , c .creds )
168181 if ! authenticated {
169182 if authErr == ErrNoCredentials {
170- return nil , status .Error (codes .InvalidArgument , authErr .Error ())
183+ return status .Error (codes .InvalidArgument , authErr .Error ())
171184 }
172185 if authErr == ErrAuthFailed {
173- return nil , status .Error (codes .Unauthenticated , authErr .Error ())
186+ return status .Error (codes .Unauthenticated , authErr .Error ())
174187 }
175188 }
176189 }
190+ return nil
191+ }
177192
178- h , err := handler (ctx , req )
179-
180- return h , err
193+ func logGRPC (method string , request , reply interface {}, err error ) {
194+ // Log JSON with the request and response for easier parsing
195+ logMessage := struct {
196+ Method string
197+ Request interface {}
198+ Response interface {}
199+ Error string
200+ }{
201+ Method : method ,
202+ Request : request ,
203+ Response : reply ,
204+ }
205+ if err != nil {
206+ logMessage .Error = err .Error ()
207+ }
208+ msg , _ := json .Marshal (logMessage )
209+ fmt .Printf ("gRPCCall: %s\n " , msg )
181210}
182211
183212func isAuthenticated (req interface {}, creds * CSICreds ) (bool , error ) {
0 commit comments