Skip to content

Commit ea79f24

Browse files
authored
Merge pull request #94 from andyzhangx/log-grpc
chore: use common lib to strip secrets in logGRPC
2 parents bd3cd44 + 1bbb19c commit ea79f24

File tree

11 files changed

+487
-29
lines changed

11 files changed

+487
-29
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/Azure/go-autorest/autorest/to v0.3.0
1010
github.com/container-storage-interface/spec v1.3.0
1111
github.com/golang/protobuf v1.3.5
12+
github.com/kubernetes-csi/csi-lib-utils v0.7.0
1213
github.com/kubernetes-csi/csi-proxy/client v0.0.0-20200330215040-9eff16441b2a
1314
github.com/kubernetes-csi/external-snapshotter/v2 v2.0.0-20200617021606-4800ca72d403
1415
github.com/onsi/ginkgo v1.11.0

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
257257
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
258258
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
259259
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
260+
github.com/kubernetes-csi/csi-lib-utils v0.7.0 h1:t1cS7HTD7z5D7h9iAdjWuHtMxJPb9s1fIv34rxytzqs=
260261
github.com/kubernetes-csi/csi-lib-utils v0.7.0/go.mod h1:bze+2G9+cmoHxN6+WyG1qT4MDxgZJMLGwc7V4acPNm0=
261262
github.com/kubernetes-csi/csi-proxy/client v0.0.0-20200330215040-9eff16441b2a h1:oUzd0gIsXuEaPLqOBc7h7KmoQaCEDrZcgEVFIlKVzsw=
262263
github.com/kubernetes-csi/csi-proxy/client v0.0.0-20200330215040-9eff16441b2a/go.mod h1:AEGb6PeX4XUKjEGycToqZQxHQS01Ch3C+C42FgORhOE=

pkg/csi-common/utils.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package csicommon
1818

1919
import (
2020
"fmt"
21+
"strings"
22+
2123
"github.com/container-storage-interface/spec/lib/go/csi"
24+
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
2225
"golang.org/x/net/context"
2326
"google.golang.org/grpc"
2427
"k8s.io/klog/v2"
25-
"regexp"
26-
"strings"
2728
)
2829

2930
func ParseEndpoint(ep string) (string, string, error) {
@@ -102,20 +103,14 @@ func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.Con
102103
s.Wait()
103104
}
104105

105-
// regex to mask secrets in log messages
106-
var reqSecretsRegex, _ = regexp.Compile("secrets\\s*:\\s*<key:\"(.*?)\"\\s*value:\".*?\"")
107-
108106
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
109-
110-
s := fmt.Sprintf("GRPC request: %+v", req)
111107
klog.V(3).Infof("GRPC call: %s", info.FullMethod)
112-
klog.V(5).Info(reqSecretsRegex.ReplaceAllString(s, "secrets:<key:\"$1\" value:\"****\""))
113-
108+
klog.V(5).Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
114109
resp, err := handler(ctx, req)
115110
if err != nil {
116111
klog.Errorf("GRPC error: %v", err)
117112
} else {
118-
klog.V(5).Infof("GRPC response: %+v", resp)
113+
klog.V(5).Infof("GRPC response: %s", protosanitizer.StripSecrets(resp))
119114
}
120115
return resp, err
121116
}

pkg/csi-common/utils_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"bytes"
2121
"context"
2222
"flag"
23+
"testing"
24+
2325
"google.golang.org/grpc"
2426
"k8s.io/klog/v2"
25-
"testing"
2627

2728
"github.com/container-storage-interface/spec/lib/go/csi"
2829
"github.com/stretchr/testify/assert"
@@ -118,14 +119,14 @@ func TestLogGRPC(t *testing.T) {
118119
},
119120
XXX_sizecache: 100,
120121
},
121-
`GRPC request: volume_id:"vol_1" secrets:<key:"account_key" value:"****" > secrets:<key:"account_name" value:"****" >`,
122+
`GRPC request: {"secrets":"***stripped***","volume_id":"vol_1"}`,
122123
},
123124
{
124125
"without secrets",
125126
&csi.ListSnapshotsRequest{
126127
StartingToken: "testtoken",
127128
},
128-
`GRPC request: starting_token:"testtoken"`,
129+
`GRPC request: {"starting_token":"testtoken"}`,
129130
},
130131
}
131132

@@ -138,7 +139,7 @@ func TestLogGRPC(t *testing.T) {
138139
// ASSERT
139140
assert.Contains(t, buf.String(), "GRPC call: fake")
140141
assert.Contains(t, buf.String(), test.expStr)
141-
assert.Contains(t, buf.String(), "GRPC response: <nil>")
142+
assert.Contains(t, buf.String(), "GRPC response: null")
142143

143144
// CLEANUP
144145
buf.Reset()

pkg/smb/controllerserver.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ import (
2222
"github.com/container-storage-interface/spec/lib/go/csi"
2323
"google.golang.org/grpc/codes"
2424
"google.golang.org/grpc/status"
25-
"k8s.io/klog/v2"
2625
)
2726

2827
// CreateVolume only supports static provisioning, no create volume action
2928
func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
30-
klog.V(2).Infof("CreateVolume called with request %+v", *req)
3129
volumeCapabilities := req.GetVolumeCapabilities()
3230
if len(volumeCapabilities) == 0 {
3331
return nil, status.Error(codes.InvalidArgument, "CreateVolume Volume capabilities must be provided")
@@ -43,7 +41,6 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
4341

4442
// DeleteVolume only supports static provisioning, no delete volume action
4543
func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
46-
klog.V(2).Infof("DeleteVolume called with request %v", *req)
4744
if len(req.GetVolumeId()) == 0 {
4845
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
4946
}
@@ -65,8 +62,6 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control
6562

6663
// ControllerGetCapabilities returns the capabilities of the Controller plugin
6764
func (d *Driver) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
68-
klog.V(2).Infof("ControllerGetCapabilities called with request %v", *req)
69-
7065
return &csi.ControllerGetCapabilitiesResponse{
7166
Capabilities: d.Cap,
7267
}, nil

pkg/smb/nodeserver.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24-
"regexp"
2524
"runtime"
2625
"strings"
2726
"time"
@@ -47,7 +46,6 @@ const (
4746

4847
// NodePublishVolume mount the volume from staging to target path
4948
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
50-
klog.V(2).Infof("NodePublishVolume called with request %v", *req)
5149
if req.GetVolumeCapability() == nil {
5250
return nil, status.Error(codes.InvalidArgument, "Volume capability missing in request")
5351
}
@@ -140,11 +138,6 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
140138

141139
// NodeStageVolume mount the volume to a staging path
142140
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
143-
// regex to mask username and password in log messages
144-
var reqSecretsRegex, _ = regexp.Compile(`map\[password:.*? `)
145-
s := fmt.Sprintf("NodeStageVolume called with request %v", *req)
146-
klog.V(5).Info(reqSecretsRegex.ReplaceAllString(s, "map[password:**** "))
147-
148141
if len(req.GetVolumeId()) == 0 {
149142
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
150143
}
@@ -254,15 +247,13 @@ func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolu
254247

255248
// NodeGetCapabilities return the capabilities of the Node plugin
256249
func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
257-
klog.V(2).Infof("NodeGetCapabilities called with request %v", *req)
258250
return &csi.NodeGetCapabilitiesResponse{
259251
Capabilities: d.NSCap,
260252
}, nil
261253
}
262254

263255
// NodeGetInfo return info of the node on which this plugin is running
264256
func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
265-
klog.V(2).Infof("NodeGetInfo called with request %v", *req)
266257
return &csi.NodeGetInfoResponse{
267258
NodeId: d.NodeID,
268259
}, nil

test/utils/smb_log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ echo "==========================================================================
2828
LABEL='app=csi-smb-controller'
2929
kubectl get pods -n${NS} -l${LABEL} \
3030
| awk 'NR>1 {print $1}' \
31-
| xargs -I {} kubectl describe po {} -n${NS}
31+
| xargs -I {} kubectl logs {} --prefix -c${CONTAINER} -n${NS}
3232

3333
echo "print out csi-smb-node logs ..."
3434
echo "======================================================================================"

vendor/github.com/golang/protobuf/descriptor/descriptor.go

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)