Skip to content

Commit d605781

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 d605781

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pkg/smb/nodeserver.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
231231
return nil, status.Error(codes.Internal, fmt.Sprintf("MkdirAll %s failed with error: %v", targetPath, err))
232232
}
233233
if requireUsernamePwdOption && !useKerberosCache {
234-
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
234+
if ContainsSpecialCharacter(password) {
235+
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
236+
} else {
237+
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
238+
}
235239
}
236240
mountOptions = mountFlags
237241
if !gidPresent && volumeMountGroup != "" {

pkg/smb/smb_common_linux.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,45 @@ 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 `word` contains a special character, i.e it can confuse mount command-line if passed as is:
31+
// mount -t cifs -o username=something,password=word,...
32+
// For now, only three such characters are known: "`,
33+
func ContainsSpecialCharacter(word string) bool {
34+
return strings.Contains(word, "\"") || strings.Contains(word, "`") || strings.Contains(word, ",")
35+
}
36+
37+
// Returns true if the `options` contains password with a special characters, and so "credentials=" needed.
38+
// (see comments for ContainsSpecialCharacter() above.
39+
// NB: implementation relies on the format:
40+
// options := []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
41+
func NeedsCredentialsOption(options []string) bool {
42+
return len(options) == 2 && strings.HasPrefix(options[1], "password=") && ContainsSpecialCharacter(options[1])
43+
}
44+
2845
func Mount(m *mount.SafeFormatAndMount, source, target, fsType string, options, sensitiveMountOptions []string, _ string) error {
46+
if NeedsCredentialsOption(sensitiveMountOptions) {
47+
file, err := os.CreateTemp("/tmp/", "*.smb.credentials")
48+
if err != nil {
49+
return err
50+
}
51+
52+
for _, option := range sensitiveMountOptions {
53+
if _, err := file.Write([]byte(fmt.Sprintf("%s\n", option))); err != nil {
54+
return err
55+
}
56+
}
57+
file.Close()
58+
defer os.Remove(file.Name())
59+
60+
sensitiveMountOptions = []string{fmt.Sprintf("credentials=%s", file.Name())}
61+
}
2962
return m.MountSensitive(source, target, fsType, options, sensitiveMountOptions)
3063
}
3164

0 commit comments

Comments
 (0)