Skip to content

Commit 283ab10

Browse files
committed
Fix password exposed in logs
Signed-off-by: Animesh Kumar <[email protected]>
1 parent 615fed0 commit 283ab10

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

pkg/smb/nodeserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io/ioutil"
2222
"os"
23+
"regexp"
2324
"runtime"
2425
"strings"
2526
"time"
@@ -117,7 +118,11 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
117118

118119
// NodeStageVolume mount the volume to a staging path
119120
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
120-
klog.V(2).Infof("NodeStageVolume called with request %v", *req)
121+
// regex to mask username and password in log messages
122+
var reqSecretsRegex, _ = regexp.Compile("map\\[password:.*? ")
123+
s := fmt.Sprintf("NodeStageVolume called with request %v", *req)
124+
klog.V(5).Info(reqSecretsRegex.ReplaceAllString(s, "map[password:**** "))
125+
121126
if len(req.GetVolumeId()) == 0 {
122127
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
123128
}

pkg/smb/nodeserver_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2020 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 smb
18+
19+
import (
20+
"bytes"
21+
"context"
22+
"flag"
23+
"github.com/container-storage-interface/spec/lib/go/csi"
24+
"github.com/stretchr/testify/assert"
25+
"k8s.io/klog"
26+
"testing"
27+
)
28+
29+
func TestNodeStageVolume(t *testing.T) {
30+
31+
klog.InitFlags(nil)
32+
if e := flag.Set("logtostderr", "false"); e != nil {
33+
t.Error(e)
34+
}
35+
if e := flag.Set("alsologtostderr", "false"); e != nil {
36+
t.Error(e)
37+
}
38+
if e := flag.Set("v", "100"); e != nil {
39+
t.Error(e)
40+
}
41+
flag.Parse()
42+
43+
buf := new(bytes.Buffer)
44+
klog.SetOutput(buf)
45+
46+
d := NewFakeDriver()
47+
48+
tests := []struct {
49+
name string
50+
req *csi.NodeStageVolumeRequest
51+
expStr string
52+
}{
53+
{
54+
"with secrets",
55+
&csi.NodeStageVolumeRequest{
56+
VolumeId: "vol_1",
57+
Secrets: map[string]string{
58+
"password": "testpassword",
59+
"username": "testuser",
60+
},
61+
VolumeCapability: &csi.VolumeCapability{},
62+
XXX_sizecache: 100,
63+
},
64+
`NodeStageVolume called with request {vol_1 map[] map[password:**** username:testuser] map[] {} [] 100}`,
65+
},
66+
}
67+
68+
for _, test := range tests {
69+
t.Run(test.name, func(t *testing.T) {
70+
// EXECUTE
71+
_, _ = d.NodeStageVolume(context.Background(), test.req)
72+
klog.Flush()
73+
74+
//ASSERT
75+
assert.Contains(t, buf.String(), test.expStr)
76+
77+
// CLEANUP
78+
buf.Reset()
79+
})
80+
}
81+
82+
}

pkg/smb/smb_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,30 @@ limitations under the License.
1717
package smb
1818

1919
import (
20+
"github.com/stretchr/testify/assert"
2021
"io/ioutil"
2122
"os"
2223
"path/filepath"
2324
"testing"
25+
)
2426

25-
"github.com/stretchr/testify/assert"
27+
const (
28+
fakeNodeID = "fakeNodeID"
2629
)
2730

31+
func NewFakeDriver() *Driver {
32+
33+
driver := NewDriver(fakeNodeID)
34+
35+
return driver
36+
}
37+
38+
func TestNewFakeDriver(t *testing.T) {
39+
// Test New fake driver.
40+
d := NewDriver(fakeNodeID)
41+
assert.NotNil(t, d)
42+
}
43+
2844
func TestIsCorruptedDir(t *testing.T) {
2945
existingMountPath, err := ioutil.TempDir(os.TempDir(), "csi-mount-test")
3046
if err != nil {

0 commit comments

Comments
 (0)