Skip to content

Commit d6de328

Browse files
committed
Sanitize CSI RPC request logs
1 parent 4aba795 commit d6de328

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

pkg/driver/controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/container-storage-interface/spec/lib/go/csi"
3232
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud"
33+
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
3334
"google.golang.org/grpc/codes"
3435
"google.golang.org/grpc/status"
3536
"k8s.io/klog/v2"
@@ -78,7 +79,7 @@ var (
7879
)
7980

8081
func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
81-
klog.V(4).Infof("CreateVolume: called with args %+v", *req)
82+
klog.V(4).Infof("CreateVolume: called with args %+v", util.SanitizeRequest(*req))
8283

8384
var reuseAccessPoint bool
8485
var err error
@@ -370,7 +371,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
370371
return nil, err
371372
}
372373

373-
klog.V(4).Infof("DeleteVolume: called with args %+v", *req)
374+
klog.V(4).Infof("DeleteVolume: called with args %+v", util.SanitizeRequest(*req))
374375
volId := req.GetVolumeId()
375376
if volId == "" {
376377
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
@@ -467,7 +468,7 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control
467468
}
468469

469470
func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
470-
klog.V(4).Infof("ValidateVolumeCapabilities: called with args %+v", *req)
471+
klog.V(4).Infof("ValidateVolumeCapabilities: called with args %+v", util.SanitizeRequest(*req))
471472
volId := req.GetVolumeId()
472473
if volId == "" {
473474
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
@@ -501,7 +502,7 @@ func (d *Driver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (
501502
}
502503

503504
func (d *Driver) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
504-
klog.V(4).Infof("ControllerGetCapabilities: called with args %+v", *req)
505+
klog.V(4).Infof("ControllerGetCapabilities: called with args %+v", util.SanitizeRequest(*req))
505506
var caps []*csi.ControllerServiceCapability
506507
for _, cap := range controllerCaps {
507508
c := &csi.ControllerServiceCapability{

pkg/driver/identity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"k8s.io/klog/v2"
2323

2424
"github.com/container-storage-interface/spec/lib/go/csi"
25+
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
2526
)
2627

2728
func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
@@ -34,7 +35,7 @@ func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoReques
3435
}
3536

3637
func (d *Driver) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
37-
klog.V(5).Infof("GetPluginCapabilities: called with args %+v", *req)
38+
klog.V(5).Infof("GetPluginCapabilities: called with args %+v", util.SanitizeRequest(*req))
3839
resp := &csi.GetPluginCapabilitiesResponse{
3940
Capabilities: []*csi.PluginCapability{
4041
{

pkg/driver/node.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/container-storage-interface/spec/lib/go/csi"
3030
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud"
31+
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
3132
"google.golang.org/grpc/codes"
3233
"google.golang.org/grpc/status"
3334
corev1 "k8s.io/api/core/v1"
@@ -54,7 +55,7 @@ func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolu
5455
}
5556

5657
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
57-
klog.V(4).Infof("NodePublishVolume: called with args %+v", req)
58+
klog.V(4).Infof("NodePublishVolume: called with args %+v", util.SanitizeRequest(*req))
5859
mountOptions := []string{}
5960

6061
target := req.GetTargetPath()
@@ -216,7 +217,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
216217
}
217218

218219
func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
219-
klog.V(4).Infof("NodeUnpublishVolume: called with args %+v", req)
220+
klog.V(4).Infof("NodeUnpublishVolume: called with args %+v", util.SanitizeRequest(*req))
220221

221222
target := req.GetTargetPath()
222223
if len(target) == 0 {
@@ -266,7 +267,7 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
266267
}
267268

268269
func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
269-
klog.V(4).Infof("NodeGetVolumeStats: called with args %+v", req)
270+
klog.V(4).Infof("NodeGetVolumeStats: called with args %+v", util.SanitizeRequest(*req))
270271

271272
volId := req.GetVolumeId()
272273
if volId == "" {
@@ -303,7 +304,7 @@ func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolume
303304
}
304305

305306
func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
306-
klog.V(4).Infof("NodeGetCapabilities: called with args %+v", req)
307+
klog.V(4).Infof("NodeGetCapabilities: called with args %+v", util.SanitizeRequest(*req))
307308
var caps []*csi.NodeServiceCapability
308309
for _, cap := range d.nodeCaps {
309310
c := &csi.NodeServiceCapability{
@@ -319,7 +320,7 @@ func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabi
319320
}
320321

321322
func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
322-
klog.V(4).Infof("NodeGetInfo: called with args %+v", req)
323+
klog.V(4).Infof("NodeGetInfo: called with args %+v", util.SanitizeRequest(*req))
323324

324325
return &csi.NodeGetInfoResponse{
325326
NodeId: d.nodeID,

pkg/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path"
2626
"path/filepath"
27+
"reflect"
2728
"strings"
2829
)
2930

@@ -67,3 +68,20 @@ func GetHttpResponse(client *http.Client, endpoint string) ([]byte, error) {
6768
}
6869
return body, nil
6970
}
71+
72+
// SanitizeRequest takes a request object and returns a copy of the request with
73+
// the "Secrets" field cleared.
74+
func SanitizeRequest(req interface{}) interface{} {
75+
v := reflect.ValueOf(&req).Elem()
76+
e := reflect.New(v.Elem().Type()).Elem()
77+
78+
e.Set(v.Elem())
79+
80+
f := reflect.Indirect(e).FieldByName("Secrets")
81+
82+
if f.IsValid() && f.CanSet() && f.Kind() == reflect.Map {
83+
f.Set(reflect.MakeMap(f.Type()))
84+
v.Set(e)
85+
}
86+
return req
87+
}

pkg/util/util_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
)
23+
24+
type TestRequest struct {
25+
Name string
26+
Secrets map[string]string
27+
}
28+
29+
func TestSanitizeRequest(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
req interface{}
33+
expected interface{}
34+
}{
35+
{
36+
name: "Request with Secrets",
37+
req: &TestRequest{
38+
Name: "Test",
39+
Secrets: map[string]string{
40+
"key1": "value1",
41+
"key2": "value2",
42+
},
43+
},
44+
expected: &TestRequest{
45+
Name: "Test",
46+
Secrets: map[string]string{},
47+
},
48+
},
49+
}
50+
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
result := SanitizeRequest(tt.req)
54+
if !reflect.DeepEqual(result, tt.expected) {
55+
t.Errorf("SanitizeRequest() = %v, expected %v", result, tt.expected)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)