Skip to content

Commit cda78e7

Browse files
committed
Fix secret values exposed in logs issue
1 parent 2e2ce0c commit cda78e7

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

pkg/csi-common/utils.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package csicommon
1818

1919
import (
2020
"fmt"
21+
"regexp"
2122
"strings"
2223

2324
"github.com/container-storage-interface/spec/lib/go/csi"
@@ -102,9 +103,15 @@ func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.Con
102103
s.Wait()
103104
}
104105

106+
// regex to mask secrets in log messages
107+
var reqSecretsRegex, _ = regexp.Compile("secrets\\s*:\\s*<key:\"(.*?)\"\\s*value:\".*?\"")
108+
105109
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
110+
111+
s := fmt.Sprintf("GRPC request: %+v", req)
106112
klog.V(3).Infof("GRPC call: %s", info.FullMethod)
107-
klog.V(5).Infof("GRPC request: %+v", req)
113+
klog.V(5).Info(reqSecretsRegex.ReplaceAllString(s, "secrets:<key:\"$1\" value:\"****\""))
114+
108115
resp, err := handler(ctx, req)
109116
if err != nil {
110117
klog.Errorf("GRPC error: %v", err)

pkg/csi-common/utils_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ limitations under the License.
1717
package csicommon
1818

1919
import (
20+
"bytes"
21+
"context"
22+
"flag"
23+
"github.com/container-storage-interface/spec/lib/go/csi"
24+
"google.golang.org/grpc"
25+
"k8s.io/klog"
2026
"testing"
2127

2228
"github.com/stretchr/testify/assert"
@@ -74,3 +80,68 @@ func TestParseEndpoint(t *testing.T) {
7480
_, _, err = ParseEndpoint("")
7581
assert.NotNil(t, err)
7682
}
83+
84+
func TestLogGRPC(t *testing.T) {
85+
// SET UP
86+
klog.InitFlags(nil)
87+
if e := flag.Set("logtostderr", "false"); e != nil {
88+
t.Error(e)
89+
}
90+
if e := flag.Set("alsologtostderr", "false"); e != nil {
91+
t.Error(e)
92+
}
93+
if e := flag.Set("v", "100"); e != nil {
94+
t.Error(e)
95+
}
96+
flag.Parse()
97+
98+
buf := new(bytes.Buffer)
99+
klog.SetOutput(buf)
100+
101+
handler := func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil }
102+
info := grpc.UnaryServerInfo{
103+
FullMethod: "fake",
104+
}
105+
106+
tests := []struct {
107+
name string
108+
req interface{}
109+
expStr string
110+
}{
111+
{
112+
"with secrets",
113+
&csi.NodeStageVolumeRequest{
114+
VolumeId: "vol_1",
115+
Secrets: map[string]string{
116+
"account_name": "k8s",
117+
"account_key": "testkey",
118+
},
119+
XXX_sizecache: 100,
120+
},
121+
`GRPC request: volume_id:"vol_1" secrets:<key:"account_key" value:"****" > secrets:<key:"account_name" value:"****" >`,
122+
},
123+
{
124+
"without secrets",
125+
&csi.ListSnapshotsRequest{
126+
StartingToken: "testtoken",
127+
},
128+
`GRPC request: starting_token:"testtoken"`,
129+
},
130+
}
131+
132+
for _, test := range tests {
133+
t.Run(test.name, func(t *testing.T) {
134+
// EXECUTE
135+
_, _ = logGRPC(context.Background(), test.req, &info, handler)
136+
klog.Flush()
137+
138+
// ASSERT
139+
assert.Contains(t, buf.String(), "GRPC call: fake")
140+
assert.Contains(t, buf.String(), test.expStr)
141+
assert.Contains(t, buf.String(), "GRPC response: <nil>")
142+
143+
// CLEANUP
144+
buf.Reset()
145+
})
146+
}
147+
}

0 commit comments

Comments
 (0)