@@ -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"
@@ -98,7 +99,7 @@ func getLogLevel(method string) int32 {
98
99
func LogGRPC (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
99
100
level := klog .Level (getLogLevel (info .FullMethod ))
100
101
klog .V (level ).Infof ("GRPC call: %s" , info .FullMethod )
101
- klog .V (level ).Infof ("GRPC request: %s" , protosanitizer .StripSecrets (req ))
102
+ klog .V (level ).Infof ("GRPC request: %s" , StripSensitiveValue ( protosanitizer .StripSecrets (req ), "csi.storage.k8s.io/serviceAccount.tokens" ))
102
103
103
104
resp , err := handler (ctx , req )
104
105
if err != nil {
@@ -108,3 +109,48 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
108
109
}
109
110
return resp , err
110
111
}
112
+
113
+ type stripSensitiveValue struct {
114
+ // volume_context[key] is the value to be stripped.
115
+ key string
116
+ // req is the csi grpc request stripped by `protosanitizer.StripSecrets`
117
+ req fmt.Stringer
118
+ }
119
+
120
+ func StripSensitiveValue (req fmt.Stringer , key string ) fmt.Stringer {
121
+ return & stripSensitiveValue {
122
+ key : key ,
123
+ req : req ,
124
+ }
125
+ }
126
+
127
+ func (s * stripSensitiveValue ) String () string {
128
+ return stripSensitiveValueByKey (s .req , s .key )
129
+ }
130
+
131
+ func stripSensitiveValueByKey (req fmt.Stringer , key string ) string {
132
+ var parsed map [string ]interface {}
133
+
134
+ err := json .Unmarshal ([]byte (req .String ()), & parsed )
135
+ if err != nil || parsed == nil {
136
+ return req .String ()
137
+ }
138
+
139
+ volumeContext , ok := parsed ["volume_context" ].(map [string ]interface {})
140
+ if ! ok {
141
+ return req .String ()
142
+ }
143
+
144
+ if _ , ok := volumeContext [key ]; ! ok {
145
+ return req .String ()
146
+ }
147
+
148
+ volumeContext [key ] = "***stripped***"
149
+
150
+ b , err := json .Marshal (parsed )
151
+ if err != nil {
152
+ return req .String ()
153
+ }
154
+
155
+ return string (b )
156
+ }
0 commit comments