Skip to content

Commit 1e86b91

Browse files
authored
Merge pull request #676 from andyzhangx/bypass-chmod
fix: bypass chmod if mounting point permissions are correct
2 parents 4fb525a + 98ef5a1 commit 1e86b91

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

pkg/blob/blob.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package blob
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"strings"
2223
"sync"
2324
"time"
@@ -747,3 +748,21 @@ func appendDefaultMountOptions(mountOptions []string, tmpPath, containerName str
747748

748749
return allMountOptions
749750
}
751+
752+
// chmodIfPermissionMismatch only perform chmod when permission mismatches
753+
func chmodIfPermissionMismatch(targetPath string, mode os.FileMode) error {
754+
info, err := os.Lstat(targetPath)
755+
if err != nil {
756+
return err
757+
}
758+
perm := info.Mode() & os.ModePerm
759+
if perm != mode {
760+
klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), mode)
761+
if err := os.Chmod(targetPath, mode); err != nil {
762+
return err
763+
}
764+
} else {
765+
klog.V(2).Infof("skip chmod on targetPath(%s) since mode is already 0%o)", targetPath, info.Mode())
766+
}
767+
return nil
768+
}

pkg/blob/blob_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,57 @@ func TestIsSupportedContainerNamePrefix(t *testing.T) {
889889
}
890890
}
891891
}
892+
893+
func TestChmodIfPermissionMismatch(t *testing.T) {
894+
permissionMatchingPath, _ := getWorkDirPath("permissionMatchingPath")
895+
_ = makeDir(permissionMatchingPath)
896+
defer os.RemoveAll(permissionMatchingPath)
897+
898+
permissionMismatchPath, _ := getWorkDirPath("permissionMismatchPath")
899+
_ = os.MkdirAll(permissionMismatchPath, os.FileMode(0721))
900+
defer os.RemoveAll(permissionMismatchPath)
901+
902+
tests := []struct {
903+
desc string
904+
path string
905+
mode os.FileMode
906+
expectedError error
907+
}{
908+
{
909+
desc: "Invalid path",
910+
path: "invalid-path",
911+
mode: 0755,
912+
expectedError: fmt.Errorf("CreateFile invalid-path: The system cannot find the file specified"),
913+
},
914+
{
915+
desc: "permission matching path",
916+
path: permissionMatchingPath,
917+
mode: 0755,
918+
expectedError: nil,
919+
},
920+
{
921+
desc: "permission mismatch path",
922+
path: permissionMismatchPath,
923+
mode: 0755,
924+
expectedError: nil,
925+
},
926+
}
927+
928+
for _, test := range tests {
929+
err := chmodIfPermissionMismatch(test.path, test.mode)
930+
if !reflect.DeepEqual(err, test.expectedError) {
931+
if err == nil || test.expectedError == nil && !strings.Contains(err.Error(), test.expectedError.Error()) {
932+
t.Errorf("test[%s]: unexpected error: %v, expected error: %v", test.desc, err, test.expectedError)
933+
}
934+
}
935+
}
936+
}
937+
938+
// getWorkDirPath returns the path to the current working directory
939+
func getWorkDirPath(dir string) (string, error) {
940+
path, err := os.Getwd()
941+
if err != nil {
942+
return "", err
943+
}
944+
return fmt.Sprintf("%s%c%s", path, os.PathSeparator, dir), nil
945+
}

pkg/blob/nodeserver.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
291291
}
292292

293293
if performChmodOp {
294-
klog.V(2).Infof("volumeID(%v): chmod targetPath(%s) with permissions(0%o)", volumeID, targetPath, mountPermissions)
295-
// set permissions for NFSv3 root folder
296-
if err := os.Chmod(targetPath, os.FileMode(mountPermissions)); err != nil {
297-
return nil, status.Error(codes.Internal, fmt.Sprintf("Chmod(%s) failed with %v", targetPath, err))
294+
if err := chmodIfPermissionMismatch(targetPath, os.FileMode(mountPermissions)); err != nil {
295+
return nil, status.Error(codes.Internal, err.Error())
298296
}
299297
} else {
300298
klog.V(2).Infof("skip chmod on targetPath(%s) since mountPermissions is set as 0", targetPath)

0 commit comments

Comments
 (0)