Skip to content

Commit f54b02e

Browse files
Merge pull request kubernetes-csi#16 from mpatlasov/OCPBUGS-39304-Fix-fsGroup-behavior
OCPBUGS-39304: UPSTREAM: 848: fix fsGroup behavior
2 parents a12ac11 + 82d699d commit f54b02e

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

pkg/smb/nodeserver.go

+28
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
200200
mountOptions = mountFlags
201201
if !gidPresent && volumeMountGroup != "" {
202202
mountOptions = append(mountOptions, fmt.Sprintf("gid=%s", volumeMountGroup))
203+
if !raiseGroupRWXInMountFlags(mountOptions, "file_mode") {
204+
mountOptions = append(mountOptions, "file_mode=0774")
205+
}
206+
if !raiseGroupRWXInMountFlags(mountOptions, "dir_mode") {
207+
mountOptions = append(mountOptions, "dir_mode=0775")
208+
}
203209
}
204210
if domain != "" {
205211
mountOptions = append(mountOptions, fmt.Sprintf("%s=%s", domainField, domain))
@@ -608,3 +614,25 @@ func deleteKerberosCache(krb5CacheDirectory, volumeID string) error {
608614

609615
return nil
610616
}
617+
618+
// Raises RWX bits for group access in the mode arg. If mode is invalid, keep it unchanged.
619+
func enableGroupRWX(mode string) string {
620+
v, e := strconv.ParseInt(mode, 0, 0)
621+
if e != nil || v < 0 {
622+
return mode
623+
}
624+
return fmt.Sprintf("0%o", v|070)
625+
}
626+
627+
// Apply enableGroupRWX() to the option "flag=xyz"
628+
func raiseGroupRWXInMountFlags(mountFlags []string, flag string) bool {
629+
for i, mountFlag := range mountFlags {
630+
mountFlagSplit := strings.Split(mountFlag, "=")
631+
if len(mountFlagSplit) != 2 || mountFlagSplit[0] != flag {
632+
continue
633+
}
634+
mountFlags[i] = fmt.Sprintf("%s=%s", flag, enableGroupRWX(mountFlagSplit[1]))
635+
return true
636+
}
637+
return false
638+
}

pkg/smb/nodeserver_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ func TestNodeStageVolume(t *testing.T) {
5757
Mount: &csi.VolumeCapability_MountVolume{},
5858
},
5959
}
60+
mountGroupVolCap := csi.VolumeCapability{
61+
AccessType: &csi.VolumeCapability_Mount{
62+
Mount: &csi.VolumeCapability_MountVolume{
63+
VolumeMountGroup: "1000",
64+
},
65+
},
66+
}
67+
mountGroupWithModesVolCap := csi.VolumeCapability{
68+
AccessType: &csi.VolumeCapability_Mount{
69+
Mount: &csi.VolumeCapability_MountVolume{
70+
VolumeMountGroup: "1000",
71+
MountFlags: []string{"file_mode=0111", "dir_mode=0111"},
72+
},
73+
},
74+
}
6075

6176
errorMountSensSource := testutil.GetWorkDirPath("error_mount_sens_source", t)
6277
smbFile := testutil.GetWorkDirPath("smb.go", t)
@@ -191,6 +206,30 @@ func TestNodeStageVolume(t *testing.T) {
191206
strings.Replace(testSource, "\\", "\\\\", -1), sourceTest, testSource, sourceTest),
192207
expectedErr: testutil.TestError{},
193208
},
209+
{
210+
desc: "[Success] Valid request with VolumeMountGroup",
211+
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1##", StagingTargetPath: sourceTest,
212+
VolumeCapability: &mountGroupVolCap,
213+
VolumeContext: volContext,
214+
Secrets: secrets},
215+
skipOnWindows: true,
216+
flakyWindowsErrorMessage: fmt.Sprintf("rpc error: code = Internal desc = volume(vol_1##) mount \"%s\" on %#v failed with "+
217+
"NewSmbGlobalMapping(%s, %s) failed with error: rpc error: code = Unknown desc = NewSmbGlobalMapping failed.",
218+
strings.Replace(testSource, "\\", "\\\\", -1), sourceTest, testSource, sourceTest),
219+
expectedErr: testutil.TestError{},
220+
},
221+
{
222+
desc: "[Success] Valid request with VolumeMountGroup and file/dir modes",
223+
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1##", StagingTargetPath: sourceTest,
224+
VolumeCapability: &mountGroupWithModesVolCap,
225+
VolumeContext: volContext,
226+
Secrets: secrets},
227+
skipOnWindows: true,
228+
flakyWindowsErrorMessage: fmt.Sprintf("rpc error: code = Internal desc = volume(vol_1##) mount \"%s\" on %#v failed with "+
229+
"NewSmbGlobalMapping(%s, %s) failed with error: rpc error: code = Unknown desc = NewSmbGlobalMapping failed.",
230+
strings.Replace(testSource, "\\", "\\\\", -1), sourceTest, testSource, sourceTest),
231+
expectedErr: testutil.TestError{},
232+
},
194233
}
195234

196235
// Setup
@@ -930,3 +969,92 @@ func TestNodePublishVolumeIdempotentMount(t *testing.T) {
930969
err = os.RemoveAll(targetTest)
931970
assert.NoError(t, err)
932971
}
972+
973+
func TestEnableGroupRWX(t *testing.T) {
974+
tests := []struct {
975+
value string
976+
expectedValue string
977+
}{
978+
{
979+
value: "qwerty",
980+
expectedValue: "qwerty",
981+
},
982+
{
983+
value: "0111",
984+
expectedValue: "0171",
985+
},
986+
}
987+
988+
for _, test := range tests {
989+
mode := enableGroupRWX(test.value)
990+
assert.Equal(t, test.expectedValue, mode)
991+
}
992+
}
993+
994+
func TestRaiseGroupRWXInMountFlags(t *testing.T) {
995+
tests := []struct {
996+
mountFlags []string
997+
flag string
998+
expectedResult bool
999+
mountFlagsUpdated bool
1000+
expectedMountFlags []string
1001+
}{
1002+
{
1003+
mountFlags: []string{""},
1004+
flag: "flag",
1005+
expectedResult: false,
1006+
},
1007+
{
1008+
mountFlags: []string{"irrelevant"},
1009+
flag: "flag",
1010+
expectedResult: false,
1011+
},
1012+
{
1013+
mountFlags: []string{"key=val"},
1014+
flag: "flag",
1015+
expectedResult: false,
1016+
},
1017+
{
1018+
mountFlags: []string{"flag=key=val"},
1019+
flag: "flag",
1020+
expectedResult: false,
1021+
},
1022+
{
1023+
// This is important: if we return false here, the caller will append another flag=...
1024+
mountFlags: []string{"flag=invalid"},
1025+
flag: "flag",
1026+
expectedResult: true,
1027+
},
1028+
{
1029+
// Main case: raising group bits in the value
1030+
mountFlags: []string{"flag=0111"},
1031+
flag: "flag",
1032+
expectedResult: true,
1033+
mountFlagsUpdated: true,
1034+
expectedMountFlags: []string{"flag=0171"},
1035+
},
1036+
}
1037+
1038+
for _, test := range tests {
1039+
savedMountFlags := make([]string, len(test.mountFlags))
1040+
copy(savedMountFlags, test.mountFlags)
1041+
1042+
result := raiseGroupRWXInMountFlags(test.mountFlags, test.flag)
1043+
if result != test.expectedResult {
1044+
t.Errorf("raiseGroupRWXInMountFlags(%v, %s) returned %t (expected: %t)",
1045+
test.mountFlags, test.flag, result, test.expectedResult)
1046+
}
1047+
1048+
if test.mountFlagsUpdated {
1049+
if !reflect.DeepEqual(test.expectedMountFlags, test.mountFlags) {
1050+
t.Errorf("raiseGroupRWXInMountFlags(%v, %s) did not update mountFlags (expected: %v)",
1051+
savedMountFlags, test.flag, test.expectedMountFlags)
1052+
}
1053+
} else {
1054+
if !reflect.DeepEqual(savedMountFlags, test.mountFlags) {
1055+
t.Errorf("raiseGroupRWXInMountFlags(%v, %s) updated mountFlags: %v",
1056+
savedMountFlags, test.flag, test.mountFlags)
1057+
}
1058+
}
1059+
}
1060+
}

0 commit comments

Comments
 (0)