Skip to content

fix: enable to use secrets with special characters #961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pkg/smb/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ 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)}
sensitiveMountOptions = []string{fmt.Sprintf("%s=%s", usernameField, username), fmt.Sprintf("%s=%s", passwordField, password)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there could be problem when there is mount process concurrently, is it possible to detect whether there is special chars in secrets first, if yes, then mount with cred file? I think that would be safer since it won't break anything, thanks.

}
mountOptions = mountFlags
if !gidPresent && volumeMountGroup != "" {
Expand Down
17 changes: 17 additions & 0 deletions pkg/smb/smb_common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,29 @@ limitations under the License.
package smb

import (
"fmt"
"os"

mount "k8s.io/mount-utils"
)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this file on a ramfs so it wouldn't touch the disk at all. But... at the very minimum, the temp file must have restricted permissions, since /tmp is world-readable on most systems. This needs to be safe in the rare cases that the driver is not run in a container and/or with multiple processes sharing /tmp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johanot , the temp file already has restricted permissions (rw for the user only) because that's how os.CreateTemp() works. See its official docs here: https://pkg.go.dev/os#CreateTemp .

I intentionally used "/tmp" instead of ramfs, because the location of ramfs mount may vary depending on environment (at least, I don't know any path that would exist everywhere). But generally I fully agree -- not touching the disk would be better. And "defer os.Remove" will wipe it anyway.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the temp file already has restricted permissions

Oh.. Never mind then :)

And "defer os.Remove" will wipe it anyway.

I guess a rare case exists where the process could get SIGKILL'ed before the deferred function is executed, but.. yeah.

because the location of ramfs mount may vary depending on environment

I think I would create a new ramfs for this purpose alone and choose the location myself, e.g. mount at a level below /tmp. However it's easier said than done, iirc, because you'd have to pull in mount-utils only for this pre-mount purpose. If the maintainers here are good with a standard temp file, then I rest my case.

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