Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip service account token #1309

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion pkg/csi-common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package csicommon

import (
"encoding/json"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -101,7 +102,7 @@ func getLogLevel(method string) int32 {
func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
level := klog.Level(getLogLevel(info.FullMethod))
klog.V(level).Infof("GRPC call: %s", info.FullMethod)
klog.V(level).Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
klog.V(level).Infof("GRPC request: %s", stripServiceAccountToken(protosanitizer.StripSecrets(req)))

resp, err := handler(ctx, req)
if err != nil {
Expand All @@ -111,3 +112,30 @@ func LogGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
}
return resp, err
}

func stripServiceAccountToken(req fmt.Stringer) string {
var parsed map[string]interface{}

err := json.Unmarshal([]byte(req.String()), &parsed)
if err != nil || parsed == nil {
return req.String()
}

volumeContext, ok := parsed["volume_context"].(map[string]interface{})
if !ok {
return req.String()
}

if _, ok := volumeContext["csi.storage.k8s.io/serviceAccount.tokens"]; !ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this func more generic, set csi.storage.k8s.io/serviceAccount.tokens as a parameter, thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename as stripSecrect(secretFieldNames []string)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, please take another look

Copy link
Member Author

@cvvz cvvz Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename as stripSecrect(secretFieldNames []string)

This will be conflict with the protosanitizer.StripSecrets function in csi lib

return req.String()
}

volumeContext["csi.storage.k8s.io/serviceAccount.tokens"] = "***stripped***"

b, err := json.Marshal(parsed)
if err != nil {
return req.String()
}

return string(b)
}
38 changes: 38 additions & 0 deletions pkg/csi-common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,44 @@ func TestLogGRPC(t *testing.T) {
},
`GRPC request: {"starting_token":"testtoken"}`,
},
{
"NodeStageVolumeRequest with service account token",
&csi.NodeStageVolumeRequest{
VolumeContext: map[string]string{
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
"csi.storage.k8s.io/testfield": "testvalue",
},
XXX_sizecache: 100,
},
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
},
{
"NodePublishVolumeRequest with service account token",
&csi.NodePublishVolumeRequest{
VolumeContext: map[string]string{
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
"csi.storage.k8s.io/testfield": "testvalue",
},
XXX_sizecache: 100,
},
`GRPC request: {"volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"}}`,
},
{
"with secrets and service account token",
&csi.NodeStageVolumeRequest{
VolumeId: "vol_1",
Secrets: map[string]string{
"account_name": "k8s",
"account_key": "testkey",
},
VolumeContext: map[string]string{
"csi.storage.k8s.io/serviceAccount.tokens": "testtoken",
"csi.storage.k8s.io/testfield": "testvalue",
},
XXX_sizecache: 100,
},
`GRPC request: {"secrets":"***stripped***","volume_context":{"csi.storage.k8s.io/serviceAccount.tokens":"***stripped***","csi.storage.k8s.io/testfield":"testvalue"},"volume_id":"vol_1"}`,
},
}

for _, test := range tests {
Expand Down
Loading