Skip to content

Commit

Permalink
Added checks to make sure delete-access-point should not delete entir…
Browse files Browse the repository at this point in the history
…e EFS
  • Loading branch information
mskanth972 committed Nov 29, 2023
1 parent 9530c7d commit 8b78393
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/sha256"
"fmt"
"github.com/google/uuid"
"io"
"os"
"path"
"sort"
Expand Down Expand Up @@ -394,11 +395,28 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
}
}

klog.V(2).Infof("Access point root directory : %s", accessPoint.AccessPointRootDir)
if accessPoint.AccessPointRootDir == "/" {
return nil, status.Errorf(codes.Internal, "Could not delete efs root dir '/'")
}

target := TempMountPathPrefix + "/" + accessPointId
if err := d.mounter.MakeDir(target); err != nil {
return nil, status.Errorf(codes.Internal, "Could not create dir %q: %v", target, err)
}
if err := d.mounter.Mount(fileSystemId, target, "efs", mountOptions); err != nil {
targetDir, err := os.Open(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not read dir %q: %v", target, err)
}
_, err = targetDir.Readdir(1)
if err != io.EOF {
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not read dir %q: %v", target, err)
} else {
return nil, status.Errorf(codes.Internal, "Expected empty directory %s", target)
}
}
os.Remove(target)
return nil, status.Errorf(codes.Internal, "Could not mount %q at %q: %v", fileSystemId, target, err)
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2974,6 +2974,41 @@ func TestDeleteVolume(t *testing.T) {
mockCtl.Finish()
},
},
{
name: "Fail: Access point root directory is '/'",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockCloud := mocks.NewMockCloud(mockCtl)
mockMounter := mocks.NewMockMounter(mockCtl)

driver := &Driver{
endpoint: endpoint,
cloud: mockCloud,
mounter: mockMounter,
gidAllocator: NewGidAllocator(),
deleteAccessPointRootDir: true,
}

req := &csi.DeleteVolumeRequest{
VolumeId: volumeId,
}

accessPoint := &cloud.AccessPoint{
AccessPointId: apId,
FileSystemId: fsId,
AccessPointRootDir: "/",
CapacityGiB: 0,
}

ctx := context.Background()
mockCloud.EXPECT().DescribeAccessPoint(gomock.Eq(ctx), gomock.Eq(apId)).Return(accessPoint, nil)
_, err := driver.DeleteVolume(ctx, req)
if err == nil {
t.Fatalf("DeleteVolume did not fail")
}
mockCtl.Finish()
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 8b78393

Please sign in to comment.