Skip to content

Commit ff556e9

Browse files
committed
setgid and permission
1 parent 93cdc96 commit ff556e9

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

pkg/blob/nodeserver.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,9 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
385385
}
386386

387387
if volumeMountGroup != "" && fsGroupChangePolicy != FSGroupChangeNone {
388-
klog.V(2).Infof("set gid of volume(%s) as %s and fsGroupChangePolicy(%s)", volumeID, volumeMountGroup, fsGroupChangePolicy)
389-
// set the GID of the volume mount point to the group ID specified in the volumeMountGroup
390-
// and files and directories in NFS share will inherit the group ID of its parent directory
391-
gid, err := strconv.Atoi(volumeMountGroup)
392-
if err != nil {
393-
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("convert %s to int failed with %v", volumeMountGroup, err))
394-
}
395-
if err := os.Lchown((targetPath), -1, gid); err != nil {
396-
return nil, status.Error(codes.Internal, fmt.Sprintf("Failed to set GID of root directory %s to %d failed with %v", targetPath, gid, err))
388+
klog.V(2).Infof("set gid of volume(%s) as %s when fsGroupChangePolicy(%s)", volumeID, volumeMountGroup, fsGroupChangePolicy)
389+
if err := volumehelper.SetRootOwnership(targetPath, volumeMountGroup); err != nil {
390+
return nil, status.Error(codes.Internal, fmt.Sprintf("Failed to set GID of root directory %s to %s failed with %v", targetPath, volumeMountGroup, err))
397391
}
398392
}
399393

pkg/util/util.go

+32
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,38 @@ func SetVolumeOwnership(path, gid, policy string) error {
395395
return volume.SetVolumeOwnership(&VolumeMounter{path: path}, path, &gidInt64, &fsGroupChangePolicy, nil)
396396
}
397397

398+
// SetRootOwnership sets the ownership of the root directory, Setgid bit and permission
399+
func SetRootOwnership(rootDir string, fsgroup string) error {
400+
gid, err := strconv.Atoi(fsgroup)
401+
if err != nil {
402+
return fmt.Errorf("convert %s to int failed with %v", fsgroup, err)
403+
}
404+
405+
if err := os.Lchown(rootDir, -1, gid); err != nil {
406+
return fmt.Errorf("set root ownership failed with %v", err)
407+
}
408+
409+
fsInfo, err := os.Stat(rootDir)
410+
if err != nil {
411+
return fmt.Errorf("failed to get file system info for %s: %v", rootDir, err)
412+
}
413+
414+
if fsInfo.Mode()&os.ModeSymlink != 0 {
415+
return nil
416+
}
417+
418+
unixPerms := os.FileMode(0660)
419+
unixPerms |= os.ModeSetgid
420+
unixPerms |= os.FileMode(0110)
421+
422+
err = os.Chmod(rootDir, fsInfo.Mode()|unixPerms)
423+
if err != nil {
424+
klog.ErrorS(err, "chmod failed", "path", rootDir)
425+
}
426+
427+
return nil
428+
}
429+
398430
// ExecFunc returns a exec function's output and error
399431
type ExecFunc func() (err error)
400432

0 commit comments

Comments
 (0)