Skip to content

Commit 0cbf17f

Browse files
committed
fix: enable to use secrets with special characters
If the password to SMB-server contained special characters (e.g. "foo,bar"), the mount failed. Now, when the password is passed to mount via "credentials=filename" option, then mount succeeds.
1 parent 032db30 commit 0cbf17f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pkg/smb/nodeserver.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ func (d *Driver) NodeUnpublishVolume(_ context.Context, req *csi.NodeUnpublishVo
124124
return &csi.NodeUnpublishVolumeResponse{}, nil
125125
}
126126

127+
// Returns true if the `word` contains a special character, i.e it can confuse mount command-line if passed as is:
128+
// mount -t cifs -o username=something,password=word,...
129+
// For now, only three such characters are known: "`,
130+
func ContainsSpecialCharacter(word string) bool {
131+
return strings.Contains(word, "\"") || strings.Contains(word, "`") || strings.Contains(word, ",")
132+
}
133+
127134
// NodeStageVolume mount the volume to a staging path
128135
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
129136
volumeID := req.GetVolumeId()
@@ -231,7 +238,11 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
231238
return nil, status.Error(codes.Internal, fmt.Sprintf("MkdirAll %s failed with error: %v", targetPath, err))
232239
}
233240
if requireUsernamePwdOption && !useKerberosCache {
234-
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
241+
if ContainsSpecialCharacter(password) {
242+
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
243+
} else {
244+
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
245+
}
235246
}
236247
mountOptions = mountFlags
237248
if !gidPresent && volumeMountGroup != "" {

pkg/smb/smb_common_linux.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,38 @@ limitations under the License.
2020
package smb
2121

2222
import (
23+
"fmt"
2324
"os"
25+
"strings"
2426

2527
mount "k8s.io/mount-utils"
2628
)
2729

30+
// Returns true if the `options` contains password with a special characters, and so "credentials=" needed.
31+
// (see comments for ContainsSpecialCharacter() in pkg/smb/nodeserver.go).
32+
// NB: implementation relies on the format:
33+
// options := []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
34+
func NeedsCredentialsOption(options []string) bool {
35+
return len(options) == 2 && strings.HasPrefix(options[1], "password=") && ContainsSpecialCharacter(options[1])
36+
}
37+
2838
func Mount(m *mount.SafeFormatAndMount, source, target, fsType string, options, sensitiveMountOptions []string, _ string) error {
39+
if NeedsCredentialsOption(sensitiveMountOptions) {
40+
file, err := os.CreateTemp("/tmp/", "*.smb.credentials")
41+
if err != nil {
42+
return err
43+
}
44+
45+
for _, option := range sensitiveMountOptions {
46+
if _, err := file.Write([]byte(fmt.Sprintf("%s\n", option))); err != nil {
47+
return err
48+
}
49+
}
50+
file.Close()
51+
defer os.Remove(file.Name())
52+
53+
sensitiveMountOptions = []string{fmt.Sprintf("credentials=%s", file.Name())}
54+
}
2955
return m.MountSensitive(source, target, fsType, options, sensitiveMountOptions)
3056
}
3157

0 commit comments

Comments
 (0)