@@ -17,6 +17,7 @@ limitations under the License.
1717package csicommon
1818
1919import (
20+ "encoding/json"
2021 "fmt"
2122 "strings"
2223
@@ -74,7 +75,7 @@ func getLogLevel(method string) int32 {
7475func logGRPC (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
7576 level := klog .Level (getLogLevel (info .FullMethod ))
7677 klog .V (level ).Infof ("GRPC call: %s" , info .FullMethod )
77- klog .V (level ).Infof ("GRPC request: %s" , protosanitizer .StripSecrets (req ))
78+ klog .V (level ).Infof ("GRPC request: %s" , StripSensitiveValue ( protosanitizer .StripSecrets (req ), "csi.storage.k8s.io/serviceAccount.tokens" ))
7879
7980 resp , err := handler (ctx , req )
8081 if err != nil {
@@ -84,3 +85,48 @@ func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
8485 }
8586 return resp , err
8687}
88+
89+ type stripSensitiveValue struct {
90+ // volume_context[key] is the value to be stripped.
91+ key string
92+ // req is the csi grpc request stripped by `protosanitizer.StripSecrets`
93+ req fmt.Stringer
94+ }
95+
96+ func StripSensitiveValue (req fmt.Stringer , key string ) fmt.Stringer {
97+ return & stripSensitiveValue {
98+ key : key ,
99+ req : req ,
100+ }
101+ }
102+
103+ func (s * stripSensitiveValue ) String () string {
104+ return stripSensitiveValueByKey (s .req , s .key )
105+ }
106+
107+ func stripSensitiveValueByKey (req fmt.Stringer , key string ) string {
108+ var parsed map [string ]interface {}
109+
110+ err := json .Unmarshal ([]byte (req .String ()), & parsed )
111+ if err != nil || parsed == nil {
112+ return req .String ()
113+ }
114+
115+ volumeContext , ok := parsed ["volume_context" ].(map [string ]interface {})
116+ if ! ok {
117+ return req .String ()
118+ }
119+
120+ if _ , ok := volumeContext [key ]; ! ok {
121+ return req .String ()
122+ }
123+
124+ volumeContext [key ] = "***stripped***"
125+
126+ b , err := json .Marshal (parsed )
127+ if err != nil {
128+ return req .String ()
129+ }
130+
131+ return string (b )
132+ }
0 commit comments