Skip to content

Commit ada8a62

Browse files
cvvzk8s-infra-cherrypick-robot
authored andcommitted
fix: strip service account token
1 parent 4f34ae5 commit ada8a62

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

pkg/csi-common/utils.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package csicommon
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"net"
2223
"os"
@@ -101,7 +102,7 @@ func getLogLevel(method string) int32 {
101102
func 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", stripServiceAccountToken(protosanitizer.StripSecrets(req)))
105106

106107
resp, err := handler(ctx, req)
107108
if err != nil {
@@ -111,3 +112,30 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
111112
}
112113
return resp, err
113114
}
115+
116+
func stripServiceAccountToken(req fmt.Stringer) string {
117+
var parsed map[string]interface{}
118+
119+
err := json.Unmarshal([]byte(req.String()), &parsed)
120+
if err != nil || parsed == nil {
121+
return req.String()
122+
}
123+
124+
volumeContext, ok := parsed["volume_context"].(map[string]interface{})
125+
if !ok {
126+
return req.String()
127+
}
128+
129+
if _, ok := volumeContext["csi.storage.k8s.io/serviceAccount.tokens"]; !ok {
130+
return req.String()
131+
}
132+
133+
volumeContext["csi.storage.k8s.io/serviceAccount.tokens"] = "***stripped***"
134+
135+
b, err := json.Marshal(parsed)
136+
if err != nil {
137+
return req.String()
138+
}
139+
140+
return string(b)
141+
}

pkg/csi-common/utils_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,44 @@ func TestLogGRPC(t *testing.T) {
119119
},
120120
`GRPC request: {"starting_token":"testtoken"}`,
121121
},
122+
{
123+
"NodeStageVolumeRequest with service account token",
124+
&csi.NodeStageVolumeRequest{
125+
VolumeContext: map[string]string{
126+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
127+
"csi.storage.k8s.io/testfield": "testvalue",
128+
},
129+
XXX_sizecache: 100,
130+
},
131+
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
132+
},
133+
{
134+
"NodePublishVolumeRequest with service account token",
135+
&csi.NodePublishVolumeRequest{
136+
VolumeContext: map[string]string{
137+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
138+
"csi.storage.k8s.io/testfield": "testvalue",
139+
},
140+
XXX_sizecache: 100,
141+
},
142+
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
143+
},
144+
{
145+
"with secrets and service account token",
146+
&csi.NodeStageVolumeRequest{
147+
VolumeId: "vol_1",
148+
Secrets: map[string]string{
149+
"account_name": "k8s",
150+
"account_key": "testkey",
151+
},
152+
VolumeContext: map[string]string{
153+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
154+
"csi.storage.k8s.io/testfield": "testvalue",
155+
},
156+
XXX_sizecache: 100,
157+
},
158+
`GRPC request: {"secrets":"***stripped***","volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"},"volume_id":"vol_1"}`,
159+
},
122160
}
123161

124162
for _, test := range tests {

0 commit comments

Comments
 (0)