Skip to content

Commit 832d577

Browse files
authored
Merge pull request #1778 from k8s-infra-cherrypick-robot/cherry-pick-1777-to-release-1.30
[release-1.30] fix: strip service account token
2 parents afc4de6 + e11ff3d commit 832d577

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

pkg/csi-common/utils.go

+47-1
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"
@@ -98,7 +99,7 @@ func getLogLevel(method string) int32 {
9899
func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
99100
level := klog.Level(getLogLevel(info.FullMethod))
100101
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"))
102103

103104
resp, err := handler(ctx, req)
104105
if err != nil {
@@ -108,3 +109,48 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
108109
}
109110
return resp, err
110111
}
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+
}

pkg/csi-common/utils_test.go

+38
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)