Skip to content

Commit a03aa0d

Browse files
committed
fix: move defer up and add unit-tests for mount with "credentials=" option
1 parent 0cbf17f commit a03aa0d

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

pkg/smb/nodeserver_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func TestNodeStageVolume(t *testing.T) {
9292
passwordField: "test_password",
9393
domainField: "test_doamin",
9494
}
95+
secretsSpecial := map[string]string{
96+
usernameField: "test_username",
97+
passwordField: "test\"`,password",
98+
domainField: "test_doamin",
99+
}
95100

96101
tests := []struct {
97102
desc string
@@ -182,6 +187,23 @@ func TestNodeStageVolume(t *testing.T) {
182187
strings.Replace(testSource, "\\", "\\\\", -1), errorMountSensSource),
183188
},
184189
},
190+
{
191+
desc: "[Error] Failed SMB mount mocked by MountSensitive (password with special characters)",
192+
req: &csi.NodeStageVolumeRequest{VolumeId: "vol_1##", StagingTargetPath: errorMountSensSource,
193+
VolumeCapability: &stdVolCap,
194+
VolumeContext: volContext,
195+
Secrets: secretsSpecial},
196+
skipOnWindows: true,
197+
flakyWindowsErrorMessage: fmt.Sprintf("rpc error: code = Internal desc = volume(vol_1##) mount \"%s\" on %#v failed "+
198+
"with NewSmbGlobalMapping(%s, %s) failed with error: rpc error: code = Unknown desc = NewSmbGlobalMapping failed.",
199+
strings.Replace(testSource, "\\", "\\\\", -1), errorMountSensSource, testSource, errorMountSensSource),
200+
expectedErr: testutil.TestError{
201+
DefaultError: status.Errorf(codes.Internal,
202+
"volume(vol_1##) mount \"%s\" on \"%s\" failed with fake "+
203+
"MountSensitive: target error",
204+
strings.Replace(testSource, "\\", "\\\\", -1), errorMountSensSource),
205+
},
206+
},
185207
{
186208
desc: "[Success] Valid request",
187209
req: &csi.NodeStageVolumeRequest{VolumeId: "vol_1##", StagingTargetPath: sourceTest,

pkg/smb/smb_common_linux.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@ func Mount(m *mount.SafeFormatAndMount, source, target, fsType string, options,
4141
if err != nil {
4242
return err
4343
}
44+
defer func() {
45+
file.Close()
46+
os.Remove(file.Name())
47+
}()
4448

4549
for _, option := range sensitiveMountOptions {
4650
if _, err := file.Write([]byte(fmt.Sprintf("%s\n", option))); err != nil {
4751
return err
4852
}
4953
}
50-
file.Close()
51-
defer os.Remove(file.Name())
54+
if err := file.Close(); err != nil {
55+
return err
56+
}
5257

5358
sensitiveMountOptions = []string{fmt.Sprintf("credentials=%s", file.Name())}
5459
}

pkg/smb/smb_common_linux_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
"github.com/stretchr/testify/assert"
21+
"testing"
22+
)
23+
24+
func TestNeedsCredentialsOption(t *testing.T) {
25+
tests := []struct {
26+
options []string
27+
expectedResult bool
28+
}{
29+
{
30+
options: []string{"username=foo,password=bar"},
31+
expectedResult: false,
32+
},
33+
{
34+
options: []string{"username=foo", "password=bar"},
35+
expectedResult: false,
36+
},
37+
{
38+
options: []string{"username=foo", "password=b\"r"},
39+
expectedResult: true,
40+
},
41+
{
42+
options: []string{"username=foo", "password=b`r"},
43+
expectedResult: true,
44+
},
45+
{
46+
options: []string{"username=foo", "password=b,r"},
47+
expectedResult: true,
48+
},
49+
}
50+
51+
for _, test := range tests {
52+
assert.Equal(t, test.expectedResult, NeedsCredentialsOption(test.options))
53+
}
54+
}

0 commit comments

Comments
 (0)