@@ -17,6 +17,7 @@ limitations under the License.
17
17
package csicommon
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
21
22
"net"
22
23
"os"
@@ -101,7 +102,7 @@ func getLogLevel(method string) int32 {
101
102
func LogGRPC (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
102
103
level := klog .Level (getLogLevel (info .FullMethod ))
103
104
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" ))
105
106
106
107
resp , err := handler (ctx , req )
107
108
if err != nil {
@@ -111,3 +112,48 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
111
112
}
112
113
return resp , err
113
114
}
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