Skip to content

Commit

Permalink
make function more general
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvz committed Mar 26, 2024
1 parent 6ad31c4 commit 6c657d4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pkg/csi-common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,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", stripServiceAccountToken(protosanitizer.StripSecrets(req)))
klog.V(level).Infof("GRPC request: %s", StripSensitiveValue(protosanitizer.StripSecrets(req), "csi.storage.k8s.io/serviceAccount.tokens"))

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

func stripServiceAccountToken(req fmt.Stringer) string {
type stripSensitiveValue struct {
// volume_context[key] is the value to be stripped.
key string
// req is the csi grpc request stripped by `protosanitizer.StripSecrets`
req fmt.Stringer
}

func StripSensitiveValue(req fmt.Stringer, key string) fmt.Stringer {
return &stripSensitiveValue{
key: key,
req: req,
}
}

func (s *stripSensitiveValue) String() string {
return stripSensitiveValueByKey(s.req, s.key)
}

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

err := json.Unmarshal([]byte(req.String()), &parsed)
Expand All @@ -126,11 +144,11 @@ func stripServiceAccountToken(req fmt.Stringer) string {
return req.String()
}

if _, ok := volumeContext["csi.storage.k8s.io/serviceAccount.tokens"]; !ok {
if _, ok := volumeContext[key]; !ok {
return req.String()
}

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

b, err := json.Marshal(parsed)
if err != nil {
Expand Down

0 comments on commit 6c657d4

Please sign in to comment.