Skip to content

Commit fce250a

Browse files
authored
Merge pull request #1310 from k8s-infra-cherrypick-robot/cherry-pick-1309-to-release-1.24
[release-1.24] fix: strip service account token
2 parents 97a01f9 + d7113a9 commit fce250a

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
"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", StripSensitiveValue(protosanitizer.StripSecrets(req), "csi.storage.k8s.io/serviceAccount.tokens"))
105106

106107
resp, err := handler(ctx, req)
107108
if err != nil {
@@ -111,3 +112,48 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
111112
}
112113
return resp, err
113114
}
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+
}

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)