Skip to content

Add uid and gid parameters #621

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

Merged
Merged
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
6 changes: 4 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ The following CSI interfaces are implemented:
| provisioningMode | efs-ap | | false | Type of volume provisioned by efs. Currently, Access Points are supported. |
| fileSystemId | | | false | File System under which access points are created. |
| directoryPerms | | | false | Directory permissions for [Access Point root directory](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html#enforce-root-directory-access-point) creation. |
| gidRangeStart | | 50000 | true | Start range of the POSIX group Id to be applied for [Access Point root directory](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html#enforce-root-directory-access-point) creation. |
| gidRangeEnd | | 7000000 | true | End range of the POSIX group Id. |
| uid | | | true | POSIX user Id to be applied for [Access Point root directory](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html#enforce-root-directory-access-point) creation. |
| gid | | | true | POSIX group Id to be applied for [Access Point root directory](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html#enforce-root-directory-access-point) creation. |
| gidRangeStart | | 50000 | true | Start range of the POSIX group Id to be applied for [Access Point root directory](https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html#enforce-root-directory-access-point) creation. Not used if uid/gid is set. |
| gidRangeEnd | | 7000000 | true | End range of the POSIX group Id. Not used if uid/gid is set. |
| basePath | | | true | Path under which access points for dynamic provisioning is created. If this parameter is not specified, access points are created under the root directory of the file system |
| az | | "" | true | Used for cross-account mount. `az` under storage class parameter is optional. If specified, mount target associated with the az will be used for cross-account mount. If not specified, a random mount target will be picked for cross account mount |

Expand Down
44 changes: 40 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ const (
DefaultTagValue = "true"
DirectoryPerms = "directoryPerms"
FsId = "fileSystemId"
Gid = "gid"
GidMin = "gidRangeStart"
GidMax = "gidRangeEnd"
MountTargetIp = "mounttargetip"
ProvisioningMode = "provisioningMode"
RoleArn = "awsRoleArn"
TempMountPathPrefix = "/var/lib/csi/pv"
Uid = "uid"
)

var (
Expand Down Expand Up @@ -78,11 +80,13 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
azName string
basePath string
err error
gid int
gidMin int
gidMax int
localCloud cloud.Cloud
provisioningMode string
roleArn string
uid int
)

//Parse parameters
Expand Down Expand Up @@ -124,6 +128,28 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
return nil, status.Errorf(codes.InvalidArgument, "Missing %v parameter", FsId)
}

uid = -1
if value, ok := volumeParams[Uid]; ok {
uid, err = strconv.Atoi(value)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", Uid, err)
}
if uid < 0 {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater or equal than 0", Uid)
}
}

gid = -1
if value, ok := volumeParams[Gid]; ok {
gid, err = strconv.Atoi(value)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", Gid, err)
}
if uid < 0 {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater or equal than 0", Gid)
}
}

if value, ok := volumeParams[GidMin]; ok {
gidMin, err = strconv.Atoi(value)
if err != nil {
Expand Down Expand Up @@ -191,16 +217,26 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}
return nil, status.Errorf(codes.Internal, "Failed to fetch File System info: %v", err)
}
gid, err := d.gidAllocator.getNextGid(accessPointsOptions.FileSystemId, gidMin, gidMax)
if err != nil {
return nil, err

var allocatedGid int
if uid == -1 || gid == -1 {
allocatedGid, err = d.gidAllocator.getNextGid(accessPointsOptions.FileSystemId, gidMin, gidMax)
if err != nil {
return nil, err
}
}
if uid == -1 {
uid = allocatedGid
}
if gid == -1 {
gid = allocatedGid
}

rootDirName := volName
rootDir := basePath + "/" + rootDirName

accessPointsOptions.Uid = int64(uid)
accessPointsOptions.Gid = int64(gid)
accessPointsOptions.Uid = int64(gid)
accessPointsOptions.DirectoryPath = rootDir

accessPointId, err := localCloud.CreateAccessPoint(ctx, volName, accessPointsOptions)
Expand Down
276 changes: 276 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,138 @@ func TestCreateVolume(t *testing.T) {
name string
testFunc func(t *testing.T)
}{
{
name: "Success: Using fixed UID/GID",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
BasePath: "test",
Uid: "1000",
Gid: "1001",
},
}

ctx := context.Background()
fileSystem := &cloud.FileSystem{
FileSystemId: fsId,
}
accessPoint := &cloud.AccessPoint{
AccessPointId: apId,
FileSystemId: fsId,
}
mockCloud.EXPECT().DescribeFileSystem(gomock.Eq(ctx), gomock.Any()).Return(fileSystem, nil)
mockCloud.EXPECT().CreateAccessPoint(gomock.Eq(ctx), gomock.Any(), gomock.Any()).Return(accessPoint, nil).
Do(func(ctx context.Context, volumeName string, accessPointOpts *cloud.AccessPointOptions) {
if accessPointOpts.Uid != 1000 {
t.Fatalf("Uid mimatched. Expected: %v, actual: %v", accessPointOpts.Uid, 1000)
}
if accessPointOpts.Gid != 1001 {
t.Fatalf("Gid mimatched. Expected: %v, actual: %v", accessPointOpts.Uid, 1001)
}
})

res, err := driver.CreateVolume(ctx, req)

if err != nil {
t.Fatalf("CreateVolume failed: %v", err)
}

if res.Volume == nil {
t.Fatal("Volume is nil")
}

if res.Volume.VolumeId != volumeId {
t.Fatalf("Volume Id mismatched. Expected: %v, Actual: %v", volumeId, res.Volume.VolumeId)
}
mockCtl.Finish()
},
},
{
name: "Success: Using fixed UID/GID and GID range",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
BasePath: "test",
GidMin: "5000",
GidMax: "10000",
Uid: "1000",
Gid: "1001",
},
}

ctx := context.Background()
fileSystem := &cloud.FileSystem{
FileSystemId: fsId,
}
accessPoint := &cloud.AccessPoint{
AccessPointId: apId,
FileSystemId: fsId,
}
mockCloud.EXPECT().DescribeFileSystem(gomock.Eq(ctx), gomock.Any()).Return(fileSystem, nil)
mockCloud.EXPECT().CreateAccessPoint(gomock.Eq(ctx), gomock.Any(), gomock.Any()).Return(accessPoint, nil).
Do(func(ctx context.Context, volumeName string, accessPointOpts *cloud.AccessPointOptions) {
if accessPointOpts.Uid != 1000 {
t.Fatalf("Uid mimatched. Expected: %v, actual: %v", accessPointOpts.Uid, 1000)
}
if accessPointOpts.Gid != 1001 {
t.Fatalf("Gid mimatched. Expected: %v, actual: %v", accessPointOpts.Uid, 1001)
}
})

res, err := driver.CreateVolume(ctx, req)

if err != nil {
t.Fatalf("CreateVolume failed: %v", err)
}

if res.Volume == nil {
t.Fatal("Volume is nil")
}

if res.Volume.VolumeId != volumeId {
t.Fatalf("Volume Id mismatched. Expected: %v, Actual: %v", volumeId, res.Volume.VolumeId)
}
mockCtl.Finish()
},
},
{
name: "Success: Normal flow",
testFunc: func(t *testing.T) {
Expand Down Expand Up @@ -528,6 +660,150 @@ func TestCreateVolume(t *testing.T) {
mockCtl.Finish()
},
},
{
name: "Fail: Uid invalid",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
Uid: "invalid",
},
}

ctx := context.Background()
_, err := driver.CreateVolume(ctx, req)
if err == nil {
t.Fatal("CreateVolume did not fail")
}
mockCtl.Finish()
},
},
{
name: "Fail: Uid cannot be negative",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
Uid: "-5",
},
}

ctx := context.Background()
_, err := driver.CreateVolume(ctx, req)
if err == nil {
t.Fatal("CreateVolume did not fail")
}
mockCtl.Finish()
},
},
{
name: "Fail: Gid invalid",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
Gid: "invalid",
},
}

ctx := context.Background()
_, err := driver.CreateVolume(ctx, req)
if err == nil {
t.Fatal("CreateVolume did not fail")
}
mockCtl.Finish()
},
},
{
name: "Fail: Gid cannot be negative",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
gidAllocator: NewGidAllocator(),
}

req := &csi.CreateVolumeRequest{
Name: volumeName,
CapacityRange: &csi.CapacityRange{
RequiredBytes: capacityRange,
},
VolumeCapabilities: []*csi.VolumeCapability{
stdVolCap,
},
Parameters: map[string]string{
ProvisioningMode: "efs-ap",
FsId: fsId,
DirectoryPerms: "777",
Gid: "-5",
},
}

ctx := context.Background()
_, err := driver.CreateVolume(ctx, req)
if err == nil {
t.Fatal("CreateVolume did not fail")
}
mockCtl.Finish()
},
},
{
name: "Fail: Gid min cannot be 0",
testFunc: func(t *testing.T) {
Expand Down