Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkg/smb/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (d *Driver) NodeUnpublishVolume(_ context.Context, req *csi.NodeUnpublishVo
return &csi.NodeUnpublishVolumeResponse{}, nil
}

// Returns true if the `word` contains a special character, i.e it can confuse mount command-line if passed as is:
// mount -t cifs -o username=something,password=word,...
// For now, only three such characters are known: "`,
func ContainsSpecialCharacter(word string) bool {
return strings.Contains(word, "\"") || strings.Contains(word, "`") || strings.Contains(word, ",")
}

// NodeStageVolume mount the volume to a staging path
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
volumeID := req.GetVolumeId()
Expand Down Expand Up @@ -231,7 +238,11 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
return nil, status.Error(codes.Internal, fmt.Sprintf("MkdirAll %s failed with error: %v", targetPath, err))
}
if requireUsernamePwdOption && !useKerberosCache {
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
if ContainsSpecialCharacter(password) {
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
} else {
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s,%s=%s", usernameField, username, passwordField, password)}
}
}
mountOptions = mountFlags
if !gidPresent && volumeMountGroup != "" {
Expand Down
26 changes: 26 additions & 0 deletions pkg/smb/smb_common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,38 @@ limitations under the License.
package smb

import (
"fmt"
"os"
"strings"

mount "k8s.io/mount-utils"
)

// Returns true if the `options` contains password with a special characters, and so "credentials=" needed.
// (see comments for ContainsSpecialCharacter() in pkg/smb/nodeserver.go).
// NB: implementation relies on the format:
// options := []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
func NeedsCredentialsOption(options []string) bool {
return len(options) == 2 && strings.HasPrefix(options[1], "password=") && ContainsSpecialCharacter(options[1])
}

func Mount(m *mount.SafeFormatAndMount, source, target, fsType string, options, sensitiveMountOptions []string, _ string) error {
if NeedsCredentialsOption(sensitiveMountOptions) {
file, err := os.CreateTemp("/tmp/", "*.smb.credentials")
if err != nil {
return err
}

for _, option := range sensitiveMountOptions {
if _, err := file.Write([]byte(fmt.Sprintf("%s\n", option))); err != nil {
return err
}
}
file.Close()
defer os.Remove(file.Name())

sensitiveMountOptions = []string{fmt.Sprintf("credentials=%s", file.Name())}
}
return m.MountSensitive(source, target, fsType, options, sensitiveMountOptions)
}

Expand Down
Loading