@@ -17,6 +17,7 @@ limitations under the License.
1717package csicommon
1818
1919import (
20+ "encoding/json"
2021 "fmt"
2122 "net"
2223 "os"
@@ -101,7 +102,7 @@ func getLogLevel(method string) int32 {
101102func LogGRPC (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
102103 level := klog .Level (getLogLevel (info .FullMethod ))
103104 klog .V (level ).Infof ("GRPC call: %s" , info .FullMethod )
104- klog .V (level ).Infof ("GRPC request: %s" , protosanitizer .StripSecrets (req ))
105+ klog .V (level ).Infof ("GRPC request: %s" , StripSensitiveValue ( protosanitizer .StripSecrets (req ), "csi.storage.k8s.io/serviceAccount.tokens" ))
105106
106107 resp , err := handler (ctx , req )
107108 if err != nil {
@@ -111,3 +112,48 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
111112 }
112113 return resp , err
113114}
115+
116+ type stripSensitiveValue struct {
117+ // volume_context[key] is the value to be stripped.
118+ key string
119+ // req is the csi grpc request stripped by `protosanitizer.StripSecrets`
120+ req fmt.Stringer
121+ }
122+
123+ func StripSensitiveValue (req fmt.Stringer , key string ) fmt.Stringer {
124+ return & stripSensitiveValue {
125+ key : key ,
126+ req : req ,
127+ }
128+ }
129+
130+ func (s * stripSensitiveValue ) String () string {
131+ return stripSensitiveValueByKey (s .req , s .key )
132+ }
133+
134+ func stripSensitiveValueByKey (req fmt.Stringer , key string ) string {
135+ var parsed map [string ]interface {}
136+
137+ err := json .Unmarshal ([]byte (req .String ()), & parsed )
138+ if err != nil || parsed == nil {
139+ return req .String ()
140+ }
141+
142+ volumeContext , ok := parsed ["volume_context" ].(map [string ]interface {})
143+ if ! ok {
144+ return req .String ()
145+ }
146+
147+ if _ , ok := volumeContext [key ]; ! ok {
148+ return req .String ()
149+ }
150+
151+ volumeContext [key ] = "***stripped***"
152+
153+ b , err := json .Marshal (parsed )
154+ if err != nil {
155+ return req .String ()
156+ }
157+
158+ return string (b )
159+ }
0 commit comments