Skip to content

Commit a1b7446

Browse files
cvvzk8s-infra-cherrypick-robot
authored andcommitted
fix: strip service account token
1 parent 8f5c601 commit a1b7446

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

pkg/csi-common/utils.go

Lines changed: 47 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
"strings"
2223

@@ -74,7 +75,7 @@ func getLogLevel(method string) int32 {
7475
func 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+
}

pkg/csi-common/utils_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,44 @@ func TestLogGRPC(t *testing.T) {
127127
},
128128
`GRPC request: {"starting_token":"testtoken"}`,
129129
},
130+
{
131+
"NodeStageVolumeRequest with service account token",
132+
&csi.NodeStageVolumeRequest{
133+
VolumeContext: map[string]string{
134+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
135+
"csi.storage.k8s.io/testfield": "testvalue",
136+
},
137+
XXX_sizecache: 100,
138+
},
139+
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
140+
},
141+
{
142+
"NodePublishVolumeRequest with service account token",
143+
&csi.NodePublishVolumeRequest{
144+
VolumeContext: map[string]string{
145+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
146+
"csi.storage.k8s.io/testfield": "testvalue",
147+
},
148+
XXX_sizecache: 100,
149+
},
150+
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
151+
},
152+
{
153+
"with secrets and service account token",
154+
&csi.NodeStageVolumeRequest{
155+
VolumeId: "vol_1",
156+
Secrets: map[string]string{
157+
"account_name": "k8s",
158+
"account_key": "testkey",
159+
},
160+
VolumeContext: map[string]string{
161+
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
162+
"csi.storage.k8s.io/testfield": "testvalue",
163+
},
164+
XXX_sizecache: 100,
165+
},
166+
`GRPC request: {"secrets":"***stripped***","volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"},"volume_id":"vol_1"}`,
167+
},
130168
}
131169

132170
for _, test := range tests {

0 commit comments

Comments
 (0)